package com.github.timtebeek.rst.controller;

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 com.github.timtebeek.rst.MyApp;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.security.test.context.support.WithMockUser;
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;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApp.class)
@WebAppConfiguration
public class MyControllerTest {
	@Autowired
	private WebApplicationContext webapp;

	private MockMvc	mvc;

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

	@Test
	@WithMockUser(roles = "myrole")
	public void testHelloWithRole() throws Exception {
		mvc.perform(get("/hello")).andDo(print()).andExpect(status().isOk());
	}

	@Test
	@WithMockUser
	public void testHelloWihtoutRole() throws Exception {
		mvc.perform(get("/hello")).andDo(print()).andExpect(status().isOk());
	}
}

@@ -39,16 +39,248 @@
 oller;%0A%0A
+import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;%0Aimport static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;%0A
 import s
@@ -380,16 +380,16 @@
 curity;%0A
-
 import s
@@ -887,32 +887,168 @@
 nConfiguration;%0A
+import org.springframework.security.authentication.TestingAuthenticationToken;%0Aimport org.springframework.security.core.Authentication;%0A
 import org.sprin
@@ -1638,17 +1638,17 @@
  MockMvc
-%09
+ 
 mvc;%0A%0A%09@
@@ -1828,16 +1828,26 @@
 estHello
+Annotation
 WithRole
@@ -1992,15 +1992,174 @@
 ello
-Wihtout
+AnnotationWithoutRole() throws Exception %7B%0A%09%09mvc.perform(get(%22/hello%22)).andDo(print()).andExpect(status().isOk());%0A%09%7D%0A%0A%09@Test%0A%09public void testHelloAuthenticationWith
 Role
@@ -2199,32 +2199,70 @@
 rm(get(%22/hello%22)
+.with(authentication(token(%22myrole%22)))
 ).andDo(print())
@@ -2292,11 +2292,481 @@
 k());%0A%09%7D
+%0A%0A%09@Test%0A%09public void testHelloAuthenticationWithoutRole() throws Exception %7B%0A%09%09mvc.perform(get(%22/hello%22).with(authentication(token(%22unrelatedrole%22)))).andDo(print()).andExpect(status().isOk());%0A%09%7D%0A%0A%09@Test%0A%09public void testHelloUser() throws Exception %7B%0A%09%09mvc.perform(get(%22/hello%22).with(user(%22user%22))).andDo(print()).andExpect(status().isOk());%0A%09%7D%0A%0A%09private static Authentication token(final String role) %7B%0A%09%09return new TestingAuthenticationToken(%22user%22, null, role);%0A%09%7D
 %0A%7D%0A

Add a few more ways of testing