SignedTokenManager implements SimpleTokenManager { @Override public boolean isValid(SignedExpiringToken token) { Preconditions.checkNotNull(token); return TokenStatus.OK.equals(getValidity(token)); } @Inject  SignedTokenManager(SignatureHandler signatureHandler, ZonedDateTimeProvider zonedDateTimeProvider); @Override TokenStatus getValidity(SignedExpiringToken token); @Override boolean isValid(SignedExpiringToken token);  }
@Test(expected = NullPointerException.class) public void isValidShouldThrowWhenTokenIsNull() throws Exception { tokenManager.isValid(null); }
@Test public void isValidShouldRecognizeValidTokens() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); assertThat( tokenManager.isValid( tokenFactory.generateContinuationToken("user"))) .isTrue(); }
@Test public void isValidShouldRecognizeTokenWhereUsernameIsModified() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken continuationToken = tokenFactory.generateContinuationToken("user"); ContinuationToken pirateContinuationToken = new ContinuationToken("pirate", continuationToken.getExpirationDate(), continuationToken.getSignature()); assertThat(tokenManager.isValid(pirateContinuationToken)).isFalse(); }
@Test public void isValidShouldRecognizeTokenWhereExpirationDateIsModified() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken continuationToken = tokenFactory.generateContinuationToken("user"); ContinuationToken pirateContinuationToken = new ContinuationToken(continuationToken.getUsername(), continuationToken.getExpirationDate().plusHours(1), continuationToken.getSignature()); assertThat(tokenManager.isValid(pirateContinuationToken)).isFalse(); }
@Test public void isValidShouldRecognizeTokenWhereSignatureIsModified() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken continuationToken = tokenFactory.generateContinuationToken("user"); ContinuationToken pirateContinuationToken = new ContinuationToken(continuationToken.getUsername(), continuationToken.getExpirationDate(), FAKE_SIGNATURE); assertThat(tokenManager.isValid(pirateContinuationToken)).isFalse(); }
@Test public void isValidShouldReturnFalseWhenTokenIsOutdated() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken continuationToken = tokenFactory.generateContinuationToken("user"); zonedDateTimeProvider.setFixedDateTime(DATE.plusHours(1)); assertThat(tokenManager.isValid(continuationToken)).isFalse(); }
@Test public void isValidShouldReturnFalseOnNonValidSignatures() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); ContinuationToken pirateContinuationToken = new ContinuationToken("user", DATE.plusMinutes(15), "fake"); assertThat(tokenManager.isValid(pirateContinuationToken)).isFalse(); }
@Test public void signedAttachmentAccessTokenShouldBeValidated() { String blobId = "blobId"; zonedDateTimeProvider.setFixedDateTime(DATE); String serializedToken = tokenFactory.generateAttachmentAccessToken("user", blobId).serialize(); assertThat(tokenManager.isValid(AttachmentAccessToken.from(serializedToken, blobId))).isTrue(); }