Skip to content

Commit

Permalink
Merge pull request #26 from rheinfabrik/feature/allow_non_expiring_to…
Browse files Browse the repository at this point in the history
…kens

Feature/allow non expiring tokens
  • Loading branch information
Giulio Petek committed Jun 21, 2015
2 parents a38ba6d + 230e0ee commit a184fc7
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
Expand Up @@ -69,6 +69,26 @@ class OAuth2AccessTokenManagerGrantNewAccessTokenSpecs extends AndroidSpecificat
newToken.expirationDate.timeInMillis == calendar.getTimeInMillis() + 3000
}

def "it should NOT generate and set the correct expiration date if expiresIn is null"() {

given: "An OAuth2AccessToken"
OAuth2AccessToken accessToken = new OAuth2AccessToken(expirationDate: null)
accessToken.expiresIn = null

and: "A grant emitting that token"
OAuth2Grant grant = Mock(OAuth2Grant)
grant.grantNewAccessToken() >> just(accessToken)

and: "An OAuth2AccessTokenManager"
OAuth2AccessTokenManager tokenManager = new OAuth2AccessTokenManager<OAuth2AccessToken>(Mock(OAuth2AccessTokenStorage))

when: "I ask for a new access token"
OAuth2AccessToken newToken = tokenManager.grantNewAccessToken(grant).toBlocking().first()

then: "The access token should have the NO expiration date"
newToken.expirationDate == null
}

def "it should store the access token"() {

given: "An OAuth2AccessToken"
Expand Down
Expand Up @@ -7,26 +7,35 @@ import spock.lang.Title
@Title("Specs for serialization in the OAuth2AccessToken class.")
class OAuth2AccessTokenSerializationSpecs extends AndroidSpecification {

// Setup

def setup() {

// Set default locale and time zone
Locale.setDefault(Locale.GERMANY)
TimeZone.setDefault(TimeZone.getTimeZone("CEST"))
}

// Scenarios

def "It should create the correct JSON for a given OAuth2AccessToken"() {

given: "An OAuth2AccessToken"
OAuth2AccessToken accessToken = new OAuth2AccessToken(refreshToken: "rt", expiresIn: 3600, accessToken: "at", tokenType: "bearer")
accessToken.expirationDate = Calendar.getInstance(Locale.UK)
accessToken.expirationDate = Calendar.getInstance()
accessToken.expirationDate.setTimeInMillis(0)

when: "I serialize it with Gson"
String json = new Gson().toJson(accessToken)

then: "The JSON should be as expected"
json == "{\"access_token\":\"at\",\"heimdall_expiration_date\":{\"year\":1970,\"month\":0,\"dayOfMonth\":1,\"hourOfDay\":1,\"minute\":0,\"second\":0},\"expires_in\":3600,\"refresh_token\":\"rt\",\"token_type\":\"bearer\"}"
json == "{\"access_token\":\"at\",\"heimdall_expiration_date\":{\"year\":1970,\"month\":0,\"dayOfMonth\":1,\"hourOfDay\":0,\"minute\":0,\"second\":0},\"expires_in\":3600,\"refresh_token\":\"rt\",\"token_type\":\"bearer\"}"
}

def "It should create the correct OAuth2AccessToken for a given JSON"() {

given: "Some JSON representing an OAuth2AccessToken"
String json = "{\"access_token\":\"at\",\"heimdall_expiration_date\":{\"year\":1970,\"month\":0,\"dayOfMonth\":1,\"hourOfDay\":1,\"minute\":0,\"second\":0},\"refresh_token\":\"rt\",\"token_type\":\"bearer\",\"expires_in\":3600}"
String json = "{\"access_token\":\"at\",\"heimdall_expiration_date\":{\"year\":1970,\"month\":0,\"dayOfMonth\":1,\"hourOfDay\":0,\"minute\":0,\"second\":0},\"expires_in\":3600,\"refresh_token\":\"rt\",\"token_type\":\"bearer\"}"

when: "I deserialize it with Gson"
OAuth2AccessToken accessToken = new Gson().fromJson(json, OAuth2AccessToken.class)
Expand All @@ -38,9 +47,9 @@ class OAuth2AccessTokenSerializationSpecs extends AndroidSpecification {
accessToken.accessToken == "at"
accessToken.tokenType == "bearer"

Calendar calendar = Calendar.getInstance(Locale.UK)
Calendar calendar = Calendar.getInstance()
calendar.setTimeInMillis(0)
accessToken.expirationDate == calendar
accessToken.expirationDate.timeInMillis == calendar.timeInMillis
})
}
}
Expand All @@ -51,7 +60,7 @@ class OAuth2AccessTokenIsExpiredSpecs extends AndroidSpecification {
// Scenarios

@SuppressWarnings("GroovyPointlessBoolean")
def "It should return true if the expirationDate is null"() {
def "It should return false if the expirationDate is null"() {

given: "An OAuth2AccessToken with null as expirationDate"
OAuth2AccessToken accessToken = new OAuth2AccessToken(expirationDate: null)
Expand All @@ -60,7 +69,7 @@ class OAuth2AccessTokenIsExpiredSpecs extends AndroidSpecification {
boolean isExpired = accessToken.isExpired()

then: "It should be true"
isExpired == true
isExpired == false
}

@SuppressWarnings("GroovyPointlessBoolean")
Expand Down
Expand Up @@ -45,7 +45,7 @@ public class OAuth2AccessToken implements Serializable {
* expiration time via other means or document the default value.
*/
@SerializedName("expires_in")
public int expiresIn;
public Integer expiresIn;

/**
* The expiration date used by Heimdall.
Expand All @@ -61,7 +61,7 @@ public class OAuth2AccessToken implements Serializable {
* @return True if expired. Otherwise false.
*/
public boolean isExpired() {
return expirationDate == null || Calendar.getInstance().after(expirationDate);
return expirationDate != null && Calendar.getInstance().after(expirationDate);
}

// Object
Expand Down
Expand Up @@ -74,9 +74,11 @@ public Observable<TAccessToken> grantNewAccessToken(OAuth2Grant<TAccessToken> gr
return grant
.grantNewAccessToken()
.doOnNext(accessToken -> {
Calendar expirationDate = (Calendar) calendar.clone();
expirationDate.add(Calendar.SECOND, accessToken.expiresIn);
accessToken.expirationDate = expirationDate;
if (accessToken.expiresIn != null) {
Calendar expirationDate = (Calendar) calendar.clone();
expirationDate.add(Calendar.SECOND, accessToken.expiresIn);
accessToken.expirationDate = expirationDate;
}

mStorage.storeAccessToken(accessToken);
});
Expand Down
Expand Up @@ -3,9 +3,7 @@
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

Expand Down
Expand Up @@ -4,8 +4,8 @@

import de.rheinfabrik.heimdall.OAuth2AccessToken;
import de.rheinfabrik.heimdalldroid.network.models.AccessTokenRequestBody;
import de.rheinfabrik.heimdalldroid.network.models.TraktTvList;
import de.rheinfabrik.heimdalldroid.network.models.RefreshTokenRequestBody;
import de.rheinfabrik.heimdalldroid.network.models.TraktTvList;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.Header;
Expand Down

0 comments on commit a184fc7

Please sign in to comment.