| 1 |
// Copyright 2013 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_ListKeys(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/repos/o/r/keys", 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}]`) |
| 26 |
}) |
| 27 |
|
| 28 |
opt := &ListOptions{Page: 2} |
| 29 |
ctx := context.Background() |
| 30 |
keys, _, err := client.Repositories.ListKeys(ctx, "o", "r", opt) |
| 31 |
if err != nil { |
| 32 |
t.Errorf("Repositories.ListKeys returned error: %v", err) |
| 33 |
} |
| 34 |
|
| 35 |
want := []*Key{{ID: Int64(1)}} |
| 36 |
if !cmp.Equal(keys, want) { |
| 37 |
t.Errorf("Repositories.ListKeys returned %+v, want %+v", keys, want) |
| 38 |
} |
| 39 |
|
| 40 |
const methodName = "ListKeys" |
| 41 |
testBadOptions(t, methodName, func() (err error) { |
| 42 |
_, _, err = client.Repositories.ListKeys(ctx, "\n", "\n", opt) |
| 43 |
return err |
| 44 |
}) |
| 45 |
|
| 46 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 47 |
got, resp, err := client.Repositories.ListKeys(ctx, "o", "r", opt) |
| 48 |
if got != nil { |
| 49 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 50 |
} |
| 51 |
return resp, err |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
func TestRepositoriesService_ListKeys_invalidOwner(t *testing.T) { |
| 56 |
client, _, _, teardown := setup() |
| 57 |
defer teardown() |
| 58 |
|
| 59 |
ctx := context.Background() |
| 60 |
_, _, err := client.Repositories.ListKeys(ctx, "%", "%", nil) |
| 61 |
testURLParseError(t, err) |
| 62 |
} |
| 63 |
|
| 64 |
func TestRepositoriesService_GetKey(t *testing.T) { |
| 65 |
client, mux, _, teardown := setup() |
| 66 |
defer teardown() |
| 67 |
|
| 68 |
mux.HandleFunc("/repos/o/r/keys/1", func(w http.ResponseWriter, r *http.Request) { |
| 69 |
testMethod(t, r, "GET") |
| 70 |
fmt.Fprint(w, `{"id":1}`) |
| 71 |
}) |
| 72 |
|
| 73 |
ctx := context.Background() |
| 74 |
key, _, err := client.Repositories.GetKey(ctx, "o", "r", 1) |
| 75 |
if err != nil { |
| 76 |
t.Errorf("Repositories.GetKey returned error: %v", err) |
| 77 |
} |
| 78 |
|
| 79 |
want := &Key{ID: Int64(1)} |
| 80 |
if !cmp.Equal(key, want) { |
| 81 |
t.Errorf("Repositories.GetKey returned %+v, want %+v", key, want) |
| 82 |
} |
| 83 |
|
| 84 |
const methodName = "GetKey" |
| 85 |
testBadOptions(t, methodName, func() (err error) { |
| 86 |
_, _, err = client.Repositories.GetKey(ctx, "\n", "\n", -1) |
| 87 |
return err |
| 88 |
}) |
| 89 |
|
| 90 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 91 |
got, resp, err := client.Repositories.GetKey(ctx, "o", "r", 1) |
| 92 |
if got != nil { |
| 93 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 94 |
} |
| 95 |
return resp, err |
| 96 |
}) |
| 97 |
} |
| 98 |
|
| 99 |
func TestRepositoriesService_GetKey_invalidOwner(t *testing.T) { |
| 100 |
client, _, _, teardown := setup() |
| 101 |
defer teardown() |
| 102 |
|
| 103 |
ctx := context.Background() |
| 104 |
_, _, err := client.Repositories.GetKey(ctx, "%", "%", 1) |
| 105 |
testURLParseError(t, err) |
| 106 |
} |
| 107 |
|
| 108 |
func TestRepositoriesService_CreateKey(t *testing.T) { |
| 109 |
client, mux, _, teardown := setup() |
| 110 |
defer teardown() |
| 111 |
|
| 112 |
input := &Key{Key: String("k"), Title: String("t")} |
| 113 |
|
| 114 |
mux.HandleFunc("/repos/o/r/keys", func(w http.ResponseWriter, r *http.Request) { |
| 115 |
v := new(Key) |
| 116 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 117 |
|
| 118 |
testMethod(t, r, "POST") |
| 119 |
if !cmp.Equal(v, input) { |
| 120 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 121 |
} |
| 122 |
|
| 123 |
fmt.Fprint(w, `{"id":1}`) |
| 124 |
}) |
| 125 |
|
| 126 |
ctx := context.Background() |
| 127 |
key, _, err := client.Repositories.CreateKey(ctx, "o", "r", input) |
| 128 |
if err != nil { |
| 129 |
t.Errorf("Repositories.GetKey returned error: %v", err) |
| 130 |
} |
| 131 |
|
| 132 |
want := &Key{ID: Int64(1)} |
| 133 |
if !cmp.Equal(key, want) { |
| 134 |
t.Errorf("Repositories.GetKey returned %+v, want %+v", key, want) |
| 135 |
} |
| 136 |
|
| 137 |
const methodName = "CreateKey" |
| 138 |
testBadOptions(t, methodName, func() (err error) { |
| 139 |
_, _, err = client.Repositories.CreateKey(ctx, "\n", "\n", input) |
| 140 |
return err |
| 141 |
}) |
| 142 |
|
| 143 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 144 |
got, resp, err := client.Repositories.CreateKey(ctx, "o", "r", input) |
| 145 |
if got != nil { |
| 146 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 147 |
} |
| 148 |
return resp, err |
| 149 |
}) |
| 150 |
} |
| 151 |
|
| 152 |
func TestRepositoriesService_CreateKey_invalidOwner(t *testing.T) { |
| 153 |
client, _, _, teardown := setup() |
| 154 |
defer teardown() |
| 155 |
|
| 156 |
ctx := context.Background() |
| 157 |
_, _, err := client.Repositories.CreateKey(ctx, "%", "%", nil) |
| 158 |
testURLParseError(t, err) |
| 159 |
} |
| 160 |
|
| 161 |
func TestRepositoriesService_DeleteKey(t *testing.T) { |
| 162 |
client, mux, _, teardown := setup() |
| 163 |
defer teardown() |
| 164 |
|
| 165 |
mux.HandleFunc("/repos/o/r/keys/1", func(w http.ResponseWriter, r *http.Request) { |
| 166 |
testMethod(t, r, "DELETE") |
| 167 |
}) |
| 168 |
|
| 169 |
ctx := context.Background() |
| 170 |
_, err := client.Repositories.DeleteKey(ctx, "o", "r", 1) |
| 171 |
if err != nil { |
| 172 |
t.Errorf("Repositories.DeleteKey returned error: %v", err) |
| 173 |
} |
| 174 |
|
| 175 |
const methodName = "DeleteKey" |
| 176 |
testBadOptions(t, methodName, func() (err error) { |
| 177 |
_, err = client.Repositories.DeleteKey(ctx, "\n", "\n", -1) |
| 178 |
return err |
| 179 |
}) |
| 180 |
|
| 181 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 182 |
return client.Repositories.DeleteKey(ctx, "o", "r", 1) |
| 183 |
}) |
| 184 |
} |
| 185 |
|
| 186 |
func TestRepositoriesService_DeleteKey_invalidOwner(t *testing.T) { |
| 187 |
client, _, _, teardown := setup() |
| 188 |
defer teardown() |
| 189 |
|
| 190 |
ctx := context.Background() |
| 191 |
_, err := client.Repositories.DeleteKey(ctx, "%", "%", 1) |
| 192 |
testURLParseError(t, err) |
| 193 |
} |