forked from JavaOPs/bootjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminUserControllerTest.java
More file actions
209 lines (184 loc) · 8.05 KB
/
AdminUserControllerTest.java
File metadata and controls
209 lines (184 loc) · 8.05 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package ru.javaops.bootjava.user.web;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import ru.javaops.bootjava.AbstractControllerTest;
import ru.javaops.bootjava.user.model.Role;
import ru.javaops.bootjava.user.model.User;
import ru.javaops.bootjava.user.repository.UserRepository;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static ru.javaops.bootjava.user.UserTestData.*;
import static ru.javaops.bootjava.user.web.AdminUserController.REST_URL;
import static ru.javaops.bootjava.user.web.UniqueMailValidator.EXCEPTION_DUPLICATE_EMAIL;
class AdminUserControllerTest extends AbstractControllerTest {
private static final String REST_URL_SLASH = REST_URL + '/';
@Autowired
private UserRepository repository;
@Test
@WithUserDetails(value = ADMIN_MAIL)
void get() throws Exception {
perform(MockMvcRequestBuilders.get(REST_URL_SLASH + ADMIN_ID))
.andExpect(status().isOk())
.andDo(print())
// https://jira.spring.io/browse/SPR-14472
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(USER_MATCHER.contentJson(admin));
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void getNotFound() throws Exception {
perform(MockMvcRequestBuilders.get(REST_URL_SLASH + NOT_FOUND))
.andDo(print())
.andExpect(status().isNotFound());
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void getByEmail() throws Exception {
perform(MockMvcRequestBuilders.get(REST_URL_SLASH + "by-email?email=" + admin.getEmail()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(USER_MATCHER.contentJson(admin));
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void delete() throws Exception {
perform(MockMvcRequestBuilders.delete(REST_URL_SLASH + USER_ID))
.andDo(print())
.andExpect(status().isNoContent());
assertFalse(repository.findById(USER_ID).isPresent());
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void deleteNotFound() throws Exception {
perform(MockMvcRequestBuilders.delete(REST_URL_SLASH + NOT_FOUND))
.andDo(print())
.andExpect(status().isNotFound());
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void enableNotFound() throws Exception {
perform(MockMvcRequestBuilders.patch(REST_URL_SLASH + NOT_FOUND)
.param("enabled", "false")
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isNotFound());
}
@Test
void getUnAuth() throws Exception {
perform(MockMvcRequestBuilders.get(REST_URL))
.andDo(print())
.andExpect(status().isUnauthorized());
}
@Test
@WithUserDetails(value = USER_MAIL)
void getForbidden() throws Exception {
perform(MockMvcRequestBuilders.get(REST_URL))
.andExpect(status().isForbidden());
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void update() throws Exception {
User updated = getUpdated();
updated.setId(null);
perform(MockMvcRequestBuilders.put(REST_URL_SLASH + USER_ID)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonWithPassword(updated, "newPass")))
.andDo(print())
.andExpect(status().isNoContent());
USER_MATCHER.assertMatch(repository.getExisted(USER_ID), getUpdated());
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void createWithLocation() throws Exception {
User newUser = getNew();
ResultActions action = perform(MockMvcRequestBuilders.post(REST_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonWithPassword(newUser, "newPass")))
.andExpect(status().isCreated());
User created = USER_MATCHER.readFromJson(action);
int newId = created.id();
newUser.setId(newId);
USER_MATCHER.assertMatch(created, newUser);
USER_MATCHER.assertMatch(repository.getExisted(newId), newUser);
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void getAll() throws Exception {
perform(MockMvcRequestBuilders.get(REST_URL))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(USER_MATCHER.contentJson(admin, guest, user));
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void enable() throws Exception {
perform(MockMvcRequestBuilders.patch(REST_URL_SLASH + USER_ID)
.param("enabled", "false")
.contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isNoContent());
assertFalse(repository.getExisted(USER_ID).isEnabled());
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void createInvalid() throws Exception {
User invalid = new User(null, null, "", "newPass", Role.USER, Role.ADMIN);
perform(MockMvcRequestBuilders.post(REST_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonWithPassword(invalid, "newPass")))
.andDo(print())
.andExpect(status().isUnprocessableEntity());
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void updateInvalid() throws Exception {
User invalid = new User(user);
invalid.setName("");
perform(MockMvcRequestBuilders.put(REST_URL_SLASH + USER_ID)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonWithPassword(invalid, "password")))
.andDo(print())
.andExpect(status().isUnprocessableEntity());
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void updateHtmlUnsafe() throws Exception {
User updated = new User(user);
updated.setName("<script>alert(123)</script>");
perform(MockMvcRequestBuilders.put(REST_URL_SLASH + USER_ID)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonWithPassword(updated, "password")))
.andDo(print())
.andExpect(status().isUnprocessableEntity());
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void updateDuplicate() throws Exception {
User updated = new User(user);
updated.setEmail(ADMIN_MAIL);
perform(MockMvcRequestBuilders.put(REST_URL_SLASH + USER_ID)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonWithPassword(updated, "password")))
.andDo(print())
.andExpect(status().isUnprocessableEntity())
.andExpect(content().string(containsString(EXCEPTION_DUPLICATE_EMAIL)));
}
@Test
@WithUserDetails(value = ADMIN_MAIL)
void createDuplicate() throws Exception {
User expected = new User(null, "New", USER_MAIL, "newPass", Role.USER, Role.ADMIN);
perform(MockMvcRequestBuilders.post(REST_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(jsonWithPassword(expected, "newPass")))
.andDo(print())
.andExpect(status().isUnprocessableEntity())
.andExpect(content().string(containsString(EXCEPTION_DUPLICATE_EMAIL)));
}
}