diff --git a/.travis.yml b/.travis.yml index 1f42d35..98aa759 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,4 +29,4 @@ before_script: - android-wait-for-emulator - adb shell input keyevent 82 & -script: ./gradlew library:connectedAndroidTest +script: ./gradlew library:connectedAndroidTest \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3e3a55d..aebe976 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -#Tue Feb 09 20:24:04 CET 2016 +#Thu Mar 30 11:28:56 CEST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/library/build.gradle b/library/build.gradle index ed5d459..811f407 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -37,8 +37,8 @@ android { defaultConfig { minSdkVersion 9 targetSdkVersion 23 - versionCode 105 - versionName "1.0.6" + versionCode 106 + versionName "1.0.7" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } @@ -71,4 +71,4 @@ dependencies { exclude group: 'junit' } androidTestCompile "com.google.dexmaker:dexmaker:1.2" -} +} \ No newline at end of file diff --git a/library/src/androidTest/groovy/de/rheinfabrik/heimdall/OAuth2AccessTokenManagerSpecs.groovy b/library/src/androidTest/groovy/de/rheinfabrik/heimdall/OAuth2AccessTokenManagerSpecs.groovy index d9c1837..40fb13f 100644 --- a/library/src/androidTest/groovy/de/rheinfabrik/heimdall/OAuth2AccessTokenManagerSpecs.groovy +++ b/library/src/androidTest/groovy/de/rheinfabrik/heimdall/OAuth2AccessTokenManagerSpecs.groovy @@ -3,12 +3,9 @@ package de.rheinfabrik.heimdall import com.andrewreitz.spock.android.AndroidSpecification import de.rheinfabrik.heimdall.grants.OAuth2Grant import de.rheinfabrik.heimdall.grants.OAuth2RefreshAccessTokenGrant +import rx.Single import spock.lang.Title -import java.util.concurrent.TimeUnit - -import static rx.Single.just - @Title("Tests for the constructor of the OAuth2AccessTokenManager class") class OAuth2AccessTokenManagerConstructorSpecs extends AndroidSpecification { @@ -56,7 +53,7 @@ class OAuth2AccessTokenManagerGrantNewAccessTokenSpecs extends AndroidSpecificat and: "A grant emitting that token" OAuth2Grant grant = Mock(OAuth2Grant) - grant.grantNewAccessToken() >> just(accessToken) + grant.grantNewAccessToken() >> Single.just(accessToken) and: "An OAuth2AccessTokenManager" OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager(Mock(OAuth2AccessTokenStorage)) @@ -79,7 +76,7 @@ class OAuth2AccessTokenManagerGrantNewAccessTokenSpecs extends AndroidSpecificat and: "A grant emitting that token" OAuth2Grant grant = Mock(OAuth2Grant) - grant.grantNewAccessToken() >> just(accessToken) + grant.grantNewAccessToken() >> Single.just(accessToken) and: "An OAuth2AccessTokenManager" OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager(Mock(OAuth2AccessTokenStorage)) @@ -98,7 +95,7 @@ class OAuth2AccessTokenManagerGrantNewAccessTokenSpecs extends AndroidSpecificat and: "A grant emitting that token" OAuth2Grant grant = Mock(OAuth2Grant) - grant.grantNewAccessToken() >> just(accessToken) + grant.grantNewAccessToken() >> Single.just(accessToken) and: "A mock storage" OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage) @@ -140,77 +137,6 @@ class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecificat // Scenarios - def "it should only request a new valid token ONCE"() { - - given: "An expired OAuth2AccessToken" - OAuth2AccessToken accessToken = Mock(OAuth2AccessToken) - accessToken.refreshToken = "rt" - accessToken.isExpired() >> true - - and: "A mock storage emitting that token" - OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage) - storage.getStoredAccessToken() >> just(accessToken) - - and: "An OAuth2AccessTokenManager with that storage" - OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager(storage) - - and: "A mock grant" - OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant) - def counter = 0 - grant.grantNewAccessToken() >> { it -> - return just(accessToken).doOnSuccess({ x -> counter++ }).delay(1, TimeUnit.SECONDS) - } - - when: "I ask for a valid access token" - tokenManager.getValidAccessToken(grant).subscribe() - - and: "I ask again" - tokenManager.getValidAccessToken(grant).subscribe() - - and: "I wait 2 seconds" - sleep(2000) - - then: "The refresh grant is asked for a new token ONCE" - counter == 1 - } - - def "it should clear the current request once done"() { - - given: "An expired OAuth2AccessToken" - OAuth2AccessToken accessToken = Mock(OAuth2AccessToken) - accessToken.refreshToken = "rt" - accessToken.isExpired() >> true - - and: "A mock storage emitting that token" - OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage) - storage.getStoredAccessToken() >> just(accessToken) - - and: "An OAuth2AccessTokenManager with that storage" - OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager(storage) - - and: "A mock grant" - OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant) - def counter = 0 - grant.grantNewAccessToken() >> { it -> - return just(accessToken).doOnSuccess({ x -> counter++ }).delay(1, TimeUnit.SECONDS) - } - - when: "I ask for a valid access token" - tokenManager.getValidAccessToken(grant).subscribe() - - and: "I ask again" - tokenManager.getValidAccessToken(grant).subscribe() - - and: "I wait 2 seconds" - sleep(2000) - - and: "I ask again" - tokenManager.getValidAccessToken(grant).subscribe() - - then: "The refresh grant is asked for a new token TWICE" - counter == 2 - } - def "it should throw an IllegalArgumentException when the refreshAccessTokenGrant parameter is null"() { given: "A null grant" @@ -234,11 +160,11 @@ class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecificat and: "A mock storage emitting that token" OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage) - storage.getStoredAccessToken() >> just(accessToken) + storage.getStoredAccessToken() >> Single.just(accessToken) and: "A mock grant" OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant) - grant.grantNewAccessToken() >> just(accessToken) + grant.grantNewAccessToken() >> Single.just(accessToken) and: "An OAuth2AccessTokenManager with that storage" OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager(storage) @@ -259,7 +185,7 @@ class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecificat and: "A mock storage emitting that token" OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage) - storage.getStoredAccessToken() >> just(accessToken) + storage.getStoredAccessToken() >> Single.just(accessToken) and: "An OAuth2AccessTokenManager with that storage" OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager(storage) @@ -271,7 +197,7 @@ class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecificat tokenManager.getValidAccessToken(grant).subscribe() then: "The refresh grant is asked for a new token" - 1 * grant.grantNewAccessToken() >> just(accessToken) + 1 * grant.grantNewAccessToken() >> Single.just(accessToken) } def "it should set the refresh token to the grant if the token is expired"() { @@ -283,14 +209,14 @@ class OAuth2AccessTokenManagerGetValidAccessTokenSpecs extends AndroidSpecificat and: "A mock storage emitting that token" OAuth2AccessTokenStorage storage = Mock(OAuth2AccessTokenStorage) - storage.getStoredAccessToken() >> just(accessToken) + storage.getStoredAccessToken() >> Single.just(accessToken) and: "An OAuth2AccessTokenManager with that storage" OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager(storage) and: "A mock grant" OAuth2RefreshAccessTokenGrant grant = Mock(OAuth2RefreshAccessTokenGrant) - grant.grantNewAccessToken() >> just(accessToken) + grant.grantNewAccessToken() >> Single.just(accessToken) when: "I ask for a valid access token" tokenManager.getValidAccessToken(grant).subscribe() diff --git a/library/src/main/java/de/rheinfabrik/heimdall/OAuth2AccessTokenManager.java b/library/src/main/java/de/rheinfabrik/heimdall/OAuth2AccessTokenManager.java index b5c852e..b8c214b 100644 --- a/library/src/main/java/de/rheinfabrik/heimdall/OAuth2AccessTokenManager.java +++ b/library/src/main/java/de/rheinfabrik/heimdall/OAuth2AccessTokenManager.java @@ -19,7 +19,6 @@ public class OAuth2AccessTokenManager { // Members private final OAuth2AccessTokenStorage mStorage; - private Single mTokenSingle; // Constructor @@ -71,23 +70,15 @@ public Single grantNewAccessToken(OAuth2Grant grant, throw new IllegalArgumentException("Grant MUST NOT be null."); } - if (mTokenSingle == null) { - mTokenSingle = grant - .grantNewAccessToken() - .doOnSuccess(accessToken -> { - if (accessToken.expiresIn != null) { - Calendar expirationDate = (Calendar) calendar.clone(); - expirationDate.add(Calendar.SECOND, accessToken.expiresIn); - accessToken.expirationDate = expirationDate; - } - - mStorage.storeAccessToken(accessToken); - - mTokenSingle = null; - }).toObservable().cache().toSingle(); - } - - return mTokenSingle; + return grant.grantNewAccessToken() + .doOnSuccess(accessToken -> { + if (accessToken.expiresIn != null) { + Calendar expirationDate = (Calendar) calendar.clone(); + expirationDate.add(Calendar.SECOND, accessToken.expiresIn); + accessToken.expirationDate = expirationDate; + } + mStorage.storeAccessToken(accessToken); + }).toObservable().cache().toSingle(); } /** diff --git a/sample/build.gradle b/sample/build.gradle index 3c0b9bb..6bbc5a4 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -56,4 +56,4 @@ dependencies { // Butterknife compile 'com.jakewharton:butterknife:6.1.0' -} +} \ No newline at end of file