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

206 lines
8.8 KiB
Java
Executable File

package com.plannaplan.controllers;
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.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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import com.plannaplan.entities.User;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class DeveloperControllerTest extends AbstractControllerTest {
private static final String ACCEPT_ENDPOINT = "/api/v1/developer/algorythm/accept";
private static final String EXCHANGE_ENDPOINT = "/api/v1/developer/algorythm/exchange";
private static final String FIRST_TOUR_ENDPOINT = "/api/v1/developer/tours/first";
private static final String NO_TOUR_ENDPOINT = "/api/v1/developer/tours/no";
private static final String SECOND_TOUR_ENDPOINT = "/api/v1/developer/tours/second";
@Autowired
private UserService userService;
/* ACCEPT TESTS */
@Test
public void shouldFailWithWrongAcces() throws Exception {
final String mail = "shouldFailWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ACCEPT_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkPerformingAcceptAlgotyrhm() throws Exception {
final String mail = "shouldOkPerformingAcceptAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ACCEPT_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldFailWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ACCEPT_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* EXCHANGE TESTS */
@Test
public void shouldFailExchangeWithWrongAcces() throws Exception {
final String mail = "shouldFailExchangeWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkExchangeAlgotyrhm() throws Exception {
final String mail = "shouldOkExchangeAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldFailExchaneWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(EXCHANGE_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* FIRST TOUR */
@Test
public void shouldFailFirstTourWithWrongAcces() throws Exception {
final String mail = "shouldFailFirstTourWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(FIRST_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkFirstTourAlgotyrhm() throws Exception {
final String mail = "shouldOkFirstTourAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(FIRST_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFirstTourWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(FIRST_TOUR_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* SECOND TOUR */
@Test
public void shouldFailSecondTourWithWrongAcces() throws Exception {
final String mail = "shouldFailSecondTourWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(SECOND_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkSecondTourAlgotyrhm() throws Exception {
final String mail = "shouldOkSecondTourAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(SECOND_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldFailSecondTourWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(SECOND_TOUR_ENDPOINT)).andExpect(status().is4xxClientError());
}
/* NO TOUR */
@Test
public void shouldNoTourWithWrongAcces() throws Exception {
final String mail = "shouldNoTourWithWrongAcces@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.TEST_USER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(NO_TOUR_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldOkNoAlgotyrhm() throws Exception {
final String mail = "shouldOkNoAlgotyrhm@DeveloperController.test";
final User usr = this.userService.save(new User(null, null, mail, UserRoles.DEVELOPER));
final String token = this.userService.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(NO_TOUR_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldFailNoTourWithNoToken() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(NO_TOUR_ENDPOINT)).andExpect(status().is4xxClientError());
}
}