| 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 TestUsersService_ListKeys_authenticatedUser(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/user/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.Users.ListKeys(ctx, "", opt) |
| 31 |
if err != nil { |
| 32 |
t.Errorf("Users.ListKeys returned error: %v", err) |
| 33 |
} |
| 34 |
|
| 35 |
want := []*Key{{ID: Int64(1)}} |
| 36 |
if !cmp.Equal(keys, want) { |
| 37 |
t.Errorf("Users.ListKeys returned %+v, want %+v", keys, want) |
| 38 |
} |
| 39 |
|
| 40 |
const methodName = "ListKeys" |
| 41 |
testBadOptions(t, methodName, func() (err error) { |
| 42 |
_, _, err = client.Users.ListKeys(ctx, "\n", opt) |
| 43 |
return err |
| 44 |
}) |
| 45 |
|
| 46 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 47 |
got, resp, err := client.Users.ListKeys(ctx, "", 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 TestUsersService_ListKeys_specifiedUser(t *testing.T) { |
| 56 |
client, mux, _, teardown := setup() |
| 57 |
defer teardown() |
| 58 |
|
| 59 |
mux.HandleFunc("/users/u/keys", func(w http.ResponseWriter, r *http.Request) { |
| 60 |
testMethod(t, r, "GET") |
| 61 |
fmt.Fprint(w, `[{"id":1}]`) |
| 62 |
}) |
| 63 |
|
| 64 |
ctx := context.Background() |
| 65 |
keys, _, err := client.Users.ListKeys(ctx, "u", nil) |
| 66 |
if err != nil { |
| 67 |
t.Errorf("Users.ListKeys returned error: %v", err) |
| 68 |
} |
| 69 |
|
| 70 |
want := []*Key{{ID: Int64(1)}} |
| 71 |
if !cmp.Equal(keys, want) { |
| 72 |
t.Errorf("Users.ListKeys returned %+v, want %+v", keys, want) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestUsersService_ListKeys_invalidUser(t *testing.T) { |
| 77 |
client, _, _, teardown := setup() |
| 78 |
defer teardown() |
| 79 |
|
| 80 |
ctx := context.Background() |
| 81 |
_, _, err := client.Users.ListKeys(ctx, "%", nil) |
| 82 |
testURLParseError(t, err) |
| 83 |
} |
| 84 |
|
| 85 |
func TestUsersService_GetKey(t *testing.T) { |
| 86 |
client, mux, _, teardown := setup() |
| 87 |
defer teardown() |
| 88 |
|
| 89 |
mux.HandleFunc("/user/keys/1", func(w http.ResponseWriter, r *http.Request) { |
| 90 |
testMethod(t, r, "GET") |
| 91 |
fmt.Fprint(w, `{"id":1}`) |
| 92 |
}) |
| 93 |
|
| 94 |
ctx := context.Background() |
| 95 |
key, _, err := client.Users.GetKey(ctx, 1) |
| 96 |
if err != nil { |
| 97 |
t.Errorf("Users.GetKey returned error: %v", err) |
| 98 |
} |
| 99 |
|
| 100 |
want := &Key{ID: Int64(1)} |
| 101 |
if !cmp.Equal(key, want) { |
| 102 |
t.Errorf("Users.GetKey returned %+v, want %+v", key, want) |
| 103 |
} |
| 104 |
|
| 105 |
const methodName = "GetKey" |
| 106 |
testBadOptions(t, methodName, func() (err error) { |
| 107 |
_, _, err = client.Users.GetKey(ctx, -1) |
| 108 |
return err |
| 109 |
}) |
| 110 |
|
| 111 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 112 |
got, resp, err := client.Users.GetKey(ctx, 1) |
| 113 |
if got != nil { |
| 114 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 115 |
} |
| 116 |
return resp, err |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
func TestUsersService_CreateKey(t *testing.T) { |
| 121 |
client, mux, _, teardown := setup() |
| 122 |
defer teardown() |
| 123 |
|
| 124 |
input := &Key{Key: String("k"), Title: String("t")} |
| 125 |
|
| 126 |
mux.HandleFunc("/user/keys", func(w http.ResponseWriter, r *http.Request) { |
| 127 |
v := new(Key) |
| 128 |
assertNilError(t, json.NewDecoder(r.Body).Decode(v)) |
| 129 |
|
| 130 |
testMethod(t, r, "POST") |
| 131 |
if !cmp.Equal(v, input) { |
| 132 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 133 |
} |
| 134 |
|
| 135 |
fmt.Fprint(w, `{"id":1}`) |
| 136 |
}) |
| 137 |
|
| 138 |
ctx := context.Background() |
| 139 |
key, _, err := client.Users.CreateKey(ctx, input) |
| 140 |
if err != nil { |
| 141 |
t.Errorf("Users.CreateKey returned error: %v", err) |
| 142 |
} |
| 143 |
|
| 144 |
want := &Key{ID: Int64(1)} |
| 145 |
if !cmp.Equal(key, want) { |
| 146 |
t.Errorf("Users.CreateKey returned %+v, want %+v", key, want) |
| 147 |
} |
| 148 |
|
| 149 |
const methodName = "CreateKey" |
| 150 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 151 |
got, resp, err := client.Users.CreateKey(ctx, input) |
| 152 |
if got != nil { |
| 153 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 154 |
} |
| 155 |
return resp, err |
| 156 |
}) |
| 157 |
} |
| 158 |
|
| 159 |
func TestUsersService_DeleteKey(t *testing.T) { |
| 160 |
client, mux, _, teardown := setup() |
| 161 |
defer teardown() |
| 162 |
|
| 163 |
mux.HandleFunc("/user/keys/1", func(w http.ResponseWriter, r *http.Request) { |
| 164 |
testMethod(t, r, "DELETE") |
| 165 |
}) |
| 166 |
|
| 167 |
ctx := context.Background() |
| 168 |
_, err := client.Users.DeleteKey(ctx, 1) |
| 169 |
if err != nil { |
| 170 |
t.Errorf("Users.DeleteKey returned error: %v", err) |
| 171 |
} |
| 172 |
|
| 173 |
const methodName = "DeleteKey" |
| 174 |
testBadOptions(t, methodName, func() (err error) { |
| 175 |
_, err = client.Users.DeleteKey(ctx, -1) |
| 176 |
return err |
| 177 |
}) |
| 178 |
|
| 179 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 180 |
return client.Users.DeleteKey(ctx, 1) |
| 181 |
}) |
| 182 |
} |
| 183 |
|
| 184 |
func TestKey_Marshal(t *testing.T) { |
| 185 |
testJSONMarshal(t, &Key{}, "{}") |
| 186 |
|
| 187 |
u := &Key{ |
| 188 |
ID: Int64(1), |
| 189 |
Key: String("abc"), |
| 190 |
URL: String("url"), |
| 191 |
Title: String("title"), |
| 192 |
ReadOnly: Bool(true), |
| 193 |
Verified: Bool(true), |
| 194 |
CreatedAt: &Timestamp{referenceTime}, |
| 195 |
} |
| 196 |
|
| 197 |
want := `{ |
| 198 |
"id": 1, |
| 199 |
"key": "abc", |
| 200 |
"url": "url", |
| 201 |
"title": "title", |
| 202 |
"read_only": true, |
| 203 |
"verified": true, |
| 204 |
"created_at": ` + referenceTimeStr + ` |
| 205 |
}` |
| 206 |
|
| 207 |
testJSONMarshal(t, u, want) |
| 208 |
} |