Skip to content

Commit

Permalink
[CLOUDTRUST-2314] Fix birthDate attribute format. Attributes manageme…
Browse files Browse the repository at this point in the history
…nt enhancement
  • Loading branch information
fperot74 committed Mar 3, 2020
1 parent 039b8a9 commit a22c8fc
Show file tree
Hide file tree
Showing 44 changed files with 375 additions and 379 deletions.
14 changes: 7 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

[[constraint]]
name = "github.com/cloudtrust/keycloak-client"
branch = "master"
branch = "ct-2314"

[[constraint]]
name = "github.com/go-kit/kit"
Expand Down
51 changes: 20 additions & 31 deletions api/account/api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package account

import (
"strconv"

"github.com/cloudtrust/common-service/validation"
msg "github.com/cloudtrust/keycloak-bridge/internal/messages"
"github.com/cloudtrust/keycloak-bridge/internal/constants"
msg "github.com/cloudtrust/keycloak-bridge/internal/constants"
kc "github.com/cloudtrust/keycloak-client"
)

Expand Down Expand Up @@ -82,27 +81,22 @@ func ConvertToAPIAccount(userKc kc.UserRepresentation) AccountRepresentation {
userRep.FirstName = userKc.FirstName
userRep.LastName = userKc.LastName

if userKc.Attributes != nil {
var m = *userKc.Attributes

if value, ok := m["phoneNumber"]; ok && len(value) > 0 {
userRep.PhoneNumber = &value[0]
}
if value, ok := m["gender"]; ok && len(value) > 0 {
userRep.Gender = &value[0]
}
if value, ok := m["birthDate"]; ok && len(value) > 0 {
userRep.BirthDate = &value[0]
}
if value, ok := m["locale"]; ok && len(value) > 0 {
userRep.Locale = &value[0]
}
if value, ok := m["phoneNumberVerified"]; ok && len(value) > 0 {
if verified, err := strconv.ParseBool(value[0]); err == nil {
userRep.PhoneNumberVerified = &verified
}
}
if value := userKc.GetAttributeString(constants.AttrbPhoneNumber); value != nil {
userRep.PhoneNumber = value
}
if value := userKc.GetAttributeString(constants.AttrbGender); value != nil {
userRep.Gender = value
}
if value := userKc.GetAttributeDate(constants.AttrbBirthDate, constants.SupportedDateLayouts); value != nil {
userRep.BirthDate = value
}
if value := userKc.GetAttributeString(constants.AttrbLocale); value != nil {
userRep.Locale = value
}
if verified, err := userKc.GetAttributeBool(constants.AttrbPhoneNumberVerified); err == nil && verified != nil {
userRep.PhoneNumberVerified = verified
}

return userRep
}

Expand All @@ -115,14 +109,9 @@ func ConvertToKCUser(user AccountRepresentation) kc.UserRepresentation {
userRep.FirstName = user.FirstName
userRep.LastName = user.LastName

var attributes = make(map[string][]string)

if user.PhoneNumber != nil {
attributes["phoneNumber"] = []string{*user.PhoneNumber}
}
if user.Locale != nil {
attributes["locale"] = []string{*user.Locale}
}
var attributes = make(kc.Attributes)
attributes.SetStringWhenNotNil(constants.AttrbPhoneNumber, user.PhoneNumber)
attributes.SetStringWhenNotNil(constants.AttrbLocale, user.Locale)

if len(attributes) > 0 {
userRep.Attributes = &attributes
Expand Down
52 changes: 31 additions & 21 deletions api/account/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package account
import (
"testing"

"github.com/cloudtrust/keycloak-bridge/internal/constants"

kc "github.com/cloudtrust/keycloak-client"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -30,27 +32,35 @@ func TestConvertToAPIAccount(t *testing.T) {
var kcUser = kc.UserRepresentation{}
assert.Nil(t, nil, ConvertToAPIAccount(kcUser))

var attributes = make(map[string][]string)
kcUser = kc.UserRepresentation{Attributes: &attributes}
assert.Nil(t, nil, ConvertToAPIAccount(kcUser).PhoneNumber)

attributes["phoneNumber"] = []string{"+41221234567"}
attributes["gender"] = []string{"M"}
attributes["birthDate"] = []string{"15.02.1920"}
attributes["locale"] = []string{"fr"}
attributes["phoneNumberVerified"] = []string{"true"}
kcUser = kc.UserRepresentation{Attributes: &attributes}

var user = ConvertToAPIAccount(kcUser)
assert.Equal(t, "+41221234567", *user.PhoneNumber)
assert.Equal(t, "M", *user.Gender)
assert.Equal(t, "15.02.1920", *user.BirthDate)
assert.Equal(t, "fr", *user.Locale)
assert.True(t, *user.PhoneNumberVerified)

attributes["phoneNumberVerified"] = []string{"vielleicht"}
user = ConvertToAPIAccount(kcUser)
assert.Nil(t, user.PhoneNumberVerified)
t.Run("Empty attributes", func(t *testing.T) {
var attributes = make(kc.Attributes)
kcUser = kc.UserRepresentation{Attributes: &attributes}
assert.Nil(t, nil, ConvertToAPIAccount(kcUser).PhoneNumber)
})

var attributes = kc.Attributes{
"phoneNumber": []string{"+41221234567"},
"gender": []string{"M"},
"birthDate": []string{"15.02.1920"},
"locale": []string{"fr"},
"phoneNumberVerified": []string{"true"},
}

t.Run("Check attributes are copied", func(t *testing.T) {
kcUser = kc.UserRepresentation{Attributes: &attributes}
var user = ConvertToAPIAccount(kcUser)
assert.Equal(t, "+41221234567", *user.PhoneNumber)
assert.Equal(t, "M", *user.Gender)
assert.Equal(t, "15.02.1920", *user.BirthDate)
assert.Equal(t, "fr", *user.Locale)
assert.True(t, *user.PhoneNumberVerified)
})

t.Run("PhoneNumberVerified is invalid", func(t *testing.T) {
attributes.SetString(constants.AttrbPhoneNumberVerified, "vielleicht")
var user = ConvertToAPIAccount(kcUser)
assert.Nil(t, user.PhoneNumberVerified)
})
}

func TestConvertToKCUser(t *testing.T) {
Expand Down
17 changes: 7 additions & 10 deletions api/kyc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/cloudtrust/common-service/validation"
"github.com/cloudtrust/keycloak-bridge/internal/constants"
kc "github.com/cloudtrust/keycloak-client"
)

Expand Down Expand Up @@ -82,24 +83,20 @@ func (u *UserRepresentation) UserToJSON() string {
func (u *UserRepresentation) ExportToKeycloak(kcUser *kc.UserRepresentation) {
var bFalse = false
var bTrue = true
var attributes = make(map[string][]string)
var attributes = make(kc.Attributes)

if kcUser.Attributes != nil {
attributes = *kcUser.Attributes
}

if u.Gender != nil {
attributes["gender"] = []string{*u.Gender}
}
attributes.SetStringWhenNotNil(constants.AttrbGender, u.Gender)
if u.PhoneNumber != nil {
if value, ok := attributes["phoneNumber"]; !ok || (len(value) > 0 && value[0] != *u.PhoneNumber) {
attributes["phoneNumber"] = []string{*u.PhoneNumber}
attributes["phoneNumberVerified"] = []string{"false"}
if value := attributes.GetString(constants.AttrbPhoneNumber); value == nil || *value != *u.PhoneNumber {
attributes.SetString(constants.AttrbPhoneNumber, *u.PhoneNumber)
attributes.SetBool(constants.AttrbPhoneNumberVerified, false)
}
}
if u.BirthDate != nil {
attributes["birthDate"] = []string{*u.BirthDate}
}
attributes.SetDateWhenNotNil(constants.AttrbBirthDate, u.BirthDate, constants.SupportedDateLayouts)

if u.Username != nil {
kcUser.Username = u.Username
Expand Down
2 changes: 1 addition & 1 deletion api/kyc/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func createValidKeycloakUser() kc.UserRepresentation {
firstName = "Marc"
lastName = "El-Bichoun"
email = "marcel.bichon@elca.ch"
attributes = map[string][]string{
attributes = kc.Attributes{
"gender": []string{"M"},
"phoneNumber": []string{"00 33 686 550011"},
"phoneNumberVerified": []string{"true"},
Expand Down
Loading

0 comments on commit a22c8fc

Please sign in to comment.