Skip to content

Commit 7f6aaf9

Browse files
committed
Update mock dates in tests
1 parent 42def06 commit 7f6aaf9

3 files changed

Lines changed: 68 additions & 11 deletions

File tree

src/test/java/eu/webeid/security/validator/AuthTokenAlgorithmTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,36 @@
2222

2323
package eu.webeid.security.validator;
2424

25-
import org.junit.jupiter.api.Test;
2625
import eu.webeid.security.authtoken.WebEidAuthToken;
27-
import eu.webeid.security.exceptions.AuthTokenParseException;
2826
import eu.webeid.security.exceptions.AuthTokenException;
27+
import eu.webeid.security.exceptions.AuthTokenParseException;
2928
import eu.webeid.security.testutil.AbstractTestWithValidator;
29+
import eu.webeid.security.util.DateAndTime;
30+
import org.junit.jupiter.api.AfterEach;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
33+
import org.mockito.MockedStatic;
3034

35+
import static eu.webeid.security.testutil.DateMocker.mockDate;
3136
import static org.assertj.core.api.Assertions.assertThatThrownBy;
37+
import static org.mockito.Mockito.mockStatic;
3238

3339
class AuthTokenAlgorithmTest extends AbstractTestWithValidator {
40+
private MockedStatic<DateAndTime.DefaultClock> mockedClock;
41+
42+
@Override
43+
@BeforeEach
44+
protected void setup() {
45+
super.setup();
46+
mockedClock = mockStatic(DateAndTime.DefaultClock.class);
47+
// Ensure that the certificates do not expire.
48+
mockDate("2021-07-23", mockedClock);
49+
}
50+
51+
@AfterEach
52+
void tearDown() {
53+
mockedClock.close();
54+
}
3455

3556
@Test
3657
void whenAlgorithmNone_thenValidationFails() throws AuthTokenException {

src/test/java/eu/webeid/security/validator/AuthTokenSignatureTest.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
import eu.webeid.security.testutil.AbstractTestWithValidator;
2929
import eu.webeid.security.testutil.AuthTokenValidators;
3030
import eu.webeid.security.util.DateAndTime;
31+
import org.junit.jupiter.api.AfterEach;
32+
import org.junit.jupiter.api.BeforeEach;
3133
import org.junit.jupiter.api.Test;
34+
import org.mockito.MockedStatic;
3235

3336
import java.security.cert.X509Certificate;
3437

@@ -46,6 +49,22 @@ class AuthTokenSignatureTest extends AbstractTestWithValidator {
4649
"\"signature\":\"arx164xRiwhIQDINe0J+ZxJWZFOQTx0PBtOaWaxAe7gofEIHRIbV1w0sOCYBJnvmvMem9hU4nc2+iJx2x8poYck4Z6eI3GwtiksIec3XQ9ZIk1n/XchXnmPn3GYV+HzJ\"," +
4750
"\"format\":\"web-eid:1.0\"}";
4851

52+
private MockedStatic<DateAndTime.DefaultClock> mockedClock;
53+
54+
@Override
55+
@BeforeEach
56+
protected void setup() {
57+
super.setup();
58+
mockedClock = mockStatic(DateAndTime.DefaultClock.class);
59+
// Ensure that the certificates do not expire.
60+
mockDate("2021-07-23", mockedClock);
61+
}
62+
63+
@AfterEach
64+
void tearDown() {
65+
mockedClock.close();
66+
}
67+
4968
@Test
5069
void whenValidTokenAndNonce_thenValidationSucceeds() throws Exception {
5170
final X509Certificate result = validator.validate(validAuthToken, VALID_CHALLENGE_NONCE);
@@ -80,15 +99,11 @@ void whenValidTokenAndWrongOrigin_thenValidationFails() throws Exception {
8099

81100
@Test
82101
void whenTokenWithWrongCert_thenValidationFails() throws Exception {
83-
// Ensure that the certificate does not expire.
84-
try (final var mockedClock = mockStatic(DateAndTime.DefaultClock.class)) {
85-
mockDate("2024-08-01", mockedClock);
86-
final AuthTokenValidator authTokenValidator = AuthTokenValidators.getAuthTokenValidator();
87-
final WebEidAuthToken authTokenWithWrongCert = authTokenValidator.parse(AUTH_TOKEN_WRONG_CERT);
88-
assertThatThrownBy(() -> authTokenValidator
89-
.validate(authTokenWithWrongCert, VALID_CHALLENGE_NONCE))
90-
.isInstanceOf(AuthTokenSignatureValidationException.class);
91-
}
102+
final AuthTokenValidator authTokenValidator = AuthTokenValidators.getAuthTokenValidator();
103+
final WebEidAuthToken authTokenWithWrongCert = authTokenValidator.parse(AUTH_TOKEN_WRONG_CERT);
104+
assertThatThrownBy(() -> authTokenValidator
105+
.validate(authTokenWithWrongCert, VALID_CHALLENGE_NONCE))
106+
.isInstanceOf(AuthTokenSignatureValidationException.class);
92107
}
93108

94109
}

src/test/java/eu/webeid/security/validator/ocsp/OcspClientOverrideTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,43 @@
2525
import eu.webeid.security.exceptions.JceException;
2626
import eu.webeid.security.testutil.AbstractTestWithValidator;
2727
import eu.webeid.security.testutil.AuthTokenValidators;
28+
import eu.webeid.security.util.DateAndTime;
2829
import eu.webeid.security.validator.AuthTokenValidator;
2930
import org.bouncycastle.cert.ocsp.OCSPReq;
3031
import org.bouncycastle.cert.ocsp.OCSPResp;
32+
import org.junit.jupiter.api.AfterEach;
33+
import org.junit.jupiter.api.BeforeEach;
3134
import org.junit.jupiter.api.Disabled;
3235
import org.junit.jupiter.api.Test;
36+
import org.mockito.MockedStatic;
3337

3438
import java.io.IOException;
3539
import java.net.URI;
3640
import java.net.http.HttpClient;
3741
import java.security.cert.CertificateException;
3842
import java.time.Duration;
3943

44+
import static eu.webeid.security.testutil.DateMocker.mockDate;
4045
import static org.assertj.core.api.Assertions.assertThatCode;
4146
import static org.assertj.core.api.Assertions.assertThatThrownBy;
47+
import static org.mockito.Mockito.mockStatic;
4248

4349
class OcspClientOverrideTest extends AbstractTestWithValidator {
50+
private MockedStatic<DateAndTime.DefaultClock> mockedClock;
51+
52+
@Override
53+
@BeforeEach
54+
protected void setup() {
55+
super.setup();
56+
mockedClock = mockStatic(DateAndTime.DefaultClock.class);
57+
// Ensure that the certificates do not expire.
58+
mockDate("2021-07-23", mockedClock);
59+
}
60+
61+
@AfterEach
62+
void tearDown() {
63+
mockedClock.close();
64+
}
4465

4566
@Test
4667
void whenOcspClientIsOverridden_thenItIsUsed() throws JceException, CertificateException, IOException {

0 commit comments

Comments
 (0)