package org.cirmmp.spring.security;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

import org.cirmmp.spring.configuration.AppConfig;

import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.ResultActions;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
		classes={ AppConfig.class }
)
@WebAppConfiguration
public class SecurityConfigurationTest {


	@Autowired
	private WebApplicationContext context;

	private MockMvc mvc;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

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

	@After
	public void tearDown() throws Exception {
	}


	@Test
	public void testCanGetLoginForm() throws Exception {
		this.mvc.perform(get("/login")).andExpect(MockMvcResultMatchers.status().isOk());
	}


	@Test
	public void testBadCsrf() throws Exception {
		this.mvc
		.perform(post("/login").with(csrf().useInvalidToken()))
		.andExpect(MockMvcResultMatchers.status().is(403));
	}

	@Test
	public void testNotLoggedInGet() throws Exception {
		this.mvc
		.perform(get("/newuser"))
		.andExpect(redirectedUrl("http://localhost/login"));
	}
	@Test
	public void testNotLoggedInPost() throws Exception {
		this.mvc
		.perform(post("/newuser").with(csrf()))
		.andExpect(redirectedUrl("http://localhost/login")); 
			}

	@Test
	public void testNotFound() throws Exception {
		this.mvc.perform(get("/nonesuch")).andExpect(MockMvcResultMatchers.status().is(404));
	}

    // TODO with dummy userid, check can log in
	// check non-admin user cannot use admin functionality
}

@@ -2183,32 +2183,55 @@
 s Exception %7B%0A%09%09
+ResultActions result = 
 this.mvc%0A%09%09.perf
@@ -2276,27 +2276,95 @@
 lidToken()))
+;
 %0A%09%09
+result.andExpect(redirectedUrl(%22/login?error%22 ));%0A%09%09// could result
 .andExpect(M
@@ -2889,60 +2889,359 @@
 %0A%09%7D%0A
-%0A    // TODO with dummy userid, check can
+%09%0A%09@Test %0A%09public void testLoginNonesuch() throws Exception %7B%0A%09%09//.loginProcessingUrl(%22/login%22).usernameParameter(%22ssoId%22).passwordParameter(%22password%22)%0A%09%09this.mvc%0A%09%09.perform( post(%22/login%22).param(%22ssoId%22, %22nonesuch%22).param(%22password%22, %22%22).with(csrf()) )%0A%09%09.andExpect(redirectedUrl(%22/login?error%22)); %0A%09%09%09%0A%09%7D%0A%0A    // TODO test successful
  log
- 
 in%0A%09//
+ TODO
  che

test failed login