| 1 |
// Copyright 2015 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_ListHooks(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/orgs/o/hooks", func(w http.ResponseWriter, r *http.Request) { |
| 23 |
testMethod(t, r, "GET") |
| 24 |
testFormValues(t, r, values{"page": "2"}) |
| 25 |
fmt.Fprint(w, `[{"id":1}, {"id":2}]`) |
| 26 |
}) |
| 27 |
|
| 28 |
opt := &ListOptions{Page: 2} |
| 29 |
|
| 30 |
ctx := context.Background() |
| 31 |
hooks, _, err := client.Organizations.ListHooks(ctx, "o", opt) |
| 32 |
if err != nil { |
| 33 |
t.Errorf("Organizations.ListHooks returned error: %v", err) |
| 34 |
} |
| 35 |
|
| 36 |
want := []*Hook{{ID: Int64(1)}, {ID: Int64(2)}} |
| 37 |
if !cmp.Equal(hooks, want) { |
| 38 |
t.Errorf("Organizations.ListHooks returned %+v, want %+v", hooks, want) |
| 39 |
} |
| 40 |
|
| 41 |
const methodName = "ListHooks" |
| 42 |
testBadOptions(t, methodName, func() (err error) { |
| 43 |
_, _, err = client.Organizations.ListHooks(ctx, "\n", opt) |
| 44 |
return err |
| 45 |
}) |
| 46 |
|
| 47 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 48 |
got, resp, err := client.Organizations.ListHooks(ctx, "o", opt) |
| 49 |
if got != nil { |
| 50 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 51 |
} |
| 52 |
return resp, err |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
func TestOrganizationsService_ListHooks_invalidOrg(t *testing.T) { |
| 57 |
client, _, _, teardown := setup() |
| 58 |
defer teardown() |
| 59 |
|
| 60 |
ctx := context.Background() |
| 61 |
_, _, err := client.Organizations.ListHooks(ctx, "%", nil) |
| 62 |
testURLParseError(t, err) |
| 63 |
} |
| 64 |
|
| 65 |
func TestOrganizationsService_CreateHook(t *testing.T) { |
| 66 |
client, mux, _, teardown := setup() |
| 67 |
defer teardown() |
| 68 |
|
| 69 |
input := &Hook{CreatedAt: &Timestamp{referenceTime}} |
| 70 |
|
| 71 |
mux.HandleFunc("/orgs/o/hooks", func(w http.ResponseWriter, r *http.Request) { |
| 72 |
v := new(createHookRequest) |
| 73 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 74 |
|
| 75 |
testMethod(t, r, "POST") |
| 76 |
want := &createHookRequest{Name: "web"} |
| 77 |
if !cmp.Equal(v, want) { |
| 78 |
t.Errorf("Request body = %+v, want %+v", v, want) |
| 79 |
} |
| 80 |
|
| 81 |
fmt.Fprint(w, `{"id":1}`) |
| 82 |
}) |
| 83 |
|
| 84 |
ctx := context.Background() |
| 85 |
hook, _, err := client.Organizations.CreateHook(ctx, "o", input) |
| 86 |
if err != nil { |
| 87 |
t.Errorf("Organizations.CreateHook returned error: %v", err) |
| 88 |
} |
| 89 |
|
| 90 |
want := &Hook{ID: Int64(1)} |
| 91 |
if !cmp.Equal(hook, want) { |
| 92 |
t.Errorf("Organizations.CreateHook returned %+v, want %+v", hook, want) |
| 93 |
} |
| 94 |
|
| 95 |
const methodName = "CreateHook" |
| 96 |
testBadOptions(t, methodName, func() (err error) { |
| 97 |
_, _, err = client.Organizations.CreateHook(ctx, "\n", input) |
| 98 |
return err |
| 99 |
}) |
| 100 |
|
| 101 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 102 |
got, resp, err := client.Organizations.CreateHook(ctx, "o", input) |
| 103 |
if got != nil { |
| 104 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 105 |
} |
| 106 |
return resp, err |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
func TestOrganizationsService_GetHook(t *testing.T) { |
| 111 |
client, mux, _, teardown := setup() |
| 112 |
defer teardown() |
| 113 |
|
| 114 |
mux.HandleFunc("/orgs/o/hooks/1", func(w http.ResponseWriter, r *http.Request) { |
| 115 |
testMethod(t, r, "GET") |
| 116 |
fmt.Fprint(w, `{"id":1}`) |
| 117 |
}) |
| 118 |
|
| 119 |
ctx := context.Background() |
| 120 |
hook, _, err := client.Organizations.GetHook(ctx, "o", 1) |
| 121 |
if err != nil { |
| 122 |
t.Errorf("Organizations.GetHook returned error: %v", err) |
| 123 |
} |
| 124 |
|
| 125 |
want := &Hook{ID: Int64(1)} |
| 126 |
if !cmp.Equal(hook, want) { |
| 127 |
t.Errorf("Organizations.GetHook returned %+v, want %+v", hook, want) |
| 128 |
} |
| 129 |
|
| 130 |
const methodName = "GetHook" |
| 131 |
testBadOptions(t, methodName, func() (err error) { |
| 132 |
_, _, err = client.Organizations.GetHook(ctx, "\n", -1) |
| 133 |
return err |
| 134 |
}) |
| 135 |
|
| 136 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 137 |
got, resp, err := client.Organizations.GetHook(ctx, "o", 1) |
| 138 |
if got != nil { |
| 139 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 140 |
} |
| 141 |
return resp, err |
| 142 |
}) |
| 143 |
} |
| 144 |
|
| 145 |
func TestOrganizationsService_GetHook_invalidOrg(t *testing.T) { |
| 146 |
client, _, _, teardown := setup() |
| 147 |
defer teardown() |
| 148 |
|
| 149 |
ctx := context.Background() |
| 150 |
_, _, err := client.Organizations.GetHook(ctx, "%", 1) |
| 151 |
testURLParseError(t, err) |
| 152 |
} |
| 153 |
|
| 154 |
func TestOrganizationsService_EditHook(t *testing.T) { |
| 155 |
client, mux, _, teardown := setup() |
| 156 |
defer teardown() |
| 157 |
|
| 158 |
input := &Hook{} |
| 159 |
|
| 160 |
mux.HandleFunc("/orgs/o/hooks/1", func(w http.ResponseWriter, r *http.Request) { |
| 161 |
v := new(Hook) |
| 162 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 163 |
|
| 164 |
testMethod(t, r, "PATCH") |
| 165 |
if !cmp.Equal(v, input) { |
| 166 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 167 |
} |
| 168 |
|
| 169 |
fmt.Fprint(w, `{"id":1}`) |
| 170 |
}) |
| 171 |
|
| 172 |
ctx := context.Background() |
| 173 |
hook, _, err := client.Organizations.EditHook(ctx, "o", 1, input) |
| 174 |
if err != nil { |
| 175 |
t.Errorf("Organizations.EditHook returned error: %v", err) |
| 176 |
} |
| 177 |
|
| 178 |
want := &Hook{ID: Int64(1)} |
| 179 |
if !cmp.Equal(hook, want) { |
| 180 |
t.Errorf("Organizations.EditHook returned %+v, want %+v", hook, want) |
| 181 |
} |
| 182 |
|
| 183 |
const methodName = "EditHook" |
| 184 |
testBadOptions(t, methodName, func() (err error) { |
| 185 |
_, _, err = client.Organizations.EditHook(ctx, "\n", -1, input) |
| 186 |
return err |
| 187 |
}) |
| 188 |
|
| 189 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 190 |
got, resp, err := client.Organizations.EditHook(ctx, "o", 1, input) |
| 191 |
if got != nil { |
| 192 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 193 |
} |
| 194 |
return resp, err |
| 195 |
}) |
| 196 |
} |
| 197 |
|
| 198 |
func TestOrganizationsService_EditHook_invalidOrg(t *testing.T) { |
| 199 |
client, _, _, teardown := setup() |
| 200 |
defer teardown() |
| 201 |
|
| 202 |
ctx := context.Background() |
| 203 |
_, _, err := client.Organizations.EditHook(ctx, "%", 1, nil) |
| 204 |
testURLParseError(t, err) |
| 205 |
} |
| 206 |
|
| 207 |
func TestOrganizationsService_PingHook(t *testing.T) { |
| 208 |
client, mux, _, teardown := setup() |
| 209 |
defer teardown() |
| 210 |
|
| 211 |
mux.HandleFunc("/orgs/o/hooks/1/pings", func(w http.ResponseWriter, r *http.Request) { |
| 212 |
testMethod(t, r, "POST") |
| 213 |
}) |
| 214 |
|
| 215 |
ctx := context.Background() |
| 216 |
_, err := client.Organizations.PingHook(ctx, "o", 1) |
| 217 |
if err != nil { |
| 218 |
t.Errorf("Organizations.PingHook returned error: %v", err) |
| 219 |
} |
| 220 |
|
| 221 |
const methodName = "PingHook" |
| 222 |
testBadOptions(t, methodName, func() (err error) { |
| 223 |
_, err = client.Organizations.PingHook(ctx, "\n", -1) |
| 224 |
return err |
| 225 |
}) |
| 226 |
|
| 227 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 228 |
return client.Organizations.PingHook(ctx, "o", 1) |
| 229 |
}) |
| 230 |
} |
| 231 |
|
| 232 |
func TestOrganizationsService_DeleteHook(t *testing.T) { |
| 233 |
client, mux, _, teardown := setup() |
| 234 |
defer teardown() |
| 235 |
|
| 236 |
mux.HandleFunc("/orgs/o/hooks/1", func(w http.ResponseWriter, r *http.Request) { |
| 237 |
testMethod(t, r, "DELETE") |
| 238 |
}) |
| 239 |
|
| 240 |
ctx := context.Background() |
| 241 |
_, err := client.Organizations.DeleteHook(ctx, "o", 1) |
| 242 |
if err != nil { |
| 243 |
t.Errorf("Organizations.DeleteHook returned error: %v", err) |
| 244 |
} |
| 245 |
|
| 246 |
const methodName = "DeleteHook" |
| 247 |
testBadOptions(t, methodName, func() (err error) { |
| 248 |
_, err = client.Organizations.DeleteHook(ctx, "\n", -1) |
| 249 |
return err |
| 250 |
}) |
| 251 |
|
| 252 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 253 |
return client.Organizations.DeleteHook(ctx, "o", 1) |
| 254 |
}) |
| 255 |
} |
| 256 |
|
| 257 |
func TestOrganizationsService_DeleteHook_invalidOrg(t *testing.T) { |
| 258 |
client, _, _, teardown := setup() |
| 259 |
defer teardown() |
| 260 |
|
| 261 |
ctx := context.Background() |
| 262 |
_, err := client.Organizations.DeleteHook(ctx, "%", 1) |
| 263 |
testURLParseError(t, err) |
| 264 |
} |