| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 1 | package gophercloud | 
|  | 2 |  | 
|  | 3 | import ( | 
| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 4 | "github.com/racker/perigee" | 
| Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame^] | 5 | "testing" | 
| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 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}, | 
| Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame^] | 21 | Actual:   404, | 
| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 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}, | 
| Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame^] | 47 | Actual:   401, | 
| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 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}, | 
| Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame^] | 70 | Actual:   401, | 
| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 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 |  | 
| Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame^] | 80 | type MyError struct{} | 
|  | 81 |  | 
| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 82 | func (*MyError) Error() string { | 
|  | 83 | return "MyError instance" | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | func TestReauthErrorShouldPropegate(t *testing.T) { | 
| Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 87 | c := TestContext().WithReauthHandler(func(_ AccessProvider) error { | 
| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 88 | return &MyError{} | 
|  | 89 | }) | 
|  | 90 |  | 
| Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 91 | err := c.WithReauth(nil, func() error { | 
|  | 92 | return &perigee.UnexpectedResponseCodeError{ | 
| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 93 | Expected: []int{204}, | 
| Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame^] | 94 | Actual:   401, | 
| Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 95 | } | 
|  | 96 | }) | 
|  | 97 |  | 
|  | 98 | if _, ok := err.(*MyError); !ok { | 
|  | 99 | t.Errorf("Expected a MyError; got %#v", err) | 
|  | 100 | return | 
|  | 101 | } | 
|  | 102 | } | 
| Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 103 |  | 
| Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame^] | 104 | type MyAccess struct{} | 
|  | 105 |  | 
| Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 106 | func (my *MyAccess) FirstEndpointUrlByCriteria(ApiCriteria) string { | 
|  | 107 | return "" | 
|  | 108 | } | 
|  | 109 | func (my *MyAccess) AuthToken() string { | 
|  | 110 | return "" | 
|  | 111 | } | 
| Samuel A. Falvo II | 0167aaa | 2013-07-16 12:36:25 -0700 | [diff] [blame] | 112 | func (my *MyAccess) Revoke(string) error { | 
|  | 113 | return nil | 
|  | 114 | } | 
| Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 115 | func (my *MyAccess) Reauthenticate() error { | 
|  | 116 | return nil | 
|  | 117 | } | 
| Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | func 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 II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame^] | 130 | Actual:   401, | 
| Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 131 | } | 
|  | 132 | }) | 
|  | 133 | } |