Skip to content

Commit

Permalink
feat: check if user exists for personal token
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Feb 2, 2024
1 parent 5b2de81 commit 7a4b022
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.sitepark.ies.userrepository.core.domain.exception;

/**
* The <code>AccessTokenDegistException</code> exception is thrown if
* the access token could't be digest.
*/
public class AccessTokenDegistException extends UserRepositoryException {

private static final long serialVersionUID = 1L;

public AccessTokenDegistException(String message) {
super(message);
}

public AccessTokenDegistException(String message, Throwable t) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public UserRepositoryException() {
public UserRepositoryException(String message) {
super(message);
}
public UserRepositoryException(String message, Throwable t) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@

import com.sitepark.ies.userrepository.core.domain.entity.AccessToken;
import com.sitepark.ies.userrepository.core.domain.exception.AccessDeniedException;
import com.sitepark.ies.userrepository.core.domain.exception.UserNotFoundException;
import com.sitepark.ies.userrepository.core.port.AccessControl;
import com.sitepark.ies.userrepository.core.port.AccessTokenRepository;
import com.sitepark.ies.userrepository.core.port.UserRepository;

public class CreateImpersonationToken {

private final AccessTokenRepository repository;

private final AccessControl accessControl;

private final UserRepository userRepository;

private static Logger LOGGER = LogManager.getLogger();

@Inject
protected CreateImpersonationToken(
AccessTokenRepository repository,
AccessControl accessControl) {
AccessControl accessControl,
UserRepository userRepository) {
this.repository = repository;
this.accessControl = accessControl;
this.userRepository = userRepository;
}

public AccessToken createPersonalAccessToken(AccessToken accessToken) {
Expand All @@ -36,6 +42,10 @@ public AccessToken createPersonalAccessToken(AccessToken accessToken) {
throw new AccessDeniedException("Not allowed manage impersonation tokens");
}

if (this.userRepository.get(accessToken.getUser()).isEmpty()) {
throw new UserNotFoundException(accessToken.getUser());
}

if (LOGGER.isInfoEnabled()) {
LOGGER.info("create: {}", accessTokenToCreate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Optional;

import org.junit.jupiter.api.Test;

import com.sitepark.ies.userrepository.core.domain.entity.AccessToken;
import com.sitepark.ies.userrepository.core.domain.entity.User;
import com.sitepark.ies.userrepository.core.domain.exception.AccessDeniedException;
import com.sitepark.ies.userrepository.core.domain.exception.UserNotFoundException;
import com.sitepark.ies.userrepository.core.port.AccessControl;
import com.sitepark.ies.userrepository.core.port.AccessTokenRepository;
import com.sitepark.ies.userrepository.core.port.UserRepository;

class CreateImpersonationTokenTest {

Expand All @@ -21,12 +27,14 @@ void testAccessDenied() {
AccessTokenRepository accessTokenRepository = mock();
AccessControl accessControl = mock(AccessControl.class);
when(accessControl.isImpersonationTokensManageable()).thenReturn(false);
UserRepository userRepository = mock(UserRepository.class);

AccessToken accessToken = AccessToken.builder().user(123).name("Test Token").build();

var createImpersonationToken = new CreateImpersonationToken(
accessTokenRepository,
accessControl);
accessControl,
userRepository);

assertThrows(AccessDeniedException.class, () -> {
createImpersonationToken.createPersonalAccessToken(accessToken);
Expand All @@ -35,18 +43,43 @@ void testAccessDenied() {
verify(accessControl).isImpersonationTokensManageable();
}

@Test
void testUserNotFound() {

AccessTokenRepository accessTokenRepository = mock();
AccessControl accessControl = mock(AccessControl.class);
when(accessControl.isImpersonationTokensManageable()).thenReturn(true);
UserRepository userRepository = mock(UserRepository.class);
when(userRepository.get(anyLong())).thenReturn(Optional.empty());

AccessToken accessToken = AccessToken.builder().user(123).name("Test Token").build();

var createImpersonationToken = new CreateImpersonationToken(
accessTokenRepository,
accessControl,
userRepository);

assertThrows(UserNotFoundException.class, () -> {
createImpersonationToken.createPersonalAccessToken(accessToken);
});
}

@Test
void testCreate() {

AccessTokenRepository accessTokenRepository = mock();
AccessControl accessControl = mock(AccessControl.class);
when(accessControl.isImpersonationTokensManageable()).thenReturn(true);
UserRepository userRepository = mock(UserRepository.class);
User user = mock(User.class);
when(userRepository.get(anyLong())).thenReturn(Optional.of(user));

AccessToken accessToken = AccessToken.builder().user(123).name("Test Token").build();

var createImpersonationToken = new CreateImpersonationToken(
accessTokenRepository,
accessControl);
accessControl,
userRepository);

createImpersonationToken.createPersonalAccessToken(accessToken);

Expand Down

0 comments on commit 7a4b022

Please sign in to comment.