Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "github.com/racker/perigee" |
| 6 | ) |
| 7 | |
| 8 | // This reauth-handler does nothing, and returns no error. |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 9 | func doNothing(_ AccessProvider) error { |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 10 | return nil |
| 11 | } |
| 12 | |
| 13 | func TestOtherErrorsPropegate(t *testing.T) { |
| 14 | calls := 0 |
| 15 | c := TestContext().WithReauthHandler(doNothing) |
| 16 | |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 17 | err := c.WithReauth(nil, func() error { |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 18 | calls++ |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 19 | return &perigee.UnexpectedResponseCodeError{ |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 20 | Expected: []int{204}, |
| 21 | Actual: 404, |
| 22 | } |
| 23 | }) |
| 24 | |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 25 | 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 | |
| 39 | func Test401ErrorCausesBodyInvokation2ndTime(t *testing.T) { |
| 40 | calls := 0 |
| 41 | c := TestContext().WithReauthHandler(doNothing) |
| 42 | |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 43 | err := c.WithReauth(nil, func() error { |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 44 | calls++ |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 45 | return &perigee.UnexpectedResponseCodeError{ |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 46 | Expected: []int{204}, |
| 47 | Actual: 401, |
| 48 | } |
| 49 | }) |
| 50 | |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 51 | 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 | |
| 61 | func TestReauthAttemptShouldHappen(t *testing.T) { |
| 62 | calls := 0 |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 63 | c := TestContext().WithReauthHandler(func(_ AccessProvider) error { |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 64 | calls++ |
| 65 | return nil |
| 66 | }) |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 67 | c.WithReauth(nil, func() error { |
| 68 | return &perigee.UnexpectedResponseCodeError{ |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 69 | 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 | |
| 80 | type MyError struct {} |
| 81 | func (*MyError) Error() string { |
| 82 | return "MyError instance" |
| 83 | } |
| 84 | |
| 85 | func TestReauthErrorShouldPropegate(t *testing.T) { |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 86 | c := TestContext().WithReauthHandler(func(_ AccessProvider) error { |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 87 | return &MyError{} |
| 88 | }) |
| 89 | |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 90 | err := c.WithReauth(nil, func() error { |
| 91 | return &perigee.UnexpectedResponseCodeError{ |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 92 | 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 II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 102 | |
| 103 | type MyAccess struct {} |
| 104 | func (my *MyAccess) FirstEndpointUrlByCriteria(ApiCriteria) string { |
| 105 | return "" |
| 106 | } |
| 107 | func (my *MyAccess) AuthToken() string { |
| 108 | return "" |
| 109 | } |
Samuel A. Falvo II | 0167aaa | 2013-07-16 12:36:25 -0700 | [diff] [blame] | 110 | func (my *MyAccess) Revoke(string) error { |
| 111 | return nil |
| 112 | } |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 113 | func (my *MyAccess) Reauthenticate() error { |
| 114 | return nil |
| 115 | } |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 116 | |
| 117 | func 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 | } |