Debian Codegraph @

github/admin_users_test.go

raw
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 "time"
15
16 "github.com/google/go-cmp/cmp"
17 )
18
19 func TestAdminUsers_Create(t *testing.T) {
20 client, mux, _, teardown := setup()
21 defer teardown()
22
23 mux.HandleFunc("/admin/users", func(w http.ResponseWriter, r *http.Request) {
24 v := new(CreateUserRequest)
25 assertNilError(t, json.NewDecoder(r.Body).Decode(v))
26
27 testMethod(t, r, "POST")
28 want := &CreateUserRequest{Login: "github", Email: String("[email protected]"), Suspended: Bool(false)}
29 if !cmp.Equal(v, want) {
30 t.Errorf("Request body = %+v, want %+v", v, want)
31 }
32
33 fmt.Fprint(w, `{"login":"github","id":1}`)
34 })
35
36 ctx := context.Background()
37 org, _, err := client.Admin.CreateUser(ctx, CreateUserRequest{
38 Login: "github",
39 Email: String("[email protected]"),
40 Suspended: Bool(false),
41 })
42 if err != nil {
43 t.Errorf("Admin.CreateUser returned error: %v", err)
44 }
45
46 want := &User{ID: Int64(1), Login: String("github")}
47 if !cmp.Equal(org, want) {
48 t.Errorf("Admin.CreateUser returned %+v, want %+v", org, want)
49 }
50
51 const methodName = "CreateUser"
52 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
53 got, resp, err := client.Admin.CreateUser(ctx, CreateUserRequest{
54 Login: "github",
55 Email: String("[email protected]"),
56 Suspended: Bool(false),
57 })
58 if got != nil {
59 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
60 }
61 return resp, err
62 })
63 }
64
65 func TestAdminUsers_Delete(t *testing.T) {
66 client, mux, _, teardown := setup()
67 defer teardown()
68
69 mux.HandleFunc("/admin/users/github", func(w http.ResponseWriter, r *http.Request) {
70 testMethod(t, r, "DELETE")
71 })
72
73 ctx := context.Background()
74 _, err := client.Admin.DeleteUser(ctx, "github")
75 if err != nil {
76 t.Errorf("Admin.DeleteUser returned error: %v", err)
77 }
78
79 const methodName = "DeleteUser"
80 testBadOptions(t, methodName, func() (err error) {
81 _, err = client.Admin.DeleteUser(ctx, "\n")
82 return err
83 })
84
85 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
86 return client.Admin.DeleteUser(ctx, "github")
87 })
88 }
89
90 func TestUserImpersonation_Create(t *testing.T) {
91 client, mux, _, teardown := setup()
92 defer teardown()
93
94 mux.HandleFunc("/admin/users/github/authorizations", func(w http.ResponseWriter, r *http.Request) {
95 testMethod(t, r, "POST")
96 testBody(t, r, `{"scopes":["repo"]}`+"\n")
97 fmt.Fprint(w, `{"id": 1234,
98 "url": "https://git.company.com/api/v3/authorizations/1234",
99 "app": {
100 "name": "GitHub Site Administrator",
101 "url": "https://docs.github.com/en/rest/enterprise/users/",
102 "client_id": "1234"
103 },
104 "token": "1234",
105 "hashed_token": "1234",
106 "token_last_eight": "1234",
107 "note": null,
108 "note_url": null,
109 "created_at": "2018-01-01T00:00:00Z",
110 "updated_at": "2018-01-01T00:00:00Z",
111 "scopes": [
112 "repo"
113 ],
114 "fingerprint": null}`)
115 })
116
117 opt := &ImpersonateUserOptions{Scopes: []string{"repo"}}
118 ctx := context.Background()
119 auth, _, err := client.Admin.CreateUserImpersonation(ctx, "github", opt)
120 if err != nil {
121 t.Errorf("Admin.CreateUserImpersonation returned error: %v", err)
122 }
123
124 date := Timestamp{Time: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}
125 want := &UserAuthorization{
126 ID: Int64(1234),
127 URL: String("https://git.company.com/api/v3/authorizations/1234"),
128 App: &OAuthAPP{
129 Name: String("GitHub Site Administrator"),
130 URL: String("https://docs.github.com/en/rest/enterprise/users/"),
131 ClientID: String("1234"),
132 },
133 Token: String("1234"),
134 HashedToken: String("1234"),
135 TokenLastEight: String("1234"),
136 Note: nil,
137 NoteURL: nil,
138 CreatedAt: &date,
139 UpdatedAt: &date,
140 Scopes: []string{"repo"},
141 Fingerprint: nil,
142 }
143 if !cmp.Equal(auth, want) {
144 t.Errorf("Admin.CreateUserImpersonation returned %+v, want %+v", auth, want)
145 }
146
147 const methodName = "CreateUserImpersonation"
148 testBadOptions(t, methodName, func() (err error) {
149 _, _, err = client.Admin.CreateUserImpersonation(ctx, "\n", opt)
150 return err
151 })
152
153 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
154 got, resp, err := client.Admin.CreateUserImpersonation(ctx, "github", opt)
155 if got != nil {
156 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
157 }
158 return resp, err
159 })
160 }
161
162 func TestUserImpersonation_Delete(t *testing.T) {
163 client, mux, _, teardown := setup()
164 defer teardown()
165
166 mux.HandleFunc("/admin/users/github/authorizations", func(w http.ResponseWriter, r *http.Request) {
167 testMethod(t, r, "DELETE")
168 })
169
170 ctx := context.Background()
171 _, err := client.Admin.DeleteUserImpersonation(ctx, "github")
172 if err != nil {
173 t.Errorf("Admin.DeleteUserImpersonation returned error: %v", err)
174 }
175
176 const methodName = "DeleteUserImpersonation"
177 testBadOptions(t, methodName, func() (err error) {
178 _, err = client.Admin.DeleteUserImpersonation(ctx, "\n")
179 return err
180 })
181
182 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
183 return client.Admin.DeleteUserImpersonation(ctx, "github")
184 })
185 }
186
187 func TestCreateUserRequest_Marshal(t *testing.T) {
188 testJSONMarshal(t, &CreateUserRequest{}, "{}")
189
190 u := &CreateUserRequest{
191 Login: "l",
192 Email: String("e"),
193 }
194
195 want := `{
196 "login": "l",
197 "email": "e"
198 }`
199
200 testJSONMarshal(t, u, want)
201 }
202
203 func TestImpersonateUserOptions_Marshal(t *testing.T) {
204 testJSONMarshal(t, &ImpersonateUserOptions{}, "{}")
205
206 u := &ImpersonateUserOptions{
207 Scopes: []string{
208 "s",
209 },
210 }
211
212 want := `{
213 "scopes": ["s"]
214 }`
215
216 testJSONMarshal(t, u, want)
217 }
218
219 func TestOAuthAPP_Marshal(t *testing.T) {
220 testJSONMarshal(t, &OAuthAPP{}, "{}")
221
222 u := &OAuthAPP{
223 URL: String("u"),
224 Name: String("n"),
225 ClientID: String("cid"),
226 }
227
228 want := `{
229 "url": "u",
230 "name": "n",
231 "client_id": "cid"
232 }`
233
234 testJSONMarshal(t, u, want)
235 }
236
237 func TestUserAuthorization_Marshal(t *testing.T) {
238 testJSONMarshal(t, &UserAuthorization{}, "{}")
239
240 u := &UserAuthorization{
241 ID: Int64(1),
242 URL: String("u"),
243 Scopes: []string{
244 "s",
245 },
246 Token: String("t"),
247 TokenLastEight: String("tle"),
248 HashedToken: String("ht"),
249 App: &OAuthAPP{
250 URL: String("u"),
251 Name: String("n"),
252 ClientID: String("cid"),
253 },
254 Note: String("n"),
255 NoteURL: String("nu"),
256 UpdatedAt: &Timestamp{referenceTime},
257 CreatedAt: &Timestamp{referenceTime},
258 Fingerprint: String("f"),
259 }
260
261 want := `{
262 "id": 1,
263 "url": "u",
264 "scopes": ["s"],
265 "token": "t",
266 "token_last_eight": "tle",
267 "hashed_token": "ht",
268 "app": {
269 "url": "u",
270 "name": "n",
271 "client_id": "cid"
272 },
273 "note": "n",
274 "note_url": "nu",
275 "updated_at": ` + referenceTimeStr + `,
276 "created_at": ` + referenceTimeStr + `,
277 "fingerprint": "f"
278 }`
279
280 testJSONMarshal(t, u, want)
281 }