Search in sources :

Example 6 with AccessToken

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

the class AccessTokenSqlMapDaoIntegrationTest method shouldNotListDeletedTokens.

@Test
public void shouldNotListDeletedTokens() {
    String user1 = "Bob";
    String user2 = "John";
    AccessToken token1 = randomAccessTokenForUser(user1);
    AccessToken token2 = randomAccessTokenForUser(user1);
    AccessToken token3 = randomAccessTokenForUser(user2);
    accessTokenSqlMapDao.saveOrUpdate(token1);
    accessTokenSqlMapDao.saveOrUpdate(token2);
    accessTokenSqlMapDao.saveOrUpdate(token3);
    List<AccessToken> user1AccessTokens = accessTokenSqlMapDao.findAllTokensForUser(user1, AccessTokenFilter.all);
    List<AccessToken> user2AccessTokens = accessTokenSqlMapDao.findAllTokensForUser(user2, AccessTokenFilter.all);
    assertThat(user1AccessTokens).hasSize(2).containsExactlyInAnyOrder(token1, token2);
    assertThat(user2AccessTokens).hasSize(1).containsExactlyInAnyOrder(token3);
    accessTokenSqlMapDao.revokeTokensBecauseOfUserDelete(Arrays.asList("bob", "john"), "admin");
    assertThat(accessTokenSqlMapDao.findAllTokensForUser("bob", AccessTokenFilter.all)).isEmpty();
    assertThat(accessTokenSqlMapDao.findAllTokensForUser("john", AccessTokenFilter.all)).isEmpty();
}
Also used : AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 7 with AccessToken

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

the class AccessTokenSqlMapDaoIntegrationTest method shouldSaveUsersIntoDatabase.

@Test
public void shouldSaveUsersIntoDatabase() {
    AccessToken accessToken = randomAccessToken();
    accessTokenSqlMapDao.saveOrUpdate(accessToken);
    AccessToken savedAccessToken = accessTokenSqlMapDao.loadForAdminUser(accessToken.getId());
    assertThat(savedAccessToken).isEqualTo(accessToken);
}
Also used : AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 8 with AccessToken

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

the class AccessTokenSqlMapDaoIntegrationTest method shouldReturnAllTheAccessTokensBelongingToAUser.

@Test
public void shouldReturnAllTheAccessTokensBelongingToAUser() {
    String user1 = "Bob";
    String user2 = "John";
    AccessToken token1 = randomAccessTokenForUser(user1);
    AccessToken token2 = randomAccessTokenForUser(user1);
    AccessToken token3 = randomAccessTokenForUser(user2);
    accessTokenSqlMapDao.saveOrUpdate(token1);
    accessTokenSqlMapDao.saveOrUpdate(token2);
    accessTokenSqlMapDao.saveOrUpdate(token3);
    List<AccessToken> user1AccessTokens = accessTokenSqlMapDao.findAllTokensForUser(user1, AccessTokenFilter.all);
    List<AccessToken> user2AccessTokens = accessTokenSqlMapDao.findAllTokensForUser(user2, AccessTokenFilter.all);
    assertThat(user1AccessTokens).hasSize(2).containsExactlyInAnyOrder(token1, token2);
    assertThat(user2AccessTokens).hasSize(1).containsExactlyInAnyOrder(token3);
}
Also used : AccessTokenMother.randomAccessToken(com.thoughtworks.go.helper.AccessTokenMother.randomAccessToken) AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 9 with AccessToken

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

the class AccessTokenSqlMapDaoIntegrationTest method shouldListAllTokensThatAreRevoked.

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

Example 10 with AccessToken

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

the class AccessTokenService method findAccessTokenForGoCD.

// load access token within GoCD
private AccessToken findAccessTokenForGoCD(long id) {
    AccessToken token;
    token = accessTokenDao.loadForAdminUser(id);
    if (token == null) {
        throw new RecordNotFoundException(EntityType.AccessToken, id);
    }
    return token;
}
Also used : RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) AccessToken(com.thoughtworks.go.domain.AccessToken)

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