Skip to content

Commit e741225

Browse files
committed
Perfecting WebAuthn RequestMatcher Related tests
Signed-off-by: smallbun <[email protected]>
1 parent 6ef3ce6 commit e741225

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

web/src/test/java/org/springframework/security/web/webauthn/registration/PublicKeyCredentialCreationOptionsFilterTests.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import java.util.Arrays;
2020

21+
import jakarta.servlet.FilterChain;
2122
import org.junit.jupiter.api.AfterEach;
23+
import org.junit.jupiter.api.BeforeEach;
2224
import org.junit.jupiter.api.Test;
2325
import org.junit.jupiter.api.extension.ExtendWith;
2426
import org.mockito.Mock;
@@ -27,12 +29,15 @@
2729
import org.springframework.http.HttpHeaders;
2830
import org.springframework.http.HttpStatus;
2931
import org.springframework.http.MediaType;
32+
import org.springframework.mock.web.MockFilterChain;
33+
import org.springframework.mock.web.MockHttpServletRequest;
3034
import org.springframework.mock.web.MockHttpServletResponse;
3135
import org.springframework.security.authentication.AnonymousAuthenticationToken;
3236
import org.springframework.security.authentication.TestingAuthenticationToken;
3337
import org.springframework.security.core.authority.AuthorityUtils;
3438
import org.springframework.security.core.context.SecurityContextHolder;
3539
import org.springframework.security.core.context.SecurityContextImpl;
40+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
3641
import org.springframework.security.web.webauthn.api.AuthenticatorTransport;
3742
import org.springframework.security.web.webauthn.api.Bytes;
3843
import org.springframework.security.web.webauthn.api.PublicKeyCredentialCreationOptions;
@@ -47,7 +52,7 @@
4752
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
4853
import static org.mockito.ArgumentMatchers.any;
4954
import static org.mockito.BDDMockito.given;
50-
import static org.mockito.Mockito.verifyNoInteractions;
55+
import static org.mockito.Mockito.*;
5156
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5257
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
5358
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
@@ -68,11 +73,40 @@ class PublicKeyCredentialCreationOptionsFilterTests {
6873
@Mock
6974
private WebAuthnRelyingPartyOperations rpOperations;
7075

76+
private PublicKeyCredentialCreationOptionsFilter filter;
77+
78+
private MockHttpServletRequest request;
79+
80+
private MockHttpServletResponse response;
81+
82+
private FilterChain chain;
83+
84+
@BeforeEach
85+
public void setup() {
86+
this.filter = new PublicKeyCredentialCreationOptionsFilter(this.rpOperations);
87+
this.request = new MockHttpServletRequest();
88+
this.response = new MockHttpServletResponse();
89+
this.chain = mock(FilterChain.class);
90+
}
91+
7192
@AfterEach
7293
void clear() {
7394
SecurityContextHolder.clearContext();
7495
}
7596

97+
@Test
98+
public void doFilterWhenCustomRequestMatcherThenUses() throws Exception {
99+
this.request.setPathInfo("/path");
100+
this.filter.setRequestMatcher(new AntPathRequestMatcher("/path"));
101+
this.filter.doFilter(this.request, this.response, this.chain);
102+
verifyNoInteractions(this.chain);
103+
}
104+
105+
@Test
106+
public void setRequestMatcherWhenNullThenIllegalArgument() {
107+
assertThatIllegalArgumentException().isThrownBy(() -> this.filter.setRequestMatcher(null));
108+
}
109+
76110
@Test
77111
void constructorWhenRpOperationsIsNullThenIllegalArgumentException() {
78112
assertThatIllegalArgumentException().isThrownBy(() -> new PublicKeyCredentialCreationOptionsFilter(null))

web/src/test/java/org/springframework/security/web/webauthn/registration/WebAuthnRegistrationFilterTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.mock.web.MockHttpServletRequest;
3131
import org.springframework.mock.web.MockHttpServletResponse;
3232
import org.springframework.mock.web.MockServletContext;
33+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
3334
import org.springframework.security.web.webauthn.api.ImmutableCredentialRecord;
3435
import org.springframework.security.web.webauthn.api.PublicKeyCredentialCreationOptions;
3536
import org.springframework.security.web.webauthn.api.TestCredentialRecord;
@@ -100,9 +101,40 @@ class WebAuthnRegistrationFilterTests {
100101

101102
private WebAuthnRegistrationFilter filter;
102103

104+
private MockHttpServletRequest request;
105+
103106
@BeforeEach
104107
void setup() {
105108
this.filter = new WebAuthnRegistrationFilter(this.userCredentials, this.operations);
109+
this.request = new MockHttpServletRequest();
110+
this.response = new MockHttpServletResponse();
111+
this.chain = mock(FilterChain.class);
112+
}
113+
114+
@Test
115+
public void doFilterWhenCustomRequestRegisterCredentialMatcherThenUses() throws Exception {
116+
this.request.setPathInfo("/register/path");
117+
this.filter.setRegisterCredentialMatcher(new AntPathRequestMatcher("/register/path"));
118+
this.filter.doFilter(this.request, this.response, this.chain);
119+
verifyNoInteractions(this.chain);
120+
}
121+
122+
@Test
123+
public void doFilterWhenCustomRequestRemoveCredentialMatcherThenUses() throws Exception {
124+
this.request.setPathInfo("/remove/path");
125+
this.filter.setRemoveCredentialMatcher(new AntPathRequestMatcher("/remove/path"));
126+
this.filter.doFilter(this.request, this.response, this.chain);
127+
verifyNoInteractions(this.chain);
128+
}
129+
130+
@Test
131+
public void setRequestRegisterCredentialWhenNullThenIllegalArgument() {
132+
assertThatIllegalArgumentException().isThrownBy(() -> this.filter.setRegisterCredentialMatcher(null));
133+
}
134+
135+
@Test
136+
public void setRequestRemoveCredentialWhenNullThenIllegalArgument() {
137+
assertThatIllegalArgumentException().isThrownBy(() -> this.filter.setRemoveCredentialMatcher(null));
106138
}
107139

108140
@Test

0 commit comments

Comments
 (0)