github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/iam/user_password_test.go (about) 1 package iam 2 3 import ( 4 "context" 5 "errors" 6 "io/ioutil" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 "github.com/tj/assert" 13 ) 14 15 func TestIAM_ResetUserPassword(t *testing.T) { 16 tests := map[string]struct { 17 params ResetUserPasswordRequest 18 responseStatus int 19 responseBody string 20 expectedPath string 21 expectedResponse ResetUserPasswordResponse 22 withError func(*testing.T, error) 23 }{ 24 "200 OK": { 25 params: ResetUserPasswordRequest{ 26 IdentityID: "1-ABCDE", 27 }, 28 responseStatus: http.StatusOK, 29 responseBody: ` 30 { 31 "newPassword": "K8QVa7Q2" 32 }`, 33 expectedResponse: ResetUserPasswordResponse{ 34 NewPassword: "K8QVa7Q2", 35 }, 36 expectedPath: "/identity-management/v2/user-admin/ui-identities/1-ABCDE/reset-password?sendEmail=false", 37 }, 38 "204 No Content": { 39 params: ResetUserPasswordRequest{ 40 IdentityID: "1-ABCDE", 41 SendEmail: true, 42 }, 43 responseStatus: http.StatusNoContent, 44 responseBody: "", 45 expectedPath: "/identity-management/v2/user-admin/ui-identities/1-ABCDE/reset-password?sendEmail=true", 46 }, 47 "404 Not Found": { 48 params: ResetUserPasswordRequest{ 49 IdentityID: "X1-ABCDE", 50 }, 51 responseStatus: http.StatusNotFound, 52 responseBody: ` 53 { 54 "instance": "", 55 "httpStatus": 404, 56 "detail": "", 57 "title": "User not found", 58 "type": "/useradmin-api/error-types/1100" 59 }`, 60 expectedPath: "/identity-management/v2/user-admin/ui-identities/X1-ABCDE/reset-password?sendEmail=false", 61 withError: func(t *testing.T, err error) { 62 want := &Error{ 63 Instance: "", 64 HTTPStatus: http.StatusNotFound, 65 Detail: "", 66 Title: "User not found", 67 Type: "/useradmin-api/error-types/1100", 68 StatusCode: http.StatusNotFound, 69 } 70 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 71 }, 72 }, 73 "500 internal server error": { 74 params: ResetUserPasswordRequest{ 75 IdentityID: "1-ABCDE", 76 }, 77 responseStatus: http.StatusInternalServerError, 78 responseBody: ` 79 { 80 "type": "internal_error", 81 "title": "Internal Server Error", 82 "detail": "Error making request", 83 "status": 500 84 }`, 85 expectedPath: "/identity-management/v2/user-admin/ui-identities/1-ABCDE/reset-password?sendEmail=false", 86 withError: func(t *testing.T, err error) { 87 want := &Error{ 88 Type: "internal_error", 89 Title: "Internal Server Error", 90 Detail: "Error making request", 91 StatusCode: http.StatusInternalServerError, 92 } 93 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 94 }, 95 }, 96 } 97 98 for name, test := range tests { 99 t.Run(name, func(t *testing.T) { 100 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 101 assert.Equal(t, test.expectedPath, r.URL.String()) 102 assert.Equal(t, http.MethodPost, r.Method) 103 w.WriteHeader(test.responseStatus) 104 _, err := w.Write([]byte(test.responseBody)) 105 assert.NoError(t, err) 106 })) 107 client := mockAPIClient(t, mockServer) 108 response, err := client.ResetUserPassword(context.Background(), test.params) 109 if test.withError != nil { 110 test.withError(t, err) 111 return 112 } 113 require.NoError(t, err) 114 assert.Equal(t, test.expectedResponse, *response) 115 }) 116 } 117 } 118 119 func TestIAM_SetUserPassword(t *testing.T) { 120 tests := map[string]struct { 121 params SetUserPasswordRequest 122 responseStatus int 123 responseBody string 124 expectedRequestBody string 125 expectedPath string 126 withError func(*testing.T, error) 127 }{ 128 "204 No Content": { 129 params: SetUserPasswordRequest{ 130 IdentityID: "1-ABCDE", 131 NewPassword: "newpwd", 132 }, 133 responseStatus: http.StatusNoContent, 134 responseBody: "", 135 expectedRequestBody: `{"newPassword":"newpwd"}`, 136 expectedPath: "/identity-management/v2/user-admin/ui-identities/1-ABCDE/set-password", 137 }, 138 "400 Bad Request - same password": { 139 params: SetUserPasswordRequest{ 140 IdentityID: "X1-ABCDE", 141 NewPassword: "newpwd", 142 }, 143 responseStatus: http.StatusNotFound, 144 responseBody: `{ 145 "instance": "", 146 "httpStatus": 400, 147 "detail": "Must not match a previously used password.", 148 "title": "Validation Exception", 149 "type": "/useradmin-api/error-types/1508" 150 }`, 151 expectedPath: "/identity-management/v2/user-admin/ui-identities/X1-ABCDE/set-password", 152 withError: func(t *testing.T, err error) { 153 want := &Error{ 154 Instance: "", 155 HTTPStatus: http.StatusBadRequest, 156 Detail: "Must not match a previously used password.", 157 Title: "Validation Exception", 158 Type: "/useradmin-api/error-types/1508", 159 StatusCode: http.StatusNotFound, 160 } 161 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 162 }, 163 }, 164 "404 Not Found": { 165 params: SetUserPasswordRequest{ 166 IdentityID: "X1-ABCDE", 167 NewPassword: "newpwd", 168 }, 169 responseStatus: http.StatusNotFound, 170 responseBody: ` 171 { 172 "instance": "", 173 "httpStatus": 404, 174 "detail": "", 175 "title": "User not found", 176 "type": "/useradmin-api/error-types/1100" 177 }`, 178 expectedPath: "/identity-management/v2/user-admin/ui-identities/X1-ABCDE/set-password", 179 withError: func(t *testing.T, err error) { 180 want := &Error{ 181 Instance: "", 182 HTTPStatus: http.StatusNotFound, 183 Detail: "", 184 Title: "User not found", 185 Type: "/useradmin-api/error-types/1100", 186 StatusCode: http.StatusNotFound, 187 } 188 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 189 }, 190 }, 191 "500 internal server error": { 192 params: SetUserPasswordRequest{ 193 IdentityID: "1-ABCDE", 194 NewPassword: "newpwd", 195 }, 196 responseStatus: http.StatusInternalServerError, 197 responseBody: ` 198 { 199 "type": "internal_error", 200 "title": "Internal Server Error", 201 "detail": "Error making request", 202 "status": 500 203 }`, 204 expectedPath: "/identity-management/v2/user-admin/ui-identities/1-ABCDE/set-password", 205 withError: func(t *testing.T, err error) { 206 want := &Error{ 207 Type: "internal_error", 208 Title: "Internal Server Error", 209 Detail: "Error making request", 210 StatusCode: http.StatusInternalServerError, 211 } 212 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 213 }, 214 }, 215 } 216 217 for name, test := range tests { 218 t.Run(name, func(t *testing.T) { 219 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 220 assert.Equal(t, test.expectedPath, r.URL.String()) 221 assert.Equal(t, http.MethodPost, r.Method) 222 w.WriteHeader(test.responseStatus) 223 _, err := w.Write([]byte(test.responseBody)) 224 assert.NoError(t, err) 225 226 if len(test.expectedRequestBody) > 0 { 227 body, err := ioutil.ReadAll(r.Body) 228 require.NoError(t, err) 229 assert.Equal(t, test.expectedRequestBody, string(body)) 230 } 231 })) 232 client := mockAPIClient(t, mockServer) 233 err := client.SetUserPassword(context.Background(), test.params) 234 if test.withError != nil { 235 test.withError(t, err) 236 return 237 } 238 require.NoError(t, err) 239 }) 240 } 241 }