generated from digitalservicebund/java-application-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for cb verification filter
- Loading branch information
1 parent
f38a02d
commit 2d3859f
Showing
2 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/test/java/de/bund/digitalservice/a2j/security/CallbackVerificationFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package de.bund.digitalservice.a2j.security; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import dev.fitko.fitconnect.api.domain.validation.ValidationResult; | ||
import dev.fitko.fitconnect.client.SenderClient; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import java.io.IOException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
public class CallbackVerificationFilterTest { | ||
|
||
private CallbackVerificationFilter filter; | ||
private MockHttpServletRequest request; | ||
|
||
@MockBean private SenderClient client; | ||
@Mock private FilterChain filterChain; | ||
|
||
@BeforeEach | ||
void setup() { | ||
filter = new CallbackVerificationFilter(client, "s3cr3t"); | ||
request = new MockHttpServletRequest(); | ||
} | ||
|
||
@Test | ||
void testShouldNotFilterForNonCallbackPath() { | ||
request.setServletPath("/some/path"); | ||
|
||
assertTrue(filter.shouldNotFilter(request)); | ||
} | ||
|
||
@Test | ||
void testShouldNotFilterForCallbackPath() { | ||
request.setServletPath("/callbacks/fit-connect"); | ||
|
||
assertFalse(filter.shouldNotFilter(request)); | ||
} | ||
|
||
@Test | ||
void testDoFilterInternalWithInvalidCallback() throws ServletException, IOException { | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
|
||
request.addHeader("callback-authentication", "invalidHmac"); | ||
request.addHeader("callback-timestamp", "123456789"); | ||
request.setContent("body".getBytes()); | ||
|
||
when(client.validateCallback("invalidHmac", 123456789L, "body", "s3cr3t")) | ||
.thenReturn(ValidationResult.error("invalid")); | ||
|
||
filter.doFilterInternal(request, response, filterChain); | ||
verify(filterChain, never()).doFilter(any(), any()); | ||
} | ||
|
||
@Test | ||
void testDoFilterInternalWithValidCallback() throws ServletException, IOException { | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
|
||
request.addHeader("callback-authentication", "validHmac"); | ||
request.addHeader("callback-timestamp", "123456789"); | ||
request.setContent("body".getBytes()); | ||
|
||
when(client.validateCallback("validHmac", 123456789L, "body", "s3cr3t")) | ||
.thenReturn(ValidationResult.ok()); | ||
|
||
filter.doFilterInternal(request, response, filterChain); | ||
verify(filterChain).doFilter(any(), any()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters