|
1 | 1 | package com.frogdevelopment.jwt; |
2 | 2 |
|
3 | 3 | 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.*; |
8 | 5 | import static org.mockito.ArgumentMatchers.anyString; |
9 | 6 | import static org.mockito.BDDMockito.given; |
| 7 | +import static org.mockito.BDDMockito.then; |
10 | 8 |
|
11 | 9 | import io.jsonwebtoken.Jwts; |
12 | 10 | import io.jsonwebtoken.SignatureAlgorithm; |
| 11 | +import io.jsonwebtoken.impl.DefaultClaims; |
13 | 12 | import java.util.List; |
14 | 13 | import java.util.Map; |
15 | | -import java.util.stream.Collectors; |
| 14 | +import org.assertj.core.api.Assertions; |
16 | 15 | import org.junit.jupiter.api.Tag; |
17 | 16 | import org.junit.jupiter.api.Test; |
18 | 17 | import org.junit.jupiter.api.extension.ExtendWith; |
19 | 18 | import org.mockito.InjectMocks; |
20 | 19 | import org.mockito.Mock; |
21 | 20 | import org.mockito.junit.jupiter.MockitoExtension; |
22 | 21 | import org.springframework.mock.web.MockHttpServletRequest; |
23 | | -import org.springframework.security.core.authority.SimpleGrantedAuthority; |
24 | 22 |
|
25 | 23 | @Tag("unitTest") |
26 | 24 | @ExtendWith(MockitoExtension.class) |
27 | 25 | class ResolveTokenToAuthenticationTest { |
28 | 26 |
|
29 | 27 | private static final String USERNAME = "USERNAME"; |
30 | 28 | private static final byte[] SIGNING_KEY = "my-signing-key".getBytes(); |
| 29 | + private static final List<String> ROLES = List.of("ADMIN", "USER"); |
31 | 30 |
|
32 | 31 | @InjectMocks |
33 | 32 | private ResolveTokenToAuthentication resolveTokenToAuthentication; |
34 | 33 | @Mock |
35 | | - private ResolveClaimsFromToken resolveClaimsFromToken; |
36 | | - @Mock |
37 | 34 | private RetrieveTokenFromRequest retrieveTokenFromRequest; |
38 | | - private static final List<String> ROLES = List.of("ADMIN", "USER"); |
| 35 | + @Mock |
| 36 | + private TokenToAuthentication tokenToAuthentication; |
39 | 37 |
|
40 | 38 | @Test |
41 | | - void createAuthentication_should_return_authentication() { |
| 39 | + void should_return_null_when_no_token() { |
42 | 40 | // given |
43 | | - MockHttpServletRequest request = givenRequest(); |
44 | | - givenClaims(); |
| 41 | + var request = new MockHttpServletRequest(); |
| 42 | + givenNotToken(request); |
45 | 43 |
|
46 | 44 | // when |
47 | 45 | var authentication = resolveTokenToAuthentication.call(request); |
48 | 46 |
|
49 | 47 | // 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(); |
58 | 50 | } |
59 | 51 |
|
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(); |
65 | 58 |
|
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(); |
71 | 64 | } |
72 | 65 |
|
73 | | - private String givenToken() { |
74 | | - return Jwts.builder() |
| 66 | + private void givenToken(MockHttpServletRequest request) { |
| 67 | + given(retrieveTokenFromRequest.call(request)).willReturn(Jwts.builder() |
75 | 68 | .setSubject(USERNAME) |
76 | 69 | .addClaims(Map.of(AUTHORITIES_KEY, ROLES)) |
77 | 70 | .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")); |
79 | 81 | } |
80 | 82 |
|
81 | 83 | } |
0 commit comments