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