blob: e6ccb17ffdb9caf487898b0aa9e69bfa9364bea0 [file] [log] [blame]
Samuel A. Falvo II1206f852013-07-15 17:56:51 -07001package gophercloud
2
3import (
4 "testing"
5 "github.com/racker/perigee"
6)
7
8// This reauth-handler does nothing, and returns no error.
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -07009func doNothing(_ AccessProvider) error {
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070010 return nil
11}
12
13func TestOtherErrorsPropegate(t *testing.T) {
14 calls := 0
15 c := TestContext().WithReauthHandler(doNothing)
16
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070017 err := c.WithReauth(nil, func() error {
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070018 calls++
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070019 return &perigee.UnexpectedResponseCodeError{
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070020 Expected: []int{204},
21 Actual: 404,
22 }
23 })
24
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070025 if err == nil {
26 t.Error("Expected MyError to be returned; got nil instead.")
27 return
28 }
29 if _, ok := err.(*perigee.UnexpectedResponseCodeError); !ok {
30 t.Error("Expected UnexpectedResponseCodeError; got %#v", err)
31 return
32 }
33 if calls != 1 {
34 t.Errorf("Expected the body to be invoked once; found %d calls instead", calls)
35 return
36 }
37}
38
39func Test401ErrorCausesBodyInvokation2ndTime(t *testing.T) {
40 calls := 0
41 c := TestContext().WithReauthHandler(doNothing)
42
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070043 err := c.WithReauth(nil, func() error {
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070044 calls++
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070045 return &perigee.UnexpectedResponseCodeError{
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070046 Expected: []int{204},
47 Actual: 401,
48 }
49 })
50
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070051 if err == nil {
52 t.Error("Expected MyError to be returned; got nil instead.")
53 return
54 }
55 if calls != 2 {
56 t.Errorf("Expected the body to be invoked once; found %d calls instead", calls)
57 return
58 }
59}
60
61func TestReauthAttemptShouldHappen(t *testing.T) {
62 calls := 0
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070063 c := TestContext().WithReauthHandler(func(_ AccessProvider) error {
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070064 calls++
65 return nil
66 })
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070067 c.WithReauth(nil, func() error {
68 return &perigee.UnexpectedResponseCodeError{
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070069 Expected: []int{204},
70 Actual: 401,
71 }
72 })
73
74 if calls != 1 {
75 t.Errorf("Expected Reauthenticator to be called once; found %d instead", calls)
76 return
77 }
78}
79
80type MyError struct {}
81func (*MyError) Error() string {
82 return "MyError instance"
83}
84
85func TestReauthErrorShouldPropegate(t *testing.T) {
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070086 c := TestContext().WithReauthHandler(func(_ AccessProvider) error {
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070087 return &MyError{}
88 })
89
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070090 err := c.WithReauth(nil, func() error {
91 return &perigee.UnexpectedResponseCodeError{
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070092 Expected: []int{204},
93 Actual: 401,
94 }
95 })
96
97 if _, ok := err.(*MyError); !ok {
98 t.Errorf("Expected a MyError; got %#v", err)
99 return
100 }
101}
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -0700102
103type MyAccess struct {}
104func (my *MyAccess) FirstEndpointUrlByCriteria(ApiCriteria) string {
105 return ""
106}
107func (my *MyAccess) AuthToken() string {
108 return ""
109}
Samuel A. Falvo II0167aaa2013-07-16 12:36:25 -0700110func (my *MyAccess) Revoke(string) error {
111 return nil
112}
Samuel A. Falvo II9e64f6b2013-07-16 14:26:50 -0700113func (my *MyAccess) Reauthenticate() error {
114 return nil
115}
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -0700116
117func TestReauthHandlerUsesSameAccessProvider(t *testing.T) {
118 fakeAccess := &MyAccess{}
119 c := TestContext().WithReauthHandler(func(acc AccessProvider) error {
120 if acc != fakeAccess {
121 t.Errorf("Expected acc = fakeAccess")
122 }
123 return nil
124 })
125 c.WithReauth(fakeAccess, func() error {
126 return &perigee.UnexpectedResponseCodeError{
127 Expected: []int{204},
128 Actual: 401,
129 }
130 })
131}