Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit e53e6f1

Browse files
author
Fabio Falzoi
committed
Changes after code review
Signed-off-by: Fabio Falzoi <fabio@develer.com>
1 parent d73f53c commit e53e6f1

File tree

2 files changed

+69
-105
lines changed

2 files changed

+69
-105
lines changed

auth/auth.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ type HTTPClient interface {
5959
Get(url string) (resp *http.Response, err error)
6060
}
6161

62-
var (
63-
client HTTPClient
64-
)
62+
var client HTTPClient
6563

6664
// Init initialize correctly HTTP client
6765
func Init() {

auth/auth_test.go

Lines changed: 68 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package auth
33
import (
44
"bytes"
55
"encoding/json"
6-
"io"
76
"io/ioutil"
87
"net/http"
98
"net/textproto"
@@ -42,12 +41,9 @@ func TestAuthStartError(t *testing.T) {
4241
}
4342

4443
data, err := StartDeviceAuth("", "0")
45-
if err == nil {
46-
t.Error(err)
47-
}
4844

45+
assert.Error(t, err)
4946
assert.Equal(t, data, DeviceCode{})
50-
assert.NotNil(t, err)
5147
}
5248

5349
func TestAuthStartData(t *testing.T) {
@@ -69,44 +65,38 @@ func TestAuthStartData(t *testing.T) {
6965
return nil, errors.New("content-type is wrong")
7066
}
7167

72-
if req.Method != "POST" {
68+
if req.Method != http.MethodPost {
7369
return nil, errors.New("Method is wrong")
7470
}
7571

7672
if !strings.Contains(req.URL.Path, "/oauth/device/code") {
7773
return nil, errors.New("url is wrong")
7874
}
7975

80-
body, err := req.GetBody()
81-
if err != nil {
82-
return nil, err
83-
}
84-
buf := new(strings.Builder)
85-
_, err = io.Copy(buf, body)
76+
body, err := ioutil.ReadAll(req.Body)
8677
if err != nil {
8778
return nil, err
8879
}
8980

90-
if !strings.Contains(buf.String(), "client_id=") ||
91-
!strings.Contains(buf.String(), "&audience=https://api.arduino.cc") {
81+
bodyStr := string(body)
82+
if !strings.Contains(bodyStr, "client_id=") ||
83+
!strings.Contains(bodyStr, "&audience=https://api.arduino.cc") {
9284
return nil, errors.New("Payload is wrong")
9385
}
9486

95-
data, err := json.Marshal(d)
96-
if err != nil {
87+
var data bytes.Buffer
88+
if err := json.NewEncoder(&data).Encode(d); err != nil {
9789
return nil, err
9890
}
9991

10092
return &http.Response{
101-
Body: ioutil.NopCloser(bytes.NewBufferString(string(data))),
93+
Body: ioutil.NopCloser(&data),
10294
}, nil
10395
}
10496

10597
data, err := StartDeviceAuth("", "0")
106-
if err != nil {
107-
t.Error(err)
108-
}
10998

99+
assert.NoError(t, err)
110100
assert.Equal(t, data, d)
111101
}
112102

@@ -127,63 +117,54 @@ func TestAuthCheck(t *testing.T) {
127117
return nil, errors.New("content-type is wrong")
128118
}
129119

130-
if req.Method != "POST" {
120+
if req.Method != http.MethodPost {
131121
return nil, errors.New("Method is wrong")
132122
}
133123

134124
if !strings.Contains(req.URL.Path, "/oauth/token") {
135125
return nil, errors.New("url is wrong")
136126
}
137127

138-
body, err := req.GetBody()
139-
if err != nil {
140-
return nil, err
141-
}
142-
buf := new(strings.Builder)
143-
_, err = io.Copy(buf, body)
128+
body, err := ioutil.ReadAll(req.Body)
144129
if err != nil {
145130
return nil, err
146131
}
147132

148-
if !strings.Contains(buf.String(), "client_id=") ||
149-
!strings.Contains(buf.String(), "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code&device_code=") {
133+
bodyStr := string(body)
134+
if !strings.Contains(bodyStr, "client_id=") ||
135+
!strings.Contains(bodyStr, "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code&device_code=") {
150136
return nil, errors.New("Payload is wrong")
151137
}
152138

153-
data, err := json.Marshal(AuthAccess{
139+
var data bytes.Buffer
140+
if err := json.NewEncoder(&data).Encode(AuthAccess{
154141
AccessToken: "asdf",
155142
ExpiresIn: 999,
156143
TokenType: "testType",
157-
})
158-
if err != nil {
144+
}); err != nil {
159145
return nil, err
160146
}
161147

162148
return &http.Response{
163149
StatusCode: 200,
164-
Body: ioutil.NopCloser(bytes.NewBufferString(string(data))),
150+
Body: ioutil.NopCloser(&data),
165151
}, nil
166152
}
167153

168154
token, err := CheckDeviceAuth("", "0", "")
169-
if err != nil {
170-
t.Error(err)
171-
}
172155

156+
assert.NoError(t, err)
173157
assert.Equal(t, "asdf", token)
174158
}
175159

176160
func TestDefaultConfig(t *testing.T) {
177-
c := New()
178-
defaultConfig := &Config{
161+
assert.Equal(t, New(), &Config{
179162
CodeURL: "https://hydra.arduino.cc/oauth2/auth",
180163
TokenURL: "https://hydra.arduino.cc/oauth2/token",
181164
ClientID: "cli",
182165
RedirectURI: "http://localhost:5000",
183166
Scopes: "profile:core offline",
184-
}
185-
186-
assert.Equal(t, defaultConfig, c)
167+
})
187168
}
188169

189170
func TestRequestAuthError(t *testing.T) {
@@ -196,11 +177,7 @@ func TestRequestAuthError(t *testing.T) {
196177
}
197178

198179
_, _, err := config.requestAuth(client)
199-
if err == nil {
200-
t.Error(err)
201-
}
202-
203-
assert.True(t, err != nil)
180+
assert.Error(t, err)
204181
}
205182

206183
func TestRequestAuth(t *testing.T) {
@@ -211,11 +188,11 @@ func TestRequestAuth(t *testing.T) {
211188
Scopes: "profile:core offline",
212189
}
213190

214-
coutGetCall := 0
191+
countGetCall := 0
215192
GetFunc = func(url string) (*http.Response, error) {
216-
coutGetCall++
193+
countGetCall++
217194

218-
if coutGetCall == 1 {
195+
if countGetCall == 1 {
219196
if !strings.Contains(url, "www.test.com?client_id=1&redirect_uri=http%3A%2F%2Flocalhost%3A5000&response_type=code&scope=profile%3Acore+offline&state=") {
220197
return nil, errors.New("Error in url")
221198
}
@@ -229,7 +206,7 @@ func TestRequestAuth(t *testing.T) {
229206
return nil, errors.New("url should be empty because no Location is provided in Header")
230207
}
231208

232-
r, err := http.NewRequest("GET", "www.test.com", bytes.NewBufferString(""))
209+
r, err := http.NewRequest("GET", "www.test.com", nil)
233210
if err != nil {
234211
return nil, err
235212
}
@@ -241,14 +218,12 @@ func TestRequestAuth(t *testing.T) {
241218
}
242219

243220
res, biscuits, err := config.requestAuth(client)
244-
if err != nil {
245-
t.Error(err)
246-
}
247221

248-
expectedCookies := cookies{}
249-
expectedCookies["hydra"] = []*http.Cookie{}
250-
expectedCookies["auth"] = []*http.Cookie{}
251-
assert.Equal(t, expectedCookies, biscuits)
222+
assert.NoError(t, err)
223+
assert.Equal(t, biscuits, cookies{
224+
"hydra": []*http.Cookie{},
225+
"auth": []*http.Cookie{},
226+
})
252227
assert.Equal(t, "www.test.com", res)
253228
}
254229

@@ -270,43 +245,37 @@ func TestAuthenticateError(t *testing.T) {
270245
return nil, errors.New("content-type is wrong")
271246
}
272247

273-
if req.Method != "POST" {
248+
if req.Method != http.MethodPost {
274249
return nil, errors.New("Method is wrong")
275250
}
276251

277252
if !strings.Contains(req.URL.Path, "www.test.io") {
278253
return nil, errors.New("url is wrong")
279254
}
280255

281-
body, err := req.GetBody()
282-
if err != nil {
283-
return nil, err
284-
}
285-
buf := new(strings.Builder)
286-
_, err = io.Copy(buf, body)
256+
body, err := ioutil.ReadAll(req.Body)
287257
if err != nil {
288258
return nil, err
289259
}
290260

291-
if !strings.Contains(buf.String(), "username=") ||
292-
!strings.Contains(buf.String(), "password") ||
293-
!strings.Contains(buf.String(), "csrf") ||
294-
!strings.Contains(buf.String(), "g-recaptcha-response") {
261+
bodyStr := string(body)
262+
if !strings.Contains(bodyStr, "username=") ||
263+
!strings.Contains(bodyStr, "password") ||
264+
!strings.Contains(bodyStr, "csrf") ||
265+
!strings.Contains(bodyStr, "g-recaptcha-response") {
295266
return nil, errors.New("Payload is wrong")
296267
}
297268

298269
return &http.Response{
299270
StatusCode: 200,
300271
Status: "200 OK",
301-
Body: ioutil.NopCloser(bytes.NewBufferString(string("testBody"))),
272+
Body: ioutil.NopCloser(strings.NewReader("testBody")),
302273
}, nil
303274
}
304275

305276
response, err := config.authenticate(client, cookies{}, "www.test.io", "user", "pw")
306-
if err == nil {
307-
t.Error("This test should return an error")
308-
}
309277

278+
assert.Error(t, err)
310279
assert.Equal(t, "", response)
311280
}
312281

@@ -331,54 +300,56 @@ func TestAuthenticate(t *testing.T) {
331300
return nil, errors.New("content-type is wrong")
332301
}
333302

334-
if req.Method != "POST" {
303+
if req.Method != http.MethodPost {
335304
return nil, errors.New("Method is wrong")
336305
}
337306

338307
if !strings.Contains(req.URL.Path, "www.test.io") {
339308
return nil, errors.New("url is wrong")
340309
}
341310

342-
body, err := req.GetBody()
343-
if err != nil {
344-
return nil, err
345-
}
346-
buf := new(strings.Builder)
347-
_, err = io.Copy(buf, body)
311+
body, err := ioutil.ReadAll(req.Body)
348312
if err != nil {
349313
return nil, err
350314
}
351315

352-
if !strings.Contains(buf.String(), "username=") ||
353-
!strings.Contains(buf.String(), "password") ||
354-
!strings.Contains(buf.String(), "csrf") ||
355-
!strings.Contains(buf.String(), "g-recaptcha-response") {
316+
bodyStr := string(body)
317+
if !strings.Contains(bodyStr, "username=") ||
318+
!strings.Contains(bodyStr, "password") ||
319+
!strings.Contains(bodyStr, "csrf") ||
320+
!strings.Contains(bodyStr, "g-recaptcha-response") {
356321
return nil, errors.New("Payload is wrong")
357322
}
358323

359-
return &http.Response{
324+
resp := &http.Response{
360325
StatusCode: 302,
361326
Status: "302 OK",
362-
Body: ioutil.NopCloser(bytes.NewBufferString(string("testBody"))),
363-
}, nil
327+
Header: http.Header{},
328+
Body: ioutil.NopCloser(strings.NewReader("testBody")),
329+
}
330+
resp.Header.Set("Location", "www.redirect.io")
331+
332+
return resp, nil
364333
}
365334

366-
if req.Method != "GET" {
335+
if req.Method != http.MethodGet {
367336
return nil, errors.New("Method is wrong")
368337
}
369338

339+
if !strings.Contains(req.URL.Path, "www.redirect.io") {
340+
return nil, errors.New("redirect url is wrong")
341+
}
342+
370343
return &http.Response{
371344
StatusCode: 200,
372345
Status: "200 OK",
373-
Body: ioutil.NopCloser(bytes.NewBufferString(string("testBody"))),
346+
Body: ioutil.NopCloser(strings.NewReader("testBody")),
374347
}, nil
375348
}
376349

377350
response, err := config.authenticate(client, cookies{}, "www.test.io", "user", "pw")
378-
if err != nil {
379-
t.Error(err)
380-
}
381351

352+
assert.NoError(t, err)
382353
assert.Equal(t, "", response)
383354
}
384355

@@ -390,10 +361,8 @@ func TestRequestTokenError(t *testing.T) {
390361
}
391362

392363
token, err := c.requestToken(client, "9")
393-
if err == nil {
394-
t.Error("This test should return an error")
395-
}
396364

365+
assert.Error(t, err)
397366
assert.True(t, token == nil)
398367
}
399368

@@ -409,23 +378,20 @@ func TestRequestToken(t *testing.T) {
409378
}
410379

411380
DoFunc = func(req *http.Request) (*http.Response, error) {
412-
413-
data, errJSON := json.Marshal(expectedToken)
414-
if errJSON != nil {
415-
return nil, errJSON
381+
var data bytes.Buffer
382+
if err := json.NewEncoder(&data).Encode(expectedToken); err != nil {
383+
return nil, err
416384
}
417385

418386
return &http.Response{
419387
StatusCode: 200,
420388
Status: "200 OK",
421-
Body: ioutil.NopCloser(bytes.NewBufferString(string(data))),
389+
Body: ioutil.NopCloser(&data),
422390
}, nil
423391
}
424392

425393
token, err := c.requestToken(client, "9")
426-
if err != nil {
427-
t.Error(err)
428-
}
429394

395+
assert.NoError(t, err)
430396
assert.Equal(t, expectedToken, *token)
431397
}

0 commit comments

Comments
 (0)