blob: e3501b87fb3e63a6d8e11e82297e668b8e134360 [file] [log] [blame]
Samuel A. Falvo II1206f852013-07-15 17:56:51 -07001package gophercloud
2
3import (
Samuel A. Falvo II1206f852013-07-15 17:56:51 -07004 "github.com/racker/perigee"
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -07005 "testing"
Samuel A. Falvo II1206f852013-07-15 17:56:51 -07006)
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},
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -070021 Actual: 404,
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070022 }
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},
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -070047 Actual: 401,
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070048 }
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},
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -070070 Actual: 401,
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070071 }
72 })
73
74 if calls != 1 {
75 t.Errorf("Expected Reauthenticator to be called once; found %d instead", calls)
76 return
77 }
78}
79
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -070080type MyError struct{}
81
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070082func (*MyError) Error() string {
83 return "MyError instance"
84}
85
86func TestReauthErrorShouldPropegate(t *testing.T) {
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070087 c := TestContext().WithReauthHandler(func(_ AccessProvider) error {
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070088 return &MyError{}
89 })
90
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -070091 err := c.WithReauth(nil, func() error {
92 return &perigee.UnexpectedResponseCodeError{
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070093 Expected: []int{204},
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -070094 Actual: 401,
Samuel A. Falvo II1206f852013-07-15 17:56:51 -070095 }
96 })
97
98 if _, ok := err.(*MyError); !ok {
99 t.Errorf("Expected a MyError; got %#v", err)
100 return
101 }
102}
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -0700103
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700104type MyAccess struct{}
105
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -0700106func (my *MyAccess) FirstEndpointUrlByCriteria(ApiCriteria) string {
107 return ""
108}
109func (my *MyAccess) AuthToken() string {
110 return ""
111}
Samuel A. Falvo II0167aaa2013-07-16 12:36:25 -0700112func (my *MyAccess) Revoke(string) error {
113 return nil
114}
Samuel A. Falvo II9e64f6b2013-07-16 14:26:50 -0700115func (my *MyAccess) Reauthenticate() error {
116 return nil
117}
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -0700118
119func TestReauthHandlerUsesSameAccessProvider(t *testing.T) {
120 fakeAccess := &MyAccess{}
121 c := TestContext().WithReauthHandler(func(acc AccessProvider) error {
122 if acc != fakeAccess {
123 t.Errorf("Expected acc = fakeAccess")
124 }
125 return nil
126 })
127 c.WithReauth(fakeAccess, func() error {
128 return &perigee.UnexpectedResponseCodeError{
129 Expected: []int{204},
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700130 Actual: 401,
Samuel A. Falvo II2f50b142013-07-16 11:38:03 -0700131 }
132 })
133}