2020-09-25 16:17:52 +02:00
|
|
|
package com.plannaplan.controllers;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
2020-12-18 17:23:41 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2020-09-25 16:17:52 +02:00
|
|
|
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;
|
2020-12-18 17:23:41 +01:00
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
2020-09-25 16:17:52 +02:00
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
2020-12-18 17:23:41 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
|
|
|
|
|
|
|
import com.plannaplan.entities.Groups;
|
|
|
|
import com.plannaplan.entities.User;
|
|
|
|
import com.plannaplan.services.GroupService;
|
|
|
|
import com.plannaplan.services.UserService;
|
|
|
|
import com.plannaplan.types.UserRoles;
|
|
|
|
import com.plannaplan.types.WeekDay;
|
2020-09-25 16:17:52 +02:00
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
@SpringBootTest
|
|
|
|
@ContextConfiguration
|
|
|
|
|
2020-10-30 16:53:41 +01:00
|
|
|
public class GroupControllerTest extends AbstractControllerTest {
|
2020-11-05 15:27:51 +01:00
|
|
|
private static final String GROUPS_BY_COURSE_ENDPOINT = "/api/v1/groups/course";
|
2020-12-18 17:23:41 +01:00
|
|
|
private static final String UPDATE_GROUPS_ENDPOINT = "/api/v1/groups/521/capacity";
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private UserService service;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private GroupService groupService;
|
2020-09-25 16:17:52 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldFailWithNoParaeter() throws Exception {
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
2020-10-30 17:22:40 +01:00
|
|
|
mockMvc.perform(get(GROUPS_BY_COURSE_ENDPOINT)).andExpect(status().isNotFound());
|
2020-09-25 16:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldReturnGroupsOk() throws Exception {
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
2020-11-05 15:27:51 +01:00
|
|
|
mockMvc.perform(get(GROUPS_BY_COURSE_ENDPOINT + "/2")).andExpect(status().isOk());
|
2020-09-25 16:17:52 +02:00
|
|
|
}
|
2020-12-18 17:23:41 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldFailWithNoToken() throws Exception {
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(put(UPDATE_GROUPS_ENDPOINT)).andExpect(status().is4xxClientError());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldFailWithStudentToken() throws Exception {
|
|
|
|
final User user = this.service
|
|
|
|
.save(new User(null, null, "shouldFailWithStudentToken@GroupController.test", UserRoles.STUDENT));
|
|
|
|
final String token = this.service.login(user).getToken();
|
|
|
|
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(put(UPDATE_GROUPS_ENDPOINT).header("Authorization", "Bearer " + token))
|
|
|
|
.andExpect(status().is4xxClientError());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldFailWithNoParams() throws Exception {
|
|
|
|
final User user = this.service
|
|
|
|
.save(new User(null, null, "shouldFailWithNoParams@GroupController.test", UserRoles.DEANERY));
|
|
|
|
final String token = this.service.login(user).getToken();
|
|
|
|
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(put(UPDATE_GROUPS_ENDPOINT).header("Authorization", "Bearer " + token))
|
|
|
|
.andExpect(status().is4xxClientError());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldFailWithWrongId() throws Exception {
|
|
|
|
final User user = this.service
|
|
|
|
.save(new User(null, null, "shouldFailWithWrongId@GroupController.test", UserRoles.DEANERY));
|
|
|
|
final String token = this.service.login(user).getToken();
|
|
|
|
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(put("/api/v1/groups/" + user.getId() + "/capacity").param("newcapacity", "23")
|
|
|
|
.header("Authorization", "Bearer " + token)).andExpect(status().isNotFound());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void shouldChangeCapacity() throws Exception {
|
|
|
|
final User user = this.service
|
|
|
|
.save(new User(null, null, "shouldChangeCapacity@GroupController.test", UserRoles.DEANERY));
|
|
|
|
final String token = this.service.login(user).getToken();
|
|
|
|
final Groups newGroup = this.groupService.save(new Groups(43, "A-Nigzdie", null, 436, WeekDay.FRIDAY, null));
|
|
|
|
|
|
|
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
|
|
|
mockMvc.perform(put("/api/v1/groups/" + newGroup.getId() + "/capacity").param("newcapacity", "23")
|
|
|
|
.header("Authorization", "Bearer " + token)).andExpect(status().isOk());
|
|
|
|
|
|
|
|
assertTrue(this.groupService.getGroupById(newGroup.getId()).get().getCapacity() == 23);
|
|
|
|
}
|
|
|
|
|
2020-09-25 16:17:52 +02:00
|
|
|
}
|