CAS Part 2

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-12-03 16:57:53 +01:00
parent 3ebfda5316
commit 453907782a
Signed by: y0rune
GPG Key ID: F204C385F57EB348
7 changed files with 51 additions and 53 deletions

View File

@ -32,8 +32,7 @@ public class UserService {
}
else {
final User newUser = new User(null,null,email.replace("\n", "").trim(),UserRoles.STUDENT);
this.repo.save(newUser);
return newUser;
return this.repo.save(newUser);
}
}
else {
@ -43,19 +42,20 @@ public class UserService {
}
else {
final User newUser = new User(null,null,email.replace("\n", "").trim(),usosId,UserRoles.STUDENT);
this.repo.save(newUser);
return newUser;
return this.repo.save(newUser);
}
}
}
public String login(String authority) throws UserNotFoundException {
User user = this.repo.getByAuthority(authority.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Can not find user with given authority"));
public String login(User authority) throws UserNotFoundException {
final String token = UUID.randomUUID().toString();
user.setToken(token);
this.repo.save(user);
try{
authority.setToken(token);
this.repo.save(authority);
}
catch (Exception e){
throw new UserNotFoundException(e.getMessage());
}
return token;
}

View File

@ -32,10 +32,9 @@ public class UserServiceTest {
@Test
public void shouldReturnToken() {
final User testUser = new User(TEST_USER_NAME, TEST_USER_SUERNAME, TEST_USER_MAIL, UserRoles.TEST_USER);
this.userService.save(testUser);
final 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(TEST_USER_MAIL);
final String token = this.userService.login(testUser);
System.out.println("Returned token: " + token);
assertTrue(token != null);
assertTrue(this.userService.getUserByEmail(TEST_USER_MAIL).getToken() != null);
@ -45,16 +44,6 @@ public class UserServiceTest {
}
}
@Test
public void shouldThrowException() {
try {
this.userService.login("thiseamilisnotindatabase@gmail.com");
assertTrue(false);
} catch (UserNotFoundException e) {
assertTrue(true);
}
}
@Test
public void shouldFindStudents() {
this.userService.save(new User("Nemo", "TheFish", "Nemo@shouldFindStudents.test", UserRoles.STUDENT));

View File

@ -1,5 +1,6 @@
package com.plannaplan.controllers;
import com.plannaplan.entities.User;
import com.plannaplan.exceptions.UserNotFoundException;
import com.plannaplan.security.cas.CasUserIdentity;
import com.plannaplan.security.cas.CasValidationExcepiton;
@ -35,11 +36,11 @@ public class TokenController {
final DefaultUAMCasValidator validator = new DefaultUAMCasValidator(SERVICE_URL, ticket);
try {
CasUserIdentity casUserIdentity = validator.validate();
String usosId = casUserIdentity.getUsosId();
String authority = casUserIdentity.getEmail();
this.userService.checkForUser(authority, usosId);
String token = this.userService.login(authority);
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);
} catch (CasValidationExcepiton e) {
return new ResponseEntity<>("Wrong ticket", HttpStatus.UNAUTHORIZED);

View File

@ -36,8 +36,8 @@ public class AssignmentsControllerTest extends AbstractControllerTest {
@Test
public void shouldReturnOk() throws Exception {
this.service.save(new User(null, null, TEST_MAIL, UserRoles.TEST_USER));
final String token = this.service.login(TEST_MAIL);
final User newuser = this.service.save(new User(null, null, TEST_MAIL, UserRoles.TEST_USER));
final String token = this.service.login(newuser);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ASSIGFNMENTS_ENDPOINT).header("Authorization", "Bearer " + token))

View File

@ -57,7 +57,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test
public void shouldFailedAddingCommisionDueToNoArgs() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token))
@ -67,7 +68,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test
public void shouldReturnOkAddingCommision() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
@ -83,7 +85,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test
public void shouldReturnOkGettingAllCommisions() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_COMMISIONS_ENDPOINT).header("Authorization", "Bearer " + token))
@ -93,7 +96,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test
public void shouldAddCommisionWithSelfIdPrivided() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
@ -105,7 +109,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldFailCommisionWithSomeoneIdPrividedAsStudent() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
@ -116,7 +121,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test
public void shouldFailCommisionAsDeanaryWithNoId() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT).header("Authorization", "Bearer " + token)
@ -125,8 +131,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test
public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.asker.getId().toString())
@ -138,7 +144,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldAddCommisionWithSomeoneIdPrividedAsDeanary() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherUser.getId().toString())
@ -150,7 +157,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldFailCommisionWithOtherDeanaryIdPrividedAsDeanary() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(post(ADD_COMMISION_ENDPOINT + "/" + CommisionControllerTest.otherAsker.getId().toString())
@ -162,7 +170,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldGetStudentCommisionsListByDeanary() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())
@ -173,7 +182,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldFailStudentCommisionsListByOtherStudent() throws Exception {
this.checkUsers();
final String token = this.service.login(TEST_COMMISIONS_STUDENT_EMAIL);
final User user = this.service.checkForUser(TEST_COMMISIONS_STUDENT_EMAIL, null);
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(GET_SOMEONE_COMMISIONS_ENDPOINT + "/" + CommisionControllerTest.user.getId().toString())

View File

@ -45,12 +45,11 @@ public class ConfigControllerTest extends AbstractControllerTest {
@Test
public void shouldReturnOKAuthorized() throws Exception {
final String mail = "shouldReturnOKAuthorized@ConfigController.test";
final User usr = new User(null, null, mail, UserRoles.ADMIN);
this.service.save(usr);
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(mail);
final String token = this.service.login(usr);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
@ -61,12 +60,11 @@ public class ConfigControllerTest extends AbstractControllerTest {
@Test
public void shouldReturnDenyNoAdminAuthorized() throws Exception {
final String mail = "shouldReturnDenyNoAdminAuthorized@ConfigController.test";
final User usr = new User(null, null, mail, UserRoles.TEST_USER);
this.service.save(usr);
final User usr = this.service.save(new User(null, null, mail, UserRoles.TEST_USER));
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
final String token = this.service.login(mail);
final String token = this.service.login(usr);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))

View File

@ -30,8 +30,8 @@ public class UsersControllerTest extends AbstractControllerTest {
@Test
public void shouldRestrun200OK() throws Exception {
final String email = "notexistingassignmentuser@shouldRestrun200OK.test";
this.service.save(new User(null, null, email, UserRoles.DEANERY));
final String token = this.service.login(email);
final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY));
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT).param("query", "").header("Authorization", "Bearer " + token))
@ -47,8 +47,8 @@ public class UsersControllerTest extends AbstractControllerTest {
@Test
public void shouldFailedDueToMissingParam() throws Exception {
final String email = "notexistingassignmentuser@shouldFailedDueToMissingParam.test";
this.service.save(new User(null, null, email, UserRoles.DEANERY));
final String token = this.service.login(email);
final User user = this.service.save(new User(null, null, email, UserRoles.DEANERY));
final String token = this.service.login(user);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
mockMvc.perform(get(ENDPOINT).header("Authorization", "Bearer " + token))