Skip to content

Commit 8a76f45

Browse files
committed
TestCases
1 parent 14b9537 commit 8a76f45

File tree

7 files changed

+158
-1
lines changed

7 files changed

+158
-1
lines changed

spring-security/getting-started/SecureApplication/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@
7171
<artifactId>problem-spring-web</artifactId>
7272
<version>0.27.0</version>
7373
</dependency>
74+
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
75+
<dependency>
76+
<groupId>org.hamcrest</groupId>
77+
<artifactId>hamcrest-library</artifactId>
78+
<version>2.2</version>
79+
<scope>test</scope>
80+
</dependency>
81+
7482
</dependencies>
7583

7684
<build>

spring-security/getting-started/SecureApplication/src/main/java/com/reflectoring/security/config/SecurityConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.core.annotation.Order;
1010
import org.springframework.http.HttpMethod;
1111
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
12+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
1213
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1314
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1415
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;

spring-security/getting-started/SecureApplication/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
server:
22
port: 8083
33

4-
#spring:
4+
spring:
55
#security:
66
#user:
77
#name: admin
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.reflectoring.security.web;
2+
3+
import com.reflectoring.security.config.BasicAuthProperties;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.security.test.context.support.WithMockUser;
10+
import org.springframework.security.test.context.support.WithUserDetails;
11+
import org.springframework.test.context.ActiveProfiles;
12+
import org.springframework.test.context.jdbc.Sql;
13+
import org.springframework.test.context.jdbc.SqlGroup;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
16+
import static org.hamcrest.Matchers.hasSize;
17+
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_METHOD;
18+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
21+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
22+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
23+
24+
@SpringBootTest
25+
@AutoConfigureMockMvc
26+
@SqlGroup({
27+
@Sql(value = "classpath:init/first.sql", executionPhase = BEFORE_TEST_METHOD),
28+
@Sql(value = "classpath:init/second.sql", executionPhase = BEFORE_TEST_METHOD)
29+
})
30+
public class BookControllerTest {
31+
32+
@Autowired
33+
private MockMvc mockMvc;
34+
35+
@Test
36+
@WithMockUser(username = "bookadmin", roles = {"USER"})
37+
void successIfSecurityApplies() throws Exception {
38+
mockMvc.perform(get("/library/books")
39+
.param("genre", "Fiction")
40+
.param("user", "bookadmin")
41+
.header("X-Application-Name", "Library"))
42+
.andDo(print())
43+
.andExpect(status().isOk())
44+
.andExpect(jsonPath("$", hasSize(3)))
45+
;
46+
}
47+
48+
@Test
49+
@WithMockUser(username = "bookadmin", roles = {"ADMIN"})
50+
void failsForWrongAuthorization() throws Exception {
51+
mockMvc.perform(get("/library/books")
52+
.param("genre", "Fiction")
53+
.param("user", "bookadmin")
54+
.header("X-Application-Name", "Library"))
55+
.andDo(print())
56+
.andExpect(status().isForbidden())
57+
;
58+
}
59+
60+
@Test
61+
void failsIfSecurityApplies() throws Exception {
62+
mockMvc.perform(get("/library/books")
63+
.param("genre", "Fiction")
64+
.param("user", "bookadmin")
65+
.header("X-Application-Name", "Library"))
66+
.andDo(print())
67+
.andExpect(status().isUnauthorized())
68+
;
69+
}
70+
71+
@Test
72+
@WithUserDetails(value="bookadmin", userDetailsServiceBeanName="userDetailsService")
73+
void testBookWithConfiguredUserDetails() throws Exception {
74+
mockMvc.perform(get("/library/books")
75+
.param("genre", "Fantasy")
76+
.param("user", "bookadmin")
77+
.header("X-Application-Name", "Library"))
78+
.andDo(print())
79+
.andExpect(status().isOk())
80+
.andExpect(jsonPath("$", hasSize(1)))
81+
;
82+
}
83+
84+
@Test
85+
@WithUserDetails(value="bookadmin", userDetailsServiceBeanName="userDetailsService")
86+
void failsIfMandatoryHeaderIsMissing() throws Exception {
87+
mockMvc.perform(get("/library/books")
88+
.param("genre", "Fantasy")
89+
.param("user", "bookadmin"))
90+
//.header("X-Application-Name", "Library"))
91+
.andDo(print())
92+
.andExpect(status().isForbidden())
93+
;
94+
}
95+
96+
@Test
97+
@WithUserDetails(value="bookadmin", userDetailsServiceBeanName="userDetailsService")
98+
void failsIfPreAuthorizeConditionFails() throws Exception {
99+
mockMvc.perform(get("/library/books")
100+
.param("genre", "Fantasy")
101+
.param("user", "bookuser")
102+
.header("X-Application-Name", "Library"))
103+
.andDo(print())
104+
.andExpect(status().isForbidden())
105+
;
106+
}
107+
108+
@Test
109+
//@WithUserDetails(value="bookadmin", userDetailsServiceBeanName="userDetailsService")
110+
void testBookWithWrongCredentialsUserDetails() throws Exception {
111+
mockMvc.perform(get("/library/books")
112+
.param("genre", "Fantasy")
113+
.param("user", "bookadmin")
114+
.header("X-Application-Name", "Library")
115+
.with(httpBasic("bookadmin", "password")))
116+
.andDo(print())
117+
.andExpect(status().isUnauthorized());
118+
}
119+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
spring:
2+
datasource:
3+
driver-class-name: org.hsqldb.jdbc.JDBCDriver
4+
url: jdbc:hsqldb:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
5+
username: sa
6+
password:
7+
jpa:
8+
hibernate:
9+
ddl-auto: create-drop
10+
defer-datasource-initialization: true
11+
show-sql: true
12+
properties:
13+
hibernate:
14+
dialect: H2Dialect
15+
format_sql: true
16+
17+
logging:
18+
level:
19+
org:
20+
hibernate:
21+
sql: info
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
TRUNCATE TABLE AUTHOR_BOOK RESTART IDENTITY;
2+
TRUNCATE TABLE BOOK RESTART IDENTITY;
3+
TRUNCATE TABLE AUTHOR RESTART IDENTITY;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
INSERT INTO BOOK (id, name, publisher, publication_year, genre) VALUES (1, 'The Kite Runner', 'Riverhead books', '2003', 'Fiction');
2+
INSERT INTO BOOK (id, name, publisher, publication_year, genre) VALUES (2, 'Exiles', 'Pan Macmillan', '2022', 'Fiction');
3+
INSERT INTO BOOK (id, name, publisher, publication_year, genre) VALUES (3, 'A Game of Thrones', 'Bantam Spectra', '1996', 'Fiction');
4+
INSERT INTO BOOK (id, name, publisher, publication_year, genre) VALUES (4, 'American Gods', 'Headline', '2001', 'Fantasy');
5+
INSERT INTO BOOK (id, name, publisher, publication_year, genre) VALUES (5, 'The Passenger', 'Knopf', '2022', 'Mystery');

0 commit comments

Comments
 (0)