1+ package ru .javaops .bootjava .web ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .springframework .beans .factory .annotation .Autowired ;
6+ import org .springframework .hateoas .MediaTypes ;
7+ import org .springframework .security .test .context .support .WithUserDetails ;
8+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
9+ import ru .javaops .bootjava .repository .UserRepository ;
10+
11+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
12+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
13+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
14+ import static ru .javaops .bootjava .UserTestUtil .*;
15+
16+ class UserControllerTest extends AbstractControllerTest {
17+ static final String URL = "/api/users/" ;
18+
19+ @ Autowired
20+ private UserRepository userRepository ;
21+
22+ @ Test
23+ @ WithUserDetails (value = ADMIN_MAIL )
24+ void get () throws Exception {
25+ perform (MockMvcRequestBuilders .get (URL + USER_ID ))
26+ .andExpect (status ().isOk ())
27+ .andDo (print ())
28+ .andExpect (content ().contentTypeCompatibleWith (MediaTypes .HAL_JSON_VALUE ));
29+ }
30+
31+ @ Test
32+ @ WithUserDetails (value = ADMIN_MAIL )
33+ void getAll () throws Exception {
34+ perform (MockMvcRequestBuilders .get (URL ))
35+ .andExpect (status ().isOk ())
36+ .andDo (print ())
37+ .andExpect (content ().contentTypeCompatibleWith (MediaTypes .HAL_JSON_VALUE ));
38+ }
39+
40+ @ Test
41+ @ WithUserDetails (value = ADMIN_MAIL )
42+ void getByEmail () throws Exception {
43+ perform (MockMvcRequestBuilders .get (URL + "search/by-email?email=" + ADMIN_MAIL ))
44+ .andExpect (status ().isOk ())
45+ .andDo (print ())
46+ .andExpect (content ().contentTypeCompatibleWith (MediaTypes .HAL_JSON_VALUE ));
47+ }
48+
49+ @ Test
50+ @ WithUserDetails (value = USER_MAIL )
51+ void getForbidden () throws Exception {
52+ perform (MockMvcRequestBuilders .get (URL ))
53+ .andExpect (status ().isForbidden ());
54+ }
55+
56+ @ Test
57+ @ WithUserDetails (value = ADMIN_MAIL )
58+ void delete () throws Exception {
59+ perform (MockMvcRequestBuilders .delete (URL + USER_ID ))
60+ .andExpect (status ().isNoContent ());
61+ Assertions .assertFalse (userRepository .findById (USER_ID ).isPresent ());
62+ Assertions .assertTrue (userRepository .findById (ADMIN_ID ).isPresent ());
63+ }
64+ }
0 commit comments