package ch.uzh.csg.reimbursement.integrationtesting;

import static org.junit.Assert.assertFalse;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
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.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import ch.uzh.csg.reimbursement.configuration.HibernateConfiguration;
import ch.uzh.csg.reimbursement.configuration.LdapConfiguration;
import ch.uzh.csg.reimbursement.configuration.MailConfiguration;
import ch.uzh.csg.reimbursement.configuration.WebMvcConfiguration;
import ch.uzh.csg.reimbursement.configuration.WebSecurityConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { HibernateConfiguration.class, LdapConfiguration.class, MailConfiguration.class,
		WebMvcConfiguration.class, WebSecurityConfiguration.class })
@WebAppConfiguration
public class LoginIT {

	@Autowired
	private WebApplicationContext context;

	private MockMvc mvc;
	private MockHttpSession session;

	@Before
	public void setup() throws Exception {
		mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
		RequestBuilder requestBuilder = formLogin().user("junior").password("password");
		MvcResult loginResult = mvc.perform(requestBuilder).andExpect(status().isOk()).andReturn();
		session = (MockHttpSession) loginResult.getRequest().getSession();
	}


	@Test
	public void performWrongLogin() throws Exception{
		RequestBuilder requestBuilder = formLogin().user("junir").password("password");
		mvc.perform(requestBuilder)
		.andDo(print())
		.andExpect(status().isUnauthorized());

		requestBuilder = formLogin().user("junior").password("passwod");
		mvc.perform(requestBuilder)
		.andDo(print())
		.andExpect(status().isUnauthorized());

		mvc
		.perform(get("/api/user").session(session))
		.andExpect(status().isUnauthorized());
	}

	@Test
	public void performCorrectLogin() throws Exception{
		RequestBuilder requestBuilder = formLogin().user("junior").password("password");
		mvc.perform(requestBuilder)
		.andDo(print())
		.andExpect(status().isOk());

		assertFalse(session.isInvalid());

		mvc
		.perform(get("/api/user").session(session))
		.andExpect(status().isOk());
	}
}

@@ -2161,257 +2161,8 @@
 );%0D%0A
-%09%09RequestBuilder requestBuilder = formLogin().user(%22junior%22).password(%22password%22);%0D%0A%09%09MvcResult loginResult = mvc.perform(requestBuilder).andExpect(status().isOk()).andReturn();%0D%0A%09%09session = (MockHttpSession) loginResult.getRequest().getSession();%0D%0A
 %09%7D%0D%0A
@@ -2464,32 +2464,56 @@
 (%22passwod%22);%0D%0A%09%09
+MvcResult loginResult = 
 mvc.perform(requ
@@ -2523,39 +2523,16 @@
 Builder)
-%0D%0A%09%09.andDo(print())%0D%0A%09%09
 .andExpe
@@ -2556,24 +2556,106 @@
 uthorized())
+.andReturn();%0D%0A%09%09session = (MockHttpSession) loginResult.getRequest().getSession()
 ;%0D%0A%0D%0A%09%09mvc%0D%0A
@@ -2663,36 +2663,32 @@
 %09.perform(get(%22/
-api/
 user%22).session(s
@@ -2885,32 +2885,56 @@
 %22password%22);%0D%0A%09%09
+MvcResult loginResult = 
 mvc.perform(requ
@@ -2948,57 +2948,116 @@
 der)
-%0D%0A%09%09.andDo(print())
+.andExpect(status().isOk()).andReturn();
 %0D%0A%09%09
-.andExpect(status().isOk()
+session = (MockHttpSession) loginResult.getRequest().getSession(
 );%0D%0A
@@ -3125,12 +3125,8 @@
 t(%22/
-api/
 user

Fix of Login Integration Test