forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_users_test.go
176 lines (151 loc) · 5.01 KB
/
admin_users_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2019 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestAdminUsers_Create(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/admin/users", func(w http.ResponseWriter, r *http.Request) {
v := new(createUserRequest)
json.NewDecoder(r.Body).Decode(v)
testMethod(t, r, "POST")
want := &createUserRequest{Login: String("github"), Email: String("email@domain.com")}
if !reflect.DeepEqual(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
}
fmt.Fprint(w, `{"login":"github","id":1}`)
})
ctx := context.Background()
org, _, err := client.Admin.CreateUser(ctx, "github", "email@domain.com")
if err != nil {
t.Errorf("Admin.CreateUser returned error: %v", err)
}
want := &User{ID: Int64(1), Login: String("github")}
if !reflect.DeepEqual(org, want) {
t.Errorf("Admin.CreateUser returned %+v, want %+v", org, want)
}
const methodName = "CreateUser"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Admin.CreateUser(ctx, "github", "email@domain.com")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
func TestAdminUsers_Delete(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/admin/users/github", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})
ctx := context.Background()
_, err := client.Admin.DeleteUser(ctx, "github")
if err != nil {
t.Errorf("Admin.DeleteUser returned error: %v", err)
}
const methodName = "DeleteUser"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Admin.DeleteUser(ctx, "\n")
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Admin.DeleteUser(ctx, "github")
})
}
func TestUserImpersonation_Create(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/admin/users/github/authorizations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testBody(t, r, `{"scopes":["repo"]}`+"\n")
fmt.Fprint(w, `{"id": 1234,
"url": "https://git.company.com/api/v3/authorizations/1234",
"app": {
"name": "GitHub Site Administrator",
"url": "https://docs.github.com/en/free-pro-team@latest/rest/reference/enterprise/users/",
"client_id": "1234"
},
"token": "1234",
"hashed_token": "1234",
"token_last_eight": "1234",
"note": null,
"note_url": null,
"created_at": "2018-01-01T00:00:00Z",
"updated_at": "2018-01-01T00:00:00Z",
"scopes": [
"repo"
],
"fingerprint": null}`)
})
opt := &ImpersonateUserOptions{Scopes: []string{"repo"}}
ctx := context.Background()
auth, _, err := client.Admin.CreateUserImpersonation(ctx, "github", opt)
if err != nil {
t.Errorf("Admin.CreateUserImpersonation returned error: %v", err)
}
date := Timestamp{Time: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC)}
want := &UserAuthorization{
ID: Int64(1234),
URL: String("https://git.company.com/api/v3/authorizations/1234"),
App: &OAuthAPP{
Name: String("GitHub Site Administrator"),
URL: String("https://docs.github.com/en/free-pro-team@latest/rest/reference/enterprise/users/"),
ClientID: String("1234"),
},
Token: String("1234"),
HashedToken: String("1234"),
TokenLastEight: String("1234"),
Note: nil,
NoteURL: nil,
CreatedAt: &date,
UpdatedAt: &date,
Scopes: []string{"repo"},
Fingerprint: nil,
}
if !reflect.DeepEqual(auth, want) {
t.Errorf("Admin.CreateUserImpersonation returned %+v, want %+v", auth, want)
}
const methodName = "CreateUserImpersonation"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Admin.CreateUserImpersonation(ctx, "\n", opt)
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Admin.CreateUserImpersonation(ctx, "github", opt)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
func TestUserImpersonation_Delete(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/admin/users/github/authorizations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})
ctx := context.Background()
_, err := client.Admin.DeleteUserImpersonation(ctx, "github")
if err != nil {
t.Errorf("Admin.DeleteUserImpersonation returned error: %v", err)
}
const methodName = "DeleteUserImpersonation"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Admin.DeleteUserImpersonation(ctx, "\n")
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Admin.DeleteUserImpersonation(ctx, "github")
})
}