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;
|
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
|
@Service
|
||||||
@ -68,6 +69,10 @@ public class GroupService {
|
|||||||
public HashMap<Long, Integer> getTakenPlaces(List<Groups> groups) {
|
public HashMap<Long, Integer> getTakenPlaces(List<Groups> groups) {
|
||||||
HashMap<Long, Integer> response = new HashMap<>();
|
HashMap<Long, Integer> response = new HashMap<>();
|
||||||
|
|
||||||
|
if (groups.size() == 0) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
List<Object[]> respoonses = this.repo
|
List<Object[]> respoonses = this.repo
|
||||||
.getAssignedAmounts(groups.stream().filter(Objects::nonNull).map(new Function<Groups, Long>() {
|
.getAssignedAmounts(groups.stream().filter(Objects::nonNull).map(new Function<Groups, Long>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -26,37 +26,33 @@ public class UserService {
|
|||||||
|
|
||||||
public User checkForUser(String email, String usosId) {
|
public User checkForUser(String email, String usosId) {
|
||||||
if (usosId == null) {
|
if (usosId == null) {
|
||||||
Optional <User> user = this.repo.getByAuthority(email.replace("\n", "").trim());
|
Optional<User> user = this.repo.getByAuthority(email.replace("\n", "").trim());
|
||||||
if (user.isPresent()){
|
if (user.isPresent()) {
|
||||||
return user.get();
|
return user.get();
|
||||||
}
|
} else {
|
||||||
else {
|
final User newUser = new User(null, null, email.replace("\n", "").trim(), UserRoles.STUDENT);
|
||||||
final User newUser = new User(null,null,email.replace("\n", "").trim(),UserRoles.STUDENT);
|
|
||||||
return this.repo.save(newUser);
|
return this.repo.save(newUser);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
Optional<User> user = this.repo.getByUsosId(usosId.replace("\n", "").trim());
|
||||||
Optional <User> user = this.repo.getByUsosId(usosId.replace("\n", "").trim());
|
if (user.isPresent()) {
|
||||||
if (user.isPresent()){
|
|
||||||
return user.get();
|
return user.get();
|
||||||
}
|
} else {
|
||||||
else {
|
final User newUser = new User(null, null, email.replace("\n", "").trim(), usosId, UserRoles.STUDENT);
|
||||||
final User newUser = new User(null,null,email.replace("\n", "").trim(),usosId,UserRoles.STUDENT);
|
|
||||||
return this.repo.save(newUser);
|
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();
|
final String token = UUID.randomUUID().toString();
|
||||||
try{
|
try {
|
||||||
authority.setToken(token);
|
authority.setToken(token);
|
||||||
this.repo.save(authority);
|
this.repo.save(authority);
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e){
|
|
||||||
throw new UserNotFoundException(e.getMessage());
|
throw new UserNotFoundException(e.getMessage());
|
||||||
}
|
}
|
||||||
return token;
|
return authority;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User save(User user) {
|
public User save(User user) {
|
||||||
|
@ -32,11 +32,12 @@ public class UserServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnToken() {
|
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 {
|
try {
|
||||||
final String token = this.userService.login(testUser);
|
testUser = this.userService.login(testUser);
|
||||||
System.out.println("Returned token: " + token);
|
System.out.println("Returned token: " + testUser.getToken());
|
||||||
assertTrue(token != null);
|
assertTrue(testUser.getToken() != null);
|
||||||
assertTrue(this.userService.getUserByEmail(TEST_USER_MAIL).getToken() != null);
|
assertTrue(this.userService.getUserByEmail(TEST_USER_MAIL).getToken() != null);
|
||||||
} catch (UserNotFoundException e) {
|
} catch (UserNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -84,7 +85,7 @@ public class UserServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateUser(){
|
public void shouldCreateUser() {
|
||||||
|
|
||||||
final User user = this.userService.checkForUser("shouldCreateUser@UserService.test", null);
|
final User user = this.userService.checkForUser("shouldCreateUser@UserService.test", null);
|
||||||
|
|
||||||
@ -92,9 +93,9 @@ public class UserServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnExistingUser(){
|
public void shouldReturnExistingUser() {
|
||||||
final String email = "shouldReturnExistingUser@UserService.test";
|
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);
|
final User user = this.userService.checkForUser(email, null);
|
||||||
|
|
||||||
assertTrue(user.getName() != "Tom");
|
assertTrue(user.getName() != "Tom");
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.plannaplan.controllers;
|
package com.plannaplan.controllers;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.plannaplan.App;
|
import com.plannaplan.App;
|
||||||
@ -7,7 +9,10 @@ import com.plannaplan.entities.Course;
|
|||||||
import com.plannaplan.responses.mappers.CoursesResponseMappers;
|
import com.plannaplan.responses.mappers.CoursesResponseMappers;
|
||||||
import com.plannaplan.responses.models.CoursesDefaultResponse;
|
import com.plannaplan.responses.models.CoursesDefaultResponse;
|
||||||
import com.plannaplan.responses.models.CoursesWithGroupsResponse;
|
import com.plannaplan.responses.models.CoursesWithGroupsResponse;
|
||||||
|
import com.plannaplan.responses.models.GroupDefaultResponse;
|
||||||
import com.plannaplan.services.CourseService;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
@ -33,12 +38,40 @@ public class CoursesController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CourseService courseService;
|
private CourseService courseService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private GroupService groupService;
|
||||||
|
|
||||||
@GetMapping("/all")
|
@GetMapping("/all")
|
||||||
@ApiOperation(value = "Return all courses")
|
@ApiOperation(value = "Return all courses")
|
||||||
public ResponseEntity<List<? extends CoursesResponse>> getMethodName(
|
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();
|
List<Course> courses = this.courseService.getAllCourses();
|
||||||
if (groups) {
|
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
|
final List<CoursesWithGroupsResponse> response = CoursesResponseMappers
|
||||||
.mapToGetCoursesWithGroupsResponse(courses);
|
.mapToGetCoursesWithGroupsResponse(courses);
|
||||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||||
|
@ -2,6 +2,7 @@ package com.plannaplan.controllers;
|
|||||||
|
|
||||||
import com.plannaplan.entities.User;
|
import com.plannaplan.entities.User;
|
||||||
import com.plannaplan.exceptions.UserNotFoundException;
|
import com.plannaplan.exceptions.UserNotFoundException;
|
||||||
|
import com.plannaplan.responses.models.TokenResponse;
|
||||||
import com.plannaplan.security.cas.CasUserIdentity;
|
import com.plannaplan.security.cas.CasUserIdentity;
|
||||||
import com.plannaplan.security.cas.CasValidationExcepiton;
|
import com.plannaplan.security.cas.CasValidationExcepiton;
|
||||||
import com.plannaplan.security.cas.CasValidator;
|
import com.plannaplan.security.cas.CasValidator;
|
||||||
@ -38,23 +39,25 @@ public class TokenController {
|
|||||||
|
|
||||||
@GetMapping("/token")
|
@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")
|
@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) {
|
@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 {
|
try {
|
||||||
final CasUserIdentity casUserIdentity = validator.validate();
|
final CasUserIdentity casUserIdentity = validator.validate();
|
||||||
final String usosId = casUserIdentity.getUsosId();
|
final String usosId = casUserIdentity.getUsosId();
|
||||||
final String authority = casUserIdentity.getEmail();
|
final String authority = casUserIdentity.getEmail();
|
||||||
final User user = this.userService.checkForUser(authority, usosId);
|
User user = this.userService.checkForUser(authority, usosId);
|
||||||
String token = this.userService.login(user);
|
user = this.userService.login(user);
|
||||||
return new ResponseEntity<>(token, HttpStatus.OK);
|
|
||||||
|
return new ResponseEntity<>(new TokenResponse(user), HttpStatus.OK);
|
||||||
} catch (CasValidationExcepiton e) {
|
} catch (CasValidationExcepiton e) {
|
||||||
return new ResponseEntity<>("Wrong ticket", HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
|
||||||
} catch (UserNotFoundException e) {
|
} catch (UserNotFoundException e) {
|
||||||
return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
|
||||||
} catch (Exception e) {
|
} 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")
|
@GetMapping("/student/search")
|
||||||
@PreAuthorize("hasRole('ROLE_DEANERY')")
|
@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")
|
@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) {
|
@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<User> searches = this.userService.searchForStudents(query);
|
||||||
final List<UserResponse> response = UserResponseMappers.mapToDefaultResponse(searches);
|
final List<UserResponse> response = UserResponseMappers.mapToDefaultResponse(searches);
|
||||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
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() {
|
public List<GroupDefaultResponse> getClasses() {
|
||||||
return this.classes;
|
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
|
@Test
|
||||||
public void shouldReturnOk() throws Exception {
|
public void shouldReturnOk() throws Exception {
|
||||||
final User newuser = this.service.save(new User(null, null, TEST_MAIL, UserRoles.TEST_USER));
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(get(ASSIGFNMENTS_ENDPOINT).header("Authorization", "Bearer " + token))
|
mockMvc.perform(get(ASSIGFNMENTS_ENDPOINT).header("Authorization", "Bearer " + token))
|
||||||
|
@ -58,7 +58,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
public void shouldFailedAddingCommisionDueToNoArgs() throws Exception {
|
public void shouldFailedAddingCommisionDueToNoArgs() throws Exception {
|
||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token))
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token))
|
||||||
@ -69,7 +69,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
public void shouldReturnOkAddingCommision() throws Exception {
|
public void shouldReturnOkAddingCommision() throws Exception {
|
||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
|
||||||
@ -86,7 +86,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
public void shouldReturnOkGettingAllCommisions() throws Exception {
|
public void shouldReturnOkGettingAllCommisions() throws Exception {
|
||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token))
|
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token))
|
||||||
@ -97,7 +97,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
public void shouldAddCommisionWithSelfIdPrivided() throws Exception {
|
public void shouldAddCommisionWithSelfIdPrivided() throws Exception {
|
||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
|
||||||
@ -110,7 +110,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
|
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
|
||||||
@ -122,7 +122,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
public void shouldFailCommisionAsDeanaryWithNoId() throws Exception {
|
public void shouldFailCommisionAsDeanaryWithNoId() throws Exception {
|
||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
|
||||||
@ -132,7 +132,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception {
|
public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception {
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.asker.getId().toString())
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.asker.getId().toString())
|
||||||
@ -145,7 +145,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
|
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
|
||||||
@ -158,7 +158,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
|
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherAsker.getId().toString())
|
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherAsker.getId().toString())
|
||||||
@ -171,7 +171,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
|
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
|
mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
|
||||||
@ -183,7 +183,7 @@ public class CommisionControllerTest extends AbstractControllerTest {
|
|||||||
this.checkUsers();
|
this.checkUsers();
|
||||||
|
|
||||||
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
|
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 InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
|
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 InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
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 mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
|
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
|
@ContextConfiguration
|
||||||
|
|
||||||
public class UsersControllerTest extends AbstractControllerTest {
|
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
|
@Autowired
|
||||||
private UserService service;
|
private UserService service;
|
||||||
|
|
||||||
|
/* SEARCH_ENDPOINT */
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldRestrun200OK() throws Exception {
|
public void shouldRestrun200OK() throws Exception {
|
||||||
final String email = "notexistingassignmentuser@shouldRestrun200OK.test";
|
final String email = "notexistingassignmentuser@shouldRestrun200OK.test";
|
||||||
final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY));
|
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 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());
|
.andExpect(status().isOk());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldRestrunForbiden() throws Exception {
|
public void shouldRestrunForbiden() throws Exception {
|
||||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||||
mockMvc.perform(get(ENDPOINT)).andExpect(status().is4xxClientError());
|
mockMvc.perform(get(SEARCH_ENDPOINT)).andExpect(status().is4xxClientError());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFailedDueToMissingParam() throws Exception {
|
public void shouldFailedDueToMissingParam() throws Exception {
|
||||||
final String email = "notexistingassignmentuser@shouldFailedDueToMissingParam.test";
|
final String email = "notexistingassignmentuser@shouldFailedDueToMissingParam.test";
|
||||||
final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY));
|
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 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());
|
.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