1414import static alexp .blog .utils .SecurityUtils .userAdmin ;
1515import static alexp .blog .utils .SecurityUtils .userBob ;
1616import static org .hamcrest .CoreMatchers .equalTo ;
17+ import static org .hamcrest .CoreMatchers .hasItems ;
1718import static org .hamcrest .CoreMatchers .is ;
1819import static org .hamcrest .Matchers .*;
1920import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
@@ -30,7 +31,8 @@ public class PostsControllerIT extends AbstractIntegrationTest {
3031 public void shouldShowPostsPage () throws Exception {
3132 mockMvc .perform (get ("/" ))
3233 .andExpect (status ().isOk ())
33- .andExpect (view ().name ("posts" ));
34+ .andExpect (view ().name ("posts" ))
35+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (2L ))));
3436 }
3537
3638 @ Test
@@ -270,6 +272,31 @@ public void shouldUnhidePost() throws Exception {
270272 .andExpect (content ().string ("ok" ));
271273 }
272274
275+ @ Test
276+ @ ExpectedDatabase ("data-post-hidden.xml" )
277+ @ DatabaseSetup ("data-post-hidden.xml" )
278+ public void shouldNotShowHiddenPostIfNotAdmin () throws Exception {
279+ mockMvc .perform (get ("/" ).with (userBob ()))
280+ .andExpect (status ().isOk ())
281+ .andExpect (view ().name ("posts" ))
282+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (1L ))));
283+
284+ mockMvc .perform (get ("/" ))
285+ .andExpect (status ().isOk ())
286+ .andExpect (view ().name ("posts" ))
287+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (1L ))));
288+ }
289+
290+ @ Test
291+ @ ExpectedDatabase ("data-post-hidden.xml" )
292+ @ DatabaseSetup ("data-post-hidden.xml" )
293+ public void shouldShowHiddenPostIfAdmin () throws Exception {
294+ mockMvc .perform (get ("/" ).with (userAdmin ()))
295+ .andExpect (status ().isOk ())
296+ .andExpect (view ().name ("posts" ))
297+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (2L ))));
298+ }
299+
273300 @ Test
274301 @ ExpectedDatabase ("data.xml" )
275302 public void shouldDenyDeletePostIfNotAdmin () throws Exception {
@@ -291,4 +318,54 @@ public void shouldDeletePost() throws Exception {
291318 .andExpect (status ().isOk ())
292319 .andExpect (content ().string ("ok" ));
293320 }
321+
322+ @ Test
323+ @ ExpectedDatabase ("data.xml" )
324+ public void shouldShowPostsByTag () throws Exception {
325+ mockMvc .perform (get ("/posts?tagged=c++" ))
326+ .andExpect (status ().isOk ())
327+ .andExpect (view ().name ("posts" ))
328+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (2L ))));
329+
330+ mockMvc .perform (get ("/posts?tagged=meow" ))
331+ .andExpect (status ().isOk ())
332+ .andExpect (view ().name ("posts" ))
333+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (1L ))))
334+ .andExpect (model ().attribute ("postsPage" , hasItems (hasProperty ("id" , equalTo (2L )))));
335+
336+ mockMvc .perform (get ("/posts?tagged=c++, hello world" ))
337+ .andExpect (status ().isOk ())
338+ .andExpect (view ().name ("posts" ))
339+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (1L ))))
340+ .andExpect (model ().attribute ("postsPage" , hasItems (hasProperty ("id" , equalTo (1L )))));
341+ }
342+
343+ @ Test
344+ @ ExpectedDatabase ("data.xml" )
345+ public void shouldShowNoPostsWhenTagNotExists () throws Exception {
346+ mockMvc .perform (get ("/posts?tagged=not exists" ))
347+ .andExpect (status ().isOk ())
348+ .andExpect (view ().name ("posts" ))
349+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (0L ))));
350+ }
351+
352+ @ Test
353+ @ ExpectedDatabase ("data-post-hidden.xml" )
354+ @ DatabaseSetup ("data-post-hidden.xml" )
355+ public void shouldNotShowHiddenPostsByTagIfNotAdmin () throws Exception {
356+ mockMvc .perform (get ("/posts?tagged=c++" ))
357+ .andExpect (status ().isOk ())
358+ .andExpect (view ().name ("posts" ))
359+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (1L ))));
360+ }
361+
362+ @ Test
363+ @ ExpectedDatabase ("data-post-hidden.xml" )
364+ @ DatabaseSetup ("data-post-hidden.xml" )
365+ public void shouldShowHiddenPostsByTagIfAdmin () throws Exception {
366+ mockMvc .perform (get ("/posts?tagged=c++" ).with (userAdmin ()))
367+ .andExpect (status ().isOk ())
368+ .andExpect (view ().name ("posts" ))
369+ .andExpect (model ().attribute ("postsPage" , hasProperty ("totalElements" , equalTo (2L ))));
370+ }
294371}
0 commit comments