User Controller Tests

This commit is contained in:
Filip Izydorczyk 2020-10-24 17:28:10 +02:00
parent 7d57157078
commit 6384f3d9b9

View File

@ -1,22 +1,60 @@
package com.plannaplan.controllers; package com.plannaplan.controllers;
import static org.junit.jupiter.api.Assertions.assertTrue; import com.plannaplan.entities.User;
import com.plannaplan.services.UserService;
import com.plannaplan.types.UserRoles;
import org.junit.Test; 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 org.springframework.web.context.WebApplicationContext;
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 { public class UsersControllerTest {
private static final String ENDPOINT = "/api/v1/users/searchForStudents";
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private UserService service;
@Test @Test
public void shouldRestrun200OK() { public void shouldRestrun200OK() throws Exception {
assertTrue(false); final String email = "notexistingassignmentuser@shouldRestrun200OK.test";
this.service.save(new User(null, null, email, UserRoles.DEANERY));
final String token = this.service.login(email);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT).param("query", "").header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
} }
@Test @Test
public void shouldRestrunForbiden() { public void shouldRestrunForbiden() throws Exception {
assertTrue(false); MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT)).andExpect(status().is4xxClientError());
} }
@Test @Test
public void shouldFailedDueToMissingParam() { public void shouldFailedDueToMissingParam() throws Exception {
assertTrue(false); final String email = "notexistingassignmentuser@shouldFailedDueToMissingParam.test";
this.service.save(new User(null, null, email, UserRoles.DEANERY));
final String token = this.service.login(email);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
} }
} }