Skip to content

Commit 80e8e14

Browse files
franticticktickrwinch
authored andcommitted
Add GenerateOneTimeTokenFilterTests
1 parent b555593 commit 80e8e14

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.authentication.ott;
18+
19+
import java.io.IOException;
20+
import java.time.Instant;
21+
22+
import jakarta.servlet.ServletException;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import org.mockito.ArgumentMatchers;
26+
27+
import org.springframework.mock.web.MockFilterChain;
28+
import org.springframework.mock.web.MockHttpServletRequest;
29+
import org.springframework.mock.web.MockHttpServletResponse;
30+
import org.springframework.security.authentication.ott.DefaultOneTimeToken;
31+
import org.springframework.security.authentication.ott.GenerateOneTimeTokenRequest;
32+
import org.springframework.security.authentication.ott.OneTimeTokenService;
33+
import org.springframework.security.web.server.authentication.ott.GenerateOneTimeTokenWebFilter;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
37+
import static org.mockito.BDDMockito.given;
38+
import static org.mockito.Mockito.mock;
39+
import static org.mockito.Mockito.never;
40+
import static org.mockito.Mockito.verify;
41+
42+
/**
43+
* Tests for {@link GenerateOneTimeTokenWebFilter}
44+
*
45+
* @author Max Batischev
46+
*/
47+
public class GenerateOneTimeTokenFilterTests {
48+
49+
private final OneTimeTokenService oneTimeTokenService = mock(OneTimeTokenService.class);
50+
51+
private final RedirectOneTimeTokenGenerationSuccessHandler successHandler = new RedirectOneTimeTokenGenerationSuccessHandler(
52+
"/login/ott");
53+
54+
private static final String TOKEN = "token";
55+
56+
private static final String USERNAME = "user";
57+
58+
private final MockHttpServletRequest request = new MockHttpServletRequest();
59+
60+
private final MockHttpServletResponse response = new MockHttpServletResponse();
61+
62+
private final MockFilterChain filterChain = new MockFilterChain();
63+
64+
@BeforeEach
65+
void setup() {
66+
this.request.setMethod("POST");
67+
this.request.setServletPath("/ott/generate");
68+
}
69+
70+
@Test
71+
void filterWhenUsernameFormParamIsPresentThenSuccess() throws ServletException, IOException {
72+
given(this.oneTimeTokenService.generate(ArgumentMatchers.any(GenerateOneTimeTokenRequest.class)))
73+
.willReturn(new DefaultOneTimeToken(TOKEN, USERNAME, Instant.now()));
74+
this.request.setParameter("username", USERNAME);
75+
76+
GenerateOneTimeTokenFilter filter = new GenerateOneTimeTokenFilter(this.oneTimeTokenService,
77+
this.successHandler);
78+
79+
filter.doFilter(this.request, this.response, this.filterChain);
80+
81+
verify(this.oneTimeTokenService).generate(ArgumentMatchers.any(GenerateOneTimeTokenRequest.class));
82+
assertThat(this.response.getRedirectedUrl()).isEqualTo("/login/ott");
83+
}
84+
85+
@Test
86+
void filterWhenUsernameFormParamIsEmptyThenNull() throws ServletException, IOException {
87+
given(this.oneTimeTokenService.generate(ArgumentMatchers.any(GenerateOneTimeTokenRequest.class)))
88+
.willReturn((new DefaultOneTimeToken(TOKEN, USERNAME, Instant.now())));
89+
GenerateOneTimeTokenFilter filter = new GenerateOneTimeTokenFilter(this.oneTimeTokenService,
90+
this.successHandler);
91+
92+
filter.doFilter(this.request, this.response, this.filterChain);
93+
94+
verify(this.oneTimeTokenService, never()).generate(ArgumentMatchers.any(GenerateOneTimeTokenRequest.class));
95+
}
96+
97+
@Test
98+
public void constructorWhenOneTimeTokenServiceNullThenIllegalArgumentException() {
99+
// @formatter:off
100+
assertThatIllegalArgumentException()
101+
.isThrownBy(() -> new GenerateOneTimeTokenFilter(null, this.successHandler));
102+
// @formatter:on
103+
}
104+
105+
@Test
106+
public void setWhenRequestMatcherNullThenIllegalArgumentException() {
107+
GenerateOneTimeTokenFilter filter = new GenerateOneTimeTokenFilter(this.oneTimeTokenService,
108+
this.successHandler);
109+
// @formatter:off
110+
assertThatIllegalArgumentException()
111+
.isThrownBy(() -> filter.setRequestMatcher(null));
112+
// @formatter:on
113+
}
114+
115+
}

0 commit comments

Comments
 (0)