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;
|
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-30 16:53:41 +01:00
|
|
|
public class UsersControllerTest extends AbstractControllerTest {
|
2020-11-05 15:27:51 +01:00
|
|
|
private static final String ENDPOINT = "/api/v1/users/student/search";
|
2020-10-24 17:28:10 +02:00
|
|
|
|
|
|
|
@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";
|
2020-12-03 16:57:53 +01:00
|
|
|
final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY));
|
2020-12-07 21:46:47 +01:00
|
|
|
final String token = this.service.login(user).getToken();
|
2020-10-24 17:28:10 +02:00
|
|
|
|
|
|
|
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";
|
2020-12-03 16:57:53 +01:00
|
|
|
final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY));
|
2020-12-07 21:46:47 +01:00
|
|
|
final String token = this.service.login(user).getToken();
|
2020-10-24 17:28:10 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|