github.com/google/go-github/v42@v42.0.0/github/orgs_actions_allowed_test.go (about)

     1  // Copyright 2021 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 TestOrganizationsService_GetActionsAllowed(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  		fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`)
    25  	})
    26  
    27  	ctx := context.Background()
    28  	org, _, err := client.Organizations.GetActionsAllowed(ctx, "o")
    29  	if err != nil {
    30  		t.Errorf("Organizations.GetActionsAllowed returned error: %v", err)
    31  	}
    32  	want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
    33  	if !cmp.Equal(org, want) {
    34  		t.Errorf("Organizations.GetActionsAllowed returned %+v, want %+v", org, want)
    35  	}
    36  
    37  	const methodName = "GetActionsAllowed"
    38  	testBadOptions(t, methodName, func() (err error) {
    39  		_, _, err = client.Organizations.GetActionsAllowed(ctx, "\n")
    40  		return err
    41  	})
    42  
    43  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    44  		got, resp, err := client.Organizations.GetActionsAllowed(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 TestOrganizationsService_EditActionsAllowed(t *testing.T) {
    53  	client, mux, _, teardown := setup()
    54  	defer teardown()
    55  	input := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
    56  
    57  	mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
    58  		v := new(ActionsAllowed)
    59  		json.NewDecoder(r.Body).Decode(v)
    60  
    61  		testMethod(t, r, "PUT")
    62  		if !cmp.Equal(v, input) {
    63  			t.Errorf("Request body = %+v, want %+v", v, input)
    64  		}
    65  
    66  		fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`)
    67  	})
    68  
    69  	ctx := context.Background()
    70  	org, _, err := client.Organizations.EditActionsAllowed(ctx, "o", *input)
    71  	if err != nil {
    72  		t.Errorf("Organizations.EditActionsAllowed returned error: %v", err)
    73  	}
    74  
    75  	want := &ActionsAllowed{GithubOwnedAllowed: Bool(true), VerifiedAllowed: Bool(false), PatternsAllowed: []string{"a/b"}}
    76  	if !cmp.Equal(org, want) {
    77  		t.Errorf("Organizations.EditActionsAllowed returned %+v, want %+v", org, want)
    78  	}
    79  
    80  	const methodName = "EditActionsAllowed"
    81  	testBadOptions(t, methodName, func() (err error) {
    82  		_, _, err = client.Organizations.EditActionsAllowed(ctx, "\n", *input)
    83  		return err
    84  	})
    85  }
    86  
    87  func TestActionsAllowed_Marshal(t *testing.T) {
    88  	testJSONMarshal(t, &ActionsAllowed{}, "{}")
    89  
    90  	u := &ActionsAllowed{
    91  		GithubOwnedAllowed: Bool(false),
    92  		VerifiedAllowed:    Bool(false),
    93  		PatternsAllowed:    []string{"s"},
    94  	}
    95  
    96  	want := `{
    97  		"github_owned_allowed": false,
    98  		"verified_allowed": false,
    99  		"patterns_allowed": [
   100  			"s"
   101  		]
   102  	}`
   103  
   104  	testJSONMarshal(t, u, want)
   105  }
   106  
   107  func TestActionsPermissions_Marshal(t *testing.T) {
   108  	testJSONMarshal(t, &ActionsPermissions{}, "{}")
   109  
   110  	u := &ActionsPermissions{
   111  		EnabledRepositories: String("e"),
   112  		AllowedActions:      String("a"),
   113  		SelectedActionsURL:  String("sau"),
   114  	}
   115  
   116  	want := `{
   117  		"enabled_repositories": "e",
   118  		"allowed_actions": "a",
   119  		"selected_actions_url": "sau"
   120  	}`
   121  
   122  	testJSONMarshal(t, u, want)
   123  }