github.com/aiven/aiven-go-client@v1.36.0/account_team_invites_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 setupAccountTeamInvitesTestCase(t *testing.T) (*Client, func(t *testing.T)) {
    12  	t.Log("setup Account Team Invites 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  		// list of invitations
    36  		if r.URL.Path == "/account/a28707e316df/team/b28707e316df/invites" {
    37  			w.Header().Set("Content-Type", "application/json")
    38  			w.WriteHeader(http.StatusOK)
    39  			err := json.NewEncoder(w).Encode(AccountTeamInvitesResponse{
    40  				APIResponse: APIResponse{},
    41  				Invites: []AccountTeamInvite{
    42  					{
    43  						AccountId:          "a28707e316df",
    44  						AccountName:        "test2@aiven.fi",
    45  						InvitedByUserEmail: "test1@aiven.fi",
    46  						TeamId:             "b28707e316df",
    47  						TeamName:           "Account Owners",
    48  						UserEmail:          "test_invite_sent_to@aiven.fi",
    49  						CreateTime:         getTime(t),
    50  					},
    51  				},
    52  			})
    53  
    54  			if err != nil {
    55  				t.Error(err)
    56  			}
    57  			return
    58  		}
    59  
    60  		//	delete an invitation
    61  		if r.URL.Path == "/account/a28707e316df/team/b28707e316df/invites/test+1@example.com" {
    62  			w.Header().Set("Content-Type", "application/json")
    63  			w.WriteHeader(http.StatusOK)
    64  			err := json.NewEncoder(w).Encode(APIResponse{})
    65  
    66  			if err != nil {
    67  				t.Error(err)
    68  			}
    69  			return
    70  		}
    71  	}))
    72  
    73  	apiUrl = ts.URL
    74  
    75  	c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version())
    76  	if err != nil {
    77  		t.Fatalf("user authentication error: %s", err)
    78  	}
    79  
    80  	return c, func(t *testing.T) {
    81  		t.Log("teardown Accounts Team Invites test case")
    82  		ts.Close()
    83  	}
    84  }
    85  
    86  func TestAccountTeamInvitesHandler_List(t *testing.T) {
    87  	c, tearDown := setupAccountTeamInvitesTestCase(t)
    88  	defer tearDown(t)
    89  
    90  	type fields struct {
    91  		client *Client
    92  	}
    93  	type args struct {
    94  		accountId string
    95  		teamId    string
    96  	}
    97  	tests := []struct {
    98  		name    string
    99  		fields  fields
   100  		args    args
   101  		want    *AccountTeamInvitesResponse
   102  		wantErr bool
   103  	}{
   104  		{
   105  			"basic",
   106  			fields{client: c},
   107  			args{
   108  				accountId: "a28707e316df",
   109  				teamId:    "b28707e316df",
   110  			},
   111  			&AccountTeamInvitesResponse{
   112  				APIResponse: APIResponse{},
   113  				Invites: []AccountTeamInvite{
   114  					{
   115  						AccountId:          "a28707e316df",
   116  						AccountName:        "test2@aiven.fi",
   117  						InvitedByUserEmail: "test1@aiven.fi",
   118  						TeamId:             "b28707e316df",
   119  						TeamName:           "Account Owners",
   120  						UserEmail:          "test_invite_sent_to@aiven.fi",
   121  						CreateTime:         getTime(t),
   122  					},
   123  				},
   124  			},
   125  			false,
   126  		},
   127  		{
   128  			"empty-account-id",
   129  			fields{client: c},
   130  			args{
   131  				accountId: "",
   132  				teamId:    "b28707e316df",
   133  			},
   134  			nil,
   135  			true,
   136  		},
   137  		{
   138  			"empty-team-id",
   139  			fields{client: c},
   140  			args{
   141  				accountId: "a28707e316dfs",
   142  				teamId:    "",
   143  			},
   144  			nil,
   145  			true,
   146  		},
   147  	}
   148  	for _, tt := range tests {
   149  		t.Run(tt.name, func(t *testing.T) {
   150  			h := AccountTeamInvitesHandler{
   151  				client: tt.fields.client,
   152  			}
   153  			got, err := h.List(tt.args.accountId, tt.args.teamId)
   154  			if (err != nil) != tt.wantErr {
   155  				t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)
   156  				return
   157  			}
   158  			if !reflect.DeepEqual(got, tt.want) {
   159  				t.Errorf("List() got = %v, want %v", got, tt.want)
   160  			}
   161  		})
   162  	}
   163  }
   164  
   165  func TestAccountTeamInvitesHandler_Delete(t *testing.T) {
   166  	c, tearDown := setupAccountTeamInvitesTestCase(t)
   167  	defer tearDown(t)
   168  
   169  	type fields struct {
   170  		client *Client
   171  	}
   172  	type args struct {
   173  		accountId string
   174  		teamId    string
   175  		userEmail string
   176  	}
   177  	tests := []struct {
   178  		name    string
   179  		fields  fields
   180  		args    args
   181  		wantErr bool
   182  	}{
   183  		{
   184  			"normal",
   185  			fields{client: c},
   186  			args{
   187  				accountId: "a28707e316df",
   188  				teamId:    "b28707e316df",
   189  				userEmail: "test+1@example.com",
   190  			},
   191  			false,
   192  		},
   193  		{
   194  			"empty-account-id",
   195  			fields{client: c},
   196  			args{
   197  				accountId: "",
   198  				teamId:    "b28707e316df",
   199  				userEmail: "test+1@example.com",
   200  			},
   201  			true,
   202  		},
   203  		{
   204  			"empty-team-id",
   205  			fields{client: c},
   206  			args{
   207  				accountId: "a28707e316df",
   208  				teamId:    "",
   209  				userEmail: "test+1@example.com",
   210  			},
   211  			true,
   212  		},
   213  		{
   214  			"empty-user-email",
   215  			fields{client: c},
   216  			args{
   217  				accountId: "a28707e316df",
   218  				teamId:    "b28707e316df",
   219  				userEmail: "",
   220  			},
   221  			true,
   222  		},
   223  	}
   224  	for _, tt := range tests {
   225  		t.Run(tt.name, func(t *testing.T) {
   226  			h := AccountTeamInvitesHandler{
   227  				client: tt.fields.client,
   228  			}
   229  			if err := h.Delete(tt.args.accountId, tt.args.teamId, tt.args.userEmail); (err != nil) != tt.wantErr {
   230  				t.Errorf("Delete() error = %v, wantErr %v", err, tt.wantErr)
   231  			}
   232  		})
   233  	}
   234  }