package my.ostrea.blog;

import my.ostrea.blog.configurations.WebSecurityConfig;
import my.ostrea.blog.models.ArticleRepository;
import my.ostrea.blog.models.MyUser;
import my.ostrea.blog.models.UserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.Filter;

import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BlogApplication.class)
@WebAppConfiguration
public class BlogApplicationTests {
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Test
    public void accessingStartPageAsAnonymousShouldReturnViewNamedIndexAndModelWithZeroAttributes()
            throws Exception {
        mockMvc.perform(get("/"))
                .andExpect(status().isOk())
                .andExpect(view().name("index"))
                .andExpect(model().attributeDoesNotExist("articles"));
    }

	@Test
    @WithUserDetails("test")
	public void accessingStartPageAsLoggedUserShouldReturnViewNamedIndexAndModelWithOneAttribute() throws Exception {
        mockMvc.perform(
                get("/"))
                .andExpect(status().isOk())
                .andExpect(view().name("index"))
                .andExpect(model().attributeExists("articles"));
    }

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .apply(springSecurity())
                .build();
    }
}

@@ -1936,16 +1936,204 @@
 ntext;%0A%0A
+    @Before%0A    public void setUp() %7B%0A        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)%0A                .apply(springSecurity())%0A                .build();%0A    %7D%0A%0A
     @Tes
@@ -2130,24 +2130,24 @@
 %0A%0A    @Test%0A
-
     public v
@@ -2838,30 +2838,28 @@
     %7D%0A%0A    @
-Before
+Test
 %0A    public 
@@ -2867,158 +2867,1350 @@
 oid 
-setUp() %7B%0A        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)%0A                .apply(springSecurity())%0A                .build(
+accessingUsernamePageAsAnonymousShouldRedirectToLoginPage()%0A            throws Exception %7B%0A        mockMvc.perform(get(%22/test%22))%0A                .andExpect(status().is3xxRedirection())%0A                .andExpect(redirectedUrlPattern(%22**/login%22));%0A    %7D%0A%0A    @Test%0A    @WithMockUser(%22userForTests%22)%0A    public void accessingUsernamePageAsLoggedUserIfUsernameIsNotLoggedUsernameShouldShowIndexViewWithModelWithOneAttr()%0A            throws Exception %7B%0A        mockMvc.perform(get(%22/admin%22))%0A                .andExpect(status().isOk())%0A                .andExpect(view().name(%22index%22))%0A                .andExpect(model().attributeExists(%22articles%22));%0A    %7D%0A%0A    @Test%0A    @WithMockUser(%22userForTests%22)%0A    public void accessingNotExistingUsernamePageAsLoggedUserShouldShowIndexViewWithModelWithZeroAttr()%0A            throws Exception %7B%0A        mockMvc.perform(get(%22/not_existing_user%22))%0A                .andExpect(status().isOk())%0A                .andExpect(view().name(%22index%22))%0A                .andExpect(model().attributeDoesNotExist(%22articles%22));%0A    %7D%0A%0A    @Test%0A    @WithUserDetails(%22test%22)%0A    public void accessingYourOwnUsernamePageAsLoggedUserShouldRedirectToIndex()%0A            throws Exception %7B%0A        mockMvc.perform(get(%22/test%22))%0A                .andExpect(status().is3xxRedirection())%0A                .andExpect(redirectedUrl(%22/%22)
 );%0A 

Add unit tests for users page