Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit 4fe2967

Browse files
fix: failing tests
1 parent 9597d67 commit 4fe2967

File tree

4 files changed

+108
-36
lines changed

4 files changed

+108
-36
lines changed

src/test/java/com/frogdevelopment/jwt/JwtProcessTokenFilterTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void shouldClearSecurityContextWhenAuthenticationIsNull() throws IOException, Se
6666
@Test
6767
void shouldClearSecurityContextWhenJwtExceptionIsRaised() throws ServletException, IOException {
6868
// given
69+
given(request.getRequestURL()).willReturn(new StringBuffer("http://local_test/my_uri"));
6970
givenAnExceptionWhenResolvingToken();
7071

7172
// when
Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,83 @@
11
package com.frogdevelopment.jwt;
22

33
import static com.frogdevelopment.jwt.JwtAuthenticationToken.AUTHORITIES_KEY;
4-
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertNotNull;
6-
import static org.junit.jupiter.api.Assertions.assertNull;
7-
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
import static org.assertj.core.api.Assertions.*;
85
import static org.mockito.ArgumentMatchers.anyString;
96
import static org.mockito.BDDMockito.given;
7+
import static org.mockito.BDDMockito.then;
108

119
import io.jsonwebtoken.Jwts;
1210
import io.jsonwebtoken.SignatureAlgorithm;
11+
import io.jsonwebtoken.impl.DefaultClaims;
1312
import java.util.List;
1413
import java.util.Map;
15-
import java.util.stream.Collectors;
14+
import org.assertj.core.api.Assertions;
1615
import org.junit.jupiter.api.Tag;
1716
import org.junit.jupiter.api.Test;
1817
import org.junit.jupiter.api.extension.ExtendWith;
1918
import org.mockito.InjectMocks;
2019
import org.mockito.Mock;
2120
import org.mockito.junit.jupiter.MockitoExtension;
2221
import org.springframework.mock.web.MockHttpServletRequest;
23-
import org.springframework.security.core.authority.SimpleGrantedAuthority;
2422

2523
@Tag("unitTest")
2624
@ExtendWith(MockitoExtension.class)
2725
class ResolveTokenToAuthenticationTest {
2826

2927
private static final String USERNAME = "USERNAME";
3028
private static final byte[] SIGNING_KEY = "my-signing-key".getBytes();
29+
private static final List<String> ROLES = List.of("ADMIN", "USER");
3130

3231
@InjectMocks
3332
private ResolveTokenToAuthentication resolveTokenToAuthentication;
3433
@Mock
35-
private ResolveClaimsFromToken resolveClaimsFromToken;
36-
@Mock
3734
private RetrieveTokenFromRequest retrieveTokenFromRequest;
38-
private static final List<String> ROLES = List.of("ADMIN", "USER");
35+
@Mock
36+
private TokenToAuthentication tokenToAuthentication;
3937

4038
@Test
41-
void createAuthentication_should_return_authentication() {
39+
void should_return_null_when_no_token() {
4240
// given
43-
MockHttpServletRequest request = givenRequest();
44-
givenClaims();
41+
var request = new MockHttpServletRequest();
42+
givenNotToken(request);
4543

4644
// when
4745
var authentication = resolveTokenToAuthentication.call(request);
4846

4947
// then
50-
assertNotNull(authentication);
51-
assertEquals(USERNAME, authentication.getPrincipal());
52-
assertEquals(USERNAME, authentication.getName());
53-
assertNotNull(authentication.getDetails());
54-
assertTrue(authentication.isAuthenticated());
55-
assertNull(authentication.getCredentials());
56-
var authorities = ROLES.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
57-
assertEquals(authentication.getAuthorities(), authorities);
48+
assertThat(authentication).isNull();
49+
then(tokenToAuthentication).shouldHaveNoInteractions();
5850
}
5951

60-
private MockHttpServletRequest givenRequest() {
61-
MockHttpServletRequest request = new MockHttpServletRequest();
62-
given(retrieveTokenFromRequest.call(request)).willReturn(givenToken());
63-
return request;
64-
}
52+
@Test
53+
void should_return_authentication_from_token() {
54+
// given
55+
var request = new MockHttpServletRequest();
56+
givenToken(request);
57+
givenJwtAuthenticationToken();
6558

66-
private void givenClaims() {
67-
given(resolveClaimsFromToken.call(anyString())).willAnswer(i -> Jwts.parser()
68-
.setSigningKey(SIGNING_KEY)
69-
.parseClaimsJws(i.getArgument(0))
70-
.getBody());
59+
// when
60+
var authentication = resolveTokenToAuthentication.call(request);
61+
62+
// then
63+
assertThat(authentication).isNotNull();
7164
}
7265

73-
private String givenToken() {
74-
return Jwts.builder()
66+
private void givenToken(MockHttpServletRequest request) {
67+
given(retrieveTokenFromRequest.call(request)).willReturn(Jwts.builder()
7568
.setSubject(USERNAME)
7669
.addClaims(Map.of(AUTHORITIES_KEY, ROLES))
7770
.signWith(SignatureAlgorithm.HS512, SIGNING_KEY)
78-
.compact();
71+
.compact());
72+
}
73+
74+
private void givenNotToken(MockHttpServletRequest request) {
75+
given(retrieveTokenFromRequest.call(request)).willReturn(null);
76+
}
77+
78+
private void givenJwtAuthenticationToken() {
79+
given(tokenToAuthentication.call(anyString()))
80+
.willAnswer(i -> new JwtAuthenticationToken(new DefaultClaims(), "token"));
7981
}
8082

8183
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.frogdevelopment.jwt;
2+
3+
import static com.frogdevelopment.jwt.JwtAuthenticationToken.AUTHORITIES_KEY;
4+
import static io.jsonwebtoken.SignatureAlgorithm.HS512;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertNull;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
import static org.mockito.ArgumentMatchers.anyString;
10+
import static org.mockito.BDDMockito.given;
11+
12+
import io.jsonwebtoken.Jwts;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.stream.Collectors;
16+
import org.junit.jupiter.api.Tag;
17+
import org.junit.jupiter.api.Test;
18+
import org.junit.jupiter.api.extension.ExtendWith;
19+
import org.mockito.InjectMocks;
20+
import org.mockito.Mock;
21+
import org.mockito.junit.jupiter.MockitoExtension;
22+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
23+
24+
@Tag("unitTest")
25+
@ExtendWith(MockitoExtension.class)
26+
class TokenToAuthenticationTest {
27+
28+
private static final String USERNAME = "USERNAME";
29+
private static final byte[] SIGNING_KEY = "my-signing-key".getBytes();
30+
private static final List<String> ROLES = List.of("ADMIN", "USER");
31+
32+
@InjectMocks
33+
private TokenToAuthentication tokenToAuthentication;
34+
@Mock
35+
private ResolveClaimsFromToken resolveClaimsFromToken;
36+
37+
@Test
38+
void createAuthentication_should_return_authentication() {
39+
// given
40+
var token = givenToken();
41+
givenClaims();
42+
43+
// when
44+
var authentication = tokenToAuthentication.call(token);
45+
46+
// then
47+
assertNotNull(authentication);
48+
assertEquals(USERNAME, authentication.getPrincipal());
49+
assertEquals(USERNAME, authentication.getName());
50+
assertNotNull(authentication.getDetails());
51+
assertTrue(authentication.isAuthenticated());
52+
assertNull(authentication.getCredentials());
53+
var authorities = ROLES.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
54+
assertEquals(authentication.getAuthorities(), authorities);
55+
}
56+
57+
private String givenToken() {
58+
return Jwts.builder()
59+
.setSubject(USERNAME)
60+
.addClaims(Map.of(AUTHORITIES_KEY, ROLES))
61+
.signWith(HS512, SIGNING_KEY)
62+
.compact();
63+
}
64+
65+
private void givenClaims() {
66+
given(resolveClaimsFromToken.call(anyString())).willAnswer(i -> Jwts.parser()
67+
.setSigningKey(SIGNING_KEY)
68+
.parseClaimsJws(i.getArgument(0))
69+
.getBody());
70+
}
71+
72+
}

src/test/java/com/frogdevelopment/jwt/app/conf/AuthenticationConfiguration.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,4 @@
88
@EnableWebSecurity
99
public class AuthenticationConfiguration extends JwtAuthorizationConfigurerAdapter {
1010

11-
public AuthenticationConfiguration() {
12-
super(jwtProcessTokenFilter);
13-
}
1411
}

0 commit comments

Comments
 (0)