2020-10-19 12:33:54 +02:00
|
|
|
package com.plannaplan.controllers;
|
|
|
|
|
2020-10-24 17:28:10 +02:00
|
|
|
import com.plannaplan.entities.User;
|
|
|
|
import com.plannaplan.services.UserService;
|
|
|
|
import com.plannaplan.types.UserRoles;
|
2020-10-19 12:33:54 +02:00
|
|
|
|
|
|
|
import org.junit.Test;
|
2020-10-24 17:28:10 +02:00
|
|
|
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;
|
2020-10-19 12:33:54 +02:00
|
|
|
|
2020-10-24 17:28:10 +02:00
|
|
|
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
|
2020-10-19 12:33:54 +02:00
|
|
|
public class UsersControllerTest {
|
2020-11-05 14:32:42 +01:00
|
|
|
private static final String ENDPOINT = "/api/v1/users/student/search";
|
2020-10-24 17:28:10 +02:00
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private WebApplicationContext webApplicationContext;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private UserService service;
|
|
|
|
|
2020-10-19 12:33:54 +02:00
|
|
|
@Test
|
2020-10-24 17:28:10 +02:00
|
|
|
public void shouldRestrun200OK() throws Exception {
|
|
|
|
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());
|
2020-10-19 12:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-24 17:28:10 +02:00
|
|
|
public void shouldRestrunForbiden() throws Exception {
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(get(ENDPOINT)).andExpect(status().is4xxClientError());
|
2020-10-19 12:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-10-24 17:28:10 +02:00
|
|
|
public void shouldFailedDueToMissingParam() throws Exception {
|
|
|
|
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());
|
2020-10-19 12:33:54 +02:00
|
|
|
}
|
|
|
|
}
|