Cloud Aggregation: make it work for users with no name (#96509)

This commit is contained in:
Charandas 2024-11-15 15:28:54 -08:00 committed by GitHub
parent 29cdfdff87
commit 9223cd8f59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 1 deletions

View File

@ -104,7 +104,18 @@ func (u *SignedInUser) IsIdentityType(expected ...claims.IdentityType) bool {
// GetName implements identity.Requester.
func (u *SignedInUser) GetName() string {
return u.Name
// kubernetesAggregator feature flag which allows Cloud Apps to become available
// in single tenant Grafana requires that GetName() returns something and not an empty string
// the logic below ensures that something is returned
if u.Name != "" {
return u.Name
}
if u.Login != "" {
return u.Login
}
return u.Email
}
// GetExtra implements Requester.

View File

@ -0,0 +1,36 @@
package user
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestIdentityGetName(t *testing.T) {
tt := []struct {
name string
user *SignedInUser
expected string
}{
{
name: "GetName on a user with empty name returns Login, if set",
user: &SignedInUser{
Login: "userLogin",
Email: "user@grafana.com",
},
expected: "userLogin",
},
{
name: "GetName on a user with empty name returns Email, if no Login is set",
user: &SignedInUser{
Email: "user@grafana.com",
},
expected: "user@grafana.com",
},
}
for _, tc := range tt {
user := tc.user
require.Equal(t, user.GetName(), tc.expected, tc.name)
}
}