Skip to content

Commit

Permalink
Merge pull request #44 from trivago/bugfix-accessToken
Browse files Browse the repository at this point in the history
Bugfix access token
  • Loading branch information
wching committed Apr 4, 2017
2 parents 3fd37e4 + 69cdec7 commit d908a4c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -29,4 +29,4 @@ before_script:
- android-wait-for-emulator
- adb shell input keyevent 82 &

script: ./gradlew library:connectedAndroidTest
script: ./gradlew library:connectedAndroidTest
2 changes: 1 addition & 1 deletion 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
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Expand Up @@ -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"
}

Expand Down Expand Up @@ -71,4 +71,4 @@ dependencies {
exclude group: 'junit'
}
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
}
}
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<OAuth2AccessToken>(Mock(OAuth2AccessTokenStorage))
Expand All @@ -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<OAuth2AccessToken>(Mock(OAuth2AccessTokenStorage))
Expand All @@ -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)
Expand Down Expand Up @@ -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<OAuth2AccessToken>(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<OAuth2AccessToken>(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"
Expand All @@ -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<OAuth2AccessToken>(storage)
Expand All @@ -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<OAuth2AccessToken>(storage)
Expand All @@ -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"() {
Expand All @@ -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<OAuth2AccessToken>(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()
Expand Down
Expand Up @@ -19,7 +19,6 @@ public class OAuth2AccessTokenManager<TAccessToken extends OAuth2AccessToken> {
// Members

private final OAuth2AccessTokenStorage<TAccessToken> mStorage;
private Single<TAccessToken> mTokenSingle;

// Constructor

Expand Down Expand Up @@ -71,23 +70,15 @@ public Single<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> 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();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle
Expand Up @@ -56,4 +56,4 @@ dependencies {

// Butterknife
compile 'com.jakewharton:butterknife:6.1.0'
}
}

0 comments on commit d908a4c

Please sign in to comment.