Skip to content

Commit

Permalink
Fix #210
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbdean committed Jan 11, 2018
1 parent 7603b89 commit 6977732
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/src/main/kotlin/net/dean/jraw/oauth/AccountHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ class AccountHelper(
* required if there is no unexpired OAuthData for userless mode.
*/
fun switchToUserless(): RedditClient {
val seamless = trySwitchToUser(AuthManager.USERNAME_USERLESS)
val seamless = trySwitchToUser(AuthManager.USERNAME_USERLESS, creds = userlessCreds)
if (seamless != null)
return switch(seamless)
// If trySwitchToUser returns a non-null value it will have already switched the current client to the
// returned value
return seamless

return switch(OAuthHelper.automatic(http, userlessCreds, tokenStore))
}
Expand All @@ -86,7 +88,9 @@ class AccountHelper(
* Tries to create a new RedditClient based on already-stored refresh token or unexpired OAuthData. Returns null if
* neither are available.
*/
fun trySwitchToUser(username: String): RedditClient? {
fun trySwitchToUser(username: String) = trySwitchToUser(username, creds)

private fun trySwitchToUser(username: String, creds: Credentials = this.creds): RedditClient? {
val current = tokenStore.fetchLatest(username)
if (current != null && !current.isExpired)
return switch(RedditClient(http, current, creds, tokenStore, username))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class AccountHelperTest : Spek({
// Should not require an HTTP request
val reddit = h.switchToUserless()
reddit.authManager.currentUsername().should.equal(AuthManager.USERNAME_USERLESS)

// Make sure it was given the userless Credentials
reddit.authMethod.isUserless.should.be.`true`
reddit.should.be.of.identity(h.reddit)
}

Expand All @@ -70,6 +73,11 @@ class AccountHelperTest : Spek({
helper = AccountHelper(NoopNetworkAdapter, creds, tokenStore, uuid)
}

it("should report a non-userless AuthMethod when switched to successfully") {
tokenStore.storeLatest(username, createMockOAuthData())
helper.trySwitchToUser(username)?.authMethod.should.equal(AuthMethod.APP)
}

it("should return null when there is no data to use") {
helper.trySwitchToUser("some random user").should.be.`null`
}
Expand All @@ -90,7 +98,7 @@ class AccountHelperTest : Spek({
// When only a refresh token is available AccountHelper gives the RedditClient a "fake" OAuthData instance
// that's already expired with a valid refresh token
r.authManager.current.should.not.be.`null`
r.authManager.current!!.isExpired().should.be.`true`
r.authManager.current!!.isExpired.should.be.`true`

// Make sure the AccountHelper forces the client to renew its access token on the first request
r.forceRenew.should.be.`true`
Expand Down

0 comments on commit 6977732

Please sign in to comment.