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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -31,7 +31,7 @@ public class UsersControllerTest extends AbstractControllerTest {
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(ENDPOINT).param("query", "").header("Authorization", "Bearer " + token))
@ -48,7 +48,7 @@ public class UsersControllerTest extends AbstractControllerTest {
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(ENDPOINT).header("Authorization", "Bearer " + token))