Skip to content

Commit

Permalink
Add Multi language (locale) attribute to an user
Browse files Browse the repository at this point in the history
  • Loading branch information
bsoniam authored and harture committed May 14, 2019
1 parent dc8ec39 commit 32151ee
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
14 changes: 12 additions & 2 deletions api/management/api.go
Expand Up @@ -22,6 +22,7 @@ type UserRepresentation struct {
CreatedTimestamp *int64 `json:"createdTimestamp,omitempty"`
Groups *[]string `json:"groups,omitempty"`
Roles *[]string `json:"roles,omitempty"`
Locale *string `json:"locale,omitempty"`
}

type RealmRepresentation struct {
Expand Down Expand Up @@ -59,8 +60,8 @@ type RoleRepresentation struct {
}

type GroupRepresentation struct {
Id *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Id *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
}

type PasswordRepresentation struct {
Expand Down Expand Up @@ -133,6 +134,11 @@ func ConvertToAPIUser(userKc kc.UserRepresentation) UserRepresentation {
var phoneNumberVerified, _ = strconv.ParseBool(m["phoneNumberVerified"][0])
userRep.PhoneNumberVerified = &phoneNumberVerified
}

if m["locale"] != nil {
var locale = m["locale"][0]
userRep.Locale = &locale
}
}
return userRep
}
Expand Down Expand Up @@ -172,6 +178,10 @@ func ConvertToKCUser(user UserRepresentation) kc.UserRepresentation {
attributes["phoneNumberVerified"] = []string{strconv.FormatBool(*user.PhoneNumberVerified)}
}

if user.Locale != nil {
attributes["locale"] = []string{*user.Locale}
}

if len(attributes) > 0 {
userRep.Attributes = &attributes
}
Expand Down
11 changes: 11 additions & 0 deletions api/management/api_test.go
Expand Up @@ -62,6 +62,12 @@ func TestConvertToAPIUser(t *testing.T) {
kcUser.Attributes = &m
m["phoneNumberVerified"] = []string{"true"}
assert.True(t, *ConvertToAPIUser(kcUser).PhoneNumberVerified)

// Locale
assert.Nil(t, ConvertToAPIUser(kcUser).Locale)
kcUser.Attributes = &m
m["locale"] = []string{"en"}
assert.NotNil(t, *ConvertToAPIUser(kcUser).Locale)
}

func TestConvertToKCUser(t *testing.T) {
Expand Down Expand Up @@ -92,4 +98,9 @@ func TestConvertToKCUser(t *testing.T) {
var verified = true
user.PhoneNumberVerified = &verified
assert.Equal(t, "true", (*ConvertToKCUser(user).Attributes)["phoneNumberVerified"][0])

// Locale
var locale = "it"
user.Locale = &locale
assert.Equal(t, locale, (*ConvertToKCUser(user).Attributes)["locale"][0])
}
3 changes: 3 additions & 0 deletions api/management/swagger-api_management.yaml
Expand Up @@ -763,6 +763,9 @@ components:
type: array
items:
type: string
locale:
type: string
default: "en"
UserStatus:
type: object
properties:
Expand Down
16 changes: 13 additions & 3 deletions pkg/management/component_test.go
Expand Up @@ -319,7 +319,7 @@ func TestCreateUser(t *testing.T) {
assert.Equal(t, locationURL, location)
}

// Create with all properties allowed by Brdige API
// Create with all properties allowed by Bridge API
{
var email = "toto@elca.ch"
var enabled = true
Expand All @@ -332,6 +332,7 @@ func TestCreateUser(t *testing.T) {
var gender = "M"
var birthDate = "01/01/1988"
var userID = "1234-7558-7645"
var locale = "de"

mockKeycloakClient.EXPECT().CreateUser(accessToken, realmName, targetRealmName, gomock.Any()).DoAndReturn(
func(accessToken, realmName, targetRealmName string, kcUserRep kc.UserRepresentation) (string, error) {
Expand All @@ -347,6 +348,7 @@ func TestCreateUser(t *testing.T) {
assert.Equal(t, label, (*kcUserRep.Attributes)["label"][0])
assert.Equal(t, gender, (*kcUserRep.Attributes)["gender"][0])
assert.Equal(t, birthDate, (*kcUserRep.Attributes)["birthDate"][0])
assert.Equal(t, locale, (*kcUserRep.Attributes)["locale"][0])

return locationURL, nil
}).Times(1)
Expand All @@ -370,6 +372,7 @@ func TestCreateUser(t *testing.T) {
Label: &label,
Gender: &gender,
BirthDate: &birthDate,
Locale: &locale,
}

location, err := managementComponent.CreateUser(ctx, targetRealmName, userRep)
Expand Down Expand Up @@ -464,13 +467,15 @@ func TestGetUser(t *testing.T) {
var gender = "M"
var birthDate = "01/01/1988"
var createdTimestamp = time.Now().UTC().Unix()
var locale = "it"

var attributes = make(map[string][]string)
attributes["phoneNumber"] = []string{phoneNumber}
attributes["label"] = []string{label}
attributes["gender"] = []string{gender}
attributes["birthDate"] = []string{birthDate}
attributes["phoneNumberVerified"] = []string{strconv.FormatBool(phoneNumberVerified)}
attributes["locale"] = []string{locale}

var kcUserRep = kc.UserRepresentation{
Id: &id,
Expand Down Expand Up @@ -507,6 +512,7 @@ func TestGetUser(t *testing.T) {
assert.Equal(t, gender, *apiUserRep.Gender)
assert.Equal(t, birthDate, *apiUserRep.BirthDate)
assert.Equal(t, createdTimestamp, *apiUserRep.CreatedTimestamp)
assert.Equal(t, locale, *apiUserRep.Locale)
}

//Error
Expand Down Expand Up @@ -548,6 +554,7 @@ func TestUpdateUser(t *testing.T) {
var label = "Label"
var gender = "M"
var birthDate = "01/01/1988"
var locale = "de"
var createdTimestamp = time.Now().UTC().Unix()

var attributes = make(map[string][]string)
Expand All @@ -556,6 +563,7 @@ func TestUpdateUser(t *testing.T) {
attributes["gender"] = []string{gender}
attributes["birthDate"] = []string{birthDate}
attributes["phoneNumberVerified"] = []string{strconv.FormatBool(phoneNumberVerified)}
attributes["locale"] = []string{locale}

var kcUserRep = kc.UserRepresentation{
Id: &id,
Expand All @@ -581,6 +589,7 @@ func TestUpdateUser(t *testing.T) {
Label: &label,
Gender: &gender,
BirthDate: &birthDate,
Locale: &locale,
}

var ctx = context.WithValue(context.Background(), "access_token", accessToken)
Expand All @@ -605,7 +614,7 @@ func TestUpdateUser(t *testing.T) {
assert.Equal(t, label, (*kcUserRep.Attributes)["label"][0])
assert.Equal(t, gender, (*kcUserRep.Attributes)["gender"][0])
assert.Equal(t, birthDate, (*kcUserRep.Attributes)["birthDate"][0])

assert.Equal(t, locale, (*kcUserRep.Attributes)["locale"][0])
return nil
}).Times(1)

Expand All @@ -629,7 +638,7 @@ func TestUpdateUser(t *testing.T) {
assert.Equal(t, label, (*kcUserRep.Attributes)["label"][0])
assert.Equal(t, gender, (*kcUserRep.Attributes)["gender"][0])
assert.Equal(t, birthDate, (*kcUserRep.Attributes)["birthDate"][0])

assert.Equal(t, locale, (*kcUserRep.Attributes)["locale"][0])
return nil
}).Times(1)

Expand All @@ -645,6 +654,7 @@ func TestUpdateUser(t *testing.T) {
Label: &label,
Gender: &gender,
BirthDate: &birthDate,
Locale: &locale,
}

err = managementComponent.UpdateUser(ctx, "master", id, userRepLocked)
Expand Down

0 comments on commit 32151ee

Please sign in to comment.