This repository was archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathauth_token_internal_test.go
More file actions
191 lines (179 loc) · 4.93 KB
/
Copy pathauth_token_internal_test.go
File metadata and controls
191 lines (179 loc) · 4.93 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package ctl
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"golang.org/x/oauth2"
)
func TestFormatPromptBox(t *testing.T) {
golden := `+----------------------------------------------------+
| |
| Please visit: |
| testingtestingtestingtestingtestingtestingtesting |
| |
| And enter the code: |
| blahblah |
| |
+----------------------------------------------------+
`
got := formatPromptBox("testingtestingtestingtestingtestingtestingtesting", "blahblah")
if got != golden {
t.Fatalf("expected:\n%s, got:\n%s", golden, got)
}
}
type authReqTest struct {
config oauth2.Config
expRsp *deviceAuthResponse
expErr error
}
func TestDeviceAuthRequest(t *testing.T) {
goodClientID := "ring-a-ding-dillo"
goodResponse := deviceAuthResponse{
DeviceCode: "Old knives are long enough as swords for hobbit-people.",
UserCode: "Sharp blades are good to have, if Shire-folk go walking, east, south, or far away into dark and danger.",
VerificationURI: "I am no weather-master, nor is aught that goes on two legs.",
VerificationURIComplete: "Hey! Come merry dol! derry dol! My hearties!",
ExpiresIn: -486846000,
Interval: 9,
}
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
clientID := r.PostFormValue("client_id")
if clientID != goodClientID {
http.Error(
w,
"Get out, you old Wight! Vanish in the sunlight!",
http.StatusBadRequest,
)
return
}
b := bytes.Buffer{}
if err := json.NewEncoder(&b).Encode(goodResponse); err != nil {
t.Fatalf("unexpected error encoding goodResponse: %v", err)
}
w.WriteHeader(http.StatusOK)
w.Write(b.Bytes())
}),
)
cli := &http.Client{}
for name, test := range map[string]authReqTest{
"badRequest": {
config: oauth2.Config{
ClientID: "Iarwain Ben-adar",
Endpoint: oauth2.Endpoint{AuthURL: srv.URL},
},
expRsp: nil,
expErr: fmt.Errorf("unsuccessful with status 400: Get out, you old Wight! Vanish in the sunlight!"),
},
"goodRequest": {
config: oauth2.Config{
ClientID: "ring-a-ding-dillo",
Endpoint: oauth2.Endpoint{AuthURL: srv.URL},
},
expRsp: &goodResponse,
expErr: nil,
},
} {
t.Run(name, func(t *testing.T) {
got, err := deviceAuthRequest(cli, test.config)
if !errEqual(err, test.expErr) {
t.Errorf("expected '%v', got '%v'", test.expErr, err)
}
if !reflect.DeepEqual(got, test.expRsp) {
t.Errorf("expected '%v', got '%v'", test.expRsp, got)
}
})
}
}
type respTest struct {
resp *http.Response
exp interface{}
}
func TestParseResponse(t *testing.T) {
sr := successResponse{
Access: "ACCESS",
Refresh: "REFRESH",
Type: "access",
ExpiresIn: 10000,
Scope: "scopity-scope-scopity-scope-pope",
}
good := bytes.Buffer{}
if err := json.NewEncoder(&good).Encode(sr); err != nil {
t.Fatalf("unexpected error: %v", err)
}
sd := errorResponse{Error: "slow_down"}
slowDown := bytes.Buffer{}
if err := json.NewEncoder(&slowDown).Encode(sd); err != nil {
t.Fatalf("unexpected error: %v", err)
}
sd.Error = "authorization_pending"
pending := bytes.Buffer{}
if err := json.NewEncoder(&pending).Encode(sd); err != nil {
t.Fatalf("unexpected error: %v", err)
}
sd.Error = "invalid_client"
genErr := bytes.Buffer{}
if err := json.NewEncoder(&genErr).Encode(sd); err != nil {
t.Fatalf("unexpected error: %v", err)
}
for name, test := range map[string]respTest{
"success": {
resp: &http.Response{
StatusCode: 200,
Body: io.NopCloser(&good),
ContentLength: int64(good.Len()),
},
exp: sr,
},
"slowDown": {
resp: &http.Response{
StatusCode: 400,
Body: io.NopCloser(&slowDown),
ContentLength: int64(slowDown.Len()),
},
exp: waitResponse{SlowDown: true},
},
"pending": {
resp: &http.Response{
StatusCode: 400,
Body: io.NopCloser(&pending),
ContentLength: int64(pending.Len()),
},
exp: waitResponse{},
},
"generalError": {
resp: &http.Response{
StatusCode: 400,
Body: io.NopCloser(&genErr),
ContentLength: int64(pending.Len()),
},
exp: errorResponse{
Error: "invalid_client",
Description: "",
URI: "",
Err: fmt.Errorf("error: %s, description: %s, uri: %s", "invalid_client",
"", ""),
},
},
} {
t.Run(name, func(t *testing.T) {
if got := parseResponse(test.resp); !reflect.DeepEqual(got, test.exp) {
t.Errorf("expected: '%v', got '%v'", test.exp, got)
}
})
}
}
func errEqual(a, b error) bool {
if a == nil {
return b == nil
}
if b == nil {
return a == nil
}
return a.Error() == b.Error()
}