...

Source file src/github.com/google/go-github/v55/github/codespaces_secrets_test.go

Documentation: github.com/google/go-github/v55/github

     1  // Copyright 2023 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"net/http"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestCodespacesService_ListSecrets(t *testing.T) {
    19  	type test struct {
    20  		name       string
    21  		handleFunc func(*http.ServeMux)
    22  		call       func(context.Context, *Client) (*Secrets, *Response, error)
    23  		badCall    func(context.Context, *Client) (*Secrets, *Response, error)
    24  		methodName string
    25  	}
    26  	opts := &ListOptions{Page: 2, PerPage: 2}
    27  	tests := []test{
    28  		{
    29  			name: "User",
    30  			handleFunc: func(mux *http.ServeMux) {
    31  				mux.HandleFunc("/user/codespaces/secrets", func(w http.ResponseWriter, r *http.Request) {
    32  					testMethod(t, r, "GET")
    33  					testFormValues(t, r, values{"per_page": "2", "page": "2"})
    34  					fmt.Fprint(w, `{"total_count":4,"secrets":[{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`)
    35  				})
    36  			},
    37  			call: func(ctx context.Context, client *Client) (*Secrets, *Response, error) {
    38  				return client.Codespaces.ListUserSecrets(ctx, opts)
    39  			},
    40  			methodName: "ListUserSecrets",
    41  		},
    42  		{
    43  			name: "Org",
    44  			handleFunc: func(mux *http.ServeMux) {
    45  				mux.HandleFunc("/orgs/o/codespaces/secrets", func(w http.ResponseWriter, r *http.Request) {
    46  					testMethod(t, r, "GET")
    47  					testFormValues(t, r, values{"per_page": "2", "page": "2"})
    48  					fmt.Fprint(w, `{"total_count":4,"secrets":[{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`)
    49  				})
    50  			},
    51  			call: func(ctx context.Context, client *Client) (*Secrets, *Response, error) {
    52  				return client.Codespaces.ListOrgSecrets(ctx, "o", opts)
    53  			},
    54  			badCall: func(ctx context.Context, client *Client) (*Secrets, *Response, error) {
    55  				return client.Codespaces.ListOrgSecrets(ctx, "\n", opts)
    56  			},
    57  			methodName: "ListOrgSecrets",
    58  		},
    59  		{
    60  			name: "Repo",
    61  			handleFunc: func(mux *http.ServeMux) {
    62  				mux.HandleFunc("/repos/o/r/codespaces/secrets", func(w http.ResponseWriter, r *http.Request) {
    63  					testMethod(t, r, "GET")
    64  					testFormValues(t, r, values{"per_page": "2", "page": "2"})
    65  					fmt.Fprint(w, `{"total_count":4,"secrets":[{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`)
    66  				})
    67  			},
    68  			call: func(ctx context.Context, client *Client) (*Secrets, *Response, error) {
    69  				return client.Codespaces.ListRepoSecrets(ctx, "o", "r", opts)
    70  			},
    71  			badCall: func(ctx context.Context, client *Client) (*Secrets, *Response, error) {
    72  				return client.Codespaces.ListRepoSecrets(ctx, "\n", "\n", opts)
    73  			},
    74  			methodName: "ListRepoSecrets",
    75  		},
    76  	}
    77  
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			client, mux, _, teardown := setup()
    81  			defer teardown()
    82  
    83  			tt.handleFunc(mux)
    84  
    85  			ctx := context.Background()
    86  			secrets, _, err := tt.call(ctx, client)
    87  			if err != nil {
    88  				t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err)
    89  			}
    90  
    91  			want := &Secrets{
    92  				TotalCount: 4,
    93  				Secrets: []*Secret{
    94  					{Name: "A", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    95  					{Name: "B", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
    96  				},
    97  			}
    98  			if !cmp.Equal(secrets, want) {
    99  				t.Errorf("Codespaces.%v returned %+v, want %+v", tt.methodName, secrets, want)
   100  			}
   101  
   102  			if tt.badCall != nil {
   103  				testBadOptions(t, tt.methodName, func() (err error) {
   104  					_, _, err = tt.badCall(ctx, client)
   105  					return err
   106  				})
   107  			}
   108  
   109  			testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) {
   110  				got, resp, err := tt.call(ctx, client)
   111  				if got != nil {
   112  					t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", tt.methodName, got)
   113  				}
   114  				return resp, err
   115  			})
   116  		})
   117  	}
   118  }
   119  
   120  func TestCodespacesService_GetSecret(t *testing.T) {
   121  	type test struct {
   122  		name       string
   123  		handleFunc func(*http.ServeMux)
   124  		call       func(context.Context, *Client) (*Secret, *Response, error)
   125  		badCall    func(context.Context, *Client) (*Secret, *Response, error)
   126  		methodName string
   127  	}
   128  	tests := []test{
   129  		{
   130  			name: "User",
   131  			handleFunc: func(mux *http.ServeMux) {
   132  				mux.HandleFunc("/user/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) {
   133  					testMethod(t, r, "GET")
   134  					fmt.Fprint(w, `{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`)
   135  				})
   136  			},
   137  			call: func(ctx context.Context, client *Client) (*Secret, *Response, error) {
   138  				return client.Codespaces.GetUserSecret(ctx, "NAME")
   139  			},
   140  			methodName: "GetUserSecret",
   141  		},
   142  		{
   143  			name: "Org",
   144  			handleFunc: func(mux *http.ServeMux) {
   145  				mux.HandleFunc("/orgs/o/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) {
   146  					testMethod(t, r, "GET")
   147  					fmt.Fprint(w, `{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`)
   148  				})
   149  			},
   150  			call: func(ctx context.Context, client *Client) (*Secret, *Response, error) {
   151  				return client.Codespaces.GetOrgSecret(ctx, "o", "NAME")
   152  			},
   153  			badCall: func(ctx context.Context, client *Client) (*Secret, *Response, error) {
   154  				return client.Codespaces.GetOrgSecret(ctx, "\n", "\n")
   155  			},
   156  			methodName: "GetOrgSecret",
   157  		},
   158  		{
   159  			name: "Repo",
   160  			handleFunc: func(mux *http.ServeMux) {
   161  				mux.HandleFunc("/repos/o/r/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) {
   162  					testMethod(t, r, "GET")
   163  					fmt.Fprint(w, `{"name":"A","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`)
   164  				})
   165  			},
   166  			call: func(ctx context.Context, client *Client) (*Secret, *Response, error) {
   167  				return client.Codespaces.GetRepoSecret(ctx, "o", "r", "NAME")
   168  			},
   169  			badCall: func(ctx context.Context, client *Client) (*Secret, *Response, error) {
   170  				return client.Codespaces.GetRepoSecret(ctx, "\n", "\n", "\n")
   171  			},
   172  			methodName: "GetRepoSecret",
   173  		},
   174  	}
   175  
   176  	for _, tt := range tests {
   177  		t.Run(tt.name, func(t *testing.T) {
   178  			client, mux, _, teardown := setup()
   179  			defer teardown()
   180  
   181  			tt.handleFunc(mux)
   182  
   183  			ctx := context.Background()
   184  			secret, _, err := tt.call(ctx, client)
   185  			if err != nil {
   186  				t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err)
   187  			}
   188  
   189  			want := &Secret{Name: "A", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}
   190  			if !cmp.Equal(secret, want) {
   191  				t.Errorf("Codespaces.%v returned %+v, want %+v", tt.methodName, secret, want)
   192  			}
   193  
   194  			if tt.badCall != nil {
   195  				testBadOptions(t, tt.methodName, func() (err error) {
   196  					_, _, err = tt.badCall(ctx, client)
   197  					return err
   198  				})
   199  			}
   200  
   201  			testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) {
   202  				got, resp, err := tt.call(ctx, client)
   203  				if got != nil {
   204  					t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", tt.methodName, got)
   205  				}
   206  				return resp, err
   207  			})
   208  		})
   209  	}
   210  }
   211  
   212  func TestCodespacesService_CreateOrUpdateSecret(t *testing.T) {
   213  	type test struct {
   214  		name       string
   215  		handleFunc func(*http.ServeMux)
   216  		call       func(context.Context, *Client, *EncryptedSecret) (*Response, error)
   217  		badCall    func(context.Context, *Client, *EncryptedSecret) (*Response, error)
   218  		methodName string
   219  	}
   220  	tests := []test{
   221  		{
   222  			name: "User",
   223  			handleFunc: func(mux *http.ServeMux) {
   224  				mux.HandleFunc("/user/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) {
   225  					testMethod(t, r, "PUT")
   226  					testHeader(t, r, "Content-Type", "application/json")
   227  					testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n")
   228  					w.WriteHeader(http.StatusCreated)
   229  				})
   230  			},
   231  			call: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) {
   232  				return client.Codespaces.CreateOrUpdateUserSecret(ctx, e)
   233  			},
   234  			methodName: "CreateOrUpdateUserSecret",
   235  		},
   236  		{
   237  			name: "Org",
   238  			handleFunc: func(mux *http.ServeMux) {
   239  				mux.HandleFunc("/orgs/o/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) {
   240  					testMethod(t, r, "PUT")
   241  					testHeader(t, r, "Content-Type", "application/json")
   242  					testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n")
   243  					w.WriteHeader(http.StatusCreated)
   244  				})
   245  			},
   246  			call: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) {
   247  				return client.Codespaces.CreateOrUpdateOrgSecret(ctx, "o", e)
   248  			},
   249  			badCall: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) {
   250  				return client.Codespaces.CreateOrUpdateOrgSecret(ctx, "\n", e)
   251  			},
   252  			methodName: "CreateOrUpdateOrgSecret",
   253  		},
   254  		{
   255  			name: "Repo",
   256  			handleFunc: func(mux *http.ServeMux) {
   257  				mux.HandleFunc("/repos/o/r/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) {
   258  					testMethod(t, r, "PUT")
   259  					testHeader(t, r, "Content-Type", "application/json")
   260  					testBody(t, r, `{"key_id":"1234","encrypted_value":"QIv="}`+"\n")
   261  					w.WriteHeader(http.StatusCreated)
   262  				})
   263  			},
   264  			call: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) {
   265  				return client.Codespaces.CreateOrUpdateRepoSecret(ctx, "o", "r", e)
   266  			},
   267  			badCall: func(ctx context.Context, client *Client, e *EncryptedSecret) (*Response, error) {
   268  				return client.Codespaces.CreateOrUpdateRepoSecret(ctx, "\n", "\n", e)
   269  			},
   270  			methodName: "CreateOrUpdateRepoSecret",
   271  		},
   272  	}
   273  
   274  	for _, tt := range tests {
   275  		t.Run(tt.name, func(t *testing.T) {
   276  			client, mux, _, teardown := setup()
   277  			defer teardown()
   278  
   279  			tt.handleFunc(mux)
   280  
   281  			input := &EncryptedSecret{
   282  				Name:           "NAME",
   283  				EncryptedValue: "QIv=",
   284  				KeyID:          "1234",
   285  			}
   286  			ctx := context.Background()
   287  			_, err := tt.call(ctx, client, input)
   288  			if err != nil {
   289  				t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err)
   290  			}
   291  
   292  			if tt.badCall != nil {
   293  				testBadOptions(t, tt.methodName, func() (err error) {
   294  					_, err = tt.badCall(ctx, client, input)
   295  					return err
   296  				})
   297  			}
   298  
   299  			testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) {
   300  				return tt.call(ctx, client, input)
   301  			})
   302  		})
   303  	}
   304  }
   305  
   306  func TestCodespacesService_DeleteSecret(t *testing.T) {
   307  	type test struct {
   308  		name       string
   309  		handleFunc func(*http.ServeMux)
   310  		call       func(context.Context, *Client) (*Response, error)
   311  		badCall    func(context.Context, *Client) (*Response, error)
   312  		methodName string
   313  	}
   314  	tests := []test{
   315  		{
   316  			name: "User",
   317  			handleFunc: func(mux *http.ServeMux) {
   318  				mux.HandleFunc("/user/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) {
   319  					testMethod(t, r, "DELETE")
   320  				})
   321  			},
   322  			call: func(ctx context.Context, client *Client) (*Response, error) {
   323  				return client.Codespaces.DeleteUserSecret(ctx, "NAME")
   324  			},
   325  			methodName: "DeleteUserSecret",
   326  		},
   327  		{
   328  			name: "Org",
   329  			handleFunc: func(mux *http.ServeMux) {
   330  				mux.HandleFunc("/orgs/o/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) {
   331  					testMethod(t, r, "DELETE")
   332  				})
   333  			},
   334  			call: func(ctx context.Context, client *Client) (*Response, error) {
   335  				return client.Codespaces.DeleteOrgSecret(ctx, "o", "NAME")
   336  			},
   337  			badCall: func(ctx context.Context, client *Client) (*Response, error) {
   338  				return client.Codespaces.DeleteOrgSecret(ctx, "\n", "\n")
   339  			},
   340  			methodName: "DeleteOrgSecret",
   341  		},
   342  		{
   343  			name: "Repo",
   344  			handleFunc: func(mux *http.ServeMux) {
   345  				mux.HandleFunc("/repos/o/r/codespaces/secrets/NAME", func(w http.ResponseWriter, r *http.Request) {
   346  					testMethod(t, r, "DELETE")
   347  				})
   348  			},
   349  			call: func(ctx context.Context, client *Client) (*Response, error) {
   350  				return client.Codespaces.DeleteRepoSecret(ctx, "o", "r", "NAME")
   351  			},
   352  			badCall: func(ctx context.Context, client *Client) (*Response, error) {
   353  				return client.Codespaces.DeleteRepoSecret(ctx, "\n", "\n", "\n")
   354  			},
   355  			methodName: "DeleteRepoSecret",
   356  		},
   357  	}
   358  
   359  	for _, tt := range tests {
   360  		t.Run(tt.name, func(t *testing.T) {
   361  			client, mux, _, teardown := setup()
   362  			defer teardown()
   363  
   364  			tt.handleFunc(mux)
   365  
   366  			ctx := context.Background()
   367  			_, err := tt.call(ctx, client)
   368  			if err != nil {
   369  				t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err)
   370  			}
   371  
   372  			if tt.badCall != nil {
   373  				testBadOptions(t, tt.methodName, func() (err error) {
   374  					_, err = tt.badCall(ctx, client)
   375  					return err
   376  				})
   377  			}
   378  
   379  			testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) {
   380  				return tt.call(ctx, client)
   381  			})
   382  		})
   383  	}
   384  }
   385  
   386  func TestCodespacesService_GetPublicKey(t *testing.T) {
   387  	type test struct {
   388  		name       string
   389  		handleFunc func(*http.ServeMux)
   390  		call       func(context.Context, *Client) (*PublicKey, *Response, error)
   391  		badCall    func(context.Context, *Client) (*PublicKey, *Response, error)
   392  		methodName string
   393  	}
   394  
   395  	tests := []test{
   396  		{
   397  			name: "User",
   398  			handleFunc: func(mux *http.ServeMux) {
   399  				mux.HandleFunc("/user/codespaces/secrets/public-key", func(w http.ResponseWriter, r *http.Request) {
   400  					testMethod(t, r, "GET")
   401  					fmt.Fprint(w, `{"key_id":"1234","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}`)
   402  				})
   403  			},
   404  			call: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) {
   405  				return client.Codespaces.GetUserPublicKey(ctx)
   406  			},
   407  			methodName: "GetUserPublicKey",
   408  		},
   409  		{
   410  			name: "Org",
   411  			handleFunc: func(mux *http.ServeMux) {
   412  				mux.HandleFunc("/orgs/o/codespaces/secrets/public-key", func(w http.ResponseWriter, r *http.Request) {
   413  					testMethod(t, r, "GET")
   414  					fmt.Fprint(w, `{"key_id":"1234","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}`)
   415  				})
   416  			},
   417  			call: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) {
   418  				return client.Codespaces.GetOrgPublicKey(ctx, "o")
   419  			},
   420  			badCall: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) {
   421  				return client.Codespaces.GetOrgPublicKey(ctx, "\n")
   422  			},
   423  			methodName: "GetOrgPublicKey",
   424  		},
   425  		{
   426  			name: "Repo",
   427  			handleFunc: func(mux *http.ServeMux) {
   428  				mux.HandleFunc("/repos/o/r/codespaces/secrets/public-key", func(w http.ResponseWriter, r *http.Request) {
   429  					testMethod(t, r, "GET")
   430  					fmt.Fprint(w, `{"key_id":"1234","key":"2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234"}`)
   431  				})
   432  			},
   433  			call: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) {
   434  				return client.Codespaces.GetRepoPublicKey(ctx, "o", "r")
   435  			},
   436  			badCall: func(ctx context.Context, client *Client) (*PublicKey, *Response, error) {
   437  				return client.Codespaces.GetRepoPublicKey(ctx, "\n", "\n")
   438  			},
   439  			methodName: "GetRepoPublicKey",
   440  		},
   441  	}
   442  
   443  	for _, tt := range tests {
   444  		t.Run(tt.name, func(t *testing.T) {
   445  			client, mux, _, teardown := setup()
   446  			defer teardown()
   447  
   448  			tt.handleFunc(mux)
   449  
   450  			ctx := context.Background()
   451  			key, _, err := tt.call(ctx, client)
   452  			if err != nil {
   453  				t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err)
   454  			}
   455  
   456  			want := &PublicKey{KeyID: String("1234"), Key: String("2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvv1234")}
   457  			if !cmp.Equal(key, want) {
   458  				t.Errorf("Codespaces.%v returned %+v, want %+v", tt.methodName, key, want)
   459  			}
   460  
   461  			if tt.badCall != nil {
   462  				testBadOptions(t, tt.methodName, func() (err error) {
   463  					_, _, err = tt.badCall(ctx, client)
   464  					return err
   465  				})
   466  			}
   467  
   468  			testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) {
   469  				got, resp, err := tt.call(ctx, client)
   470  				if got != nil {
   471  					t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", tt.methodName, got)
   472  				}
   473  				return resp, err
   474  			})
   475  		})
   476  	}
   477  }
   478  
   479  func TestCodespacesService_ListSelectedReposForSecret(t *testing.T) {
   480  	type test struct {
   481  		name       string
   482  		handleFunc func(*http.ServeMux)
   483  		call       func(context.Context, *Client) (*SelectedReposList, *Response, error)
   484  		badCall    func(context.Context, *Client) (*SelectedReposList, *Response, error)
   485  		methodName string
   486  	}
   487  	opts := &ListOptions{Page: 2, PerPage: 2}
   488  	tests := []test{
   489  		{
   490  			name: "User",
   491  			handleFunc: func(mux *http.ServeMux) {
   492  				mux.HandleFunc("/user/codespaces/secrets/NAME/repositories", func(w http.ResponseWriter, r *http.Request) {
   493  					testMethod(t, r, "GET")
   494  					fmt.Fprintf(w, `{"total_count":1,"repositories":[{"id":1}]}`)
   495  				})
   496  			},
   497  			call: func(ctx context.Context, client *Client) (*SelectedReposList, *Response, error) {
   498  				return client.Codespaces.ListSelectedReposForUserSecret(ctx, "NAME", opts)
   499  			},
   500  			methodName: "ListSelectedReposForUserSecret",
   501  		},
   502  		{
   503  			name: "Org",
   504  			handleFunc: func(mux *http.ServeMux) {
   505  				mux.HandleFunc("/orgs/o/codespaces/secrets/NAME/repositories", func(w http.ResponseWriter, r *http.Request) {
   506  					testMethod(t, r, "GET")
   507  					fmt.Fprintf(w, `{"total_count":1,"repositories":[{"id":1}]}`)
   508  				})
   509  			},
   510  			call: func(ctx context.Context, client *Client) (*SelectedReposList, *Response, error) {
   511  				return client.Codespaces.ListSelectedReposForOrgSecret(ctx, "o", "NAME", opts)
   512  			},
   513  			badCall: func(ctx context.Context, client *Client) (*SelectedReposList, *Response, error) {
   514  				return client.Codespaces.ListSelectedReposForOrgSecret(ctx, "\n", "\n", opts)
   515  			},
   516  			methodName: "ListSelectedReposForOrgSecret",
   517  		},
   518  	}
   519  
   520  	for _, tt := range tests {
   521  		t.Run(tt.name, func(t *testing.T) {
   522  			client, mux, _, teardown := setup()
   523  			defer teardown()
   524  
   525  			tt.handleFunc(mux)
   526  
   527  			ctx := context.Background()
   528  			repos, _, err := tt.call(ctx, client)
   529  			if err != nil {
   530  				t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err)
   531  			}
   532  
   533  			want := &SelectedReposList{
   534  				TotalCount: Int(1),
   535  				Repositories: []*Repository{
   536  					{ID: Int64(1)},
   537  				},
   538  			}
   539  
   540  			if !cmp.Equal(repos, want) {
   541  				t.Errorf("Codespaces.%v returned %+v, want %+v", tt.methodName, repos, want)
   542  			}
   543  
   544  			if tt.badCall != nil {
   545  				testBadOptions(t, tt.methodName, func() (err error) {
   546  					_, _, err = tt.badCall(ctx, client)
   547  					return err
   548  				})
   549  			}
   550  
   551  			testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) {
   552  				got, resp, err := tt.call(ctx, client)
   553  				if got != nil {
   554  					t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", tt.methodName, got)
   555  				}
   556  				return resp, err
   557  			})
   558  		})
   559  	}
   560  }
   561  
   562  func TestCodespacesService_SetSelectedReposForSecret(t *testing.T) {
   563  	type test struct {
   564  		name       string
   565  		handleFunc func(*http.ServeMux)
   566  		call       func(context.Context, *Client) (*Response, error)
   567  		badCall    func(context.Context, *Client) (*Response, error)
   568  		methodName string
   569  	}
   570  	ids := SelectedRepoIDs{64780797}
   571  	tests := []test{
   572  		{
   573  			name: "User",
   574  			handleFunc: func(mux *http.ServeMux) {
   575  				mux.HandleFunc("/user/codespaces/secrets/NAME/repositories", func(w http.ResponseWriter, r *http.Request) {
   576  					testMethod(t, r, "PUT")
   577  					testHeader(t, r, "Content-Type", "application/json")
   578  					testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n")
   579  				})
   580  			},
   581  			call: func(ctx context.Context, client *Client) (*Response, error) {
   582  				return client.Codespaces.SetSelectedReposForUserSecret(ctx, "NAME", ids)
   583  			},
   584  			methodName: "SetSelectedReposForUserSecret",
   585  		},
   586  		{
   587  			name: "Org",
   588  			handleFunc: func(mux *http.ServeMux) {
   589  				mux.HandleFunc("/orgs/o/codespaces/secrets/NAME/repositories", func(w http.ResponseWriter, r *http.Request) {
   590  					testMethod(t, r, "PUT")
   591  					testHeader(t, r, "Content-Type", "application/json")
   592  					testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n")
   593  				})
   594  			},
   595  			call: func(ctx context.Context, client *Client) (*Response, error) {
   596  				return client.Codespaces.SetSelectedReposForOrgSecret(ctx, "o", "NAME", ids)
   597  			},
   598  			badCall: func(ctx context.Context, client *Client) (*Response, error) {
   599  				return client.Codespaces.SetSelectedReposForOrgSecret(ctx, "\n", "\n", ids)
   600  			},
   601  			methodName: "SetSelectedReposForOrgSecret",
   602  		},
   603  	}
   604  
   605  	for _, tt := range tests {
   606  		t.Run(tt.name, func(t *testing.T) {
   607  			client, mux, _, teardown := setup()
   608  			defer teardown()
   609  
   610  			tt.handleFunc(mux)
   611  
   612  			ctx := context.Background()
   613  			_, err := tt.call(ctx, client)
   614  			if err != nil {
   615  				t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err)
   616  			}
   617  
   618  			if tt.badCall != nil {
   619  				testBadOptions(t, tt.methodName, func() (err error) {
   620  					_, err = tt.badCall(ctx, client)
   621  					return err
   622  				})
   623  			}
   624  
   625  			testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) {
   626  				return tt.call(ctx, client)
   627  			})
   628  		})
   629  	}
   630  }
   631  
   632  func TestCodespacesService_AddSelectedReposForSecret(t *testing.T) {
   633  	type test struct {
   634  		name       string
   635  		handleFunc func(*http.ServeMux)
   636  		call       func(context.Context, *Client) (*Response, error)
   637  		badCall    func(context.Context, *Client) (*Response, error)
   638  		methodName string
   639  	}
   640  	repo := &Repository{ID: Int64(1234)}
   641  	tests := []test{
   642  		{
   643  			name: "User",
   644  			handleFunc: func(mux *http.ServeMux) {
   645  				mux.HandleFunc("/user/codespaces/secrets/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) {
   646  					testMethod(t, r, "PUT")
   647  				})
   648  			},
   649  			call: func(ctx context.Context, client *Client) (*Response, error) {
   650  				return client.Codespaces.AddSelectedRepoToUserSecret(ctx, "NAME", repo)
   651  			},
   652  			methodName: "AddSelectedRepoToUserSecret",
   653  		},
   654  		{
   655  			name: "Org",
   656  			handleFunc: func(mux *http.ServeMux) {
   657  				mux.HandleFunc("/orgs/o/codespaces/secrets/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) {
   658  					testMethod(t, r, "PUT")
   659  				})
   660  			},
   661  			call: func(ctx context.Context, client *Client) (*Response, error) {
   662  				return client.Codespaces.AddSelectedRepoToOrgSecret(ctx, "o", "NAME", repo)
   663  			},
   664  			badCall: func(ctx context.Context, client *Client) (*Response, error) {
   665  				return client.Codespaces.AddSelectedRepoToOrgSecret(ctx, "\n", "\n", repo)
   666  			},
   667  			methodName: "AddSelectedRepoToOrgSecret",
   668  		},
   669  	}
   670  
   671  	for _, tt := range tests {
   672  		t.Run(tt.name, func(t *testing.T) {
   673  			client, mux, _, teardown := setup()
   674  			defer teardown()
   675  
   676  			tt.handleFunc(mux)
   677  
   678  			ctx := context.Background()
   679  			_, err := tt.call(ctx, client)
   680  			if err != nil {
   681  				t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err)
   682  			}
   683  
   684  			if tt.badCall != nil {
   685  				testBadOptions(t, tt.methodName, func() (err error) {
   686  					_, err = tt.badCall(ctx, client)
   687  					return err
   688  				})
   689  			}
   690  
   691  			testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) {
   692  				return tt.call(ctx, client)
   693  			})
   694  		})
   695  	}
   696  }
   697  
   698  func TestCodespacesService_RemoveSelectedReposFromSecret(t *testing.T) {
   699  	type test struct {
   700  		name       string
   701  		handleFunc func(*http.ServeMux)
   702  		call       func(context.Context, *Client) (*Response, error)
   703  		badCall    func(context.Context, *Client) (*Response, error)
   704  		methodName string
   705  	}
   706  	repo := &Repository{ID: Int64(1234)}
   707  	tests := []test{
   708  		{
   709  			name: "User",
   710  			handleFunc: func(mux *http.ServeMux) {
   711  				mux.HandleFunc("/user/codespaces/secrets/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) {
   712  					testMethod(t, r, "DELETE")
   713  				})
   714  			},
   715  			call: func(ctx context.Context, client *Client) (*Response, error) {
   716  				return client.Codespaces.RemoveSelectedRepoFromUserSecret(ctx, "NAME", repo)
   717  			},
   718  			methodName: "RemoveSelectedRepoFromUserSecret",
   719  		},
   720  		{
   721  			name: "Org",
   722  			handleFunc: func(mux *http.ServeMux) {
   723  				mux.HandleFunc("/orgs/o/codespaces/secrets/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) {
   724  					testMethod(t, r, "DELETE")
   725  				})
   726  			},
   727  			call: func(ctx context.Context, client *Client) (*Response, error) {
   728  				return client.Codespaces.RemoveSelectedRepoFromOrgSecret(ctx, "o", "NAME", repo)
   729  			},
   730  			badCall: func(ctx context.Context, client *Client) (*Response, error) {
   731  				return client.Codespaces.RemoveSelectedRepoFromOrgSecret(ctx, "\n", "\n", repo)
   732  			},
   733  			methodName: "RemoveSelectedRepoFromOrgSecret",
   734  		},
   735  	}
   736  
   737  	for _, tt := range tests {
   738  		t.Run(tt.name, func(t *testing.T) {
   739  			client, mux, _, teardown := setup()
   740  			defer teardown()
   741  
   742  			tt.handleFunc(mux)
   743  
   744  			ctx := context.Background()
   745  			_, err := tt.call(ctx, client)
   746  			if err != nil {
   747  				t.Errorf("Codespaces.%v returned error: %v", tt.methodName, err)
   748  			}
   749  
   750  			if tt.badCall != nil {
   751  				testBadOptions(t, tt.methodName, func() (err error) {
   752  					_, err = tt.badCall(ctx, client)
   753  					return err
   754  				})
   755  			}
   756  
   757  			testNewRequestAndDoFailure(t, tt.methodName, client, func() (*Response, error) {
   758  				return tt.call(ctx, client)
   759  			})
   760  		})
   761  	}
   762  }
   763  
   764  // func TestActionsService_ListSelectedReposForOrgSecret(t *testing.T) {
   765  // 	client, mux, _, teardown := setup()
   766  // 	defer teardown()
   767  
   768  // 	mux.HandleFunc("/orgs/o/actions/secrets/NAME/repositories", func(w http.ResponseWriter, r *http.Request) {
   769  // 		testMethod(t, r, "GET")
   770  // 		fmt.Fprintf(w, `{"total_count":1,"repositories":[{"id":1}]}`)
   771  // 	})
   772  
   773  // 	opts := &ListOptions{Page: 2, PerPage: 2}
   774  // 	ctx := context.Background()
   775  // 	repos, _, err := client.Actions.ListSelectedReposForOrgSecret(ctx, "o", "NAME", opts)
   776  // 	if err != nil {
   777  // 		t.Errorf("Actions.ListSelectedReposForOrgSecret returned error: %v", err)
   778  // 	}
   779  
   780  // 	want := &SelectedReposList{
   781  // 		TotalCount: Int(1),
   782  // 		Repositories: []*Repository{
   783  // 			{ID: Int64(1)},
   784  // 		},
   785  // 	}
   786  // 	if !cmp.Equal(repos, want) {
   787  // 		t.Errorf("Actions.ListSelectedReposForOrgSecret returned %+v, want %+v", repos, want)
   788  // 	}
   789  
   790  // 	const methodName = "ListSelectedReposForOrgSecret"
   791  // 	testBadOptions(t, methodName, func() (err error) {
   792  // 		_, _, err = client.Actions.ListSelectedReposForOrgSecret(ctx, "\n", "\n", opts)
   793  // 		return err
   794  // 	})
   795  
   796  // 	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   797  // 		got, resp, err := client.Actions.ListSelectedReposForOrgSecret(ctx, "o", "NAME", opts)
   798  // 		if got != nil {
   799  // 			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   800  // 		}
   801  // 		return resp, err
   802  // 	})
   803  // }
   804  

View as plain text