Merge pull request 'token-package' (#28) from token-package into master
Reviewed-on: http://git.plannaplan.pl/filipizydorczyk/backend/pulls/28
This commit is contained in:
commit
acf80cc78d
@ -14,7 +14,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* Service of GroupService which can find(optional), get(By Course, Groups Ammount, Group By Id, find Not Existing Group), save, delete group.
|
||||
* Service of GroupService which can find(optional), get(By Course, Groups
|
||||
* Ammount, Group By Id, find Not Existing Group), save, delete group.
|
||||
*/
|
||||
|
||||
@Service
|
||||
@ -68,6 +69,10 @@ public class GroupService {
|
||||
public HashMap<Long, Integer> getTakenPlaces(List<Groups> groups) {
|
||||
HashMap<Long, Integer> response = new HashMap<>();
|
||||
|
||||
if (groups.size() == 0) {
|
||||
return response;
|
||||
}
|
||||
|
||||
List<Object[]> respoonses = this.repo
|
||||
.getAssignedAmounts(groups.stream().filter(Objects::nonNull).map(new Function<Groups, Long>() {
|
||||
@Override
|
||||
|
@ -13,7 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* Service of UserService which can get(By Email), login, save user.
|
||||
* Service of UserService which can get(By Email), login, save user.
|
||||
*/
|
||||
@Service
|
||||
public class UserService {
|
||||
@ -26,37 +26,33 @@ public class UserService {
|
||||
|
||||
public User checkForUser(String email, String usosId) {
|
||||
if (usosId == null) {
|
||||
Optional <User> user = this.repo.getByAuthority(email.replace("\n", "").trim());
|
||||
if (user.isPresent()){
|
||||
Optional<User> user = this.repo.getByAuthority(email.replace("\n", "").trim());
|
||||
if (user.isPresent()) {
|
||||
return user.get();
|
||||
}
|
||||
else {
|
||||
final User newUser = new User(null,null,email.replace("\n", "").trim(),UserRoles.STUDENT);
|
||||
} else {
|
||||
final User newUser = new User(null, null, email.replace("\n", "").trim(), UserRoles.STUDENT);
|
||||
return this.repo.save(newUser);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Optional <User> user = this.repo.getByUsosId(usosId.replace("\n", "").trim());
|
||||
if (user.isPresent()){
|
||||
} else {
|
||||
Optional<User> user = this.repo.getByUsosId(usosId.replace("\n", "").trim());
|
||||
if (user.isPresent()) {
|
||||
return user.get();
|
||||
}
|
||||
else {
|
||||
final User newUser = new User(null,null,email.replace("\n", "").trim(),usosId,UserRoles.STUDENT);
|
||||
} else {
|
||||
final User newUser = new User(null, null, email.replace("\n", "").trim(), usosId, UserRoles.STUDENT);
|
||||
return this.repo.save(newUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String login(User authority) throws UserNotFoundException {
|
||||
public User login(User authority) throws UserNotFoundException {
|
||||
final String token = UUID.randomUUID().toString();
|
||||
try{
|
||||
try {
|
||||
authority.setToken(token);
|
||||
this.repo.save(authority);
|
||||
}
|
||||
catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
throw new UserNotFoundException(e.getMessage());
|
||||
}
|
||||
return token;
|
||||
return authority;
|
||||
}
|
||||
|
||||
public User save(User user) {
|
||||
|
@ -32,11 +32,12 @@ public class UserServiceTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnToken() {
|
||||
final User testUser = this.userService.save(new User(TEST_USER_NAME, TEST_USER_SUERNAME, TEST_USER_MAIL, UserRoles.TEST_USER));
|
||||
User testUser = this.userService
|
||||
.save(new User(TEST_USER_NAME, TEST_USER_SUERNAME, TEST_USER_MAIL, UserRoles.TEST_USER));
|
||||
try {
|
||||
final String token = this.userService.login(testUser);
|
||||
System.out.println("Returned token: " + token);
|
||||
assertTrue(token != null);
|
||||
testUser = this.userService.login(testUser);
|
||||
System.out.println("Returned token: " + testUser.getToken());
|
||||
assertTrue(testUser.getToken() != null);
|
||||
assertTrue(this.userService.getUserByEmail(TEST_USER_MAIL).getToken() != null);
|
||||
} catch (UserNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
@ -84,17 +85,17 @@ public class UserServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateUser(){
|
||||
|
||||
public void shouldCreateUser() {
|
||||
|
||||
final User user = this.userService.checkForUser("shouldCreateUser@UserService.test", null);
|
||||
|
||||
|
||||
assertTrue(user.getId() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnExistingUser(){
|
||||
public void shouldReturnExistingUser() {
|
||||
final String email = "shouldReturnExistingUser@UserService.test";
|
||||
this.userService.save(new User("Tom","Smieszne",email,UserRoles.TEST_USER));
|
||||
this.userService.save(new User("Tom", "Smieszne", email, UserRoles.TEST_USER));
|
||||
final User user = this.userService.checkForUser(email, null);
|
||||
|
||||
assertTrue(user.getName() != "Tom");
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.plannaplan.controllers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.plannaplan.App;
|
||||
@ -7,7 +9,10 @@ import com.plannaplan.entities.Course;
|
||||
import com.plannaplan.responses.mappers.CoursesResponseMappers;
|
||||
import com.plannaplan.responses.models.CoursesDefaultResponse;
|
||||
import com.plannaplan.responses.models.CoursesWithGroupsResponse;
|
||||
import com.plannaplan.responses.models.GroupDefaultResponse;
|
||||
import com.plannaplan.services.CourseService;
|
||||
import com.plannaplan.services.GroupService;
|
||||
import com.plannaplan.types.GroupType;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -33,12 +38,40 @@ public class CoursesController {
|
||||
@Autowired
|
||||
private CourseService courseService;
|
||||
|
||||
@Autowired
|
||||
private GroupService groupService;
|
||||
|
||||
@GetMapping("/all")
|
||||
@ApiOperation(value = "Return all courses")
|
||||
public ResponseEntity<List<? extends CoursesResponse>> getMethodName(
|
||||
@RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if you want to have resopnse with associated groups or without") Boolean groups) {
|
||||
@RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if you want to have resopnse with associated groups or without") Boolean groups,
|
||||
@RequestParam(name = "takenPlaces", defaultValue = "false") @ApiParam(value = "Boolean if we want to have respoonse with information about taken places by other students. Needs to be set groups true first") Boolean takenPlaces) {
|
||||
List<Course> courses = this.courseService.getAllCourses();
|
||||
if (groups) {
|
||||
if (takenPlaces) {
|
||||
|
||||
final List<CoursesWithGroupsResponse> response = new ArrayList<>();
|
||||
|
||||
courses.forEach(course -> {
|
||||
final List<GroupDefaultResponse> lectures = new ArrayList<>();
|
||||
final List<GroupDefaultResponse> classes = new ArrayList<>();
|
||||
|
||||
final HashMap<Long, Integer> ammounts = this.groupService.getTakenPlaces(course.getGroups());
|
||||
|
||||
course.getGroups().stream().forEach(group -> {
|
||||
if (group.getType() == GroupType.CLASS) {
|
||||
classes.add(new GroupDefaultResponse(group, ammounts.get(group.getId())));
|
||||
} else {
|
||||
lectures.add(new GroupDefaultResponse(group, ammounts.get(group.getId())));
|
||||
}
|
||||
});
|
||||
|
||||
response.add(new CoursesWithGroupsResponse(course, lectures, classes));
|
||||
});
|
||||
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
|
||||
final List<CoursesWithGroupsResponse> response = CoursesResponseMappers
|
||||
.mapToGetCoursesWithGroupsResponse(courses);
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
|
@ -2,6 +2,7 @@ package com.plannaplan.controllers;
|
||||
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.exceptions.UserNotFoundException;
|
||||
import com.plannaplan.responses.models.TokenResponse;
|
||||
import com.plannaplan.security.cas.CasUserIdentity;
|
||||
import com.plannaplan.security.cas.CasValidationExcepiton;
|
||||
import com.plannaplan.security.cas.CasValidator;
|
||||
@ -38,23 +39,25 @@ public class TokenController {
|
||||
|
||||
@GetMapping("/token")
|
||||
@ApiOperation(value = "Endpoint to access token required to call secured endpoints. In order to access token we need to provide access token comming from unviersity CAS system")
|
||||
public ResponseEntity<String> getToken(
|
||||
public ResponseEntity<TokenResponse> getToken(
|
||||
@RequestParam("ticket") @ApiParam(value = "Ticket get from CAS system. It should look like ST-1376572-wo41gty5R0JCZFKMMie2-cas.amu.edu.psl") final String ticket) {
|
||||
final CasValidator validator = isDev ? new DefaultUAMCasValidator(serviceUrl, ticket) : new CustomUAMCasValidator(serviceUrl, ticket);
|
||||
final CasValidator validator = isDev ? new DefaultUAMCasValidator(serviceUrl, ticket)
|
||||
: new CustomUAMCasValidator(serviceUrl, ticket);
|
||||
|
||||
try {
|
||||
final CasUserIdentity casUserIdentity = validator.validate();
|
||||
final String usosId = casUserIdentity.getUsosId();
|
||||
final String authority = casUserIdentity.getEmail();
|
||||
final User user = this.userService.checkForUser(authority, usosId);
|
||||
String token = this.userService.login(user);
|
||||
return new ResponseEntity<>(token, HttpStatus.OK);
|
||||
User user = this.userService.checkForUser(authority, usosId);
|
||||
user = this.userService.login(user);
|
||||
|
||||
return new ResponseEntity<>(new TokenResponse(user), HttpStatus.OK);
|
||||
} catch (CasValidationExcepiton e) {
|
||||
return new ResponseEntity<>("Wrong ticket", HttpStatus.UNAUTHORIZED);
|
||||
return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
|
||||
} catch (UserNotFoundException e) {
|
||||
return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND);
|
||||
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -36,10 +36,19 @@ public class UsersController {
|
||||
@GetMapping("/student/search")
|
||||
@PreAuthorize("hasRole('ROLE_DEANERY')")
|
||||
@ApiOperation(value = "Serch for user by providing query. If query is empty it will return all students. You need token with DEANERY role to call this")
|
||||
public ResponseEntity<List<UserResponse>> configApp(
|
||||
public ResponseEntity<List<UserResponse>> searchForStudent(
|
||||
@RequestParam("query") @ApiParam(value = "Query to filter all students. If empty will match everyone") String query) {
|
||||
final List<User> searches = this.userService.searchForStudents(query);
|
||||
final List<UserResponse> response = UserResponseMappers.mapToDefaultResponse(searches);
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/students")
|
||||
@PreAuthorize("hasRole('ROLE_DEANERY')")
|
||||
@ApiOperation(value = "Gets all students. You need token with DEANERY role to call this")
|
||||
public ResponseEntity<List<UserResponse>> getAllStudents() {
|
||||
final List<User> searches = this.userService.searchForStudents("");
|
||||
final List<UserResponse> response = UserResponseMappers.mapToDefaultResponse(searches);
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,13 @@ public class CoursesWithGroupsResponse extends CoursesResponse {
|
||||
});
|
||||
}
|
||||
|
||||
public CoursesWithGroupsResponse(Course course, List<GroupDefaultResponse> lectures,
|
||||
List<GroupDefaultResponse> classes) {
|
||||
super(course);
|
||||
this.lectures = lectures;
|
||||
this.classes = classes;
|
||||
}
|
||||
|
||||
public List<GroupDefaultResponse> getClasses() {
|
||||
return this.classes;
|
||||
}
|
||||
|
42
restservice/src/main/java/com/plannaplan/responses/models/TokenResponse.java
Executable file
42
restservice/src/main/java/com/plannaplan/responses/models/TokenResponse.java
Executable file
@ -0,0 +1,42 @@
|
||||
package com.plannaplan.responses.models;
|
||||
|
||||
import com.plannaplan.entities.User;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
@ApiModel(description = "Response shows information about logged user.", value = "TokenResponse")
|
||||
public class TokenResponse {
|
||||
@ApiModelProperty(value = "user token used to verify requests")
|
||||
private String token;
|
||||
@ApiModelProperty(value = "user id in database")
|
||||
private Long id;
|
||||
@ApiModelProperty(value = "user app role")
|
||||
private String authorityRole;
|
||||
@ApiModelProperty(value = "user unviersity email")
|
||||
private String email;
|
||||
|
||||
public TokenResponse(User user) {
|
||||
this.id = user.getId();
|
||||
this.authorityRole = user.getRole().toString();
|
||||
this.email = user.getEmail();
|
||||
this.token = user.getToken();
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public String getAuthorityRole() {
|
||||
return authorityRole;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
}
|
@ -37,7 +37,7 @@ public class AssignmentsControllerTest extends AbstractControllerTest {
|
||||
@Test
|
||||
public void shouldReturnOk() throws Exception {
|
||||
final User newuser = this.service.save(new User(null, null, TEST_MAIL, UserRoles.TEST_USER));
|
||||
final String token = this.service.login(newuser);
|
||||
final String token = this.service.login(newuser).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(get(ASSIGFNMENTS_ENDPOINT).header("Authorization", "Bearer " + token))
|
||||
|
@ -58,7 +58,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
public void shouldFailedAddingCommisionDueToNoArgs() throws Exception {
|
||||
this.checkUsers();
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token))
|
||||
@ -69,7 +69,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
public void shouldReturnOkAddingCommision() throws Exception {
|
||||
this.checkUsers();
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
|
||||
@ -86,7 +86,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
public void shouldReturnOkGettingAllCommisions() throws Exception {
|
||||
this.checkUsers();
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token))
|
||||
@ -97,7 +97,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
public void shouldAddCommisionWithSelfIdPrivided() throws Exception {
|
||||
this.checkUsers();
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
|
||||
@ -110,7 +110,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
this.checkUsers();
|
||||
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
|
||||
@ -122,7 +122,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
public void shouldFailCommisionAsDeanaryWithNoId() throws Exception {
|
||||
this.checkUsers();
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
|
||||
@ -132,7 +132,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
@Test
|
||||
public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception {
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.asker.getId().toString())
|
||||
@ -145,7 +145,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
this.checkUsers();
|
||||
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
|
||||
@ -158,7 +158,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
this.checkUsers();
|
||||
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherAsker.getId().toString())
|
||||
@ -171,7 +171,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
this.checkUsers();
|
||||
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
|
||||
@ -181,9 +181,9 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
||||
@Test
|
||||
public void shouldFailStudentCommisionsListByOtherStudent() throws Exception {
|
||||
this.checkUsers();
|
||||
|
||||
|
||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
||||
final String token = this.service.login(user);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
|
||||
|
@ -49,7 +49,7 @@ public class ConfigControllerTest extends AbstractControllerTest {
|
||||
|
||||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
||||
final String token = this.service.login(usr);
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
|
||||
@ -64,7 +64,7 @@ public class ConfigControllerTest extends AbstractControllerTest {
|
||||
|
||||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
||||
final String token = this.service.login(usr);
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
|
||||
|
@ -22,36 +22,69 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||
@ContextConfiguration
|
||||
|
||||
public class UsersControllerTest extends AbstractControllerTest {
|
||||
private static final String ENDPOINT = "/api/v1/users/student/search";
|
||||
private static final String SEARCH_ENDPOINT = "/api/v1/users/student/search";
|
||||
private static final String ALL_USERS_ENDPOINT = "/api/v1/users/students";
|
||||
|
||||
@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);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(get(ENDPOINT).param("query", "").header("Authorization", "Bearer " + token))
|
||||
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(ENDPOINT)).andExpect(status().is4xxClientError());
|
||||
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);
|
||||
final String token = this.service.login(user).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(get(ENDPOINT).header("Authorization", "Bearer " + token))
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,69 @@
|
||||
package com.plannaplan.responses.models;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.types.UserRoles;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TokenResponseTest {
|
||||
|
||||
public TokenResponse testMapUserEntiutyToTokenResponse(UserRoles role)
|
||||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
|
||||
final String token = "totalnie-prawdziwy-token";
|
||||
final String mail = "shouldMapUserEntiutyToTokenResponse@TokenResponse.test";
|
||||
|
||||
final User userToMap = new User("Franek", "Kimono", mail, null, role);
|
||||
|
||||
Field reader = User.class.getDeclaredField("id");
|
||||
reader.setAccessible(true);
|
||||
reader.set(userToMap, Long.valueOf(12));
|
||||
|
||||
reader = User.class.getDeclaredField("token");
|
||||
reader.setAccessible(true);
|
||||
reader.set(userToMap, token);
|
||||
|
||||
final TokenResponse response = new TokenResponse(userToMap);
|
||||
|
||||
assertTrue(response.getId() == 12);
|
||||
assertTrue(response.getToken().equals(token));
|
||||
assertTrue(response.getEmail().equals(mail));
|
||||
return response;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapAdminToTokenResonse()
|
||||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
|
||||
|
||||
final TokenResponse response = testMapUserEntiutyToTokenResponse(UserRoles.ADMIN);
|
||||
assertTrue(response.getAuthorityRole().equals("ADMIN"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapStudentToTokenResonse()
|
||||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
|
||||
|
||||
final TokenResponse response = testMapUserEntiutyToTokenResponse(UserRoles.STUDENT);
|
||||
assertTrue(response.getAuthorityRole().equals("STUDENT"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapDeaneryToTokenResonse()
|
||||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
|
||||
|
||||
final TokenResponse response = testMapUserEntiutyToTokenResponse(UserRoles.DEANERY);
|
||||
assertTrue(response.getAuthorityRole().equals("DEANERY"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapTestUserToTokenResonse()
|
||||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
|
||||
|
||||
final TokenResponse response = testMapUserEntiutyToTokenResponse(UserRoles.TEST_USER);
|
||||
assertTrue(response.getAuthorityRole().equals("TEST_USER"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user