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

View File

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

View File

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

View File

@ -36,8 +36,8 @@ public class AssignmentsControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldReturnOk() throws Exception { public void shouldReturnOk() throws Exception {
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(TEST_MAIL); final String token = this.service.login(newuser);
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

@ -57,7 +57,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldFailedAddingCommisionDueToNoArgs() throws Exception { public void shouldFailedAddingCommisionDueToNoArgs() throws Exception {
this.checkUsers(); 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 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))
@ -67,7 +68,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldReturnOkAddingCommision() throws Exception { public void shouldReturnOkAddingCommision() throws Exception {
this.checkUsers(); 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 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)
@ -83,7 +85,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldReturnOkGettingAllCommisions() throws Exception { public void shouldReturnOkGettingAllCommisions() throws Exception {
this.checkUsers(); 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 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))
@ -93,7 +96,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldAddCommisionWithSelfIdPrivided() throws Exception { public void shouldAddCommisionWithSelfIdPrivided() throws Exception {
this.checkUsers(); 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 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())
@ -105,7 +109,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldFailCommisionWithSomeoneIdPrividedAsStudent() throws Exception { public void shouldFailCommisionWithSomeoneIdPrividedAsStudent() throws Exception {
this.checkUsers(); 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 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())
@ -116,7 +121,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldFailCommisionAsDeanaryWithNoId() throws Exception { public void shouldFailCommisionAsDeanaryWithNoId() throws Exception {
this.checkUsers(); 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 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)
@ -125,8 +131,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception { public void shouldFailCommisionWithSelfIdPrividedAsDeanary() throws Exception {
this.checkUsers(); final User user = this.service.checkForUser(TEST_COMMISIONS_DEANERY_EMAIL, null);
final String token = this.service.login(TEST_COMMISIONS_DEANERY_EMAIL); final String token = this.service.login(user);
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())
@ -138,7 +144,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldAddCommisionWithSomeoneIdPrividedAsDeanary() throws Exception { public void shouldAddCommisionWithSomeoneIdPrividedAsDeanary() throws Exception {
this.checkUsers(); 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 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())
@ -150,7 +157,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldFailCommisionWithOtherDeanaryIdPrividedAsDeanary() throws Exception { public void shouldFailCommisionWithOtherDeanaryIdPrividedAsDeanary() throws Exception {
this.checkUsers(); 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 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())
@ -162,7 +170,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldGetStudentCommisionsListByDeanary() throws Exception { public void shouldGetStudentCommisionsListByDeanary() throws Exception {
this.checkUsers(); 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 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())
@ -173,7 +182,8 @@ public class CommisionControllerTest extends AbstractControllerTest {
public void shouldFailStudentCommisionsListByOtherStudent() throws Exception { public void shouldFailStudentCommisionsListByOtherStudent() throws Exception {
this.checkUsers(); 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 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

@ -45,12 +45,11 @@ public class ConfigControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldReturnOKAuthorized() throws Exception { public void shouldReturnOKAuthorized() throws Exception {
final String mail = "shouldReturnOKAuthorized@ConfigController.test"; final String mail = "shouldReturnOKAuthorized@ConfigController.test";
final User usr = new User(null, null, mail, UserRoles.ADMIN); final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
this.service.save(usr);
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(mail); final String token = this.service.login(usr);
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))
@ -61,12 +60,11 @@ public class ConfigControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldReturnDenyNoAdminAuthorized() throws Exception { public void shouldReturnDenyNoAdminAuthorized() throws Exception {
final String mail = "shouldReturnDenyNoAdminAuthorized@ConfigController.test"; final String mail = "shouldReturnDenyNoAdminAuthorized@ConfigController.test";
final User usr = new User(null, null, mail, UserRoles.TEST_USER); final User usr = this.service.save(new User(null, null, mail, UserRoles.TEST_USER));
this.service.save(usr);
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(mail); final String token = this.service.login(usr);
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

@ -30,8 +30,8 @@ public class UsersControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldRestrun200OK() throws Exception { public void shouldRestrun200OK() throws Exception {
final String email = "notexistingassignmentuser@shouldRestrun200OK.test"; final String email = "notexistingassignmentuser@shouldRestrun200OK.test";
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(email); final String token = this.service.login(user);
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))
@ -47,8 +47,8 @@ public class UsersControllerTest extends AbstractControllerTest {
@Test @Test
public void shouldFailedDueToMissingParam() throws Exception { public void shouldFailedDueToMissingParam() throws Exception {
final String email = "notexistingassignmentuser@shouldFailedDueToMissingParam.test"; final String email = "notexistingassignmentuser@shouldFailedDueToMissingParam.test";
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(email); final String token = this.service.login(user);
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))