github.com/google/go-github/v68@v68.0.0/github/admin_users_test.go (about) 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 t.Parallel() 21 client, mux, _ := setup(t) 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: Ptr("email@domain.com"), Suspended: Ptr(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: Ptr("email@domain.com"), 40 Suspended: Ptr(false), 41 }) 42 if err != nil { 43 t.Errorf("Admin.CreateUser returned error: %v", err) 44 } 45 46 want := &User{ID: Ptr(int64(1)), Login: Ptr("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: Ptr("email@domain.com"), 56 Suspended: Ptr(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 t.Parallel() 67 client, mux, _ := setup(t) 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 t.Parallel() 92 client, mux, _ := setup(t) 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: Ptr(int64(1234)), 127 URL: Ptr("https://git.company.com/api/v3/authorizations/1234"), 128 App: &OAuthAPP{ 129 Name: Ptr("GitHub Site Administrator"), 130 URL: Ptr("https://docs.github.com/en/rest/enterprise/users/"), 131 ClientID: Ptr("1234"), 132 }, 133 Token: Ptr("1234"), 134 HashedToken: Ptr("1234"), 135 TokenLastEight: Ptr("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 t.Parallel() 164 client, mux, _ := setup(t) 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 t.Parallel() 189 testJSONMarshal(t, &CreateUserRequest{}, "{}") 190 191 u := &CreateUserRequest{ 192 Login: "l", 193 Email: Ptr("e"), 194 } 195 196 want := `{ 197 "login": "l", 198 "email": "e" 199 }` 200 201 testJSONMarshal(t, u, want) 202 } 203 204 func TestImpersonateUserOptions_Marshal(t *testing.T) { 205 t.Parallel() 206 testJSONMarshal(t, &ImpersonateUserOptions{}, "{}") 207 208 u := &ImpersonateUserOptions{ 209 Scopes: []string{ 210 "s", 211 }, 212 } 213 214 want := `{ 215 "scopes": ["s"] 216 }` 217 218 testJSONMarshal(t, u, want) 219 } 220 221 func TestOAuthAPP_Marshal(t *testing.T) { 222 t.Parallel() 223 testJSONMarshal(t, &OAuthAPP{}, "{}") 224 225 u := &OAuthAPP{ 226 URL: Ptr("u"), 227 Name: Ptr("n"), 228 ClientID: Ptr("cid"), 229 } 230 231 want := `{ 232 "url": "u", 233 "name": "n", 234 "client_id": "cid" 235 }` 236 237 testJSONMarshal(t, u, want) 238 } 239 240 func TestUserAuthorization_Marshal(t *testing.T) { 241 t.Parallel() 242 testJSONMarshal(t, &UserAuthorization{}, "{}") 243 244 u := &UserAuthorization{ 245 ID: Ptr(int64(1)), 246 URL: Ptr("u"), 247 Scopes: []string{ 248 "s", 249 }, 250 Token: Ptr("t"), 251 TokenLastEight: Ptr("tle"), 252 HashedToken: Ptr("ht"), 253 App: &OAuthAPP{ 254 URL: Ptr("u"), 255 Name: Ptr("n"), 256 ClientID: Ptr("cid"), 257 }, 258 Note: Ptr("n"), 259 NoteURL: Ptr("nu"), 260 UpdatedAt: &Timestamp{referenceTime}, 261 CreatedAt: &Timestamp{referenceTime}, 262 Fingerprint: Ptr("f"), 263 } 264 265 want := `{ 266 "id": 1, 267 "url": "u", 268 "scopes": ["s"], 269 "token": "t", 270 "token_last_eight": "tle", 271 "hashed_token": "ht", 272 "app": { 273 "url": "u", 274 "name": "n", 275 "client_id": "cid" 276 }, 277 "note": "n", 278 "note_url": "nu", 279 "updated_at": ` + referenceTimeStr + `, 280 "created_at": ` + referenceTimeStr + `, 281 "fingerprint": "f" 282 }` 283 284 testJSONMarshal(t, u, want) 285 }