github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/iam/support_test.go (about) 1 package iam 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 "github.com/tj/assert" 12 ) 13 14 func TestIAM_SupportedCountries(t *testing.T) { 15 tests := map[string]struct { 16 responseStatus int 17 responseBody string 18 expectedPath string 19 expectedResponse []string 20 withError func(*testing.T, error) 21 }{ 22 "200 OK": { 23 responseStatus: http.StatusOK, 24 responseBody: ` 25 [ 26 "Greece", 27 "Greenland", 28 "Grenada" 29 ]`, 30 expectedPath: "/identity-management/v2/user-admin/common/countries", 31 expectedResponse: []string{"Greece", "Greenland", "Grenada"}, 32 }, 33 "500 internal server error": { 34 responseStatus: http.StatusInternalServerError, 35 responseBody: ` 36 { 37 "type": "internal_error", 38 "title": "Internal Server Error", 39 "detail": "Error making request", 40 "status": 500 41 }`, 42 expectedPath: "/identity-management/v2/user-admin/common/countries", 43 withError: func(t *testing.T, err error) { 44 want := &Error{ 45 Type: "internal_error", 46 Title: "Internal Server Error", 47 Detail: "Error making request", 48 StatusCode: http.StatusInternalServerError, 49 } 50 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 51 }, 52 }, 53 } 54 55 for name, test := range tests { 56 t.Run(name, func(t *testing.T) { 57 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 58 assert.Equal(t, test.expectedPath, r.URL.String()) 59 assert.Equal(t, http.MethodGet, r.Method) 60 w.WriteHeader(test.responseStatus) 61 _, err := w.Write([]byte(test.responseBody)) 62 assert.NoError(t, err) 63 })) 64 client := mockAPIClient(t, mockServer) 65 result, err := client.SupportedCountries(context.Background()) 66 if test.withError != nil { 67 test.withError(t, err) 68 return 69 } 70 require.NoError(t, err) 71 assert.Equal(t, test.expectedResponse, result) 72 }) 73 } 74 } 75 76 func TestIAM_SupportedTimezones(t *testing.T) { 77 tests := map[string]struct { 78 responseStatus int 79 responseBody string 80 expectedPath string 81 expectedResponse []Timezone 82 withError func(*testing.T, error) 83 }{ 84 "200 OK": { 85 responseStatus: http.StatusOK, 86 responseBody: ` 87 [ 88 { 89 "timezone": "Asia/Rangoon", 90 "description": "Asia/Rangoon GMT+6", 91 "offset": "+6", 92 "posix": "Asia/Rangoon" 93 } 94 ]`, 95 expectedPath: "/identity-management/v2/user-admin/common/timezones", 96 expectedResponse: []Timezone{ 97 { 98 Timezone: "Asia/Rangoon", 99 Description: "Asia/Rangoon GMT+6", 100 Offset: "+6", 101 Posix: "Asia/Rangoon", 102 }, 103 }, 104 }, 105 "500 internal server error": { 106 responseStatus: http.StatusInternalServerError, 107 responseBody: ` 108 { 109 "type": "internal_error", 110 "title": "Internal Server Error", 111 "detail": "Error making request", 112 "status": 500 113 }`, 114 expectedPath: "/identity-management/v2/user-admin/common/timezones", 115 withError: func(t *testing.T, err error) { 116 want := &Error{ 117 Type: "internal_error", 118 Title: "Internal Server Error", 119 Detail: "Error making request", 120 StatusCode: http.StatusInternalServerError, 121 } 122 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 123 }, 124 }, 125 } 126 127 for name, test := range tests { 128 t.Run(name, func(t *testing.T) { 129 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 130 assert.Equal(t, test.expectedPath, r.URL.String()) 131 assert.Equal(t, http.MethodGet, r.Method) 132 w.WriteHeader(test.responseStatus) 133 _, err := w.Write([]byte(test.responseBody)) 134 assert.NoError(t, err) 135 })) 136 client := mockAPIClient(t, mockServer) 137 result, err := client.SupportedTimezones(context.Background()) 138 if test.withError != nil { 139 test.withError(t, err) 140 return 141 } 142 require.NoError(t, err) 143 assert.Equal(t, test.expectedResponse, result) 144 }) 145 } 146 } 147 148 func TestIAM_SupportedContactTypes(t *testing.T) { 149 tests := map[string]struct { 150 responseStatus int 151 responseBody string 152 expectedPath string 153 expectedResponse []string 154 withError func(*testing.T, error) 155 }{ 156 "200 OK": { 157 responseStatus: http.StatusOK, 158 responseBody: ` 159 [ 160 "Billing", 161 "Security" 162 ]`, 163 expectedPath: "/identity-management/v2/user-admin/common/contact-types", 164 expectedResponse: []string{"Billing", "Security"}, 165 }, 166 "500 internal server error": { 167 responseStatus: http.StatusInternalServerError, 168 responseBody: ` 169 { 170 "type": "internal_error", 171 "title": "Internal Server Error", 172 "detail": "Error making request", 173 "status": 500 174 }`, 175 expectedPath: "/identity-management/v2/user-admin/common/contact-types", 176 withError: func(t *testing.T, err error) { 177 want := &Error{ 178 Type: "internal_error", 179 Title: "Internal Server Error", 180 Detail: "Error making request", 181 StatusCode: http.StatusInternalServerError, 182 } 183 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 184 }, 185 }, 186 } 187 188 for name, test := range tests { 189 t.Run(name, func(t *testing.T) { 190 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 191 assert.Equal(t, test.expectedPath, r.URL.String()) 192 assert.Equal(t, http.MethodGet, r.Method) 193 w.WriteHeader(test.responseStatus) 194 _, err := w.Write([]byte(test.responseBody)) 195 assert.NoError(t, err) 196 })) 197 client := mockAPIClient(t, mockServer) 198 result, err := client.SupportedContactTypes(context.Background()) 199 if test.withError != nil { 200 test.withError(t, err) 201 return 202 } 203 require.NoError(t, err) 204 assert.Equal(t, test.expectedResponse, result) 205 }) 206 } 207 } 208 209 func TestIAM_SupportedLanguages(t *testing.T) { 210 tests := map[string]struct { 211 responseStatus int 212 responseBody string 213 expectedPath string 214 expectedResponse []string 215 withError func(*testing.T, error) 216 }{ 217 "200 OK": { 218 responseStatus: http.StatusOK, 219 responseBody: ` 220 [ 221 "Deutsch", 222 "English" 223 ]`, 224 expectedPath: "/identity-management/v2/user-admin/common/supported-languages", 225 expectedResponse: []string{"Deutsch", "English"}, 226 }, 227 "500 internal server error": { 228 responseStatus: http.StatusInternalServerError, 229 responseBody: ` 230 { 231 "type": "internal_error", 232 "title": "Internal Server Error", 233 "detail": "Error making request", 234 "status": 500 235 }`, 236 expectedPath: "/identity-management/v2/user-admin/common/supported-languages", 237 withError: func(t *testing.T, err error) { 238 want := &Error{ 239 Type: "internal_error", 240 Title: "Internal Server Error", 241 Detail: "Error making request", 242 StatusCode: http.StatusInternalServerError, 243 } 244 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 245 }, 246 }, 247 } 248 249 for name, test := range tests { 250 t.Run(name, func(t *testing.T) { 251 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 252 assert.Equal(t, test.expectedPath, r.URL.String()) 253 assert.Equal(t, http.MethodGet, r.Method) 254 w.WriteHeader(test.responseStatus) 255 _, err := w.Write([]byte(test.responseBody)) 256 assert.NoError(t, err) 257 })) 258 client := mockAPIClient(t, mockServer) 259 result, err := client.SupportedLanguages(context.Background()) 260 if test.withError != nil { 261 test.withError(t, err) 262 return 263 } 264 require.NoError(t, err) 265 assert.Equal(t, test.expectedResponse, result) 266 }) 267 } 268 } 269 270 func TestIAM_ListProducts(t *testing.T) { 271 tests := map[string]struct { 272 responseStatus int 273 responseBody string 274 expectedPath string 275 expectedResponse []string 276 withError func(*testing.T, error) 277 }{ 278 "200 OK": { 279 responseStatus: http.StatusOK, 280 responseBody: ` 281 [ 282 "EdgeComputing for Java", 283 "Streaming" 284 ]`, 285 expectedPath: "/identity-management/v2/user-admin/common/notification-products", 286 expectedResponse: []string{"EdgeComputing for Java", "Streaming"}, 287 }, 288 "500 internal server error": { 289 responseStatus: http.StatusInternalServerError, 290 responseBody: ` 291 { 292 "type": "internal_error", 293 "title": "Internal Server Error", 294 "detail": "Error making request", 295 "status": 500 296 }`, 297 expectedPath: "/identity-management/v2/user-admin/common/notification-products", 298 withError: func(t *testing.T, err error) { 299 want := &Error{ 300 Type: "internal_error", 301 Title: "Internal Server Error", 302 Detail: "Error making request", 303 StatusCode: http.StatusInternalServerError, 304 } 305 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 306 }, 307 }, 308 } 309 310 for name, test := range tests { 311 t.Run(name, func(t *testing.T) { 312 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 313 assert.Equal(t, test.expectedPath, r.URL.String()) 314 assert.Equal(t, http.MethodGet, r.Method) 315 w.WriteHeader(test.responseStatus) 316 _, err := w.Write([]byte(test.responseBody)) 317 assert.NoError(t, err) 318 })) 319 client := mockAPIClient(t, mockServer) 320 result, err := client.ListProducts(context.Background()) 321 if test.withError != nil { 322 test.withError(t, err) 323 return 324 } 325 require.NoError(t, err) 326 assert.Equal(t, test.expectedResponse, result) 327 }) 328 } 329 } 330 331 func TestIAM_ListTimeoutPolicies(t *testing.T) { 332 tests := map[string]struct { 333 responseStatus int 334 responseBody string 335 expectedPath string 336 expectedResponse []TimeoutPolicy 337 withError func(*testing.T, error) 338 }{ 339 "200 OK": { 340 responseStatus: http.StatusOK, 341 responseBody: ` 342 [ 343 { 344 "name": "after15Minutes", 345 "value": 900 346 }, 347 { 348 "name": "after30Minutes", 349 "value": 1800 350 } 351 ]`, 352 expectedPath: "/identity-management/v2/user-admin/common/timeout-policies", 353 expectedResponse: []TimeoutPolicy{ 354 { 355 Name: "after15Minutes", 356 Value: 900, 357 }, 358 { 359 Name: "after30Minutes", 360 Value: 1800, 361 }, 362 }, 363 }, 364 "500 internal server error": { 365 responseStatus: http.StatusInternalServerError, 366 responseBody: ` 367 { 368 "type": "internal_error", 369 "title": "Internal Server Error", 370 "detail": "Error making request", 371 "status": 500 372 }`, 373 expectedPath: "/identity-management/v2/user-admin/common/timeout-policies", 374 withError: func(t *testing.T, err error) { 375 want := &Error{ 376 Type: "internal_error", 377 Title: "Internal Server Error", 378 Detail: "Error making request", 379 StatusCode: http.StatusInternalServerError, 380 } 381 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 382 }, 383 }, 384 } 385 386 for name, test := range tests { 387 t.Run(name, func(t *testing.T) { 388 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 389 assert.Equal(t, test.expectedPath, r.URL.String()) 390 assert.Equal(t, http.MethodGet, r.Method) 391 w.WriteHeader(test.responseStatus) 392 _, err := w.Write([]byte(test.responseBody)) 393 assert.NoError(t, err) 394 })) 395 client := mockAPIClient(t, mockServer) 396 result, err := client.ListTimeoutPolicies(context.Background()) 397 if test.withError != nil { 398 test.withError(t, err) 399 return 400 } 401 require.NoError(t, err) 402 assert.Equal(t, test.expectedResponse, result) 403 }) 404 } 405 } 406 407 func TestIAM_ListStates(t *testing.T) { 408 tests := map[string]struct { 409 params ListStatesRequest 410 responseStatus int 411 responseBody string 412 expectedPath string 413 expectedResponse []string 414 withError func(*testing.T, error) 415 }{ 416 "200 OK": { 417 params: ListStatesRequest{ 418 Country: "canada", 419 }, 420 responseStatus: http.StatusOK, 421 responseBody: ` 422 [ 423 "AB", 424 "BC" 425 ]`, 426 expectedPath: "/identity-management/v2/user-admin/common/countries/canada/states", 427 expectedResponse: []string{"AB", "BC"}, 428 }, 429 "500 internal server error": { 430 params: ListStatesRequest{ 431 Country: "canada", 432 }, 433 responseStatus: http.StatusInternalServerError, 434 responseBody: ` 435 { 436 "type": "internal_error", 437 "title": "Internal Server Error", 438 "detail": "Error making request", 439 "status": 500 440 }`, 441 expectedPath: "/identity-management/v2/user-admin/common/countries/canada/states", 442 withError: func(t *testing.T, err error) { 443 want := &Error{ 444 Type: "internal_error", 445 Title: "Internal Server Error", 446 Detail: "Error making request", 447 StatusCode: http.StatusInternalServerError, 448 } 449 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 450 }, 451 }, 452 "missing country": { 453 params: ListStatesRequest{}, 454 withError: func(t *testing.T, err error) { 455 want := ErrStructValidation 456 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 457 }, 458 }, 459 } 460 461 for name, test := range tests { 462 t.Run(name, func(t *testing.T) { 463 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 464 assert.Equal(t, test.expectedPath, r.URL.String()) 465 assert.Equal(t, http.MethodGet, r.Method) 466 w.WriteHeader(test.responseStatus) 467 _, err := w.Write([]byte(test.responseBody)) 468 assert.NoError(t, err) 469 })) 470 client := mockAPIClient(t, mockServer) 471 result, err := client.ListStates(context.Background(), test.params) 472 if test.withError != nil { 473 test.withError(t, err) 474 return 475 } 476 require.NoError(t, err) 477 assert.Equal(t, test.expectedResponse, result) 478 }) 479 } 480 }