Skip to content

Commit b92c072

Browse files
therepanicjzheaux
authored andcommitted
add tests
Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
1 parent 6335caa commit b92c072

File tree

4 files changed

+214
-6
lines changed

4 files changed

+214
-6
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2004-present 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.docs.servlet.addingcustomfilter;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.security.access.AccessDeniedException;
27+
import org.springframework.security.core.userdetails.User;
28+
import org.springframework.security.core.userdetails.UserDetails;
29+
import org.springframework.security.core.userdetails.UserDetailsService;
30+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
31+
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.junit.jupiter.SpringExtension;
33+
import org.springframework.test.context.web.WebAppConfiguration;
34+
import org.springframework.test.web.servlet.MockMvc;
35+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
36+
import org.springframework.web.context.WebApplicationContext;
37+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
38+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
39+
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
40+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
41+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
42+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
43+
import org.springframework.web.bind.annotation.GetMapping;
44+
import org.springframework.web.bind.annotation.RestController;
45+
46+
@ExtendWith(SpringExtension.class)
47+
@ContextConfiguration(classes = {
48+
CustomFilterAfterTests.UserDetailsConfig.class,
49+
CustomFilterAfterTests.ApiController.class,
50+
SecurityConfigAfter.class })
51+
@WebAppConfiguration
52+
public class CustomFilterAfterTests {
53+
54+
@Autowired
55+
private WebApplicationContext context;
56+
57+
private MockMvc mvc;
58+
59+
@BeforeEach
60+
void setup() {
61+
this.mvc = MockMvcBuilders.webAppContextSetup(this.context)
62+
.defaultRequest(get("/api").with(user("user")))
63+
.apply(springSecurity())
64+
.build();
65+
}
66+
67+
@Test
68+
void tenantFilterWhenHeaderMissingThenAccessDenied() {
69+
assertThatExceptionOfType(AccessDeniedException.class)
70+
.isThrownBy(() -> this.mvc.perform(get("/api")).andReturn());
71+
}
72+
73+
@Test
74+
void tenantFilterWhenHeaderPresentThenContinuesFilterChain() throws Exception {
75+
this.mvc.perform(get("/api").header("X-Tenant-Id", "some-tenant-id"))
76+
.andExpect(status().isOk())
77+
.andExpect(authenticated().withUsername("user"));
78+
}
79+
80+
@Configuration
81+
static class UserDetailsConfig {
82+
@Bean
83+
UserDetailsService userDetailsService() {
84+
UserDetails user = User.withDefaultPasswordEncoder()
85+
.username("user")
86+
.password("password")
87+
.roles("USER")
88+
.build();
89+
return new InMemoryUserDetailsManager(user);
90+
}
91+
}
92+
93+
@RestController
94+
static class ApiController {
95+
96+
@GetMapping("/api")
97+
String api() {
98+
return "ok";
99+
}
100+
101+
}
102+
}

docs/src/test/java/org/springframework/security/docs/servlet/addingcustomfilter/CustomFilterTests.java renamed to docs/src/test/java/org/springframework/security/docs/servlet/addingcustomfilter/CustomFilterBeforeTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@
4545

4646
@ExtendWith(SpringExtension.class)
4747
@ContextConfiguration(classes = {
48-
CustomFilterTests.UserDetailsConfig.class,
49-
CustomFilterTests.ApiController.class,
48+
CustomFilterBeforeTests.UserDetailsConfig.class,
49+
CustomFilterBeforeTests.ApiController.class,
5050
SecurityConfigBefore.class })
5151
@WebAppConfiguration
52-
public class CustomFilterTests {
52+
public class CustomFilterBeforeTests {
5353

5454
@Autowired
5555
private WebApplicationContext context;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2004-present 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.kt.docs.servlet.addingcustomfilter
18+
19+
import org.assertj.core.api.Assertions.assertThatExceptionOfType
20+
import org.junit.jupiter.api.BeforeEach
21+
import org.junit.jupiter.api.Test
22+
import org.junit.jupiter.api.extension.ExtendWith
23+
import org.springframework.beans.factory.annotation.Autowired
24+
import org.springframework.context.annotation.Bean
25+
import org.springframework.context.annotation.Configuration
26+
import org.springframework.security.core.userdetails.User
27+
import org.springframework.security.core.userdetails.UserDetails
28+
import org.springframework.security.core.userdetails.UserDetailsService
29+
import org.springframework.security.provisioning.InMemoryUserDetailsManager
30+
import org.springframework.test.context.ContextConfiguration
31+
import org.springframework.test.context.junit.jupiter.SpringExtension
32+
import org.springframework.test.context.web.WebAppConfiguration
33+
import org.springframework.test.web.servlet.MockMvc
34+
import org.springframework.test.web.servlet.setup.MockMvcBuilders
35+
import org.springframework.web.context.WebApplicationContext
36+
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user
37+
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity
38+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
39+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
40+
import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated
41+
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder
42+
import org.springframework.web.bind.annotation.GetMapping
43+
import org.springframework.web.bind.annotation.RestController
44+
45+
@ExtendWith(SpringExtension::class)
46+
@ContextConfiguration(
47+
classes = [
48+
CustomFilterAfterTests.UserDetailsConfig::class,
49+
CustomFilterAfterTests.ApiController::class,
50+
SecurityConfigAfter::class
51+
]
52+
)
53+
@WebAppConfiguration
54+
class CustomFilterAfterTests {
55+
56+
@Autowired
57+
private lateinit var context: WebApplicationContext
58+
59+
private lateinit var mvc: MockMvc
60+
61+
@BeforeEach
62+
fun setup() {
63+
this.mvc = MockMvcBuilders.webAppContextSetup(this.context)
64+
.apply<DefaultMockMvcBuilder>(springSecurity())
65+
.build();
66+
}
67+
68+
@Test
69+
fun tenantFilterWhenHeaderMissingThenAccessDenied() {
70+
assertThatExceptionOfType(Exception::class.java)
71+
.isThrownBy { this.mvc.perform(get("/api").with(user("user"))).andReturn() }
72+
}
73+
74+
@Test
75+
fun tenantFilterWhenHeaderPresentThenContinuesFilterChain() {
76+
this.mvc.perform(get("/api")
77+
.with(user("user"))
78+
.header("X-Tenant-Id", "some-tenant-id"))
79+
.andExpect(status().isOk)
80+
.andExpect(authenticated().withUsername("user"))
81+
}
82+
83+
@Configuration
84+
open class UserDetailsConfig {
85+
@Bean
86+
open fun userDetailsService(): UserDetailsService {
87+
@Suppress("DEPRECATION")
88+
val user: UserDetails = User.withDefaultPasswordEncoder()
89+
.username("user")
90+
.password("password")
91+
.roles("USER")
92+
.build()
93+
return InMemoryUserDetailsManager(user)
94+
}
95+
}
96+
97+
@RestController
98+
class ApiController {
99+
100+
@GetMapping("/api")
101+
fun api(): String {
102+
return "ok"
103+
}
104+
105+
}
106+
}

docs/src/test/kotlin/org/springframework/security/kt/docs/servlet/addingcustomfilter/CustomFilterTests.kt renamed to docs/src/test/kotlin/org/springframework/security/kt/docs/servlet/addingcustomfilter/CustomFilterBeforeTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ import org.springframework.web.bind.annotation.RestController
4545
@ExtendWith(SpringExtension::class)
4646
@ContextConfiguration(
4747
classes = [
48-
CustomFilterTests.UserDetailsConfig::class,
49-
CustomFilterTests.ApiController::class,
48+
CustomFilterBeforeTests.UserDetailsConfig::class,
49+
CustomFilterBeforeTests.ApiController::class,
5050
SecurityConfigBefore::class
5151
]
5252
)
5353
@WebAppConfiguration
54-
class CustomFilterTests {
54+
class CustomFilterBeforeTests {
5555

5656
@Autowired
5757
private lateinit var context: WebApplicationContext

0 commit comments

Comments
 (0)