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

70 lines
2.8 KiB
Java
Raw Normal View History

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-09-25 16:17:52 +02:00
import com.plannaplan.services.UserService;
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;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class ConfigControllerTest {
private static final String FILE_NAME = "Zajecia.xlsx";
private static final String CONFIG_ENDPOINT = "/api/v1/configurator/config";
@Autowired
private WebApplicationContext webApplicationContext;
2020-09-25 16:17:52 +02:00
@Autowired
private UserService service;
2020-09-24 19:12:04 +02:00
@Test
public void shouldReturnOK() throws Exception {
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file)).andExpect(status().isOk());
}
@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 {
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login("notexisting@mail.comain");
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Bearer ", token))
.andExpect(status().is4xxClientError());
}
2020-09-24 19:12:04 +02:00
}