forked from Onlineberatung/onlineBeratung-agencyService
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from virtualidentityag/DIAKONIE-180
feat: add validation on agency create and update
- Loading branch information
Showing
3 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...a/de/caritas/cob/agencyservice/api/admin/validation/validators/AgencyTenantValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.caritas.cob.agencyservice.api.admin.validation.validators; | ||
|
||
import static com.fasterxml.jackson.databind.util.ClassUtil.nonNull; | ||
|
||
import de.caritas.cob.agencyservice.api.admin.validation.validators.annotation.CreateAgencyValidator; | ||
import de.caritas.cob.agencyservice.api.admin.validation.validators.annotation.UpdateAgencyValidator; | ||
import de.caritas.cob.agencyservice.api.admin.validation.validators.model.ValidateAgencyDTO; | ||
import de.caritas.cob.agencyservice.api.service.ApplicationSettingsService; | ||
import de.caritas.cob.agencyservice.api.service.TenantService; | ||
import de.caritas.cob.agencyservice.api.tenant.TenantContext; | ||
import de.caritas.cob.agencyservice.api.util.AuthenticatedUser; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@UpdateAgencyValidator | ||
@CreateAgencyValidator | ||
@Slf4j | ||
public class AgencyTenantValidator implements ConcreteAgencyValidator { | ||
|
||
private final @NonNull AuthenticatedUser authenticatedUser; | ||
|
||
@Override | ||
public void validate(ValidateAgencyDTO validateAgencyDto) { | ||
if (authenticatedUser.isTenantSuperAdmin()) { | ||
nonNull(validateAgencyDto.getTenantId(), "Tenant id must not be null."); | ||
} else { | ||
if (validateAgencyDto.getTenantId() != null && !TenantContext.getCurrentTenant() | ||
.equals(validateAgencyDto.getTenantId())) { | ||
throw new AccessDeniedException( | ||
"Access denied. Tenant id in the request does not match current tenant."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
.../caritas/cob/agencyservice/api/admin/validation/validators/AgencyTenantValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package de.caritas.cob.agencyservice.api.admin.validation.validators; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
import de.caritas.cob.agencyservice.api.admin.validation.validators.model.ValidateAgencyDTO; | ||
import de.caritas.cob.agencyservice.api.tenant.TenantContext; | ||
import de.caritas.cob.agencyservice.api.util.AuthenticatedUser; | ||
import org.assertj.core.api.Fail; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.security.access.AccessDeniedException; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AgencyTenantValidatorTest { | ||
|
||
@Mock | ||
AuthenticatedUser authenticatedUser; | ||
AgencyTenantValidator agencyTenantValidator; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
TenantContext.clear(); | ||
agencyTenantValidator = new AgencyTenantValidator(authenticatedUser); | ||
} | ||
|
||
@Test | ||
void validate_shouldNotThrowException_When_AuthenticatedUserIsNotSuperAdminAndTenantIdNull() { | ||
var validateAgencyDTO = ValidateAgencyDTO.builder().tenantId(null).build(); | ||
try { | ||
agencyTenantValidator.validate(validateAgencyDTO); | ||
} catch (Exception e) { | ||
Fail.fail("Should not throw exception"); | ||
} | ||
} | ||
|
||
@Test | ||
void validate_shouldNotThrowException_When_AuthenticatedUserIsNotSuperAdminAndTenantIdMatchesTenantContext() { | ||
TenantContext.setCurrentTenant(1L); | ||
var validateAgencyDTO = ValidateAgencyDTO.builder().tenantId(1L).build(); | ||
try { | ||
agencyTenantValidator.validate(validateAgencyDTO); | ||
} catch (Exception e) { | ||
Fail.fail("Should not throw exception"); | ||
} | ||
} | ||
|
||
@Test | ||
void validate_shouldThrowAccessDeniedException_When_AuthenticatedUserIsNotSuperAdminAndTenantIdDoesNotMatchTenantContext() { | ||
TenantContext.setCurrentTenant(2L); | ||
var validateAgencyDTO = ValidateAgencyDTO.builder().tenantId(1L).build(); | ||
try { | ||
agencyTenantValidator.validate(validateAgencyDTO); | ||
} catch (AccessDeniedException e) { | ||
assert e.getMessage().equals("Access denied. Tenant id in the request does not match current tenant."); | ||
} catch (Exception ex) { | ||
Fail.fail("Unexpected exception: " + ex.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
void validate_shouldNotThrowException_When_AuthenticatedUserIsSuperAdminAndTenantIdNotMatchingTenantContext() { | ||
TenantContext.setCurrentTenant(0L); | ||
when(authenticatedUser.isTenantSuperAdmin()).thenReturn(true); | ||
var validateAgencyDTO = ValidateAgencyDTO.builder().tenantId(1L).build(); | ||
try { | ||
agencyTenantValidator.validate(validateAgencyDTO); | ||
} catch (Exception e) { | ||
Fail.fail("Should not throw exception"); | ||
} | ||
} | ||
|
||
|
||
|
||
} |