github.com/aiven/aiven-go-client@v1.36.0/accounts_test.go (about) 1 package aiven 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "reflect" 8 "testing" 9 "time" 10 ) 11 12 func setupAccountsTestCase(t *testing.T) (*Client, func(t *testing.T)) { 13 t.Log("setup Accounts test case") 14 15 const ( 16 UserName = "test@aiven.io" 17 UserPassword = "testabcd" 18 AccessToken = "some-random-token" 19 ) 20 21 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 22 if r.URL.Path == "/userauth" { 23 w.Header().Set("Content-Type", "application/json") 24 w.WriteHeader(http.StatusOK) 25 err := json.NewEncoder(w).Encode(authResponse{ 26 Token: AccessToken, 27 State: "active", 28 }) 29 30 if err != nil { 31 t.Error(err) 32 } 33 return 34 } 35 36 if r.URL.Path == "/account" { 37 // get a list of account 38 if r.Method == "GET" { 39 w.Header().Set("Content-Type", "application/json") 40 w.WriteHeader(http.StatusOK) 41 err := json.NewEncoder(w).Encode(AccountsResponse{ 42 APIResponse: APIResponse{}, 43 Accounts: []Account{ 44 { 45 Id: "a28707e316df", 46 Name: "test@aiven.fi", 47 OwnerTeamId: "at28707cc9aa3", 48 CreateTime: getTime(t), 49 UpdateTime: getTime(t), 50 BillingEnabled: false, 51 TenantId: "aiven", 52 }, 53 }, 54 }) 55 56 if err != nil { 57 t.Error(err) 58 } 59 return 60 } 61 62 // create mew account 63 if r.Method == "POST" { 64 w.Header().Set("Content-Type", "application/json") 65 w.WriteHeader(http.StatusOK) 66 err := json.NewEncoder(w).Encode(getAccountResponse(t)) 67 68 if err != nil { 69 t.Error(err) 70 } 71 return 72 } 73 } 74 75 // get account by id 76 if r.URL.Path == "/account/a28707e316df" { 77 w.Header().Set("Content-Type", "application/json") 78 w.WriteHeader(http.StatusOK) 79 err := json.NewEncoder(w).Encode(getAccountResponse(t)) 80 81 if err != nil { 82 t.Error(err) 83 } 84 return 85 } 86 87 })) 88 89 apiUrl = ts.URL 90 91 c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version()) 92 if err != nil { 93 t.Fatalf("user authentication error: %s", err) 94 } 95 96 return c, func(t *testing.T) { 97 t.Log("teardown Accounts test case") 98 ts.Close() 99 } 100 } 101 102 func getTime(t *testing.T) *time.Time { 103 var err error 104 tt, err := time.Parse(time.RFC3339, "2020-01-15T10:35:33Z") 105 if err != nil { 106 t.Error(err) 107 } 108 109 return &tt 110 } 111 112 func TestAccountsHandler_List(t *testing.T) { 113 c, tearDown := setupAccountsTestCase(t) 114 defer tearDown(t) 115 116 type fields struct { 117 client *Client 118 } 119 tests := []struct { 120 name string 121 fields fields 122 want *AccountsResponse 123 wantErr bool 124 }{ 125 { 126 "", 127 fields{client: c}, 128 &AccountsResponse{ 129 APIResponse: APIResponse{}, 130 Accounts: []Account{ 131 { 132 Id: "a28707e316df", 133 Name: "test@aiven.fi", 134 OwnerTeamId: "at28707cc9aa3", 135 CreateTime: getTime(t), 136 UpdateTime: getTime(t), 137 BillingEnabled: false, 138 TenantId: "aiven", 139 }, 140 }, 141 }, 142 false, 143 }, 144 } 145 for _, tt := range tests { 146 t.Run(tt.name, func(t *testing.T) { 147 h := AccountsHandler{ 148 client: tt.fields.client, 149 } 150 got, err := h.List() 151 if (err != nil) != tt.wantErr { 152 t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr) 153 return 154 } 155 if !reflect.DeepEqual(got, tt.want) { 156 t.Errorf("Get() got = %v, want %v", got, tt.want) 157 } 158 }) 159 } 160 } 161 162 func TestAccountsHandler_Get(t *testing.T) { 163 c, tearDown := setupAccountsTestCase(t) 164 defer tearDown(t) 165 166 type fields struct { 167 client *Client 168 } 169 type args struct { 170 id string 171 } 172 tests := []struct { 173 name string 174 fields fields 175 args args 176 want *AccountResponse 177 wantErr bool 178 }{ 179 { 180 "normal", 181 fields{client: c}, 182 args{id: "a28707e316df"}, 183 getAccountResponse(t), 184 false, 185 }, 186 { 187 "error-empty-id", 188 fields{client: c}, 189 args{id: ""}, 190 nil, 191 true, 192 }, 193 } 194 for _, tt := range tests { 195 t.Run(tt.name, func(t *testing.T) { 196 h := AccountsHandler{ 197 client: tt.fields.client, 198 } 199 got, err := h.Get(tt.args.id) 200 if (err != nil) != tt.wantErr { 201 t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) 202 return 203 } 204 if !reflect.DeepEqual(got, tt.want) { 205 t.Errorf("Get() got = %v, want %v", got, tt.want) 206 } 207 }) 208 } 209 } 210 211 func getAccountResponse(t *testing.T) *AccountResponse { 212 return &AccountResponse{ 213 APIResponse: APIResponse{}, 214 Account: Account{ 215 Id: "a28707e316df", 216 Name: "test@aiven.fi", 217 CreateTime: getTime(t), 218 UpdateTime: getTime(t), 219 BillingEnabled: false, 220 TenantId: "aiven", 221 }, 222 } 223 } 224 225 func TestAccountsHandler_Create(t *testing.T) { 226 c, tearDown := setupAccountsTestCase(t) 227 defer tearDown(t) 228 229 type fields struct { 230 client *Client 231 } 232 type args struct { 233 account Account 234 } 235 tests := []struct { 236 name string 237 fields fields 238 args args 239 want *AccountResponse 240 wantErr bool 241 }{ 242 { 243 "", 244 fields{client: c}, 245 args{account: Account{ 246 Name: "test@aiven.fi", 247 }}, 248 getAccountResponse(t), 249 false, 250 }, 251 } 252 for _, tt := range tests { 253 t.Run(tt.name, func(t *testing.T) { 254 h := AccountsHandler{ 255 client: tt.fields.client, 256 } 257 got, err := h.Create(tt.args.account) 258 if (err != nil) != tt.wantErr { 259 t.Errorf("Create() error = %v, wantErr %v", err, tt.wantErr) 260 return 261 } 262 if !reflect.DeepEqual(got, tt.want) { 263 t.Errorf("Create() got = %v, want %v", got, tt.want) 264 } 265 }) 266 } 267 } 268 269 func TestAccountsHandler_Update(t *testing.T) { 270 c, tearDown := setupAccountsTestCase(t) 271 defer tearDown(t) 272 273 type fields struct { 274 client *Client 275 } 276 type args struct { 277 id string 278 account Account 279 } 280 tests := []struct { 281 name string 282 fields fields 283 args args 284 want *AccountResponse 285 wantErr bool 286 }{ 287 { 288 "normal", 289 fields{client: c}, 290 args{ 291 id: "a28707e316df", 292 account: Account{ 293 Name: "test@aiven.fi", 294 }, 295 }, 296 getAccountResponse(t), 297 false, 298 }, 299 { 300 "error-empty-id", 301 fields{client: c}, 302 args{ 303 id: "", 304 account: Account{ 305 Name: "test@aiven.fi", 306 }, 307 }, 308 nil, 309 true, 310 }, 311 } 312 for _, tt := range tests { 313 t.Run(tt.name, func(t *testing.T) { 314 h := AccountsHandler{ 315 client: tt.fields.client, 316 } 317 got, err := h.Update(tt.args.id, tt.args.account) 318 if (err != nil) != tt.wantErr { 319 t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr) 320 return 321 } 322 if !reflect.DeepEqual(got, tt.want) { 323 t.Errorf("Update() got = %v, want %v", got, tt.want) 324 } 325 }) 326 } 327 } 328 329 func TestAccountsHandler_Delete(t *testing.T) { 330 c, tearDown := setupAccountsTestCase(t) 331 defer tearDown(t) 332 333 type fields struct { 334 client *Client 335 } 336 type args struct { 337 id string 338 } 339 tests := []struct { 340 name string 341 fields fields 342 args args 343 wantErr bool 344 }{ 345 { 346 "normal", 347 fields{client: c}, 348 args{id: "a28707e316df"}, 349 false, 350 }, 351 { 352 "error-empty-id", 353 fields{client: c}, 354 args{id: ""}, 355 true, 356 }, 357 } 358 for _, tt := range tests { 359 t.Run(tt.name, func(t *testing.T) { 360 h := AccountsHandler{ 361 client: tt.fields.client, 362 } 363 if err := h.Delete(tt.args.id); (err != nil) != tt.wantErr { 364 t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr) 365 } 366 }) 367 } 368 }