Skip to content

Commit

Permalink
Perfecting WebAuthn RequestMatcher Related tests
Browse files Browse the repository at this point in the history
Signed-off-by: smallbun <[email protected]>
  • Loading branch information
leshalv committed Feb 23, 2025
1 parent 75d252e commit 0123bd6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.nio.charset.StandardCharsets;

import jakarta.servlet.FilterChain;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -30,10 +31,13 @@

import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextHolderStrategy;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.security.web.webauthn.api.PublicKeyCredentialCreationOptions;
import org.springframework.security.web.webauthn.api.PublicKeyCredentialRequestOptions;
import org.springframework.security.web.webauthn.api.TestPublicKeyCredentialRequestOptions;
Expand All @@ -48,6 +52,7 @@
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.verifyNoInteractions;
import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -75,20 +80,35 @@ class PublicKeyCredentialRequestOptionsFilterTests {

private PublicKeyCredentialRequestOptionsFilter filter;

private MockHttpServletRequest request;

private MockHttpServletResponse response;

private MockMvc mockMvc;

@BeforeEach
void setup() {
this.filter = new PublicKeyCredentialRequestOptionsFilter(this.relyingPartyOperations);
this.filter.setRequestOptionsRepository(this.requestOptionsRepository);
this.mockMvc = MockMvcBuilders.standaloneSetup().addFilter(this.filter).build();
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
}

@AfterEach
void cleanup() {
SecurityContextHolder.clearContext();
}

@Test
public void doFilterWhenCustomRequestMatcherThenUses() throws Exception {
RequestMatcher requestMatcher = mock(RequestMatcher.class);
this.filter.setRequestMatcher(requestMatcher);
FilterChain mock = mock(FilterChain.class);
this.filter.doFilter(request, response, mock);
verify(requestMatcher).matches(any());
}

@Test
void constructorWhenNull() {
assertThatExceptionOfType(IllegalArgumentException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.security.web.webauthn.api.AuthenticatorTransport;
import org.springframework.security.web.webauthn.api.Bytes;
import org.springframework.security.web.webauthn.api.PublicKeyCredentialCreationOptions;
Expand Down Expand Up @@ -78,14 +78,11 @@ class PublicKeyCredentialCreationOptionsFilterTests {

private MockHttpServletResponse response;

private FilterChain chain;

@BeforeEach
public void setup() {
this.filter = new PublicKeyCredentialCreationOptionsFilter(this.rpOperations);
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
this.chain = mock(FilterChain.class);
}

@AfterEach
Expand All @@ -95,10 +92,11 @@ void clear() {

@Test
public void doFilterWhenCustomRequestMatcherThenUses() throws Exception {
this.request.setPathInfo("/path");
this.filter.setRequestMatcher(new AntPathRequestMatcher("/path"));
this.filter.doFilter(this.request, this.response, this.chain);
verifyNoInteractions(this.chain);
RequestMatcher requestMatcher = mock(RequestMatcher.class);
this.filter.setRequestMatcher(requestMatcher);
FilterChain mock = mock(FilterChain.class);
this.filter.doFilter(request, response, mock);
verify(requestMatcher).matches(any());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.security.web.webauthn.api.ImmutableCredentialRecord;
import org.springframework.security.web.webauthn.api.PublicKeyCredentialCreationOptions;
import org.springframework.security.web.webauthn.api.TestCredentialRecord;
Expand Down Expand Up @@ -113,18 +113,20 @@ void setup() {

@Test
public void doFilterWhenCustomRequestRegisterCredentialMatcherThenUses() throws Exception {
this.request.setPathInfo("/register/path");
this.filter.setRegisterCredentialMatcher(new AntPathRequestMatcher("/register/path"));
this.filter.doFilter(this.request, this.response, this.chain);
verifyNoInteractions(this.chain);
RequestMatcher requestMatcher = mock(RequestMatcher.class);
this.filter.setRegisterCredentialMatcher(requestMatcher);
FilterChain mock = mock(FilterChain.class);
this.filter.doFilter(request, response, mock);
verify(requestMatcher).matches(any());
}

@Test
public void doFilterWhenCustomRequestRemoveCredentialMatcherThenUses() throws Exception {
this.request.setPathInfo("/remove/path");
this.filter.setRemoveCredentialMatcher(new AntPathRequestMatcher("/remove/path"));
this.filter.doFilter(this.request, this.response, this.chain);
verifyNoInteractions(this.chain);
RequestMatcher requestMatcher = mock(RequestMatcher.class);
this.filter.setRemoveCredentialMatcher(requestMatcher);
FilterChain mock = mock(FilterChain.class);
this.filter.doFilter(request, response, mock);
verify(requestMatcher).matches(any());
}

@Test
Expand Down

0 comments on commit 0123bd6

Please sign in to comment.