Files
SCM-Manager/scm-webapp/src/test/java/sonia/scm/security/JwtAccessTokenRefresherTest.java

153 lines
5.1 KiB
Java
Raw Normal View History

2018-11-29 08:01:25 +01:00
package sonia.scm.security;
import com.github.sdorra.shiro.ShiroRule;
import com.github.sdorra.shiro.SubjectAware;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.sql.Date;
2018-11-29 17:04:38 +01:00
import java.time.Clock;
import java.time.Instant;
2018-11-29 08:01:25 +01:00
import java.util.Collections;
import java.util.Optional;
2018-11-29 17:04:38 +01:00
import static java.time.Duration.ofMinutes;
import static java.time.temporal.ChronoUnit.SECONDS;
2018-11-29 17:04:38 +01:00
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.assertj.core.api.Assertions.assertThat;
2018-11-29 08:01:25 +01:00
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
2018-11-29 08:01:25 +01:00
import static org.mockito.Mockito.when;
import static sonia.scm.security.SecureKeyTestUtil.createSecureKey;
2018-11-29 08:01:25 +01:00
@SubjectAware(
username = "user",
password = "secret",
configuration = "classpath:sonia/scm/repository/shiro.ini"
)
@RunWith(MockitoJUnitRunner.class)
public class JwtAccessTokenRefresherTest {
private static final Instant NOW = Instant.now().truncatedTo(SECONDS);
2018-11-30 09:43:13 +01:00
private static final Instant TOKEN_CREATION = NOW.minus(ofMinutes(1));
2018-11-29 08:01:25 +01:00
@Rule
public ShiroRule shiro = new ShiroRule();
@Mock
private SecureKeyResolver keyResolver;
@Mock
private JwtAccessTokenRefreshStrategy refreshStrategy;
2018-11-29 17:04:38 +01:00
@Mock
2018-11-30 09:43:13 +01:00
private Clock refreshClock;
2018-11-29 17:04:38 +01:00
2018-11-30 09:22:02 +01:00
private KeyGenerator keyGenerator = () -> "key";
2018-11-29 08:01:25 +01:00
private JwtAccessTokenRefresher refresher;
private JwtAccessTokenBuilder tokenBuilder;
@Before
public void initKeyResolver() {
when(keyResolver.getSecureKey(any())).thenReturn(createSecureKey());
2018-11-29 08:01:25 +01:00
Clock creationClock = mock(Clock.class);
2018-11-30 09:43:13 +01:00
when(creationClock.instant()).thenReturn(TOKEN_CREATION);
tokenBuilder = new JwtAccessTokenBuilderFactory(keyGenerator, keyResolver, Collections.emptySet(), creationClock).create();
JwtAccessTokenBuilderFactory refreshBuilderFactory = new JwtAccessTokenBuilderFactory(keyGenerator, keyResolver, Collections.emptySet(), refreshClock);
refresher = new JwtAccessTokenRefresher(refreshBuilderFactory, refreshStrategy, refreshClock);
2018-11-30 09:43:13 +01:00
when(refreshClock.instant()).thenReturn(NOW);
2018-11-29 17:04:38 +01:00
when(refreshStrategy.shouldBeRefreshed(any())).thenReturn(true);
2018-11-30 09:22:02 +01:00
// set default expiration values
tokenBuilder
.expiresIn(5, MINUTES)
.refreshableFor(10, MINUTES);
2018-11-29 08:01:25 +01:00
}
@Test
public void shouldNotRefreshTokenWithDisabledRefresh() {
JwtAccessToken oldToken = tokenBuilder
2018-11-29 17:04:38 +01:00
.refreshableFor(0, MINUTES)
.build();
Optional<JwtAccessToken> refreshedToken = refresher.refresh(oldToken);
assertThat(refreshedToken).isEmpty();
}
@Test
public void shouldNotRefreshTokenWhenTokenExpired() {
2018-11-30 09:43:13 +01:00
Instant afterNormalExpiration = NOW.plus(ofMinutes(6));
when(refreshClock.instant()).thenReturn(afterNormalExpiration);
2018-11-30 09:22:02 +01:00
JwtAccessToken oldToken = tokenBuilder.build();
2018-11-29 17:04:38 +01:00
Optional<JwtAccessToken> refreshedToken = refresher.refresh(oldToken);
assertThat(refreshedToken).isEmpty();
}
@Test
public void shouldNotRefreshTokenWhenRefreshExpired() {
2018-11-30 09:22:02 +01:00
Instant afterRefreshExpiration = Instant.now().plus(ofMinutes(2));
2018-11-30 09:43:13 +01:00
when(refreshClock.instant()).thenReturn(afterRefreshExpiration);
2018-11-29 17:04:38 +01:00
JwtAccessToken oldToken = tokenBuilder
.refreshableFor(1, MINUTES)
2018-11-29 08:01:25 +01:00
.build();
Optional<JwtAccessToken> refreshedToken = refresher.refresh(oldToken);
2018-11-29 17:04:38 +01:00
assertThat(refreshedToken).isEmpty();
2018-11-29 08:01:25 +01:00
}
@Test
public void shouldNotRefreshTokenWhenStrategyDoesNotSaySo() {
2018-11-30 09:22:02 +01:00
JwtAccessToken oldToken = tokenBuilder.build();
2018-11-29 08:01:25 +01:00
when(refreshStrategy.shouldBeRefreshed(oldToken)).thenReturn(false);
Optional<JwtAccessToken> refreshedToken = refresher.refresh(oldToken);
2018-11-29 17:04:38 +01:00
assertThat(refreshedToken).isEmpty();
2018-11-29 08:01:25 +01:00
}
@Test
public void shouldRefreshTokenWithParentId() {
2018-11-30 09:22:02 +01:00
JwtAccessToken oldToken = tokenBuilder.build();
2018-11-29 08:01:25 +01:00
when(refreshStrategy.shouldBeRefreshed(oldToken)).thenReturn(true);
2018-11-30 09:43:13 +01:00
Optional<JwtAccessToken> refreshedTokenResult = refresher.refresh(oldToken);
2018-11-29 08:01:25 +01:00
2018-11-30 09:43:13 +01:00
assertThat(refreshedTokenResult).isNotEmpty();
JwtAccessToken refreshedToken = refreshedTokenResult.get();
assertThat(refreshedToken.getParentKey()).get().isEqualTo("key");
2018-11-29 08:01:25 +01:00
}
@Test
public void shouldRefreshTokenWithSameExpiration() {
JwtAccessToken oldToken = tokenBuilder.build();
when(refreshStrategy.shouldBeRefreshed(oldToken)).thenReturn(true);
Optional<JwtAccessToken> refreshedTokenResult = refresher.refresh(oldToken);
assertThat(refreshedTokenResult).isNotEmpty();
JwtAccessToken refreshedToken = refreshedTokenResult.get();
assertThat(refreshedToken.getExpiration()).isEqualTo(Date.from(NOW.plus(ofMinutes(5))));
}
2018-11-30 10:15:12 +01:00
@Test
public void shouldRefreshTokenWithSameRefreshExpiration() {
JwtAccessToken oldToken = tokenBuilder.build();
when(refreshStrategy.shouldBeRefreshed(oldToken)).thenReturn(true);
Optional<JwtAccessToken> refreshedTokenResult = refresher.refresh(oldToken);
assertThat(refreshedTokenResult).isNotEmpty();
JwtAccessToken refreshedToken = refreshedTokenResult.get();
assertThat(refreshedToken.getRefreshExpiration()).get().isEqualTo(Date.from(TOKEN_CREATION.plus(ofMinutes(10))));
}
2018-11-29 08:01:25 +01:00
}