github.com/aiven/aiven-go-client@v1.36.0/account_team_projects_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 setupAccountTeamProjectsTestCase(t *testing.T) (*Client, func(t *testing.T)) {
    12  	t.Log("setup Account Team Projects 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/team/at28707ea77e2/projects" {
    36  			w.Header().Set("Content-Type", "application/json")
    37  			w.WriteHeader(http.StatusOK)
    38  			err := json.NewEncoder(w).Encode(AccountTeamProjectsResponse{
    39  				Projects: []AccountTeamProject{
    40  					{
    41  						ProjectName: "test-pr",
    42  						TeamType:    "admin",
    43  					},
    44  				},
    45  			})
    46  
    47  			if err != nil {
    48  				t.Error(err)
    49  			}
    50  			return
    51  		}
    52  
    53  		if r.URL.Path == "/account/a28707e316df/team/at28707ea77e2/project/test-pr" {
    54  			w.Header().Set("Content-Type", "application/json")
    55  			w.WriteHeader(http.StatusOK)
    56  			err := json.NewEncoder(w).Encode(struct {
    57  				APIResponse
    58  				Message string `json:"message"`
    59  			}{
    60  				Message: "associated",
    61  			})
    62  
    63  			if err != nil {
    64  				t.Error(err)
    65  			}
    66  			return
    67  		}
    68  
    69  	}))
    70  
    71  	apiUrl = ts.URL
    72  
    73  	c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version())
    74  	if err != nil {
    75  		t.Fatalf("user authentication error: %s", err)
    76  	}
    77  
    78  	return c, func(t *testing.T) {
    79  		t.Log("teardown Accounts Team Projects test case")
    80  		ts.Close()
    81  	}
    82  }
    83  
    84  func TestAccountTeamProjectsHandler_List(t *testing.T) {
    85  	c, tearDown := setupAccountTeamProjectsTestCase(t)
    86  	defer tearDown(t)
    87  
    88  	type fields struct {
    89  		client *Client
    90  	}
    91  	type args struct {
    92  		accountId string
    93  		teamId    string
    94  	}
    95  	tests := []struct {
    96  		name    string
    97  		fields  fields
    98  		args    args
    99  		want    *AccountTeamProjectsResponse
   100  		wantErr bool
   101  	}{
   102  		{
   103  			"normal",
   104  			fields{client: c},
   105  			args{
   106  				accountId: "a28707e316df",
   107  				teamId:    "at28707ea77e2",
   108  			},
   109  			&AccountTeamProjectsResponse{
   110  				APIResponse: APIResponse{},
   111  				Projects: []AccountTeamProject{
   112  					{
   113  						ProjectName: "test-pr",
   114  						TeamType:    "admin",
   115  					},
   116  				},
   117  			},
   118  			false,
   119  		},
   120  		{
   121  			"empty-account-id",
   122  			fields{client: c},
   123  			args{
   124  				accountId: "",
   125  				teamId:    "at28707ea77e2",
   126  			},
   127  			nil,
   128  			true,
   129  		},
   130  		{
   131  			"empty-team-id",
   132  			fields{client: c},
   133  			args{
   134  				accountId: "a28707e316df",
   135  				teamId:    "",
   136  			},
   137  			nil,
   138  			true,
   139  		},
   140  	}
   141  	for _, tt := range tests {
   142  		t.Run(tt.name, func(t *testing.T) {
   143  			h := AccountTeamProjectsHandler{
   144  				client: tt.fields.client,
   145  			}
   146  			got, err := h.List(tt.args.accountId, tt.args.teamId)
   147  			if (err != nil) != tt.wantErr {
   148  				t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)
   149  				return
   150  			}
   151  			if !reflect.DeepEqual(got, tt.want) {
   152  				t.Errorf("List() got = %v, want %v", got, tt.want)
   153  			}
   154  		})
   155  	}
   156  }
   157  
   158  func TestAccountTeamProjectsHandler_Create(t *testing.T) {
   159  	c, tearDown := setupAccountTeamProjectsTestCase(t)
   160  	defer tearDown(t)
   161  
   162  	type fields struct {
   163  		client *Client
   164  	}
   165  	type args struct {
   166  		accountId string
   167  		teamId    string
   168  		p         AccountTeamProject
   169  	}
   170  	tests := []struct {
   171  		name    string
   172  		fields  fields
   173  		args    args
   174  		wantErr bool
   175  	}{
   176  		{
   177  			"normal",
   178  			fields{client: c},
   179  			args{
   180  				accountId: "a28707e316df",
   181  				teamId:    "at28707ea77e2",
   182  				p: AccountTeamProject{
   183  					ProjectName: "test-pr",
   184  					TeamType:    "admin",
   185  				},
   186  			},
   187  			false,
   188  		},
   189  		{
   190  			"empty-account-id",
   191  			fields{client: c},
   192  			args{
   193  				accountId: "",
   194  				teamId:    "at28707ea77e2",
   195  				p: AccountTeamProject{
   196  					ProjectName: "test-pr",
   197  					TeamType:    "admin",
   198  				},
   199  			},
   200  			true,
   201  		},
   202  		{
   203  			"empty-team-id",
   204  			fields{client: c},
   205  			args{
   206  				accountId: "a28707e316df",
   207  				teamId:    "",
   208  				p: AccountTeamProject{
   209  					ProjectName: "test-pr",
   210  					TeamType:    "admin",
   211  				},
   212  			},
   213  			true,
   214  		},
   215  		{
   216  			"empty-project-name",
   217  			fields{client: c},
   218  			args{
   219  				accountId: "a28707e316df",
   220  				teamId:    "at28707ea77e2",
   221  				p: AccountTeamProject{
   222  					ProjectName: "",
   223  					TeamType:    "admin",
   224  				},
   225  			},
   226  			true,
   227  		},
   228  	}
   229  	for _, tt := range tests {
   230  		t.Run(tt.name, func(t *testing.T) {
   231  			h := AccountTeamProjectsHandler{
   232  				client: tt.fields.client,
   233  			}
   234  			if err := h.Create(tt.args.accountId, tt.args.teamId, tt.args.p); (err != nil) != tt.wantErr {
   235  				t.Errorf("Create() error = %v, wantErr %v", err, tt.wantErr)
   236  			}
   237  		})
   238  	}
   239  }
   240  
   241  func TestAccountTeamProjectsHandler_Update(t *testing.T) {
   242  	c, tearDown := setupAccountTeamProjectsTestCase(t)
   243  	defer tearDown(t)
   244  
   245  	type fields struct {
   246  		client *Client
   247  	}
   248  	type args struct {
   249  		accountId string
   250  		teamId    string
   251  		p         AccountTeamProject
   252  	}
   253  	tests := []struct {
   254  		name    string
   255  		fields  fields
   256  		args    args
   257  		wantErr bool
   258  	}{
   259  		{
   260  			"normal",
   261  			fields{client: c},
   262  			args{
   263  				accountId: "a28707e316df",
   264  				teamId:    "at28707ea77e2",
   265  				p: AccountTeamProject{
   266  					ProjectName: "test-pr",
   267  					TeamType:    "developer",
   268  				},
   269  			},
   270  			false,
   271  		},
   272  		{
   273  			"empty-account-id",
   274  			fields{client: c},
   275  			args{
   276  				accountId: "",
   277  				teamId:    "at28707ea77e2",
   278  				p: AccountTeamProject{
   279  					ProjectName: "test-pr",
   280  					TeamType:    "admin",
   281  				},
   282  			},
   283  			true,
   284  		},
   285  		{
   286  			"empty-team-id",
   287  			fields{client: c},
   288  			args{
   289  				accountId: "a28707e316df",
   290  				teamId:    "",
   291  				p: AccountTeamProject{
   292  					ProjectName: "test-pr",
   293  					TeamType:    "admin",
   294  				},
   295  			},
   296  			true,
   297  		},
   298  		{
   299  			"empty-project-name",
   300  			fields{client: c},
   301  			args{
   302  				accountId: "a28707e316df",
   303  				teamId:    "at28707ea77e2",
   304  				p: AccountTeamProject{
   305  					ProjectName: "",
   306  					TeamType:    "admin",
   307  				},
   308  			},
   309  			true,
   310  		},
   311  	}
   312  	for _, tt := range tests {
   313  		t.Run(tt.name, func(t *testing.T) {
   314  			h := AccountTeamProjectsHandler{
   315  				client: tt.fields.client,
   316  			}
   317  			if err := h.Update(tt.args.accountId, tt.args.teamId, tt.args.p); (err != nil) != tt.wantErr {
   318  				t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr)
   319  			}
   320  		})
   321  	}
   322  }
   323  
   324  func TestAccountTeamProjectsHandler_Delete(t *testing.T) {
   325  	c, tearDown := setupAccountTeamProjectsTestCase(t)
   326  	defer tearDown(t)
   327  
   328  	type fields struct {
   329  		client *Client
   330  	}
   331  	type args struct {
   332  		accountId   string
   333  		teamId      string
   334  		projectName string
   335  	}
   336  	tests := []struct {
   337  		name    string
   338  		fields  fields
   339  		args    args
   340  		wantErr bool
   341  	}{
   342  		{
   343  			"normal",
   344  			fields{client: c},
   345  			args{
   346  				accountId:   "a28707e316df",
   347  				teamId:      "at28707ea77e2",
   348  				projectName: "test-pr",
   349  			},
   350  			false,
   351  		},
   352  		{
   353  			"empty-account-id",
   354  			fields{client: c},
   355  			args{
   356  				accountId:   "",
   357  				teamId:      "at28707ea77e2",
   358  				projectName: "test-pr",
   359  			},
   360  			true,
   361  		},
   362  		{
   363  			"empty-team-id",
   364  			fields{client: c},
   365  			args{
   366  				accountId:   "a28707e316df",
   367  				teamId:      "",
   368  				projectName: "test-pr",
   369  			},
   370  			true,
   371  		},
   372  		{
   373  			"empty-project-name",
   374  			fields{client: c},
   375  			args{
   376  				accountId:   "a28707e316df",
   377  				teamId:      "at28707ea77e2",
   378  				projectName: "",
   379  			},
   380  			true,
   381  		},
   382  	}
   383  	for _, tt := range tests {
   384  		t.Run(tt.name, func(t *testing.T) {
   385  			h := AccountTeamProjectsHandler{
   386  				client: tt.fields.client,
   387  			}
   388  			if err := h.Delete(tt.args.accountId, tt.args.teamId, tt.args.projectName); (err != nil) != tt.wantErr {
   389  				t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
   390  			}
   391  		})
   392  	}
   393  }