Skip to content

Commit

Permalink
add tests for the AuthenticationService
Browse files Browse the repository at this point in the history
  • Loading branch information
maximAtanasov committed Aug 7, 2020
1 parent c0790b6 commit 7cf85aa
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package io.reflectoring.coderadar.rest.authentication;

import io.reflectoring.coderadar.CoderadarConfigurationProperties;
import io.reflectoring.coderadar.graph.projectadministration.domain.ProjectEntity;
import io.reflectoring.coderadar.graph.projectadministration.project.repository.ProjectRepository;
import io.reflectoring.coderadar.graph.useradministration.domain.UserEntity;
import io.reflectoring.coderadar.graph.useradministration.repository.UserRepository;
import io.reflectoring.coderadar.rest.ControllerTestTemplate;
import io.reflectoring.coderadar.useradministration.domain.ProjectRole;
import io.reflectoring.coderadar.useradministration.port.driver.login.LoginUserCommand;
import io.reflectoring.coderadar.useradministration.service.UserUnauthenticatedException;
import io.reflectoring.coderadar.useradministration.service.UserUnauthorizedException;
import io.reflectoring.coderadar.useradministration.service.login.LoginUserService;
import io.reflectoring.coderadar.useradministration.service.permissions.SetUserRoleForProjectService;
import io.reflectoring.coderadar.useradministration.service.security.AuthenticationService;
import io.reflectoring.coderadar.useradministration.service.security.PasswordUtil;
import java.util.Date;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;

class AuthenticationServiceIntegrationTest extends ControllerTestTemplate {

@Autowired private UserRepository userRepository;

@Autowired private ProjectRepository projectRepository;

@Autowired private AuthenticationService authenticationService;

@Autowired private SetUserRoleForProjectService setUserRoleForProjectService;

@Autowired private LoginUserService loginUserService;

@Autowired private CoderadarConfigurationProperties coderadarConfigurationProperties;

private ProjectEntity testProject;
private UserEntity testUser;

@BeforeEach
void setUp() {
coderadarConfigurationProperties.setAuthentication(
new CoderadarConfigurationProperties.Authentication().setEnabled(true));

testProject = new ProjectEntity();
testProject.setVcsUrl("https://valid.url");
testProject.setName("project");
testProject.setVcsStart(new Date());
testProject.setVcsOnline(true);
testProject.setVcsPassword("testPassword");
testProject.setVcsUsername("testUser");
projectRepository.save(testProject);

testUser = new UserEntity();
testUser.setUsername("username1");
testUser.setPassword(PasswordUtil.hash("password1"));
userRepository.save(testUser);
loginUserService.login(new LoginUserCommand("username1", "password1"));
}

@AfterEach
void tearDown() {
coderadarConfigurationProperties.setAuthentication(
new CoderadarConfigurationProperties.Authentication().setEnabled(false));
SecurityContextHolder.getContext().setAuthentication(null);
}

@Test
void testUserIsAuthenticatedAsAdmin() {
SecurityContextHolder.getContext()
.setAuthentication(new UsernamePasswordAuthenticationToken("username1", "password1"));

// Set up
setUserRoleForProjectService.setRole(testProject.getId(), testUser.getId(), ProjectRole.ADMIN);

// Test
Assertions.assertDoesNotThrow(
() -> authenticationService.authenticateAdmin(testProject.getId()));
Assertions.assertDoesNotThrow(
() -> authenticationService.authenticateMember(testProject.getId()));
}

@Test
void testUserIsAuthenticatedAsMember() {
SecurityContextHolder.getContext()
.setAuthentication(new UsernamePasswordAuthenticationToken("username1", "password1"));

// Set up
setUserRoleForProjectService.setRole(testProject.getId(), testUser.getId(), ProjectRole.MEMBER);

// Test
Assertions.assertDoesNotThrow(
() -> authenticationService.authenticateMember(testProject.getId()));
Assertions.assertThrows(
UserUnauthorizedException.class,
() -> authenticationService.authenticateAdmin(testProject.getId()));
}

@Test
void testThrowsExceptionWhenUserIsUnauthenticated() {
Assertions.assertThrows(
UserUnauthenticatedException.class,
() -> authenticationService.authenticateAdmin(testProject.getId()));
Assertions.assertThrows(
UserUnauthenticatedException.class,
() -> authenticationService.authenticateMember(testProject.getId()));
}

@Test
void testThrowsExceptionWhenUserIsNotAdmin() {
SecurityContextHolder.getContext()
.setAuthentication(new UsernamePasswordAuthenticationToken("username1", "password1"));

// Set up
setUserRoleForProjectService.setRole(testProject.getId(), testUser.getId(), ProjectRole.MEMBER);

// Test
Assertions.assertThrows(
UserUnauthorizedException.class,
() -> authenticationService.authenticateAdmin(testProject.getId()));
Assertions.assertDoesNotThrow(
() -> authenticationService.authenticateMember(testProject.getId()));
}

@Test
void testThrowsExceptionWhenUserHasNoRole() {
SecurityContextHolder.getContext()
.setAuthentication(new UsernamePasswordAuthenticationToken("username1", "password1"));

Assertions.assertThrows(
UserUnauthorizedException.class,
() -> authenticationService.authenticateAdmin(testProject.getId()));
Assertions.assertThrows(
UserUnauthorizedException.class,
() -> authenticationService.authenticateMember(testProject.getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ void setUp() {

@Test
void setUserRoleForProjectSuccessfully() throws Exception {

ConstrainedFields<ProjectRoleJsonWrapper> fields = fields(ProjectRoleJsonWrapper.class);

mvc()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export abstract class DependencyBase {

currentNode.dependencies.forEach(dependency => {
// find last visible element for dependency as end
const end = this.findLastHTMLElement(dependency.path) as HTMLElement;
const end = this.findLastHTMLElement(dependency.path);

// if activeDependency is set, draw only activeDependency related dependencies
if (this.activeDependency !== undefined) {
Expand Down

0 comments on commit 7cf85aa

Please sign in to comment.