| 1 |
// Copyright 2019 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 TestInteractionsService_GetRestrictionsForOrgs(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/orgs/o/interaction-limits", func(w http.ResponseWriter, r *http.Request) { |
| 23 |
testMethod(t, r, "GET") |
| 24 |
testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) |
| 25 |
fmt.Fprint(w, `{"origin":"organization"}`) |
| 26 |
}) |
| 27 |
|
| 28 |
ctx := context.Background() |
| 29 |
organizationInteractions, _, err := client.Interactions.GetRestrictionsForOrg(ctx, "o") |
| 30 |
if err != nil { |
| 31 |
t.Errorf("Interactions.GetRestrictionsForOrg returned error: %v", err) |
| 32 |
} |
| 33 |
|
| 34 |
want := &InteractionRestriction{Origin: String("organization")} |
| 35 |
if !cmp.Equal(organizationInteractions, want) { |
| 36 |
t.Errorf("Interactions.GetRestrictionsForOrg returned %+v, want %+v", organizationInteractions, want) |
| 37 |
} |
| 38 |
|
| 39 |
const methodName = "GetRestrictionsForOrg" |
| 40 |
testBadOptions(t, methodName, func() (err error) { |
| 41 |
_, _, err = client.Interactions.GetRestrictionsForOrg(ctx, "\n") |
| 42 |
return err |
| 43 |
}) |
| 44 |
|
| 45 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 46 |
got, resp, err := client.Interactions.GetRestrictionsForOrg(ctx, "o") |
| 47 |
if got != nil { |
| 48 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 49 |
} |
| 50 |
return resp, err |
| 51 |
}) |
| 52 |
} |
| 53 |
|
| 54 |
func TestInteractionsService_UpdateRestrictionsForOrg(t *testing.T) { |
| 55 |
client, mux, _, teardown := setup() |
| 56 |
defer teardown() |
| 57 |
|
| 58 |
input := &InteractionRestriction{Limit: String("existing_users")} |
| 59 |
|
| 60 |
mux.HandleFunc("/orgs/o/interaction-limits", func(w http.ResponseWriter, r *http.Request) { |
| 61 |
v := new(InteractionRestriction) |
| 62 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 63 |
|
| 64 |
testMethod(t, r, "PUT") |
| 65 |
testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) |
| 66 |
if !cmp.Equal(v, input) { |
| 67 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 68 |
} |
| 69 |
fmt.Fprint(w, `{"origin":"organization"}`) |
| 70 |
}) |
| 71 |
|
| 72 |
ctx := context.Background() |
| 73 |
organizationInteractions, _, err := client.Interactions.UpdateRestrictionsForOrg(ctx, "o", input.GetLimit()) |
| 74 |
if err != nil { |
| 75 |
t.Errorf("Interactions.UpdateRestrictionsForOrg returned error: %v", err) |
| 76 |
} |
| 77 |
|
| 78 |
want := &InteractionRestriction{Origin: String("organization")} |
| 79 |
if !cmp.Equal(organizationInteractions, want) { |
| 80 |
t.Errorf("Interactions.UpdateRestrictionsForOrg returned %+v, want %+v", organizationInteractions, want) |
| 81 |
} |
| 82 |
|
| 83 |
const methodName = "UpdateRestrictionsForOrg" |
| 84 |
testBadOptions(t, methodName, func() (err error) { |
| 85 |
_, _, err = client.Interactions.UpdateRestrictionsForOrg(ctx, "\n", input.GetLimit()) |
| 86 |
return err |
| 87 |
}) |
| 88 |
|
| 89 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 90 |
got, resp, err := client.Interactions.UpdateRestrictionsForOrg(ctx, "o", input.GetLimit()) |
| 91 |
if got != nil { |
| 92 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 93 |
} |
| 94 |
return resp, err |
| 95 |
}) |
| 96 |
} |
| 97 |
|
| 98 |
func TestInteractionsService_RemoveRestrictionsFromOrg(t *testing.T) { |
| 99 |
client, mux, _, teardown := setup() |
| 100 |
defer teardown() |
| 101 |
|
| 102 |
mux.HandleFunc("/orgs/o/interaction-limits", func(w http.ResponseWriter, r *http.Request) { |
| 103 |
testMethod(t, r, "DELETE") |
| 104 |
testHeader(t, r, "Accept", mediaTypeInteractionRestrictionsPreview) |
| 105 |
}) |
| 106 |
|
| 107 |
ctx := context.Background() |
| 108 |
_, err := client.Interactions.RemoveRestrictionsFromOrg(ctx, "o") |
| 109 |
if err != nil { |
| 110 |
t.Errorf("Interactions.RemoveRestrictionsFromOrg returned error: %v", err) |
| 111 |
} |
| 112 |
|
| 113 |
const methodName = "RemoveRestrictionsFromOrg" |
| 114 |
testBadOptions(t, methodName, func() (err error) { |
| 115 |
_, err = client.Interactions.RemoveRestrictionsFromOrg(ctx, "\n") |
| 116 |
return err |
| 117 |
}) |
| 118 |
|
| 119 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 120 |
return client.Interactions.RemoveRestrictionsFromOrg(ctx, "o") |
| 121 |
}) |
| 122 |
} |