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