-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathdeviceauth_test.go
88 lines (83 loc) · 3.16 KB
/
deviceauth_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
// Copyright 2014 The Go 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 internal
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestDeviceAuth_ClientAuthnInParams(t *testing.T) {
styleCache := new(AuthStyleCache)
const clientID = "client-id"
const clientSecret = "client-secret"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got, want := r.FormValue("client_id"), clientID; got != want {
t.Errorf("client_id = %q; want %q", got, want)
}
if got, want := r.FormValue("client_secret"), clientSecret; got != want {
t.Errorf("client_secret = %q; want %q", got, want)
}
io.WriteString(w, `{"device_code":"code","user_code":"user_code","verification_uri":"http://example.device.com","expires_in":300,"interval":5}`)
}))
defer ts.Close()
_, err := RetrieveDeviceAuth(context.Background(), clientID, clientSecret, ts.URL, url.Values{}, AuthStyleInParams, styleCache)
if err != nil {
t.Errorf("RetrieveDeviceAuth = %v; want no error", err)
}
}
func TestDeviceAuth_ClientAuthnInHeader(t *testing.T) {
styleCache := new(AuthStyleCache)
const clientID = "client-id"
const clientSecret = "client-secret"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
if !ok {
io.WriteString(w, `{"error":"invalid_client"}`)
w.WriteHeader(http.StatusBadRequest)
}
if got, want := u, clientID; got != want {
io.WriteString(w, `{"error":"invalid_client"}`)
w.WriteHeader(http.StatusBadRequest)
}
if got, want := p, clientSecret; got != want {
io.WriteString(w, `{"error":"invalid_client"}`)
w.WriteHeader(http.StatusBadRequest)
}
io.WriteString(w, `{"device_code":"code","user_code":"user_code","verification_uri":"http://example.device.com","expires_in":300,"interval":5}`)
}))
defer ts.Close()
_, err := RetrieveDeviceAuth(context.Background(), clientID, clientSecret, ts.URL, url.Values{}, AuthStyleInHeader, styleCache)
if err != nil {
t.Errorf("RetrieveDeviceAuth = %v; want no error", err)
}
}
func TestDeviceAuth_ClientAuthnProbe(t *testing.T) {
styleCache := new(AuthStyleCache)
const clientID = "client-id"
const clientSecret = "client-secret"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
if !ok {
io.WriteString(w, `{"error":"invalid_client"}`)
w.WriteHeader(http.StatusBadRequest)
}
if got, want := u, clientID; got != want {
io.WriteString(w, `{"error":"invalid_client"}`)
w.WriteHeader(http.StatusBadRequest)
}
if got, want := p, clientSecret; got != want {
io.WriteString(w, `{"error":"invalid_client"}`)
w.WriteHeader(http.StatusBadRequest)
}
io.WriteString(w, `{"device_code":"code","user_code":"user_code","verification_uri":"http://example.device.com","expires_in":300,"interval":5}`)
}))
defer ts.Close()
_, err := RetrieveDeviceAuth(context.Background(), clientID, clientSecret, ts.URL, url.Values{}, AuthStyleUnknown, styleCache)
if err != nil {
t.Errorf("RetrieveDeviceAuth = %v; want no error", err)
}
}