Search in sources :

Example 21 with AccessToken

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

the class AccessTokenSqlMapDaoIntegrationTest method adminsShouldBeAbleToLoadTokensRevokedByAnyone.

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

Example 22 with AccessToken

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

the class AccessTokenServiceIntegrationTest method shouldCreateAnAccessToken.

@Test
public void shouldCreateAnAccessToken() {
    String tokenDescription = "This is my first token";
    AccessToken.AccessTokenWithDisplayValue createdToken = accessTokenService.create(tokenDescription, "bob", authConfigId);
    AccessToken fetchedToken = accessTokenService.find(createdToken.getId(), "bob");
    assertThat(createdToken.getDescription()).isEqualTo(tokenDescription);
    assertThat(createdToken.getValue()).isNotNull();
    assertThat(createdToken.getDisplayValue()).isNotNull();
    assertThat(createdToken.getCreatedAt()).isNotNull();
    assertThat(createdToken.getLastUsed()).isNull();
    assertThat(createdToken.isRevoked()).isFalse();
    assertThat(fetchedToken.getValue()).isEqualTo(createdToken.getValue());
    assertThat(fetchedToken.getDescription()).isEqualTo(createdToken.getDescription());
    assertThat(fetchedToken.getCreatedAt()).isEqualTo(createdToken.getCreatedAt());
    assertThat(fetchedToken.getLastUsed()).isNull();
    assertThat(fetchedToken.isRevoked()).isEqualTo(createdToken.isRevoked());
}
Also used : AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 23 with AccessToken

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

the class AccessTokenServiceIntegrationTest method shouldRevokeAccessTokenWithinGoCD.

@Test
public void shouldRevokeAccessTokenWithinGoCD() {
    String tokenDescription = "This is my first Token";
    AccessToken createdToken = accessTokenService.create(tokenDescription, "BOB", authConfigId);
    assertThat(createdToken.isRevoked()).isFalse();
    assertThat(createdToken.getRevokeCause()).isBlank();
    accessTokenService.revokeAccessTokenByGoCD(createdToken.getId(), "from GoCD");
    AccessToken tokenAfterRevoking = accessTokenService.find(createdToken.getId(), "bOb");
    assertThat(tokenAfterRevoking.isRevoked()).isTrue();
    assertThat(tokenAfterRevoking.getRevokedBy()).isEqualTo("GoCD");
    assertThat(tokenAfterRevoking.getRevokeCause()).isEqualTo("from GoCD");
}
Also used : AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 24 with AccessToken

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

the class AccessTokenServiceIntegrationTest method shouldRevokeAnAccessToken.

@Test
public void shouldRevokeAnAccessToken() {
    String tokenDescription = "This is my first Token";
    AccessToken createdToken = accessTokenService.create(tokenDescription, "BOB", authConfigId);
    assertThat(createdToken.isRevoked()).isFalse();
    assertThat(createdToken.getRevokeCause()).isBlank();
    accessTokenService.revokeAccessToken(createdToken.getId(), "bob", "blah");
    AccessToken tokenAfterRevoking = accessTokenService.find(createdToken.getId(), "bOb");
    assertThat(tokenAfterRevoking.isRevoked()).isTrue();
    assertThat(tokenAfterRevoking.getRevokeCause()).isEqualTo("blah");
}
Also used : AccessToken(com.thoughtworks.go.domain.AccessToken) Test(org.junit.jupiter.api.Test)

Example 25 with AccessToken

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

the class AccessTokenServiceIntegrationTest method shouldFailToRevokeAnAlreadyRevokedAccessToken.

@Test
public void shouldFailToRevokeAnAlreadyRevokedAccessToken() {
    String tokenDescription = "This is my first Token";
    AccessToken createdToken = accessTokenService.create(tokenDescription, "BOB", authConfigId);
    assertThat(createdToken.isRevoked()).isFalse();
    accessTokenService.revokeAccessToken(createdToken.getId(), "bOb", null);
    AccessToken tokenAfterRevoking = accessTokenService.find(createdToken.getId(), "bOb");
    assertThat(tokenAfterRevoking.isRevoked()).isTrue();
    assertThatCode(() -> accessTokenService.revokeAccessToken(createdToken.getId(), "bOb", null)).isInstanceOf(ConflictException.class).hasMessage("Access token has already been revoked!");
}
Also used : ConflictException(com.thoughtworks.go.config.exceptions.ConflictException) 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