-
Notifications
You must be signed in to change notification settings - Fork 0
/
login_test.go
98 lines (85 loc) · 2.86 KB
/
login_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
package ccclient_test
import (
"bytes"
"fmt"
"net/http"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/xchapter7x/cloudcontroller-client"
)
var _ = Describe("New cloud controller client", func() {
var (
sampleSuccessTokenStringFormat = `{"access_token":"%s","token_type":"bearer","refresh_token":"%s","expires_in":599,"scope":"password.write cloud_controller.write openid cloud_controller.read","jti":"%s"}`
sampleFailureTokenString = `{"error":"unauthorized","error_description":"Authentication failed"}`
sampleUser = "randomeuser@pivotal.io"
samplePass = "mypass"
)
Describe("given a AccessTokenDecorate method", func() {
Context("when called with a request object", func() {
var (
request *http.Request
controlAccessToken = "testtoken"
controlTokenType = "faketype"
)
BeforeEach(func() {
request, _ = http.NewRequest("GET", "", nil)
client := new(Client)
client.AccessToken = controlAccessToken
client.TokenType = controlTokenType
client.AccessTokenDecorate(request)
})
It("then the request object should get decorated with a accesstoken from the client", func() {
controlValue := fmt.Sprintf("%s %s", controlTokenType, controlAccessToken)
Ω(request.Header.Get(HeaderAuth)).Should(Equal(controlValue))
})
})
})
Context("when calling login w/ valid arguments", func() {
var (
client *Client
err error
controlAccessToken = "my control access token"
controlRefreshToken = "my control refresh token"
controlJti = "my control jti"
)
BeforeEach(func() {
client, err = New(URLPWSLogin, sampleUser, samplePass, &mockDoer{
res: &http.Response{
Body: nopCloser{bytes.NewBufferString(fmt.Sprintf(sampleSuccessTokenStringFormat, controlAccessToken, controlRefreshToken, controlJti))},
},
}).Login()
})
It("should return a nil error", func() {
Ω(err).Should(BeNil())
})
It("should set proper values (AccessToken) from the response token object", func() {
Ω(client.AccessToken).Should(Equal(controlAccessToken))
})
})
Context("when calling login w/ in-valid arguments", func() {
var (
client *Client
err error
)
BeforeEach(func() {
client, err = New(URLPWSLogin, sampleUser, samplePass, &mockDoer{
res: &http.Response{
Body: nopCloser{bytes.NewBufferString(sampleFailureTokenString)},
},
}).Login()
})
It("should return a NON-nil error", func() {
Ω(err).ShouldNot(BeNil())
})
It("should not set token", func() {
Ω(client.AccessToken).Should(BeEmpty())
})
})
XContext("non-functional integration test", func() {
It("should not run right now", func() {
client := New(URLPWSLogin, "jcalabrese@pivotal.io", "pass", new(http.Client))
fmt.Println(client.Login())
Ω(true).Should(BeFalse())
})
})
})