-
Notifications
You must be signed in to change notification settings - Fork 6
/
AccessCheckControllerTest.java
114 lines (95 loc) · 3.78 KB
/
AccessCheckControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package io.sfe.authorization;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
// TODO: 15.03.2023 Fix the tests as a part of https://github.com/vrudas/spring-framework-examples/issues/101
// implementation
@Disabled("Because of https://github.com/vrudas/spring-framework-examples/issues/101")
@WebMvcTest(AccessCheckController.class)
class AccessCheckControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
@WithMockUser
void login_page_is_available_for_user() throws Exception {
mockMvc.perform(get("/login"))
.andExpect(status().isOk());
}
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
void login_page_is_available_for_admin() throws Exception {
mockMvc.perform(get("/login"))
.andExpect(status().isOk());
}
@Test
@WithMockUser
void admin_endpoint_is_not_available_for_user() throws Exception {
mockMvc.perform(get("/admin/system-info"))
.andExpect(status().isForbidden());
}
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
void admin_endpoint_is_available_for_admin() throws Exception {
mockMvc.perform(get("/admin/system-info"))
.andExpect(status().isOk());
}
@Test
@WithMockUser
void user_name_endpoint_is_available_for_user() throws Exception {
mockMvc.perform(get("/name"))
.andExpect(status().isOk());
}
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
void user_name_endpoint_is_available_for_admin() throws Exception {
mockMvc.perform(get("/name"))
.andExpect(status().isOk());
}
@Test
@WithMockUser
void read_user_endpoint_is_not_available_for_user_without_authority_but_with_role_USER() throws Exception {
mockMvc.perform(get("/read-user"))
.andExpect(status().isForbidden());
}
@Test
@WithMockUser(authorities = "READ_USERS")
void read_user_endpoint_is_available_for_user_with_authority() throws Exception {
mockMvc.perform(get("/read-user"))
.andExpect(status().isOk());
}
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
void read_user_endpoint_is_not_available_for_admin_without_authority() throws Exception {
mockMvc.perform(get("/read-user"))
.andExpect(status().isForbidden());
}
@Test
@WithMockUser(username = "admin", authorities = {"ROLE_ADMIN", "READ_USERS"})
void read_user_endpoint_is_available_for_admin_with_authority() throws Exception {
mockMvc.perform(get("/read-user"))
.andExpect(status().isOk());
}
@Test
@WithMockUser
void delete_user_endpoint_is_not_available_for_user() throws Exception {
mockMvc.perform(get("/read-user"))
.andExpect(status().isForbidden());
}
@Test
@WithMockUser(username = "admin", authorities = {"ROLE_ADMIN"})
void delete_user_endpoint_is_not_available_for_admin_without_authority() throws Exception {
mockMvc.perform(get("/delete-user"))
.andExpect(status().isForbidden());
}
@Test
@WithMockUser(username = "admin", authorities = {"ROLE_ADMIN", "DELETE_USERS"})
void delete_user_endpoint_is_available_for_admin_with_authority() throws Exception {
mockMvc.perform(get("/delete-user"))
.andExpect(status().isOk());
}
}