backend/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java
2020-10-30 16:53:41 +01:00

62 lines
2.5 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.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import java.io.InputStream;
import com.plannaplan.entities.User;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
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
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 TEST_MAIL = "notexisting@mail.domain";
@Autowired
private UserService service;
@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 User usr = new User(null, null, TEST_MAIL, UserRoles.ADMIN);
this.service.save(usr);
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(TEST_MAIL);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
}