-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add auth tests #522
base: master
Are you sure you want to change the base?
Add auth tests #522
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"golang.org/x/oauth2" | ||
) | ||
|
||
func TestAccessNoTokenCallsLoginAndStoresToken(t *testing.T) { | ||
httpAuth := HttpAuth{ | ||
DeviceResp: &oauth2.DeviceAuthResponse{ | ||
UserCode: "YOLO", | ||
VerificationURIComplete: "localhost", | ||
}, | ||
} | ||
dummyStore := DummyStore{ | ||
token: nil, | ||
} | ||
ua := UserAuthenticator{ | ||
conf: &httpAuth, | ||
ts: &dummyStore, | ||
autoLogin: true, | ||
popBrowser: false, | ||
} | ||
|
||
client, err := ua.Access(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if client == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and assert.NotNil etc for the tests below |
||
t.Fatal("client is nil") | ||
} | ||
if dummyStore.tokenRead == false { | ||
t.Fatal("expected a read of the token") | ||
} | ||
if dummyStore.tokenStored == false { | ||
t.Fatal("expected the token to be stored") | ||
} | ||
} | ||
|
||
func TestAccessInvalidTokenCallsLoginAndStoresToken(t *testing.T) { | ||
httpAuth := HttpAuth{ | ||
DeviceResp: &oauth2.DeviceAuthResponse{ | ||
UserCode: "YOLO", | ||
VerificationURIComplete: "localhost", | ||
}, | ||
} | ||
dummyStore := DummyStore{ | ||
token: &oauth2.Token{ | ||
AccessToken: "", | ||
}, | ||
} | ||
ua := UserAuthenticator{ | ||
conf: &httpAuth, | ||
ts: &dummyStore, | ||
autoLogin: true, | ||
popBrowser: false, | ||
} | ||
|
||
client, err := ua.Access(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if client == nil { | ||
t.Fatal("client is nil") | ||
} | ||
if dummyStore.tokenRead == false { | ||
t.Fatal("expected a read of the token") | ||
} | ||
if dummyStore.tokenStored == false { | ||
t.Fatal("expected the token to be stored") | ||
} | ||
} | ||
|
||
func TestAccessValidTokenNoLogin(t *testing.T) { | ||
httpAuth := HttpAuth{ | ||
DeviceResp: &oauth2.DeviceAuthResponse{ | ||
UserCode: "YOLO", | ||
VerificationURIComplete: "localhost", | ||
}, | ||
} | ||
dummyStore := DummyStore{ | ||
token: &oauth2.Token{ | ||
AccessToken: "ACCESSTOKEN", | ||
Expiry: time.Now().Add(1000 * time.Minute), | ||
}, | ||
} | ||
ua := UserAuthenticator{ | ||
conf: &httpAuth, | ||
ts: &dummyStore, | ||
autoLogin: true, | ||
popBrowser: false, | ||
} | ||
|
||
client, err := ua.Access(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if client == nil { | ||
t.Fatal("client is nil") | ||
} | ||
if !dummyStore.tokenRead { | ||
t.Fatal("expected a read of the token") | ||
} | ||
if dummyStore.tokenStored { | ||
t.Fatal("did not expect the token to be stored") | ||
} | ||
if httpAuth.loginCalled { | ||
t.Fatal("did not expect login to be called") | ||
} | ||
} | ||
|
||
type HttpAuth struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a reason you're using a mocking framework? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you prefer this approach then I am fine xD |
||
DeviceResp *oauth2.DeviceAuthResponse | ||
token *oauth2.Token | ||
loginCalled bool | ||
} | ||
|
||
func (ha *HttpAuth) DeviceAuth(ctx context.Context, opts ...oauth2.AuthCodeOption) (*oauth2.DeviceAuthResponse, error) { | ||
ha.loginCalled = true | ||
return ha.DeviceResp, nil | ||
} | ||
|
||
func (ha *HttpAuth) DeviceAccessToken(ctx context.Context, da *oauth2.DeviceAuthResponse, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) { | ||
return ha.token, nil | ||
} | ||
|
||
func (ha *HttpAuth) Client(ctx context.Context, t *oauth2.Token) *http.Client { | ||
return http.DefaultClient | ||
} | ||
|
||
type DummyStore struct { | ||
token *oauth2.Token | ||
tokenStored bool | ||
tokenRead bool | ||
} | ||
|
||
func (ds *DummyStore) storeAccessToken(token *oauth2.Token) error { | ||
ds.tokenStored = true | ||
return nil | ||
} | ||
func (ds *DummyStore) readAccessToken() (*oauth2.Token, error) { | ||
ds.tokenRead = true | ||
if ds.token == nil { | ||
return nil, fmt.Errorf("no token found") | ||
} | ||
return ds.token, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend to use require.NoError instead with a msg if wanted.