Search in sources :

Example 16 with AccessToken

use of com.thoughtworks.go.domain.AccessToken in project gocd by gocd.

the class AccessTokenSqlMapDaoIntegrationTest method shouldReturnNullWhenNoAccessTokenFound.

@Test
public void shouldReturnNullWhenNoAccessTokenFound() {
    AccessToken savedAccessToken = accessTokenSqlMapDao.loadForAdminUser(-1);
    assertThat(savedAccessToken).isNull();
}
Also used : AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 17 with AccessToken

use of com.thoughtworks.go.domain.AccessToken in project gocd by gocd.

the class AccessTokenSqlMapDaoIntegrationTest method adminsShouldBeAbleToLoadTokensRevokedBecauseOfUserDeletionButUsersCannot.

@Test
public void adminsShouldBeAbleToLoadTokensRevokedBecauseOfUserDeletionButUsersCannot() {
    String user = RandomStringUtils.random(32);
    AccessToken token = randomAccessTokenForUser(user);
    accessTokenSqlMapDao.saveOrUpdate(token);
    accessTokenSqlMapDao.saveOrUpdate(token.revokeBecauseOfUserDelete("admin", clock.currentTimestamp()));
    assertThat(accessTokenSqlMapDao.loadForAdminUser(token.getId())).isEqualTo(token);
    assertThat(accessTokenSqlMapDao.loadNotDeletedTokenForUser(token.getId(), user)).isNull();
}
Also used : AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 18 with AccessToken

use of com.thoughtworks.go.domain.AccessToken in project gocd by gocd.

the class AccessTokenSqlMapDaoIntegrationTest method shouldUpdateLastUsedTimeForGivenTokens.

@Test
public void shouldUpdateLastUsedTimeForGivenTokens() {
    AccessToken token1 = randomAccessTokenForUser("bob");
    accessTokenSqlMapDao.saveOrUpdate(token1);
    AccessToken token2 = randomAccessTokenForUser("bob");
    accessTokenSqlMapDao.saveOrUpdate(token2);
    assertThat(token1.getLastUsed()).isNull();
    assertThat(token2.getLastUsed()).isNull();
    final DateTime now = DateTime.now();
    final Timestamp lastUsedTimeForToken1 = new Timestamp(now.getMillis());
    now.plusHours(2);
    final Timestamp lastUsedTimeForToken2 = new Timestamp(now.getMillis());
    final HashMap<Long, Timestamp> accessTokenIdToLastUsedTimestamp = new HashMap<>();
    accessTokenIdToLastUsedTimestamp.put(token1.getId(), lastUsedTimeForToken1);
    accessTokenIdToLastUsedTimestamp.put(token2.getId(), lastUsedTimeForToken2);
    accessTokenSqlMapDao.updateLastUsedTime(accessTokenIdToLastUsedTimestamp);
    final List<AccessToken> allTokensForUser = accessTokenSqlMapDao.findAllTokensForUser("bob", AccessTokenFilter.all);
    assertThat(allTokensForUser).hasSize(2);
    assertThat(allTokensForUser.get(0).getLastUsed()).isEqualTo(lastUsedTimeForToken1);
    assertThat(allTokensForUser.get(1).getLastUsed()).isEqualTo(lastUsedTimeForToken2);
}
Also used : HashMap(java.util.HashMap) AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Timestamp(java.sql.Timestamp) DateTime(org.joda.time.DateTime) Test(org.junit.jupiter.api.Test)

Example 19 with AccessToken

use of com.thoughtworks.go.domain.AccessToken in project gocd by gocd.

the class AccessTokenSqlMapDaoIntegrationTest method shouldListAllTokensThatAreActive.

@Test
public void shouldListAllTokensThatAreActive() {
    String user1 = "bob";
    String user2 = "will-be-revoked";
    AccessToken token1 = randomAccessTokenForUser(user1);
    AccessToken token2 = randomAccessTokenForUser(user2);
    accessTokenSqlMapDao.saveOrUpdate(token1);
    accessTokenSqlMapDao.saveOrUpdate(token2);
    accessTokenSqlMapDao.saveOrUpdate(token2.revoke("admin", "user is making too many requests", clock.currentTimestamp()));
    assertThat(accessTokenSqlMapDao.findAllTokens(AccessTokenFilter.active)).hasSize(1).containsExactlyInAnyOrder(accessTokenSqlMapDao.loadForAdminUser(token1.getId()));
}
Also used : AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 20 with AccessToken

use of com.thoughtworks.go.domain.AccessToken in project gocd by gocd.

the class AccessTokenSqlMapDaoIntegrationTest method shouldReturnNullWhenNoAccessTokenFoundForTheSpecifiedSaltId.

@Test
public void shouldReturnNullWhenNoAccessTokenFoundForTheSpecifiedSaltId() {
    String saltId = "access-token-for-apis";
    AccessToken savedAccessToken = accessTokenSqlMapDao.findAccessTokenBySaltId(saltId);
    assertThat(savedAccessToken).isNull();
}
Also used : AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Aggregations

AccessToken (com.thoughtworks.go.domain.AccessToken)25 Test (org.junit.jupiter.api.Test)18 AccessTokenMother.randomAccessToken (com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken)12 JsonReader (com.thoughtworks.go.api.representers.JsonReader)2 AccessTokenFilter (com.thoughtworks.go.server.service.AccessTokenFilter)2 Timestamp (java.sql.Timestamp)2 SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)1 ConflictException (com.thoughtworks.go.config.exceptions.ConflictException)1 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)1 InvalidAccessTokenException (com.thoughtworks.go.server.exceptions.InvalidAccessTokenException)1 RevokedAccessTokenException (com.thoughtworks.go.server.exceptions.RevokedAccessTokenException)1 AccessTokenCredential (com.thoughtworks.go.server.newsecurity.models.AccessTokenCredential)1 TransactionTemplate (com.thoughtworks.go.server.transaction.TransactionTemplate)1 Clock (com.thoughtworks.go.util.Clock)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1