Commit 44c50dc5 authored by Sascha Herzinger's avatar Sascha Herzinger
Browse files

First working integration test for api

parent b0e00e01
Pipeline #10846 failed with stage
in 62 minutes and 30 seconds
......@@ -105,8 +105,11 @@
<reflections.version>0.9.11</reflections.version>
<osgi.version>1.0.0</osgi.version>
<junit.version>4.12</junit.version>
<hamcrest.version>2.1</hamcrest.version>
<surefire-api.version>2.22.2</surefire-api.version>
<surefire.rerunFailingTestsCount>4</surefire.rerunFailingTestsCount>
......
......@@ -238,6 +238,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
......@@ -94,6 +94,7 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
.cors()
.and()
.logout()
.logoutUrl("/api/doLogout")
.deleteCookies(lcsb.mapviewer.common.Configuration.AUTH_TOKEN);
}
......
......@@ -10,7 +10,7 @@ import org.springframework.web.servlet.config.annotation.*;
@EnableWebMvc
@Import(SpringRestApiConfig.class)
@ComponentScan(basePackages = {"lcsb.mapviewer.web"})
public class SpringWebConfig extends WebMvcConfigurerAdapter {
public class SpringWebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
......
package lcsb.mapviewer.web;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.web.config.SpringWebConfig;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
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.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.HashMap;
import java.util.Map;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringWebConfig.class})
@WebAppConfiguration
public class ProjectControllerIntegrationTest {
@ContextConfiguration(classes = SpringWebConfig.class)
public class SpringSecurityGeneralIntegrationTest {
private final static MediaType JSON = MediaType.APPLICATION_JSON_UTF8;
private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
......@@ -34,20 +33,43 @@ public class ProjectControllerIntegrationTest {
@Autowired
private IUserService userService;
private MockMvc mockMvc;
@Autowired
private PasswordEncoder passwordEncoder;
private User user = new User();
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.apply(springSecurity())
.build();
user.setLogin("test_user");
user.setCryptedPassword(passwordEncoder.encode("test_pass"));
userService.addUser(user);
}
@After
public void teardown() {
userService.deleteUser(user);
}
@Test
public void loginWorks() throws Exception {
Map<String, String> params = new HashMap<>();
params.put("login", "test_user");
params.put("password", "test_pass");
String body = new JSONObject(params).toJSONString();
mockMvc.perform(post("/api/doLogin").contentType(JSON).content(body))
public void login2xxWithValidCredentials() throws Exception {
RequestBuilder request = post("/api/doLogin")
.param("login", "test_user")
.param("password", "test_pass");
mockMvc.perform(request)
.andExpect(status().isOk());
}
@Test
public void login4xxWithInvalidCredentials() throws Exception {
RequestBuilder request = post("/api/doLogin")
.param("login", "test_user")
.param("password", "test_foo");
mockMvc.perform(request)
.andExpect(status().is4xxClientError());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment