github.com/aiven/aiven-go-client@v1.36.0/account_authentications_test.go (about) 1 package aiven 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "reflect" 8 "testing" 9 ) 10 11 func setupAccountAuthenticationsTestCase(t *testing.T) (*Client, func(t *testing.T)) { 12 t.Log("setup Account Authentications test case") 13 14 const ( 15 UserName = "test@aiven.io" 16 UserPassword = "testabcd" 17 AccessToken = "some-random-token" 18 ) 19 20 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 21 if r.URL.Path == "/userauth" { 22 w.Header().Set("Content-Type", "application/json") 23 w.WriteHeader(http.StatusOK) 24 err := json.NewEncoder(w).Encode(authResponse{ 25 Token: AccessToken, 26 State: "active", 27 }) 28 29 if err != nil { 30 t.Error(err) 31 } 32 return 33 } 34 35 if r.URL.Path == "/account/a28707e316df/authentication/am28707eb0055" { 36 w.Header().Set("Content-Type", "application/json") 37 w.WriteHeader(http.StatusOK) 38 39 var err error 40 switch r.Method { 41 case "PUT": 42 err = json.NewEncoder(w).Encode(AccountAuthenticationResponse{ 43 APIResponse: APIResponse{}, 44 AuthenticationMethod: AccountAuthenticationMethod{ 45 AccountID: "a28707e316df", 46 State: "active", 47 CreateTime: getTime(t), 48 UpdateTime: getTime(t), 49 DeleteTime: getTime(t), 50 }, 51 }) 52 default: 53 err = json.NewEncoder(w).Encode(AccountAuthenticationResponse{ 54 APIResponse: APIResponse{}, 55 AuthenticationMethod: AccountAuthenticationMethod{ 56 AccountID: "a28707e316df", 57 AuthenticationMethodEnabled: true, 58 AuthenticationMethodName: "test", 59 AuthenticationMethodType: "saml", 60 State: "active", 61 CreateTime: getTime(t), 62 UpdateTime: getTime(t), 63 DeleteTime: getTime(t), 64 }, 65 }) 66 } 67 68 if err != nil { 69 t.Error(err) 70 } 71 return 72 } 73 74 if r.URL.Path == "/account/a28707e316df/authentication" { 75 if r.Method == "POST" { 76 w.Header().Set("Content-Type", "application/json") 77 w.WriteHeader(http.StatusOK) 78 err := json.NewEncoder(w).Encode(AccountAuthenticationResponse{ 79 APIResponse: APIResponse{}, 80 AuthenticationMethod: AccountAuthenticationMethod{ 81 AccountID: "a28707e316df", 82 AuthenticationMethodEnabled: true, 83 AuthenticationMethodName: "test", 84 AuthenticationMethodType: "saml", 85 State: "active", 86 CreateTime: getTime(t), 87 UpdateTime: getTime(t), 88 DeleteTime: getTime(t), 89 }, 90 }) 91 92 if err != nil { 93 t.Error(err) 94 } 95 return 96 } 97 98 w.Header().Set("Content-Type", "application/json") 99 w.WriteHeader(http.StatusOK) 100 err := json.NewEncoder(w).Encode(AccountAuthenticationListResponse{ 101 AuthenticationMethods: []AccountAuthenticationMethod{ 102 { 103 AccountID: "a28707e316df", 104 AuthenticationMethodID: "am28707eb0055", 105 AuthenticationMethodEnabled: true, 106 AuthenticationMethodName: "Platform authentication", 107 AuthenticationMethodType: "internal", 108 State: "active", 109 CreateTime: getTime(t), 110 UpdateTime: getTime(t), 111 DeleteTime: getTime(t), 112 }, 113 }, 114 }) 115 116 if err != nil { 117 t.Error(err) 118 } 119 return 120 } 121 122 })) 123 124 apiUrl = ts.URL 125 126 c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version()) 127 if err != nil { 128 t.Fatalf("user authentication error: %s", err) 129 } 130 131 return c, func(t *testing.T) { 132 t.Log("teardown Accounts Authentications test case") 133 ts.Close() 134 } 135 } 136 137 func TestAccountAuthenticationsHandler_List(t *testing.T) { 138 c, tearDown := setupAccountAuthenticationsTestCase(t) 139 defer tearDown(t) 140 141 type fields struct { 142 client *Client 143 } 144 type args struct { 145 accountId string 146 } 147 tests := []struct { 148 name string 149 fields fields 150 args args 151 want *AccountAuthenticationListResponse 152 wantErr bool 153 }{ 154 { 155 "normal", 156 fields{client: c}, 157 args{accountId: "a28707e316df"}, 158 &AccountAuthenticationListResponse{ 159 APIResponse: APIResponse{}, 160 AuthenticationMethods: []AccountAuthenticationMethod{ 161 { 162 AccountID: "a28707e316df", 163 AuthenticationMethodEnabled: true, 164 AuthenticationMethodID: "am28707eb0055", 165 AuthenticationMethodName: "Platform authentication", 166 AuthenticationMethodType: "internal", 167 State: "active", 168 CreateTime: getTime(t), 169 UpdateTime: getTime(t), 170 DeleteTime: getTime(t), 171 }, 172 }, 173 }, 174 false, 175 }, 176 { 177 "empty-account-id", 178 fields{client: c}, 179 args{accountId: ""}, 180 nil, 181 true, 182 }, 183 } 184 for _, tt := range tests { 185 t.Run(tt.name, func(t *testing.T) { 186 h := AccountAuthenticationsHandler{ 187 client: tt.fields.client, 188 } 189 got, err := h.List(tt.args.accountId) 190 if (err != nil) != tt.wantErr { 191 t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr) 192 return 193 } 194 if !reflect.DeepEqual(got, tt.want) { 195 t.Errorf("List() got = %v, want %v", got, tt.want) 196 } 197 }) 198 } 199 } 200 201 func TestAccountAuthenticationsHandler_Create(t *testing.T) { 202 c, tearDown := setupAccountAuthenticationsTestCase(t) 203 defer tearDown(t) 204 205 type fields struct { 206 client *Client 207 } 208 type args struct { 209 accountId string 210 accountAuthMethId string 211 a AccountAuthenticationMethodCreate 212 } 213 tests := []struct { 214 name string 215 fields fields 216 args args 217 want *AccountAuthenticationResponse 218 wantErr bool 219 }{ 220 { 221 "normal", 222 fields{client: c}, 223 args{ 224 accountId: "a28707e316df", 225 accountAuthMethId: "am28707eb0055", 226 a: AccountAuthenticationMethodCreate{ 227 AuthenticationMethodName: "test1", 228 AuthenticationMethodType: "saml", 229 }, 230 }, 231 &AccountAuthenticationResponse{ 232 APIResponse: APIResponse{}, 233 AuthenticationMethod: AccountAuthenticationMethod{ 234 AccountID: "a28707e316df", 235 AuthenticationMethodEnabled: true, 236 AuthenticationMethodName: "test", 237 AuthenticationMethodType: "saml", 238 State: "active", 239 CreateTime: getTime(t), 240 UpdateTime: getTime(t), 241 DeleteTime: getTime(t), 242 }, 243 }, 244 false, 245 }, 246 { 247 "empty-account-id", 248 fields{client: c}, 249 args{ 250 accountId: "", 251 a: AccountAuthenticationMethodCreate{ 252 AuthenticationMethodName: "test1", 253 AuthenticationMethodType: "saml", 254 }, 255 }, 256 nil, 257 true, 258 }, 259 } 260 for _, tt := range tests { 261 t.Run(tt.name, func(t *testing.T) { 262 h := AccountAuthenticationsHandler{ 263 client: tt.fields.client, 264 } 265 got, err := h.Create(tt.args.accountId, tt.args.a) 266 if (err != nil) != tt.wantErr { 267 t.Errorf("Create() error = %v, wantErr %v", err, tt.wantErr) 268 return 269 } 270 if !reflect.DeepEqual(got, tt.want) { 271 t.Errorf("Create() got = %v, want %v", got, tt.want) 272 } 273 }) 274 } 275 } 276 277 func TestAccountAuthenticationsHandler_Update(t *testing.T) { 278 c, tearDown := setupAccountAuthenticationsTestCase(t) 279 defer tearDown(t) 280 281 type fields struct { 282 client *Client 283 } 284 type args struct { 285 accountId string 286 accountAuthMethId string 287 a AccountAuthenticationMethodUpdate 288 } 289 tests := []struct { 290 name string 291 fields fields 292 args args 293 want *AccountAuthenticationResponse 294 wantErr bool 295 }{ 296 { 297 "normal", 298 fields{client: c}, 299 args{ 300 accountId: "a28707e316df", 301 accountAuthMethId: "am28707eb0055", 302 a: AccountAuthenticationMethodUpdate{ 303 AuthenticationMethodEnabled: true, 304 AuthenticationMethodName: "test", 305 }, 306 }, 307 &AccountAuthenticationResponse{ 308 APIResponse: APIResponse{}, 309 AuthenticationMethod: AccountAuthenticationMethod{ 310 AccountID: "a28707e316df", 311 State: "active", 312 CreateTime: getTime(t), 313 UpdateTime: getTime(t), 314 DeleteTime: getTime(t), 315 }, 316 }, 317 false, 318 }, 319 { 320 "empty-account-id", 321 fields{client: c}, 322 args{ 323 accountId: "", 324 accountAuthMethId: "am28707eb0055", 325 a: AccountAuthenticationMethodUpdate{ 326 AuthenticationMethodEnabled: true, 327 AuthenticationMethodName: "test", 328 }, 329 }, 330 nil, 331 true, 332 }, 333 { 334 "empty-id", 335 fields{client: c}, 336 args{ 337 accountId: "a28707e316df", 338 accountAuthMethId: "", 339 a: AccountAuthenticationMethodUpdate{ 340 AuthenticationMethodEnabled: true, 341 AuthenticationMethodName: "test", 342 }, 343 }, 344 nil, 345 true, 346 }, 347 } 348 for _, tt := range tests { 349 t.Run(tt.name, func(t *testing.T) { 350 h := AccountAuthenticationsHandler{ 351 client: tt.fields.client, 352 } 353 got, err := h.Update(tt.args.accountId, tt.args.accountAuthMethId, tt.args.a) 354 if (err != nil) != tt.wantErr { 355 t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr) 356 return 357 } 358 if !reflect.DeepEqual(got, tt.want) { 359 t.Errorf("Update() got = %v, want %v", got, tt.want) 360 } 361 }) 362 } 363 } 364 365 func TestAccountAuthenticationsHandler_Delete(t *testing.T) { 366 c, tearDown := setupAccountAuthenticationsTestCase(t) 367 defer tearDown(t) 368 369 type fields struct { 370 client *Client 371 } 372 type args struct { 373 accountId string 374 authId string 375 } 376 tests := []struct { 377 name string 378 fields fields 379 args args 380 wantErr bool 381 }{ 382 { 383 "normal", 384 fields{client: c}, 385 args{ 386 accountId: "a28707e316df", 387 authId: "am28707eb0055", 388 }, 389 false, 390 }, 391 { 392 "empty-account-id", 393 fields{client: c}, 394 args{ 395 accountId: "", 396 authId: "am28707eb0055", 397 }, 398 true, 399 }, { 400 "empty-auth-id", 401 fields{client: c}, 402 args{ 403 accountId: "a28707e316df", 404 authId: "", 405 }, 406 true, 407 }, 408 } 409 for _, tt := range tests { 410 t.Run(tt.name, func(t *testing.T) { 411 h := AccountAuthenticationsHandler{ 412 client: tt.fields.client, 413 } 414 if err := h.Delete(tt.args.accountId, tt.args.authId); (err != nil) != tt.wantErr { 415 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr) 416 } 417 }) 418 } 419 } 420 421 func TestAccountAuthenticationsHandler_Get(t *testing.T) { 422 c, tearDown := setupAccountAuthenticationsTestCase(t) 423 defer tearDown(t) 424 425 type fields struct { 426 client *Client 427 } 428 type args struct { 429 accountId string 430 authId string 431 } 432 tests := []struct { 433 name string 434 fields fields 435 args args 436 want *AccountAuthenticationResponse 437 wantErr bool 438 }{ 439 { 440 "normal", 441 fields{client: c}, 442 args{ 443 accountId: "a28707e316df", 444 authId: "am28707eb0055", 445 }, 446 &AccountAuthenticationResponse{ 447 APIResponse: APIResponse{}, 448 AuthenticationMethod: AccountAuthenticationMethod{ 449 AccountID: "a28707e316df", 450 AuthenticationMethodEnabled: true, 451 AuthenticationMethodName: "test", 452 AuthenticationMethodType: "saml", 453 State: "active", 454 CreateTime: getTime(t), 455 UpdateTime: getTime(t), 456 DeleteTime: getTime(t), 457 }, 458 }, 459 false, 460 }, 461 { 462 "empty-account-id", 463 fields{client: c}, 464 args{ 465 accountId: "", 466 authId: "am28707eb0055", 467 }, 468 nil, 469 true, 470 }, { 471 "empty-auth-id", 472 fields{client: c}, 473 args{ 474 accountId: "a28707e316df", 475 authId: "", 476 }, 477 nil, 478 true, 479 }, 480 } 481 for _, tt := range tests { 482 t.Run(tt.name, func(t *testing.T) { 483 h := AccountAuthenticationsHandler{ 484 client: tt.fields.client, 485 } 486 got, err := h.Get(tt.args.accountId, tt.args.authId) 487 if (err != nil) != tt.wantErr { 488 t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) 489 return 490 } 491 if !reflect.DeepEqual(got, tt.want) { 492 t.Errorf("Get() got = %v, want %v", got, tt.want) 493 } 494 }) 495 } 496 }