Skip to content

Commit

Permalink
Add more tests for user mapper template
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-arella committed Feb 12, 2025
1 parent 744761f commit c1dfcc8
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 8 deletions.
16 changes: 10 additions & 6 deletions internal/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,18 @@ func NewMapper(userTemplate string) (UserMapper, error) {
{
{{- with .Id -}}"externalId": {{ . | quote }},{{- end -}}
"userName": {{ .PrimaryEmail | quote }},
{{- with .Name -}}
"name": {
"familyName": {{ .Name.FamilyName | quote }},
"givenName": {{ .Name.GivenName | quote }}
{{- with .FamilyName -}}"familyName": {{ . | quote }}{{- with $.Name.GivenName -}},{{- end -}}{{- end -}}
{{- with .GivenName -}}
"givenName": {{ . | quote }}
{{- end -}}
},
"displayName": {{ printf "%s %s" .Name.GivenName .Name.FamilyName | quote }},
"displayName": {{ list .GivenName .FamilyName | join " " | trim | quote }},
{{- end -}}
{{- with .Websites -}}
{{- with listFindFirst . (dict "Primary" true) -}}
{{- with .Value -}}"ProfileUrl": {{ .Value | quote }},{{- end -}}
{{- with default (first .) (listFindFirst . (dict "Primary" true)) -}}
{{- with .Value -}}"ProfileUrl": {{ . | quote }},{{- end -}}
{{- end -}}
{{- end -}}
{{- with .Emails -}}
Expand Down Expand Up @@ -167,7 +171,7 @@ func NewMapper(userTemplate string) (UserMapper, error) {
{{- end -}}
{{- with .Phones -}}
{{- with default (first .) (listFindFirst . (dict "Primary" true)) -}}
"phones": [{
"phoneNumbers": [{
{{- with .Value -}}"value": {{ . | quote }},{{- end -}}
"type": {{ default "work" (.Type | quote) }}
}],
Expand Down
146 changes: 144 additions & 2 deletions internal/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ func TestMapUser(t *testing.T) {
Primary: false,
},
},
Websites: []admin.UserWebsite{
{
Primary: true,
Type: "work",
Value: "https://test.user.com",
},
},
Phones: []admin.UserPhone{
{
Primary: true,
Type: "work",
Value: "5550279999",
},
{
Type: "work",
Value: "5554468748",
},
},
Organizations: []admin.UserOrganization{
{
Name: "Universal Studios",
Expand Down Expand Up @@ -110,6 +128,13 @@ func TestMapUser(t *testing.T) {
Primary: true,
},
},
ProfileUrl: "https://test.user.com",
PhoneNumbers: []aws.UserPhoneNumber{
{
Type: "work",
Value: "5550279999",
},
},
Enterprise: &aws.EnterpriseUser{
EmployeeNumber: "701984",
Organization: "Universal Studios",
Expand All @@ -124,14 +149,131 @@ func TestMapUser(t *testing.T) {
},
},
},
{
name: "Map user with missing name",
args: args{
googleUser: &admin.User{
PrimaryEmail: "test.user@example.com",
Suspended: false,
},
},
wantUser: &aws.User{
Username: "test.user@example.com",
Active: true,
Schemas: []string{
"urn:ietf:params:scim:schemas:core:2.0:User",
},
},
},
{
name: "Map user with missing given name",
args: args{
googleUser: &admin.User{
PrimaryEmail: "test.user@example.com",
Name: &admin.UserName{
FamilyName: "a",
},
Suspended: false,
},
},
wantUser: &aws.User{
Username: "test.user@example.com",
Name: aws.UserName{
FamilyName: "a",
},
DisplayName: "a",
Active: true,
Schemas: []string{
"urn:ietf:params:scim:schemas:core:2.0:User",
},
},
},
{
name: "Map user with missing family name",
args: args{
googleUser: &admin.User{
PrimaryEmail: "test.user@example.com",
Name: &admin.UserName{
GivenName: "b",
},
Suspended: false,
},
},
wantUser: &aws.User{
Username: "test.user@example.com",
Name: aws.UserName{
GivenName: "b",
},
DisplayName: "b",
Active: true,
Schemas: []string{
"urn:ietf:params:scim:schemas:core:2.0:User",
},
},
},
{
name: "Map user with multiple websites",
args: args{
googleUser: &admin.User{
PrimaryEmail: "test.user@example.com",
Websites: []admin.UserWebsite{
{
Type: "blog",
Value: "https://test.user.com",
},
{
Primary: true,
Type: "work",
Value: "https://work.test.user.com",
},
},
Suspended: false,
},
},
wantUser: &aws.User{
Username: "test.user@example.com",
ProfileUrl: "https://work.test.user.com",
Active: true,
Schemas: []string{
"urn:ietf:params:scim:schemas:core:2.0:User",
},
},
},
{
name: "Map user with missing primary website",
args: args{
googleUser: &admin.User{
PrimaryEmail: "test.user@example.com",
Websites: []admin.UserWebsite{
{
Type: "blog",
Value: "https://test.user.com",
},
{
Type: "work",
Value: "https://work.test.user.com",
},
},
Suspended: false,
},
},
wantUser: &aws.User{
Username: "test.user@example.com",
ProfileUrl: "https://test.user.com",
Active: true,
Schemas: []string{
"urn:ietf:params:scim:schemas:core:2.0:User",
},
},
},
{
name: "Override user nickname",
args: args{
googleUser: &admin.User{
PrimaryEmail: "test.user@example.com",
Name: &admin.UserName{
FamilyName: "a",
GivenName: "b",
GivenName: "b",
},
Suspended: false,
},
Expand All @@ -141,7 +283,7 @@ func TestMapUser(t *testing.T) {
Username: "test.user@example.com",
Name: aws.UserName{
FamilyName: "a",
GivenName: "b",
GivenName: "b",
},
DisplayName: "b a",
Nickname: "testuser",
Expand Down

0 comments on commit c1dfcc8

Please sign in to comment.