backend/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java

374 lines
20 KiB
Java
Executable File

package com.plannaplan.controllers;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.junit.jupiter.api.Assertions.assertTrue;
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 java.io.InputStream;
import java.sql.Date;
import com.plannaplan.entities.AppConfig;
import com.plannaplan.entities.User;
import com.plannaplan.services.ConfiguratorService;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.MethodMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class ConfigControllerTest extends AbstractControllerTest {
private static final String FILE_NAME = "Zajecia.xlsx";
private static final String CONFIG_ENDPOINT = "/api/v1/configurator/config";
private static final String COURSE_ENDPOINT = "/api/v1/configurator/config/courses";
private static final String TOURS_ENDPOINT = "/api/v1/configurator/config/tours";
private static final String ADMIN_INIT_ENDPOINT = "/api/v1/configurator/admin/init";
private static final String TOUR_GET_ENDPOINT = "/api/v1/configurator/admin/tour";
private static final String FIRST_TOUR_START = "firstTourBegin";
private static final String FIRST_TOUR_END = "firstTourEnd";
private static final String SECOND_TOUR_START = "secondTourBegin";
private static final String SECOND_TOUR_END = "secondTourEnd";
@Autowired
private UserService service;
@Autowired
private ConfiguratorService configService;
@Test
public void shouldReturnNoAuthorized() throws Exception {
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file)).andExpect(status().is4xxClientError());
}
@Test
public void shouldReturnOKAuthorized() throws Exception {
final String mail = "shouldReturnOKAuthorized@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldInsertedDataBeSameInDatabase() throws Exception {
final String mail = "shouldReturnOKAuthorized@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
final AppConfig config = this.configService.getCurrentConfig();
assertTrue(config.getFirstTourStart().compareTo(Date.valueOf("2020-12-12")) == 0,
"Date in database is wrong. Perhabs database has wrong timezone set.");
assertTrue(config.getFirstTourEnd().compareTo(Date.valueOf("2020-12-14")) == 0,
"Date in database is wrong. Perhabs database has wrong timezone set.");
assertTrue(config.getSecondTourStart().compareTo(Date.valueOf("2020-12-16")) == 0,
"Date in database is wrong. Perhabs database has wrong timezone set.");
assertTrue(config.getSecondTourEnd().compareTo(Date.valueOf("2020-12-20")) == 0,
"Date in database is wrong. Perhabs database has wrong timezone set.");
// yyyy-mm-dd
}
@Test
public void shouldReturnDenyNoAdminAuthorized() throws Exception {
final String mail = "shouldReturnDenyNoAdminAuthorized@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.TEST_USER));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldFailDueToWrongFirstTour() throws Exception {
final String mail = "shouldFailDueToWrongFirstTour@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "12.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldFailDueToWrongSecondTour() throws Exception {
final String mail = "shouldFailDueToWrongSecondTour@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "16.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldFailDueToWrongBothTour() throws Exception {
final String mail = "shouldFailDueToWrongBothTour@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "12.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "16.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldFailDueToWrongTourTransition() throws Exception {
final String mail = "shouldFailDueToWrongTourTransition@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "13.12.2020")
.param(SECOND_TOUR_END, "16.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldFailDueToWrongDateFormat() throws Exception {
final String mail = "shouldFailDueToWrongDateFormat@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "20/12/2020").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldFailDueToNoDate() throws Exception {
final String mail = "shouldFailDueToNoDate@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldFailDueToNoFile() throws Exception {
final String mail = "shouldFailDueToNoFile@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020")
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
@Ignore
@DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
public void shouldCreateAdminBecouseOfEmptyDatabase() throws Exception {
// have no idea how to make this test independent from user that run this
final String ticket = "";
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(get(ADMIN_INIT_ENDPOINT).param("ticket", ticket)).andExpect(status().isOk());
}
@Test
public void shouldFailDueToExistingAdmin() throws Exception {
this.service.save(new User(null, null, "shouldFailDueToExistingAdmin@ConfigController.Test", UserRoles.ADMIN));
final String ticket = "hfewlhfjlewhipfqwehipqwef";
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(get(ADMIN_INIT_ENDPOINT).param("ticket", ticket)).andExpect(status().is4xxClientError());
}
@Test
public void shouldReturnOKAuthorizedForCourses() throws Exception {
final String mail = "shouldReturnOKAuthorizedForCourses@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(COURSE_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldDenyForCoursesDueToWrongRole() throws Exception {
final String mail = "shouldReturnOKAuthorizedForCourses@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.TEST_USER));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(COURSE_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldReturnOKAuthorizedForTours() throws Exception {
final String mail = "shouldReturnOKAuthorizedForTours@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020")
.param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "20.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldFailWithWrongSecondTour() throws Exception {
final String mail = "shouldFailToursWithWrongSecondTour@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020")
.param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "13.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@Test
public void shouldFailWithWrongFirstTour() throws Exception {
final String mail = "shouldFailWithWrongFirstTour@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "10.12.2020")
.param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "20.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@Test
public void shouldFailWithWrongSecondTourBegin() throws Exception {
final String mail = "shouldFailWithWrongSecondTourBegin@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020")
.param(SECOND_TOUR_START, "13.12.2020").param(SECOND_TOUR_END, "20.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@Test
public void shouldDenyForTours() throws Exception {
final String mail = "shouldDenyForTours@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(TOURS_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020").param(FIRST_TOUR_END, "14.12.2020")
.param(SECOND_TOUR_START, "16.12.2020").param(SECOND_TOUR_END, "20.12.2020")
.header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError());
}
@Test
public void shouldReturnOKAuthorizedForGetTours() throws Exception {
final String mail = "shouldReturnOKAuthorizedForGetTours@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(TOUR_GET_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldDenyForGetTours() throws Exception {
final String mail = "shouldDenyForGetTours@ConfigController.test";
final User usr = this.service.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(TOUR_GET_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldDenyWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(TOUR_GET_ENDPOINT)).andExpect(status().is4xxClientError());
}
}