github.com/google/go-github/v65@v65.0.0/github/actions_permissions_orgs_test.go (about)

     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  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestActionsService_GetActionsPermissions(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	mux.HandleFunc("/orgs/o/actions/permissions", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  		fmt.Fprint(w, `{"enabled_repositories": "all", "allowed_actions": "all"}`)
    25  	})
    26  
    27  	ctx := context.Background()
    28  	org, _, err := client.Actions.GetActionsPermissions(ctx, "o")
    29  	if err != nil {
    30  		t.Errorf("Actions.GetActionsPermissions returned error: %v", err)
    31  	}
    32  	want := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("all")}
    33  	if !cmp.Equal(org, want) {
    34  		t.Errorf("Actions.GetActionsPermissions returned %+v, want %+v", org, want)
    35  	}
    36  
    37  	const methodName = "GetActionsPermissions"
    38  	testBadOptions(t, methodName, func() (err error) {
    39  		_, _, err = client.Actions.GetActionsPermissions(ctx, "\n")
    40  		return err
    41  	})
    42  
    43  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    44  		got, resp, err := client.Actions.GetActionsPermissions(ctx, "o")
    45  		if got != nil {
    46  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    47  		}
    48  		return resp, err
    49  	})
    50  }
    51  
    52  func TestActionsService_EditActionsPermissions(t *testing.T) {
    53  	client, mux, _, teardown := setup()
    54  	defer teardown()
    55  
    56  	input := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("selected")}
    57  
    58  	mux.HandleFunc("/orgs/o/actions/permissions", func(w http.ResponseWriter, r *http.Request) {
    59  		v := new(ActionsPermissions)
    60  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
    61  
    62  		testMethod(t, r, "PUT")
    63  		if !cmp.Equal(v, input) {
    64  			t.Errorf("Request body = %+v, want %+v", v, input)
    65  		}
    66  
    67  		fmt.Fprint(w, `{"enabled_repositories": "all", "allowed_actions": "selected"}`)
    68  	})
    69  
    70  	ctx := context.Background()
    71  	org, _, err := client.Actions.EditActionsPermissions(ctx, "o", *input)
    72  	if err != nil {
    73  		t.Errorf("Actions.EditActionsPermissions returned error: %v", err)
    74  	}
    75  
    76  	want := &ActionsPermissions{EnabledRepositories: String("all"), AllowedActions: String("selected")}
    77  	if !cmp.Equal(org, want) {
    78  		t.Errorf("Actions.EditActionsPermissions returned %+v, want %+v", org, want)
    79  	}
    80  
    81  	const methodName = "EditActionsPermissions"
    82  	testBadOptions(t, methodName, func() (err error) {
    83  		_, _, err = client.Actions.EditActionsPermissions(ctx, "\n", *input)
    84  		return err
    85  	})
    86  
    87  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    88  		got, resp, err := client.Actions.EditActionsPermissions(ctx, "o", *input)
    89  		if got != nil {
    90  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    91  		}
    92  		return resp, err
    93  	})
    94  }
    95  
    96  func TestActionsService_ListEnabledReposInOrg(t *testing.T) {
    97  	client, mux, _, teardown := setup()
    98  	defer teardown()
    99  
   100  	mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) {
   101  		testMethod(t, r, "GET")
   102  		testFormValues(t, r, values{
   103  			"page": "1",
   104  		})
   105  		fmt.Fprint(w, `{"total_count":2,"repositories":[{"id":2}, {"id": 3}]}`)
   106  	})
   107  
   108  	ctx := context.Background()
   109  	opt := &ListOptions{
   110  		Page: 1,
   111  	}
   112  	got, _, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt)
   113  	if err != nil {
   114  		t.Errorf("Actions.ListEnabledRepos returned error: %v", err)
   115  	}
   116  
   117  	want := &ActionsEnabledOnOrgRepos{TotalCount: int(2), Repositories: []*Repository{
   118  		{ID: Int64(2)},
   119  		{ID: Int64(3)},
   120  	}}
   121  	if !cmp.Equal(got, want) {
   122  		t.Errorf("Actions.ListEnabledRepos returned %+v, want %+v", got, want)
   123  	}
   124  
   125  	const methodName = "ListEnabledRepos"
   126  	testBadOptions(t, methodName, func() (err error) {
   127  		_, _, err = client.Actions.ListEnabledReposInOrg(ctx, "\n", opt)
   128  		return err
   129  	})
   130  
   131  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   132  		got, resp, err := client.Actions.ListEnabledReposInOrg(ctx, "o", opt)
   133  		if got != nil {
   134  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   135  		}
   136  		return resp, err
   137  	})
   138  }
   139  
   140  func TestActionsService_SetEnabledReposInOrg(t *testing.T) {
   141  	client, mux, _, teardown := setup()
   142  	defer teardown()
   143  
   144  	mux.HandleFunc("/orgs/o/actions/permissions/repositories", func(w http.ResponseWriter, r *http.Request) {
   145  		testMethod(t, r, "PUT")
   146  		testHeader(t, r, "Content-Type", "application/json")
   147  		testBody(t, r, `{"selected_repository_ids":[123,1234]}`+"\n")
   148  		w.WriteHeader(http.StatusNoContent)
   149  	})
   150  
   151  	ctx := context.Background()
   152  	_, err := client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234})
   153  	if err != nil {
   154  		t.Errorf("Actions.SetEnabledRepos returned error: %v", err)
   155  	}
   156  
   157  	const methodName = "SetEnabledRepos"
   158  
   159  	testBadOptions(t, methodName, func() (err error) {
   160  		_, err = client.Actions.SetEnabledReposInOrg(ctx, "\n", []int64{123, 1234})
   161  		return err
   162  	})
   163  
   164  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   165  		return client.Actions.SetEnabledReposInOrg(ctx, "o", []int64{123, 1234})
   166  	})
   167  }
   168  
   169  func TestActionsService_AddEnabledReposInOrg(t *testing.T) {
   170  	client, mux, _, teardown := setup()
   171  	defer teardown()
   172  
   173  	mux.HandleFunc("/orgs/o/actions/permissions/repositories/123", func(w http.ResponseWriter, r *http.Request) {
   174  		testMethod(t, r, "PUT")
   175  		w.WriteHeader(http.StatusNoContent)
   176  	})
   177  
   178  	ctx := context.Background()
   179  	_, err := client.Actions.AddEnabledReposInOrg(ctx, "o", 123)
   180  	if err != nil {
   181  		t.Errorf("Actions.AddEnabledReposInOrg returned error: %v", err)
   182  	}
   183  
   184  	const methodName = "AddEnabledReposInOrg"
   185  
   186  	testBadOptions(t, methodName, func() (err error) {
   187  		_, err = client.Actions.AddEnabledReposInOrg(ctx, "\n", 123)
   188  		return err
   189  	})
   190  
   191  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   192  		return client.Actions.AddEnabledReposInOrg(ctx, "o", 123)
   193  	})
   194  }
   195  
   196  func TestActionsService_RemoveEnabledReposInOrg(t *testing.T) {
   197  	client, mux, _, teardown := setup()
   198  	defer teardown()
   199  
   200  	mux.HandleFunc("/orgs/o/actions/permissions/repositories/123", func(w http.ResponseWriter, r *http.Request) {
   201  		testMethod(t, r, "DELETE")
   202  		w.WriteHeader(http.StatusNoContent)
   203  	})
   204  
   205  	ctx := context.Background()
   206  	_, err := client.Actions.RemoveEnabledReposInOrg(ctx, "o", 123)
   207  	if err != nil {
   208  		t.Errorf("Actions.RemoveEnabledReposInOrg returned error: %v", err)
   209  	}
   210  
   211  	const methodName = "RemoveEnabledReposInOrg"
   212  
   213  	testBadOptions(t, methodName, func() (err error) {
   214  		_, err = client.Actions.RemoveEnabledReposInOrg(ctx, "\n", 123)
   215  		return err
   216  	})
   217  
   218  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   219  		return client.Actions.RemoveEnabledReposInOrg(ctx, "o", 123)
   220  	})
   221  }
   222  
   223  func TestActionsService_GetActionsAllowed(t *testing.T) {
   224  	client, mux, _, teardown := setup()
   225  	defer teardown()
   226  
   227  	mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
   228  		testMethod(t, r, "GET")
   229  		fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`)
   230  	})
   231  
   232  	ctx := context.Background()
   233  	org, _, err := client.Actions.GetActionsAllowed(ctx, "o")
   234  	if err != nil {
   235  		t.Errorf("Actions.GetActionsAllowed returned error: %v", err)
   236  	}
   237  	want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
   238  	if !cmp.Equal(org, want) {
   239  		t.Errorf("Actions.GetActionsAllowed returned %+v, want %+v", org, want)
   240  	}
   241  
   242  	const methodName = "GetActionsAllowed"
   243  	testBadOptions(t, methodName, func() (err error) {
   244  		_, _, err = client.Actions.GetActionsAllowed(ctx, "\n")
   245  		return err
   246  	})
   247  
   248  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   249  		got, resp, err := client.Actions.GetActionsAllowed(ctx, "o")
   250  		if got != nil {
   251  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   252  		}
   253  		return resp, err
   254  	})
   255  }
   256  
   257  func TestActionsService_EditActionsAllowed(t *testing.T) {
   258  	client, mux, _, teardown := setup()
   259  	defer teardown()
   260  	input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
   261  
   262  	mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
   263  		v := new(ActionsAllowed)
   264  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
   265  
   266  		testMethod(t, r, "PUT")
   267  		if !cmp.Equal(v, input) {
   268  			t.Errorf("Request body = %+v, want %+v", v, input)
   269  		}
   270  
   271  		fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`)
   272  	})
   273  
   274  	ctx := context.Background()
   275  	org, _, err := client.Actions.EditActionsAllowed(ctx, "o", *input)
   276  	if err != nil {
   277  		t.Errorf("Actions.EditActionsAllowed returned error: %v", err)
   278  	}
   279  
   280  	want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
   281  	if !cmp.Equal(org, want) {
   282  		t.Errorf("Actions.EditActionsAllowed returned %+v, want %+v", org, want)
   283  	}
   284  
   285  	const methodName = "EditActionsAllowed"
   286  	testBadOptions(t, methodName, func() (err error) {
   287  		_, _, err = client.Actions.EditActionsAllowed(ctx, "\n", *input)
   288  		return err
   289  	})
   290  
   291  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   292  		got, resp, err := client.Actions.EditActionsAllowed(ctx, "o", *input)
   293  		if got != nil {
   294  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   295  		}
   296  		return resp, err
   297  	})
   298  }
   299  
   300  func TestActionsAllowed_Marshal(t *testing.T) {
   301  	testJSONMarshal(t, &ActionsAllowed{}, "{}")
   302  
   303  	u := &ActionsAllowed{
   304  		GithubOwnedAllowed: Bool(false),
   305  		VerifiedAllowed:    Bool(false),
   306  		PatternsAllowed:    []string{"s"},
   307  	}
   308  
   309  	want := `{
   310  		"github_owned_allowed": false,
   311  		"verified_allowed": false,
   312  		"patterns_allowed": [
   313  			"s"
   314  		]
   315  	}`
   316  
   317  	testJSONMarshal(t, u, want)
   318  }
   319  
   320  func TestActionsPermissions_Marshal(t *testing.T) {
   321  	testJSONMarshal(t, &ActionsPermissions{}, "{}")
   322  
   323  	u := &ActionsPermissions{
   324  		EnabledRepositories: String("e"),
   325  		AllowedActions:      String("a"),
   326  		SelectedActionsURL:  String("sau"),
   327  	}
   328  
   329  	want := `{
   330  		"enabled_repositories": "e",
   331  		"allowed_actions": "a",
   332  		"selected_actions_url": "sau"
   333  	}`
   334  
   335  	testJSONMarshal(t, u, want)
   336  }
   337  
   338  func TestActionsService_GetDefaultWorkflowPermissionsInOrganization(t *testing.T) {
   339  	client, mux, _, teardown := setup()
   340  	defer teardown()
   341  
   342  	mux.HandleFunc("/orgs/o/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) {
   343  		testMethod(t, r, "GET")
   344  		fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`)
   345  	})
   346  
   347  	ctx := context.Background()
   348  	org, _, err := client.Actions.GetDefaultWorkflowPermissionsInOrganization(ctx, "o")
   349  	if err != nil {
   350  		t.Errorf("Actions.GetDefaultWorkflowPermissionsInOrganization returned error: %v", err)
   351  	}
   352  	want := &DefaultWorkflowPermissionOrganization{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)}
   353  	if !cmp.Equal(org, want) {
   354  		t.Errorf("Actions.GetDefaultWorkflowPermissionsInOrganization returned %+v, want %+v", org, want)
   355  	}
   356  
   357  	const methodName = "GetDefaultWorkflowPermissionsInOrganization"
   358  	testBadOptions(t, methodName, func() (err error) {
   359  		_, _, err = client.Actions.GetDefaultWorkflowPermissionsInOrganization(ctx, "\n")
   360  		return err
   361  	})
   362  
   363  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   364  		got, resp, err := client.Actions.GetDefaultWorkflowPermissionsInOrganization(ctx, "o")
   365  		if got != nil {
   366  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   367  		}
   368  		return resp, err
   369  	})
   370  }
   371  
   372  func TestActionsService_EditDefaultWorkflowPermissionsInOrganization(t *testing.T) {
   373  	client, mux, _, teardown := setup()
   374  	defer teardown()
   375  	input := &DefaultWorkflowPermissionOrganization{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)}
   376  
   377  	mux.HandleFunc("/orgs/o/actions/permissions/workflow", func(w http.ResponseWriter, r *http.Request) {
   378  		v := new(DefaultWorkflowPermissionOrganization)
   379  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
   380  
   381  		testMethod(t, r, "PUT")
   382  		if !cmp.Equal(v, input) {
   383  			t.Errorf("Request body = %+v, want %+v", v, input)
   384  		}
   385  
   386  		fmt.Fprint(w, `{ "default_workflow_permissions": "read", "can_approve_pull_request_reviews": true }`)
   387  	})
   388  
   389  	ctx := context.Background()
   390  	org, _, err := client.Actions.EditDefaultWorkflowPermissionsInOrganization(ctx, "o", *input)
   391  	if err != nil {
   392  		t.Errorf("Actions.EditDefaultWorkflowPermissionsInOrganization returned error: %v", err)
   393  	}
   394  
   395  	want := &DefaultWorkflowPermissionOrganization{DefaultWorkflowPermissions: String("read"), CanApprovePullRequestReviews: Bool(true)}
   396  	if !cmp.Equal(org, want) {
   397  		t.Errorf("Actions.EditDefaultWorkflowPermissionsInOrganization returned %+v, want %+v", org, want)
   398  	}
   399  
   400  	const methodName = "EditDefaultWorkflowPermissionsInOrganization"
   401  	testBadOptions(t, methodName, func() (err error) {
   402  		_, _, err = client.Actions.EditDefaultWorkflowPermissionsInOrganization(ctx, "\n", *input)
   403  		return err
   404  	})
   405  
   406  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   407  		got, resp, err := client.Actions.EditDefaultWorkflowPermissionsInOrganization(ctx, "o", *input)
   408  		if got != nil {
   409  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   410  		}
   411  		return resp, err
   412  	})
   413  }