fix: set displayname correctly in EnsureDisplayName (#5702)

fix: EnsureDisplayName
This commit is contained in:
Livio Spring 2023-04-17 08:26:40 +02:00 committed by GitHub
parent e79e280dc7
commit 8e19f0f6c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 1 deletions

View File

@ -91,7 +91,13 @@ func (u *Human) CheckDomainPolicy(policy *DomainPolicy) error {
}
func (u *Human) EnsureDisplayName() {
if u.Profile != nil && u.DisplayName == "" && u.FirstName != "" && u.LastName != "" {
if u.Profile == nil {
u.Profile = new(Profile)
}
if u.DisplayName != "" {
return
}
if u.FirstName != "" && u.LastName != "" {
u.DisplayName = u.FirstName + " " + u.LastName
return
}

View File

@ -0,0 +1,78 @@
package domain
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestHuman_EnsureDisplayName(t *testing.T) {
type fields struct {
Username string
Profile *Profile
Email *Email
}
tests := []struct {
name string
fields fields
want string
}{
{
"display name set",
fields{
Username: "username",
Profile: &Profile{
FirstName: "firstName",
LastName: "lastName",
DisplayName: "displayName",
},
Email: &Email{
EmailAddress: "email",
},
},
"displayName",
},
{
"first and lastname set",
fields{
Username: "username",
Profile: &Profile{
FirstName: "firstName",
LastName: "lastName",
},
Email: &Email{
EmailAddress: "email",
},
},
"firstName lastName",
},
{
"profile nil, email set",
fields{
Username: "username",
Email: &Email{
EmailAddress: "email",
},
},
"email",
},
{
"email nil, username set",
fields{
Username: "username",
},
"username",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &Human{
Username: tt.fields.Username,
Profile: tt.fields.Profile,
Email: tt.fields.Email,
}
u.EnsureDisplayName()
assert.Equal(t, tt.want, u.DisplayName)
})
}
}