2020-09-24 19:12:04 +02:00
|
|
|
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.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
2020-10-02 16:51:49 +02:00
|
|
|
import com.plannaplan.entities.User;
|
2020-09-25 16:17:52 +02:00
|
|
|
import com.plannaplan.services.UserService;
|
2020-10-02 16:51:49 +02:00
|
|
|
import com.plannaplan.types.UserRoles;
|
2020-09-25 16:17:52 +02:00
|
|
|
|
2020-09-24 19:12:04 +02:00
|
|
|
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.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
|
2020-10-30 16:53:41 +01:00
|
|
|
public class ConfigControllerTest extends AbstractControllerTest {
|
2020-09-24 19:12:04 +02:00
|
|
|
|
|
|
|
private static final String FILE_NAME = "Zajecia.xlsx";
|
|
|
|
private static final String CONFIG_ENDPOINT = "/api/v1/configurator/config";
|
2020-12-09 15:02:06 +01:00
|
|
|
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";
|
2020-09-24 19:12:04 +02:00
|
|
|
|
2020-09-25 16:17:52 +02:00
|
|
|
@Autowired
|
|
|
|
private UserService service;
|
|
|
|
|
2020-09-24 19:12:04 +02:00
|
|
|
@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());
|
|
|
|
|
|
|
|
}
|
2020-09-25 16:17:52 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldReturnOKAuthorized() throws Exception {
|
2020-12-01 17:31:55 +01:00
|
|
|
final String mail = "shouldReturnOKAuthorized@ConfigController.test";
|
2020-12-03 16:57:53 +01:00
|
|
|
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
|
2020-10-02 16:51:49 +02:00
|
|
|
|
2020-09-25 16:17:52 +02:00
|
|
|
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
|
|
|
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
2020-12-07 21:46:47 +01:00
|
|
|
final String token = this.service.login(usr).getToken();
|
2020-09-25 16:17:52 +02:00
|
|
|
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
2020-12-09 15:02:06 +01:00
|
|
|
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))
|
2020-10-05 16:52:52 +02:00
|
|
|
.andExpect(status().isOk());
|
2020-09-25 16:17:52 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:31:55 +01:00
|
|
|
@Test
|
|
|
|
public void shouldReturnDenyNoAdminAuthorized() throws Exception {
|
|
|
|
final String mail = "shouldReturnDenyNoAdminAuthorized@ConfigController.test";
|
2020-12-03 16:57:53 +01:00
|
|
|
final User usr = this.service.save(new User(null, null, mail, UserRoles.TEST_USER));
|
2020-12-01 17:31:55 +01:00
|
|
|
|
|
|
|
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
|
|
|
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
2020-12-07 21:46:47 +01:00
|
|
|
final String token = this.service.login(usr).getToken();
|
2020-12-01 17:31:55 +01:00
|
|
|
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
|
|
|
|
.andExpect(status().is4xxClientError());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:02:06 +01:00
|
|
|
@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());
|
|
|
|
}
|
|
|
|
|
2020-09-24 19:12:04 +02:00
|
|
|
}
|