From 6977732b50b98e38a37d717592b2d98747658974 Mon Sep 17 00:00:00 2001 From: Matthew Dean Date: Wed, 10 Jan 2018 22:13:15 -0500 Subject: [PATCH] Fix #210 --- .../main/kotlin/net/dean/jraw/oauth/AccountHelper.kt | 10 +++++++--- .../net/dean/jraw/test/unit/AccountHelperTest.kt | 10 +++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/src/main/kotlin/net/dean/jraw/oauth/AccountHelper.kt b/lib/src/main/kotlin/net/dean/jraw/oauth/AccountHelper.kt index 55de0622f..17671f032 100644 --- a/lib/src/main/kotlin/net/dean/jraw/oauth/AccountHelper.kt +++ b/lib/src/main/kotlin/net/dean/jraw/oauth/AccountHelper.kt @@ -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)) } @@ -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)) diff --git a/lib/src/test/kotlin/net/dean/jraw/test/unit/AccountHelperTest.kt b/lib/src/test/kotlin/net/dean/jraw/test/unit/AccountHelperTest.kt index ad6061c4f..ae9d3e75c 100644 --- a/lib/src/test/kotlin/net/dean/jraw/test/unit/AccountHelperTest.kt +++ b/lib/src/test/kotlin/net/dean/jraw/test/unit/AccountHelperTest.kt @@ -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) } @@ -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` } @@ -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`