Changed token response in controller

This commit is contained in:
Filip Izydorczyk 2020-12-07 21:46:47 +01:00
parent 3061fb1edf
commit f9baed5df7
7 changed files with 52 additions and 52 deletions

View File

@ -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) {

View File

@ -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");

View File

@ -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);
}
}

View File

@ -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))

View File

@ -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())

View File

@ -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))

View File

@ -31,7 +31,7 @@ public class UsersControllerTest extends AbstractControllerTest {
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))
@ -48,7 +48,7 @@ public class UsersControllerTest extends AbstractControllerTest {
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))