backend/restservice/src/test/java/com/plannaplan/controllers/UsersControllerTest.java
Marcin Woźniak 817350e85e
Added UserRepositoryTest and users/admin users/deanery
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
2020-12-27 13:54:33 +01:00

210 lines
11 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.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.junit.Assert.assertTrue;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Optional;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class UsersControllerTest extends AbstractControllerTest {
private static final String SEARCH_ENDPOINT = "/api/v1/users/student/search";
private static final String ALL_USERS_ENDPOINT = "/api/v1/users/students";
private static final String ADD_ADMIN_ENDPOINT = "/api/v1/users/admin";
private static final String ADD_DEANERY_ENDPOINT = "/api/v1/users/deanery";
@Autowired
private UserService service;
/* SEARCH_ENDPOINT */
@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(SEARCH_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(SEARCH_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(SEARCH_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
/* ALL_USERS_ENDPOINT */
@Test
public void shouldRestrunAllStudents200OK() throws Exception {
final String email = "notexistingassignmentuser@shouldRestrunAllStudents200OK.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(ALL_USERS_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().isOk());
}
@Test
public void shouldRestrunAllStudentsForbiden() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ALL_USERS_ENDPOINT)).andExpect(status().is4xxClientError());
}
@Test
public void shouldDenyAllStudentsTryByStudent() throws Exception {
final String email = "notexistingassignmentuser@shouldDenyAllStudentsTryByStudent.test";
final User user = this.service.save(new User(null, null, email, UserRoles.STUDENT));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ALL_USERS_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldDenyNewAdminWithWrongRole() throws Exception {
final String email = "shouldDenyNewAdminWithWrongRole@shouldDenyAllStudentsTryByStudent.test";
final User user = this.service.save(new User(null, null, email, UserRoles.TEST_USER));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_ADMIN_ENDPOINT).param("authority","45611").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldDenyNewAdminWithNoParams() throws Exception {
final String email = "shouldDenyNewAdminWithNoParams@shouldDenyAllStudentsTryByStudent.test";
final User user = this.service.save(new User(null, null, email, UserRoles.ADMIN));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_ADMIN_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldCreateNewAdmin() throws Exception {
final String email = "shouldCreateNewAdmin@shouldDenyAllStudentsTryByStudent.test";
final String usosId = "121321";
final User user = this.service.save(new User(null, null, email, UserRoles.ADMIN));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_ADMIN_ENDPOINT).param("authority",usosId).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
final Optional<User> reponse = this.service.getByAuthority(usosId);
assertTrue(reponse.get().getUsosId().equals(usosId));
}
@Test
public void shouldChangeExistingUserIntoAdmin() throws Exception {
final String email = "shouldChangeExistingUserIntoAdmin@shouldDenyAllStudentsTryByStudent.test";
final String email2 = "shouldChangeExistingUserIntoAdmin2@shouldDenyAllStudentsTryByStudent.test";
final String usosId = "121327";
final User user = this.service.save(new User(null, null, email, UserRoles.ADMIN));
this.service.save(new User(null, null, email2, usosId, UserRoles.TEST_USER));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_ADMIN_ENDPOINT).param("authority",usosId).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
final Optional<User> reponse = this.service.getByAuthority(usosId);
assertTrue(reponse.get().getUsosId().equals(usosId));
assertTrue(reponse.get().getRole().equals(UserRoles.ADMIN));
}
@Test
public void shouldDenyNewDeaneryWithWrongRole() throws Exception {
final String email = "shouldDenyNewDeaneryWithWrongRole@shouldDenyAllStudentsTryByStudent.test";
final User user = this.service.save(new User(null, null, email, UserRoles.TEST_USER));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_DEANERY_ENDPOINT).param("authority","45611").header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldDenyNewDeaneryWithNoParams() throws Exception {
final String email = "shouldDenyNewDeaneryWithNoParams@shouldDenyAllStudentsTryByStudent.test";
final User user = this.service.save(new User(null, null, email, UserRoles.ADMIN));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_DEANERY_ENDPOINT).header("Authorization", "Bearer " + token))
.andExpect(status().is4xxClientError());
}
@Test
public void shouldCreateNewDeanery() throws Exception {
final String email = "shouldCreateNewDeanery@shouldDenyAllStudentsTryByStudent.test";
final String usosId = "121322";
final User user = this.service.save(new User(null, null, email, UserRoles.ADMIN));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_DEANERY_ENDPOINT).param("authority",usosId).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
final Optional<User> reponse = this.service.getByAuthority(usosId);
assertTrue(reponse.get().getUsosId().equals(usosId));
}
@Test
public void shouldChangeExistingUserIntoDeanery() throws Exception {
final String email = "shouldChangeExistingUserIntoDeanery@shouldDenyAllStudentsTryByStudent.test";
final String email2 = "shouldChangeExistingUserIntoDeanery2@shouldDenyAllStudentsTryByStudent.test";
final String usosId = "121328";
final User user = this.service.save(new User(null, null, email, UserRoles.ADMIN));
this.service.save(new User(null, null, email2, usosId, UserRoles.TEST_USER));
final String token = this.service.login(user).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_DEANERY_ENDPOINT).param("authority",usosId).header("Authorization", "Bearer " + token))
.andExpect(status().isOk());
final Optional<User> reponse = this.service.getByAuthority(usosId);
assertTrue(reponse.get().getUsosId().equals(usosId));
assertTrue(reponse.get().getRole().equals(UserRoles.DEANERY));
}
}