1616import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
1717import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
1818import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
19+ import static ru .javawebinar .topjava .TestUtil .userHttpBasic ;
1920import static ru .javawebinar .topjava .UserTestData .*;
2021
2122class AdminRestControllerTest extends AbstractControllerTest {
@@ -27,7 +28,8 @@ class AdminRestControllerTest extends AbstractControllerTest {
2728
2829 @ Test
2930 void get () throws Exception {
30- perform (MockMvcRequestBuilders .get (REST_URL + ADMIN_ID ))
31+ perform (MockMvcRequestBuilders .get (REST_URL + ADMIN_ID )
32+ .with (userHttpBasic (admin )))
3133 .andExpect (status ().isOk ())
3234 .andDo (print ())
3335 // https://jira.spring.io/browse/SPR-14472
@@ -37,25 +39,41 @@ void get() throws Exception {
3739
3840 @ Test
3941 void getByEmail () throws Exception {
40- perform (MockMvcRequestBuilders .get (REST_URL + "by-email?email=" + user .getEmail ()))
42+ perform (MockMvcRequestBuilders .get (REST_URL + "by-email?email=" + user .getEmail ())
43+ .with (userHttpBasic (admin )))
4144 .andExpect (status ().isOk ())
4245 .andExpect (content ().contentTypeCompatibleWith (MediaType .APPLICATION_JSON ))
4346 .andExpect (USER_MATCHER .contentJson (user ));
4447 }
4548
4649 @ Test
4750 void delete () throws Exception {
48- perform (MockMvcRequestBuilders .delete (REST_URL + USER_ID ))
51+ perform (MockMvcRequestBuilders .delete (REST_URL + USER_ID )
52+ .with (userHttpBasic (admin )))
4953 .andDo (print ())
5054 .andExpect (status ().isNoContent ());
5155 assertThrows (NotFoundException .class , () -> userService .get (USER_ID ));
5256 }
5357
58+ @ Test
59+ void getUnAuth () throws Exception {
60+ perform (MockMvcRequestBuilders .get (REST_URL ))
61+ .andExpect (status ().isUnauthorized ());
62+ }
63+
64+ @ Test
65+ void getForbidden () throws Exception {
66+ perform (MockMvcRequestBuilders .get (REST_URL )
67+ .with (userHttpBasic (user )))
68+ .andExpect (status ().isForbidden ());
69+ }
70+
5471 @ Test
5572 void update () throws Exception {
5673 User updated = getUpdated ();
5774 perform (MockMvcRequestBuilders .put (REST_URL + USER_ID )
5875 .contentType (MediaType .APPLICATION_JSON )
76+ .with (userHttpBasic (admin ))
5977 .content (JsonUtil .writeValue (updated )))
6078 .andExpect (status ().isNoContent ());
6179
@@ -67,6 +85,7 @@ void createWithLocation() throws Exception {
6785 User newUser = getNew ();
6886 ResultActions action = perform (MockMvcRequestBuilders .post (REST_URL )
6987 .contentType (MediaType .APPLICATION_JSON )
88+ .with (userHttpBasic (admin ))
7089 .content (JsonUtil .writeValue (newUser )))
7190 .andExpect (status ().isCreated ());
7291
@@ -79,7 +98,8 @@ void createWithLocation() throws Exception {
7998
8099 @ Test
81100 void getAll () throws Exception {
82- perform (MockMvcRequestBuilders .get (REST_URL ))
101+ perform (MockMvcRequestBuilders .get (REST_URL )
102+ .with (userHttpBasic (admin )))
83103 .andExpect (status ().isOk ())
84104 .andExpect (content ().contentTypeCompatibleWith (MediaType .APPLICATION_JSON ))
85105 .andExpect (USER_MATCHER .contentJson (admin , guest , user ));
@@ -88,7 +108,8 @@ void getAll() throws Exception {
88108 @ Test
89109 void getWithMeals () throws Exception {
90110 assumeDataJpa ();
91- perform (MockMvcRequestBuilders .get (REST_URL + ADMIN_ID + "/with-meals" ))
111+ perform (MockMvcRequestBuilders .get (REST_URL + ADMIN_ID + "/with-meals" )
112+ .with (userHttpBasic (admin )))
92113 .andExpect (status ().isOk ())
93114 .andDo (print ())
94115 .andExpect (content ().contentTypeCompatibleWith (MediaType .APPLICATION_JSON ))
@@ -99,7 +120,8 @@ void getWithMeals() throws Exception {
99120 void enable () throws Exception {
100121 perform (MockMvcRequestBuilders .patch (REST_URL + USER_ID )
101122 .param ("enabled" , "false" )
102- .contentType (MediaType .APPLICATION_JSON ))
123+ .contentType (MediaType .APPLICATION_JSON )
124+ .with (userHttpBasic (admin )))
103125 .andDo (print ())
104126 .andExpect (status ().isNoContent ());
105127
0 commit comments