Skip to content

Commit 6d081e1

Browse files
committed
JUnit - testing JWT (integration test as we are fetching secret key)
* Test if token has expired * Test if token has not expired
1 parent 1e55570 commit 6d081e1

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed
Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.appsdeveloperblog.app.ws.shared;
22

3-
import java.security.SecureRandom;
4-
import java.util.Date;
5-
import java.util.Random;
6-
import org.springframework.stereotype.Service;
7-
83
import com.appsdeveloperblog.app.ws.security.SecurityConstants;
94
import com.appsdeveloperblog.app.ws.security.TokenUtil;
10-
115
import io.jsonwebtoken.Claims;
6+
import io.jsonwebtoken.JwtException;
127
import io.jsonwebtoken.Jwts;
8+
import java.security.SecureRandom;
9+
import java.util.Date;
10+
import java.util.Random;
11+
import org.springframework.stereotype.Service;
1312

1413
@Service
1514
public class Utils {
@@ -35,40 +34,49 @@ private String generateRandomString(int length) {
3534
}
3635

3736
public static boolean hasTokenExpired(String token) {
38-
39-
Claims claims = Jwts
40-
.parser()
41-
.setSigningKey(TokenUtil.getSecretKey())
42-
.parseClaimsJws(token)
43-
.getBody();
37+
boolean returnValue = false;
38+
39+
try {
40+
Claims claims = Jwts
41+
.parser()
42+
.setSigningKey(TokenUtil.getSecretKey())
43+
.parseClaimsJws(token)
44+
.getBody();
4445

45-
Date tokenExpirationDate = claims.getExpiration();
46-
Date todayDate = new Date();
47-
48-
return tokenExpirationDate.before(todayDate);
46+
Date tokenExpirationDate = claims.getExpiration();
47+
Date todayDate = new Date();
48+
49+
return tokenExpirationDate.before(todayDate);
50+
} catch (JwtException ex) {
51+
returnValue = true;
52+
}
53+
54+
return returnValue;
4955
}
5056

5157
public String generateEmailVerificationToken(String userId) {
52-
53-
String token = Jwts
58+
String token = Jwts
5459
.builder()
5560
.setId(userId)
56-
.setExpiration(new Date(System.currentTimeMillis() + SecurityConstants.EXPIRATION_TIME))
61+
.setExpiration(
62+
new Date(System.currentTimeMillis() + SecurityConstants.EXPIRATION_TIME)
63+
)
5764
.signWith(TokenUtil.getSecretKey())
5865
.compact();
5966

60-
return token;
67+
return token;
6168
}
6269

6370
public String generatePasswordResetToken(String userId) {
64-
6571
String token = Jwts
66-
.builder()
67-
.setId(userId)
68-
.setExpiration(new Date(System.currentTimeMillis() + SecurityConstants.EXPIRATION_TIME))
69-
.signWith(TokenUtil.getSecretKey())
70-
.compact();
71-
72+
.builder()
73+
.setId(userId)
74+
.setExpiration(
75+
new Date(System.currentTimeMillis() + SecurityConstants.EXPIRATION_TIME)
76+
)
77+
.signWith(TokenUtil.getSecretKey())
78+
.compact();
79+
7280
return token;
7381
}
7482
}

src/test/java/shared/UtilsTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package shared;
22

3+
import static org.junit.jupiter.api.Assertions.assertFalse;
34
import static org.junit.jupiter.api.Assertions.assertNotNull;
4-
import static org.junit.jupiter.api.Assertions.fail;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
import org.junit.jupiter.api.BeforeEach;
7-
import org.junit.jupiter.api.Disabled;
88
import org.junit.jupiter.api.Test;
99
import org.junit.jupiter.api.extension.ExtendWith;
1010
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,9 +35,19 @@ void testGenerateUserId() {
3535
}
3636

3737
@Test
38-
@Disabled
38+
void testHasTokenNotExpired() {
39+
String token = utils.generateEmailVerificationToken("asdfghjkl");
40+
assertNotNull(token);
41+
boolean hasTokenExpired = utils.hasTokenExpired(token);
42+
assertFalse(hasTokenExpired);
43+
}
44+
45+
@Test
3946
void testHasTokenExpired() {
40-
fail("Not yet implemented");
47+
String expiredToken = "qwerasdfzxcv1234567890";
48+
assertNotNull(expiredToken);
49+
boolean hasTokenExpired = utils.hasTokenExpired(expiredToken);
50+
assertTrue(hasTokenExpired);
4151
}
4252

4353
}

0 commit comments

Comments
 (0)