Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 6a1005d

Browse files
committed
Return newly created User instead of bool
1 parent a919be1 commit 6a1005d

File tree

2 files changed

+110
-27
lines changed

2 files changed

+110
-27
lines changed

iam/users_service.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ type UserList struct {
6767
}
6868

6969
// CreateUser creates a new IAM user.
70-
func (u *UsersService) CreateUser(person Person) (bool, *Response, error) {
70+
func (u *UsersService) CreateUser(person Person) (*User, *Response, error) {
7171
if err := u.validate.Struct(person); err != nil {
72-
return false, nil, err
72+
return nil, nil, err
7373
}
7474
req, err := u.client.NewRequest(IDM, "POST", "authorize/identity/User", &person, nil)
7575
if err != nil {
76-
return false, nil, err
76+
return nil, nil, err
7777
}
7878
req.Header.Set("api-version", userAPIVersion)
7979

@@ -86,10 +86,22 @@ func (u *UsersService) CreateUser(person Person) (bool, *Response, error) {
8686
resp, err := doFunc(req, &bundleResponse)
8787

8888
if err != nil {
89-
return false, resp, err
89+
return nil, resp, err
9090
}
9191
ok := resp != nil && (resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated)
92-
return ok, resp, err
92+
if !ok {
93+
return nil, resp, ErrCouldNoReadResourceAfterCreate
94+
}
95+
// Retrieve user details
96+
var id string
97+
count, err := fmt.Sscanf(resp.Header.Get("Location"), "/authorize/identity/User/%s", &id)
98+
if err != nil {
99+
return nil, resp, ErrCouldNoReadResourceAfterCreate
100+
}
101+
if count == 0 {
102+
return nil, resp, ErrCouldNoReadResourceAfterCreate
103+
}
104+
return u.GetUserByID(id)
93105
}
94106

95107
// DeleteUser deletes the IAM user.

iam/users_service_test.go

+93-22
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,34 @@ func TestCreateUserSelfRegistration(t *testing.T) {
1515
teardown := setup(t)
1616
defer teardown()
1717

18+
newUserUUID := "867128a6-0e02-431c-ba1e-9e764436dae4"
19+
loginID := "loafoe"
20+
email := "[email protected]"
21+
firstName := "La"
22+
lastName := "Foe"
23+
1824
muxIDM.HandleFunc("/authorize/identity/User", func(w http.ResponseWriter, r *http.Request) {
19-
if r.Method != "POST" {
20-
t.Errorf("Expected ‘POST’ request, got ‘%s’", r.Method)
21-
}
2225
if auth := r.Header.Get("Authorization"); auth != "" {
2326
t.Errorf("No Authorization header expected, Got: %s", auth)
27+
w.WriteHeader(http.StatusUnauthorized)
28+
return
2429
}
25-
body, _ := ioutil.ReadAll(r.Body)
26-
j, _ := gabs.ParseJSON(body)
27-
ageValidated, ok := j.Path("isAgeValidated").Data().(string)
28-
if !ok {
29-
t.Errorf("Missing isAgeValidated field")
30-
}
31-
if ageValidated != "true" {
32-
t.Errorf("ageValidated should be true")
33-
}
34-
35-
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
36-
w.WriteHeader(http.StatusCreated)
37-
_, _ = io.WriteString(w, `{
30+
switch r.Method {
31+
case "POST":
32+
body, _ := ioutil.ReadAll(r.Body)
33+
j, _ := gabs.ParseJSON(body)
34+
ageValidated, ok := j.Path("isAgeValidated").Data().(string)
35+
if !ok {
36+
t.Errorf("Missing isAgeValidated field")
37+
}
38+
if ageValidated != "true" {
39+
t.Errorf("ageValidated should be true")
40+
}
41+
42+
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
43+
w.Header().Set("Location", "/authorize/identity/User/"+newUserUUID)
44+
w.WriteHeader(http.StatusCreated)
45+
_, _ = io.WriteString(w, `{
3846
"resourceType": "OperationOutcome",
3947
"issue": [
4048
{
@@ -46,13 +54,72 @@ func TestCreateUserSelfRegistration(t *testing.T) {
4654
}
4755
]
4856
}`)
57+
case "GET":
58+
w.WriteHeader(http.StatusOK)
59+
_, _ = io.WriteString(w, `{
60+
"total": 1,
61+
"entry": [
62+
{
63+
"preferredLanguage": "en-US",
64+
"loginId": "`+loginID+`",
65+
"emailAddress": "`+email+`",
66+
"id": "`+newUserUUID+`",
67+
"managingOrganization": "c29cdb88-7cda-4fc1-af8b-ee5947659958",
68+
"name": {
69+
"given": "`+firstName+`",
70+
"family": "`+lastName+`"
71+
},
72+
"memberships": [
73+
{
74+
"organizationId": "c29cdb88-7cda-4fc1-af8b-ee5947659958",
75+
"organizationName": "Pawnee",
76+
"roles": [
77+
"ANALYZE",
78+
"S3CREDSADMINROLE",
79+
"ADMIN"
80+
],
81+
"groups": [
82+
"S3CredsAdminGroup",
83+
"AdminGroup"
84+
]
85+
},
86+
{
87+
"organizationId": "d4be75cc-e81b-4d7d-b034-baf6f3f10792",
88+
"organizationName": "Eagleton",
89+
"roles": [
90+
"LOGUSER"
91+
],
92+
"groups": [
93+
"LogUserGroup"
94+
]
95+
}
96+
],
97+
"passwordStatus": {
98+
"passwordExpiresOn": "2022-02-04T10:07:55Z",
99+
"passwordChangedOn": "2020-02-15T10:07:55Z"
100+
},
101+
"accountStatus": {
102+
"mfaStatus": "NOTREQUIRED",
103+
"lastLoginTime": "2020-05-09T12:27:41Z",
104+
"emailVerified": true,
105+
"numberOfInvalidAttempt": 0,
106+
"disabled": false
107+
},
108+
"consentedApps": [
109+
"default default default"
110+
]
111+
}
112+
]
113+
}`)
114+
}
115+
49116
})
50117
person := Person{
51118
ResourceType: "Person",
52-
LoginID: "loafoe",
119+
LoginID: loginID,
53120
Name: Name{
54-
Family: "Foe",
55-
Given: "La",
121+
Family: lastName,
122+
Given: firstName,
56123
},
57124
Telecom: []TelecomEntry{
58125
{
@@ -66,13 +133,17 @@ func TestCreateUserSelfRegistration(t *testing.T) {
66133
},
67134
IsAgeValidated: "true",
68135
}
69-
ok, resp, err := client.Users.CreateUser(person)
136+
user, resp, err := client.Users.CreateUser(person)
70137
if !assert.NotNil(t, resp) {
71138
return
72139
}
140+
assert.Equal(t, http.StatusOK, resp.StatusCode)
73141
assert.Nil(t, err)
74-
assert.True(t, ok)
75-
assert.Equal(t, http.StatusCreated, resp.StatusCode)
142+
if !assert.NotNil(t, user) {
143+
return
144+
}
145+
assert.Equal(t, newUserUUID, user.ID)
146+
assert.Equal(t, loginID, user.LoginID)
76147
}
77148

78149
func TestDeleteUser(t *testing.T) {

0 commit comments

Comments
 (0)