Skip to content

Commit cb4e523

Browse files
committed
test: add regression test for CreateOrGetUser with no emails
Add a test case that verifies CreateOrGetUser does not panic when the existing user retrieved from AWS SSO has an empty emails slice. Adds the testdata/ListUserResponse_no_emails.json fixture to support it.
1 parent e0e8f51 commit cb4e523

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

pkg/aws/scim_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,91 @@ func TestCreateOrGetUser(t *testing.T) {
10471047
assert.Equal(t, true, got.Emails[0].Primary)
10481048
assert.Equal(t, true, got.Active)
10491049
})
1050+
1051+
t.Run("should not panic when existing user has no emails and fields changed", func(t *testing.T) {
1052+
CreateUserResponseConflictFile := "testdata/CreateUserResponse_Conflict.json"
1053+
ListUserResponseFile := "testdata/ListUserResponse_no_emails.json"
1054+
PutUserResponseFile := "testdata/PutUserResponse.json"
1055+
1056+
mockHTTPClient := mocks.NewMockHTTPClient(mockCtrl)
1057+
jsonRespConflict := ReadJSONFileAsString(t, CreateUserResponseConflictFile)
1058+
jsonListRespOK := ReadJSONFileAsString(t, ListUserResponseFile)
1059+
jsonPutUserRespOK := ReadJSONFileAsString(t, PutUserResponseFile)
1060+
1061+
httpRespConflict := &http.Response{
1062+
Status: "409 Conflict",
1063+
StatusCode: http.StatusConflict,
1064+
Header: http.Header{
1065+
"Date": []string{"Fri, 18 Mar 2022 10:57:08 GMT"},
1066+
"Content-Type": []string{"application/json"},
1067+
"x-amzn-RequestId": []string{"81abca44-4ee3-47fa-b4d9-729908ef1dd9"},
1068+
},
1069+
Proto: "HTTP/1.1",
1070+
Body: io.NopCloser(strings.NewReader(jsonRespConflict)),
1071+
ContentLength: int64(len(jsonRespConflict)),
1072+
}
1073+
1074+
ListRespOK := &http.Response{
1075+
Status: "200 OK",
1076+
StatusCode: http.StatusOK,
1077+
Header: http.Header{
1078+
"Date": []string{"Tue, 31 Mar 2020 02:36:15 GMT"},
1079+
"Content-Type": []string{"application/json"},
1080+
"x-amzn-RequestId": []string{"abbf9e53-9ecc-46d2-8efe-104a66ff128f"},
1081+
},
1082+
Proto: "HTTP/1.1",
1083+
Body: io.NopCloser(strings.NewReader(jsonListRespOK)),
1084+
ContentLength: int64(len(jsonListRespOK)),
1085+
}
1086+
1087+
PutUserRespOK := &http.Response{
1088+
Status: "200 OK",
1089+
StatusCode: http.StatusOK,
1090+
Header: http.Header{
1091+
"Date": []string{"Tue, 31 Mar 2020 02:36:15 GMT"},
1092+
"Content-Type": []string{"application/json"},
1093+
"x-amzn-RequestId": []string{"abbf9e53-9ecc-46d2-8efe-104a66ff128f"},
1094+
},
1095+
Proto: "HTTP/1.1",
1096+
Body: io.NopCloser(strings.NewReader(jsonPutUserRespOK)),
1097+
ContentLength: int64(len(jsonPutUserRespOK)),
1098+
}
1099+
1100+
mockHTTPClient.EXPECT().Do(gomock.Any()).Return(httpRespConflict, nil).Times(1)
1101+
mockHTTPClient.EXPECT().Do(gomock.Any()).Return(ListRespOK, nil).Times(1)
1102+
mockHTTPClient.EXPECT().Do(gomock.Any()).Return(PutUserRespOK, nil).Times(1)
1103+
1104+
service, err := NewSCIMService(mockHTTPClient, endpoint, "MyToken")
1105+
assert.NoError(t, err)
1106+
assert.NotNil(t, service)
1107+
1108+
usrr := &CreateUserRequest{
1109+
ID: "90677c608a-7afcdc23-0bd4-4fb7-b2ff-10ccffdff447",
1110+
ExternalID: "702135",
1111+
UserName: "mjack",
1112+
Name: &Name{
1113+
FamilyName: "Jackson",
1114+
GivenName: "Mark",
1115+
},
1116+
DisplayName: "Mark Jackson",
1117+
Emails: []Email{
1118+
{
1119+
Value: "mjack@example.com",
1120+
Type: "work",
1121+
Primary: true,
1122+
},
1123+
},
1124+
Active: true,
1125+
}
1126+
1127+
var got *CreateUserResponse
1128+
assert.NotPanics(t, func() {
1129+
var callErr error
1130+
got, callErr = service.CreateOrGetUser(context.Background(), usrr)
1131+
assert.NoError(t, callErr)
1132+
})
1133+
assert.NotNil(t, got)
1134+
})
10501135
}
10511136

10521137
func TestDeleteUser(t *testing.T) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"totalResults": 1,
3+
"itemsPerPage": 1,
4+
"startIndex": 1,
5+
"schemas": [
6+
"urn:ietf:params:scim:api:messages:2.0:ListResponse"
7+
],
8+
"Resources": [
9+
{
10+
"id": "90677c608a-7afcdc23-0bd4-4fb7-b2ff-10ccffdff447",
11+
"externalId": "702135",
12+
"meta": {
13+
"resourceType": "User",
14+
"created": "2020-07-22T22:32:58Z",
15+
"lastModified": "2020-07-22T22:32:58Z"
16+
},
17+
"schemas": [
18+
"urn:ietf:params:scim:schemas:core:2.0:User",
19+
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
20+
],
21+
"userName": "mjack",
22+
"name": {
23+
"familyName": "Jackson",
24+
"givenName": "Mark"
25+
},
26+
"displayName": "mjack",
27+
"active": false,
28+
"emails": []
29+
}
30+
]
31+
}

0 commit comments

Comments
 (0)