| 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 TestRepositoriesService_GetHookConfiguration(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/repos/o/r/hooks/1/config", func(w http.ResponseWriter, r *http.Request) { |
| 23 |
testMethod(t, r, "GET") |
| 24 |
fmt.Fprint(w, `{"content_type": "json", "insecure_ssl": "0", "secret": "********", "url": "https://example.com/webhook"}`) |
| 25 |
}) |
| 26 |
|
| 27 |
ctx := context.Background() |
| 28 |
config, _, err := client.Repositories.GetHookConfiguration(ctx, "o", "r", 1) |
| 29 |
if err != nil { |
| 30 |
t.Errorf("Repositories.GetHookConfiguration returned error: %v", err) |
| 31 |
} |
| 32 |
|
| 33 |
want := &HookConfig{ |
| 34 |
ContentType: String("json"), |
| 35 |
InsecureSSL: String("0"), |
| 36 |
Secret: String("********"), |
| 37 |
URL: String("https://example.com/webhook"), |
| 38 |
} |
| 39 |
if !cmp.Equal(config, want) { |
| 40 |
t.Errorf("Repositories.GetHookConfiguration returned %+v, want %+v", config, want) |
| 41 |
} |
| 42 |
|
| 43 |
const methodName = "GetHookConfiguration" |
| 44 |
testBadOptions(t, methodName, func() (err error) { |
| 45 |
_, _, err = client.Repositories.GetHookConfiguration(ctx, "\n", "\n", -1) |
| 46 |
return err |
| 47 |
}) |
| 48 |
|
| 49 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 50 |
got, resp, err := client.Repositories.GetHookConfiguration(ctx, "o", "r", 1) |
| 51 |
if got != nil { |
| 52 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 53 |
} |
| 54 |
return resp, err |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
func TestRepositoriesService_GetHookConfiguration_invalidOrg(t *testing.T) { |
| 59 |
client, _, _, teardown := setup() |
| 60 |
defer teardown() |
| 61 |
|
| 62 |
ctx := context.Background() |
| 63 |
_, _, err := client.Repositories.GetHookConfiguration(ctx, "%", "%", 1) |
| 64 |
testURLParseError(t, err) |
| 65 |
} |
| 66 |
|
| 67 |
func TestRepositoriesService_EditHookConfiguration(t *testing.T) { |
| 68 |
client, mux, _, teardown := setup() |
| 69 |
defer teardown() |
| 70 |
|
| 71 |
input := &HookConfig{} |
| 72 |
|
| 73 |
mux.HandleFunc("/repos/o/r/hooks/1/config", func(w http.ResponseWriter, r *http.Request) { |
| 74 |
v := new(HookConfig) |
| 75 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 76 |
|
| 77 |
testMethod(t, r, "PATCH") |
| 78 |
if !cmp.Equal(v, input) { |
| 79 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 80 |
} |
| 81 |
|
| 82 |
fmt.Fprint(w, `{"content_type": "json", "insecure_ssl": "0", "secret": "********", "url": "https://example.com/webhook"}`) |
| 83 |
}) |
| 84 |
|
| 85 |
ctx := context.Background() |
| 86 |
config, _, err := client.Repositories.EditHookConfiguration(ctx, "o", "r", 1, input) |
| 87 |
if err != nil { |
| 88 |
t.Errorf("Repositories.EditHookConfiguration returned error: %v", err) |
| 89 |
} |
| 90 |
|
| 91 |
want := &HookConfig{ |
| 92 |
ContentType: String("json"), |
| 93 |
InsecureSSL: String("0"), |
| 94 |
Secret: String("********"), |
| 95 |
URL: String("https://example.com/webhook"), |
| 96 |
} |
| 97 |
if !cmp.Equal(config, want) { |
| 98 |
t.Errorf("Repositories.EditHookConfiguration returned %+v, want %+v", config, want) |
| 99 |
} |
| 100 |
|
| 101 |
const methodName = "EditHookConfiguration" |
| 102 |
testBadOptions(t, methodName, func() (err error) { |
| 103 |
_, _, err = client.Repositories.EditHookConfiguration(ctx, "\n", "\n", -1, input) |
| 104 |
return err |
| 105 |
}) |
| 106 |
|
| 107 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 108 |
got, resp, err := client.Repositories.EditHookConfiguration(ctx, "o", "r", 1, input) |
| 109 |
if got != nil { |
| 110 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 111 |
} |
| 112 |
return resp, err |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
func TestRepositoriesService_EditHookConfiguration_invalidOrg(t *testing.T) { |
| 117 |
client, _, _, teardown := setup() |
| 118 |
defer teardown() |
| 119 |
|
| 120 |
ctx := context.Background() |
| 121 |
_, _, err := client.Repositories.EditHookConfiguration(ctx, "%", "%", 1, nil) |
| 122 |
testURLParseError(t, err) |
| 123 |
} |