-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprovider_test.go
91 lines (74 loc) · 2.52 KB
/
provider_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
package main
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
type ClientRequest struct {
GrantType string `json:"grant_type"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Audience string `json:"audience"`
}
func TestProviderConfigRawSad(t *testing.T) {
assert := assert.New(t)
clientSecret := "cauliflower"
clientId := "joebang"
testServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(400)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{"error":"access_denied","error_description":"Service not enabled within domain: https://dshbreak.auth0.com/api/v2/"}`)
}))
defer testServer.Close()
testDomain := testServer.URL[8:]
result, _ := providerConfigureRaw(testServer.Client(), testDomain, clientId, clientSecret, "")
assert.Equal(Config{}, result)
}
func TestProviderConfigWithToken(t *testing.T) {
assert := assert.New(t)
result, err := providerConfigureRaw(http.DefaultClient, "contoso.auth0.com", "", "", "not_a_real_token")
assert.Equal("contoso.auth0.com", result.(Config).domain)
assert.Equal("not_a_real_token", result.(Config).accessToken)
assert.Equal(err, nil)
}
func TestProviderConfigRaw(t *testing.T) {
assert := assert.New(t)
times := 0
clientSecret := "cauliflower"
clientId := "joebang"
token := "wubbalubbadubdub"
testServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
times++
assert.Equal("POST", r.Method)
body, readErr := ioutil.ReadAll(r.Body)
if readErr != nil {
t.Fatalf("Failed to read request: %s", readErr)
}
var clientRequest ClientRequest
unmarshalErr := json.Unmarshal(body, &clientRequest)
if unmarshalErr != nil {
t.Fatalf("Failed to parse request: %s", unmarshalErr)
}
assert.Equal(clientSecret, clientRequest.ClientSecret)
assert.Equal(clientId, clientRequest.ClientId)
assert.Equal("client_credentials", clientRequest.GrantType)
clientResponse := &Auth0Token{
AccessToken: token,
ExpiresIn: 86400,
Scope: "superman:all",
TokenType: "type",
}
w.WriteHeader(200)
json.NewEncoder(w).Encode(clientResponse)
}))
defer testServer.Close()
testDomain := testServer.URL[8:]
result, _ := providerConfigureRaw(testServer.Client(), testDomain, clientId, clientSecret, "")
assert.Equal(1, times)
assert.Equal(testDomain, result.(Config).domain)
assert.Equal(token, result.(Config).accessToken)
}