From 4b77103aa10a0b11345d8c3c6e4b77a48741d6da Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sat, 26 Nov 2016 23:12:21 +0700 Subject: test adapted for spring security --- .../java/com/juick/api/tests/MessagesTests.java | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'juick-api/src/test/java/com') diff --git a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java index fd233a03..788582a0 100644 --- a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java +++ b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java @@ -18,6 +18,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Primary; import org.springframework.http.MediaType; +import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -31,10 +32,9 @@ import java.util.Collections; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.mockito.Mockito.when; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; /** * Created by vitalyster on 25.11.2016. @@ -44,13 +44,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @WebAppConfiguration public class MessagesTests { @Configuration - @Import(value={ApiMvcConfiguration.class, ApiAppConfiguration.class, ApiSecurityConfig.class, DataConfiguration.class}) + @Import(value = {ApiMvcConfiguration.class, ApiAppConfiguration.class, ApiSecurityConfig.class, DataConfiguration.class}) static class Config { @Bean @Primary MessagesService messagesService() { return Mockito.mock(MessagesService.class); } + @Bean @Primary UserService userService() { @@ -70,33 +71,41 @@ public class MessagesTests { @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) + .apply(SecurityMockMvcConfigurers.springSecurity()) .build(); } @Test public void messageTests() throws Exception { String ugnichName = "ugnich"; + String uginchPassword = "MyPassw0rd!"; String msgText = "Привет, я - Угнич"; + User user = new User(); user.setName(ugnichName); user.setUid(1); + user.setCredentials(uginchPassword); + user.setBanned(false); + Message msg = new Message(); msg.setMid(1); msg.setUser(user); msg.setText(msgText); msg.setTags(Collections.singletonList(new Tag("yo"))); + mockMvc.perform(get("/home")) .andExpect(status().is4xxClientError()); - when(userService.getUserByName(null)) + when(userService.getUserByName(ugnichName)) + .thenReturn(user); + when(userService.getFullyUserByName(ugnichName)) .thenReturn(user); - when(messagesService.getMyFeed(1, 0)) .thenReturn(Collections.singletonList(1)); when(messagesService.getMessages(Collections.singletonList(1))) .thenReturn(Collections.singletonList(msg)); - mockMvc.perform(get("/home")) + mockMvc.perform(get("/home").with(httpBasic(ugnichName, uginchPassword))) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$", hasSize(1))) -- cgit v1.2.3