Merge pull request 'scurity-roles' (#13) from scurity-roles into master

Reviewed-on: http://git.plannaplan.pl/filipizydorczyk/backend/pulls/13
LGMT
This commit is contained in:
2020-10-24 18:01:29 +02:00
18 changed files with 317 additions and 26 deletions

View File

@ -37,16 +37,6 @@ public class ConfigControllerTest {
@Autowired
private UserService service;
@Test
public void shouldReturnOK() throws Exception {
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file)).andExpect(status().isOk());
}
@Test
public void shouldReturnNoAuthorized() throws Exception {
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
@ -59,7 +49,7 @@ public class ConfigControllerTest {
@Test
public void shouldReturnOKAuthorized() throws Exception {
final User usr = new User(null, null, TEST_MAIL, UserRoles.TEST_USER);
final User usr = new User(null, null, TEST_MAIL, UserRoles.ADMIN);
this.service.save(usr);
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);

View File

@ -0,0 +1,60 @@
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 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 {
private static final String ENDPOINT = "/api/v1/users/searchForStudents";
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private UserService service;
@Test
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());
}
@Test
public void shouldRestrunForbiden() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT)).andExpect(status().is4xxClientError());
}
@Test
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());
}
}

View File

@ -0,0 +1,32 @@
package com.plannaplan.responses.mappers;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.List;
import com.plannaplan.entities.User;
import com.plannaplan.types.UserRoles;
import com.plannaplan.responses.models.SearchForStudentsResponse;
import org.junit.Test;
public class UserResponseMappersTest {
private static final String F_NAME = "Tytus";
private static final String F_SURNAMENAME = "Romek";
private static final String F_EMAIL = "i@Atomek.pl";
private static final String S_NAME = "Ed";
private static final String S_SURNAMENAME = "Edd";
private static final String S_EMAIL = "i@Eddy.pl";
@Test
public void shlouldMapListToResponseList() {
final List<User> users = Arrays.asList(new User(F_NAME, F_SURNAMENAME, F_EMAIL, UserRoles.TEST_USER),
new User(S_NAME, S_SURNAMENAME, S_EMAIL, UserRoles.TEST_USER));
final List<SearchForStudentsResponse> resposne = UserResponseMappers.mapToDefaultResponse(users);
assertTrue(resposne.size() == 2);
assertTrue(resposne.get(0) instanceof SearchForStudentsResponse);
}
}

View File

@ -0,0 +1,23 @@
package com.plannaplan.responses.models;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.plannaplan.entities.User;
import com.plannaplan.types.UserRoles;
import org.junit.Test;
public class SearchForStudentsResponseTest {
private static final String NAME = "Tytus";
private static final String SURNAMENAME = "Romek";
private static final String EMAIL = "i@Atomek.pl";
@Test
public void shouldMapUserToResponseElement() {
final SearchForStudentsResponse response = new SearchForStudentsResponse(
new User(NAME, SURNAMENAME, EMAIL, UserRoles.TEST_USER));
assertTrue(response.getName().equals(NAME));
assertTrue(response.getSurname().equals(SURNAMENAME));
assertTrue(response.getEmail().equals(EMAIL));
}
}