backend/restservice/src/test/java/com/plannaplan/controllers/UsersControllerTest.java
2020-12-07 21:46:47 +01:00

58 lines
2.5 KiB
Java
Executable File

package com.plannaplan.controllers;
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.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.get;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class UsersControllerTest extends AbstractControllerTest {
private static final String ENDPOINT = "/api/v1/users/student/search";
@Autowired
private UserService service;
@Test
public void shouldRestrun200OK() throws Exception {
final String email = "notexistingassignmentuser@shouldRestrun200OK.test";
final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT).param("query", "").header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
}
@Test
public void shouldRestrunForbiden() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT)).andExpect(status().is4xxClientError());
}
@Test
public void shouldFailedDueToMissingParam() throws Exception {
final String email = "notexistingassignmentuser@shouldFailedDueToMissingParam.test";
final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
}