package org.itevents;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.inject.Inject;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:applicationContext.xml",
		"classpath*:mvc-dispatcher-servlet.xml", "classpath*:spring-security.xml"})
@WebAppConfiguration
public class SequrityTests {

	@Inject
	private WebApplicationContext context;

	private MockMvc mvc;

	@Before
	public void setup() {
		mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
	}

	@Test
	public void testLoginFail() throws Exception {
		mvc.perform(formLogin().user("vlasov@email.com").password("wrongPassword"))
				.andExpect(unauthenticated())
				.andExpect(status().isUnauthorized());

	}

	@Test
	public void testLoginSuccess() throws Exception {
		mvc.perform(formLogin().user("vlasov@email.com").password("alex"))
				.andExpect(authenticated())
				.andExpect(status().isOk());

	}
}

@@ -97,16 +97,90 @@
 unWith;%0A
+import org.springframework.security.test.context.support.WithUserDetails;%0A
 import o
@@ -692,16 +692,123 @@
 mLogin;%0A
+import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.logout;%0A
 import s
@@ -2074,10 +2074,197 @@
 ));%0A%0A%09%7D%0A
+%0A%09@Test%0A%09@WithUserDetails(%22vlasov@email.com%22)%0A%09public void testLogout() throws Exception %7B%0A%09%09mvc.perform(logout())%0A%09%09%09%09.andExpect(unauthenticated())%0A%09%09%09%09.andExpect(status().isOk());%0A%0A%09%7D%0A%0A
 %7D%0A

add testLogout