|
| 1 | +// Copyright 2022 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 TestRepositoriesService_GetActionsAccessLevel(t *testing.T) { |
| 19 | + client, mux, _, teardown := setup() |
| 20 | + defer teardown() |
| 21 | + |
| 22 | + mux.HandleFunc("/repos/o/r/actions/permissions/access", func(w http.ResponseWriter, r *http.Request) { |
| 23 | + testMethod(t, r, "GET") |
| 24 | + fmt.Fprintf(w, `{"access_level": "none"}`) |
| 25 | + }) |
| 26 | + |
| 27 | + ctx := context.Background() |
| 28 | + org, _, err := client.Repositories.GetActionsAccessLevel(ctx, "o", "r") |
| 29 | + if err != nil { |
| 30 | + t.Errorf("Repositories.GetActionsAccessLevel returned error: %v", err) |
| 31 | + } |
| 32 | + want := &RepositoryActionsAccessLevel{AccessLevel: String("none")} |
| 33 | + if !cmp.Equal(org, want) { |
| 34 | + t.Errorf("Repositories.GetActionsAccessLevel returned %+v, want %+v", org, want) |
| 35 | + } |
| 36 | + |
| 37 | + const methodName = "GetActionsAccessLevel" |
| 38 | + testBadOptions(t, methodName, func() (err error) { |
| 39 | + _, _, err = client.Repositories.GetActionsAccessLevel(ctx, "\n", "\n") |
| 40 | + return err |
| 41 | + }) |
| 42 | + |
| 43 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 44 | + got, resp, err := client.Repositories.GetActionsAccessLevel(ctx, "o", "r") |
| 45 | + if got != nil { |
| 46 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 47 | + } |
| 48 | + return resp, err |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func TestRepositoriesService_EditActionsAccessLevel(t *testing.T) { |
| 53 | + client, mux, _, teardown := setup() |
| 54 | + defer teardown() |
| 55 | + |
| 56 | + input := &RepositoryActionsAccessLevel{AccessLevel: String("organization")} |
| 57 | + |
| 58 | + mux.HandleFunc("/repos/o/r/actions/permissions/access", func(w http.ResponseWriter, r *http.Request) { |
| 59 | + v := new(RepositoryActionsAccessLevel) |
| 60 | + 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 | + |
| 68 | + ctx := context.Background() |
| 69 | + _, err := client.Repositories.EditActionsAccessLevel(ctx, "o", "r", *input) |
| 70 | + if err != nil { |
| 71 | + t.Errorf("Repositories.EditActionsAccessLevel returned error: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + const methodName = "EditActionsAccessLevel" |
| 75 | + testBadOptions(t, methodName, func() (err error) { |
| 76 | + _, err = client.Repositories.EditActionsAccessLevel(ctx, "\n", "\n", *input) |
| 77 | + return err |
| 78 | + }) |
| 79 | + |
| 80 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 81 | + resp, err := client.Repositories.EditActionsAccessLevel(ctx, "o", "r", *input) |
| 82 | + return resp, err |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func TestRepositoryActionsAccessLevel_Marshal(t *testing.T) { |
| 87 | + testJSONMarshal(t, &ActionsPermissions{}, "{}") |
| 88 | + |
| 89 | + u := &RepositoryActionsAccessLevel{ |
| 90 | + AccessLevel: String("enterprise"), |
| 91 | + } |
| 92 | + |
| 93 | + want := `{ |
| 94 | + "access_level": "enterprise" |
| 95 | + }` |
| 96 | + |
| 97 | + testJSONMarshal(t, u, want) |
| 98 | +} |
0 commit comments