Skip to content
Snippets Groups Projects
Commit 0eb0ab84 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4771 validate trust level before building servic konto

parent d856579f
No related branches found
No related tags found
No related merge requests found
package de.ozgcloud.eingang.common.errorhandling;
public class UnexpectedTrustLevelException extends TechnicalException {
public UnexpectedTrustLevelException(String message) {
super(message);
}
}
...@@ -9,7 +9,6 @@ import java.util.Optional; ...@@ -9,7 +9,6 @@ import java.util.Optional;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import de.ozgcloud.eingang.common.errorhandling.UnexpectedTrustLevelException;
import de.ozgcloud.eingang.common.formdata.FormData; import de.ozgcloud.eingang.common.formdata.FormData;
import de.ozgcloud.eingang.common.formdata.PostfachAddressIdentifier; import de.ozgcloud.eingang.common.formdata.PostfachAddressIdentifier;
import de.ozgcloud.eingang.common.formdata.ServiceKonto; import de.ozgcloud.eingang.common.formdata.ServiceKonto;
...@@ -96,12 +95,15 @@ public class ServiceKontoFactory { ...@@ -96,12 +95,15 @@ public class ServiceKontoFactory {
if (Objects.isNull(formDataHeaders) || !formDataHeaders.containsKey(KEY_BAYERN_ID_POSTFACH_ID)) { if (Objects.isNull(formDataHeaders) || !formDataHeaders.containsKey(KEY_BAYERN_ID_POSTFACH_ID)) {
return Optional.empty(); return Optional.empty();
} }
try { if (isValidTrustLevel(formDataHeaders)) {
return Optional.of(buildBayernIdServiceKonto(formDataHeaders)); return Optional.of(buildBayernIdServiceKonto(formDataHeaders));
} catch (UnexpectedTrustLevelException e) { }
LOG.error("Error while creating BayernID ServiceKonto", e); LOG.error("TrustLevel has an unexpected value '{}'. BayernID user account is not connected", getTrustLevel(formDataHeaders));
return Optional.empty(); return Optional.empty();
} }
boolean isValidTrustLevel(Map<String, Object> formDataHeader) {
return TrustLevel.hasValue(getTrustLevel(formDataHeader));
} }
ServiceKonto buildBayernIdServiceKonto(Map<String, Object> formDataHeaders) { ServiceKonto buildBayernIdServiceKonto(Map<String, Object> formDataHeaders) {
...@@ -112,6 +114,10 @@ public class ServiceKontoFactory { ...@@ -112,6 +114,10 @@ public class ServiceKontoFactory {
.build(); .build();
} }
String getTrustLevel(Map<String, Object> formDataHeaders) {
return MapUtils.getString(formDataHeaders, KEY_BAYERN_ID_TRUST_LEVEL);
}
PostfachAddress buildPostfachAddress(String postkorbHandle) { PostfachAddress buildPostfachAddress(String postkorbHandle) {
return PostfachAddress.builder() return PostfachAddress.builder()
.type(POSTFACH_ADDRESS_DEFAULT) .type(POSTFACH_ADDRESS_DEFAULT)
...@@ -120,15 +126,6 @@ public class ServiceKontoFactory { ...@@ -120,15 +126,6 @@ public class ServiceKontoFactory {
.build(); .build();
} }
String getTrustLevel(Map<String, Object> formDataHeader) {
var trustLevel = MapUtils.getString(formDataHeader, KEY_BAYERN_ID_TRUST_LEVEL);
if (TrustLevel.hasValue(trustLevel)) {
return trustLevel;
}
throw new UnexpectedTrustLevelException(
"TrustLevel has an unexpected value '%s'. BayernID user account is not connected".formatted(trustLevel));
}
private PostfachAddressIdentifier buildIdentifier(String postfachId) { private PostfachAddressIdentifier buildIdentifier(String postfachId) {
return StringBasedIdentifier.builder().postfachId(postfachId).build(); return StringBasedIdentifier.builder().postfachId(postfachId).build();
} }
......
package de.ozgcloud.eingang.semantik.common; package de.ozgcloud.eingang.semantik.common;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
...@@ -15,12 +14,12 @@ import org.junit.jupiter.api.Test; ...@@ -15,12 +14,12 @@ import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Spy; import org.mockito.Spy;
import de.ozgcloud.eingang.common.errorhandling.UnexpectedTrustLevelException;
import de.ozgcloud.eingang.common.formdata.FormData; import de.ozgcloud.eingang.common.formdata.FormData;
import de.ozgcloud.eingang.common.formdata.FormDataUtils; import de.ozgcloud.eingang.common.formdata.FormDataUtils;
import de.ozgcloud.eingang.common.formdata.PostfachAddressTestFactory; import de.ozgcloud.eingang.common.formdata.PostfachAddressTestFactory;
import de.ozgcloud.eingang.common.formdata.ServiceKonto; import de.ozgcloud.eingang.common.formdata.ServiceKonto;
import de.ozgcloud.eingang.common.formdata.ServiceKonto.TrustLevel; import de.ozgcloud.eingang.common.formdata.ServiceKonto.TrustLevel;
import de.ozgcloud.eingang.common.formdata.ServiceKontoTestFactory;
import de.ozgcloud.eingang.common.formdata.StringBasedIdentifier; import de.ozgcloud.eingang.common.formdata.StringBasedIdentifier;
import de.ozgcloud.eingang.common.formdata.ServiceKonto.PostfachAddress; import de.ozgcloud.eingang.common.formdata.ServiceKonto.PostfachAddress;
import de.ozgcloud.eingang.semantik.enginebased.afm.AfmHeaderTestFactory; import de.ozgcloud.eingang.semantik.enginebased.afm.AfmHeaderTestFactory;
...@@ -149,12 +148,9 @@ class ServiceKontoFactoryTest { ...@@ -149,12 +148,9 @@ class ServiceKontoFactoryTest {
@Nested @Nested
class TestCreateBayernIdServicekonto { class TestCreateBayernIdServicekonto {
private static final String POSTFACH_ID = "postfach-id";
private static final PostfachAddress POSTFACH_ADDRESS = PostfachAddressTestFactory.create();
private final Map<String, Object> formDataHeaders = Map.of( private final Map<String, Object> formDataHeaders = Map.of(
ServiceKontoFactory.KEY_BAYERN_ID_POSTFACH_ID, POSTFACH_ID, ServiceKontoFactory.KEY_BAYERN_ID_POSTFACH_ID, PostfachAddressTestFactory.POSTFACH_ID,
ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, "STORK-QAA-Level-2" ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, ServiceKontoTestFactory.TRUST_LEVEL
); );
@DisplayName("should return empty when headers map is null") @DisplayName("should return empty when headers map is null")
...@@ -173,6 +169,13 @@ class ServiceKontoFactoryTest { ...@@ -173,6 +169,13 @@ class ServiceKontoFactoryTest {
assertThat(serviceKonto).isEmpty(); assertThat(serviceKonto).isEmpty();
} }
@Test
void shouldCallIsValidTrustLevel() {
factory.createBayernIdServiceKonto(formDataHeaders);
verify(factory).isValidTrustLevel(formDataHeaders);
}
@Test @Test
void shouldCallBuildBayernIdServiceKonto() { void shouldCallBuildBayernIdServiceKonto() {
factory.createBayernIdServiceKonto(formDataHeaders); factory.createBayernIdServiceKonto(formDataHeaders);
...@@ -193,7 +196,7 @@ class ServiceKontoFactoryTest { ...@@ -193,7 +196,7 @@ class ServiceKontoFactoryTest {
@DisplayName("should return empty when trust level has unexpected value") @DisplayName("should return empty when trust level has unexpected value")
@Test @Test
void shouldReturnEmptyWhenTrustLevelCorrupted() { void shouldReturnEmptyWhenTrustLevelCorrupted() {
doThrow(UnexpectedTrustLevelException.class).when(factory).buildBayernIdServiceKonto(any()); doReturn(false).when(factory).isValidTrustLevel(any());
var serviceKonto = factory.createBayernIdServiceKonto(formDataHeaders); var serviceKonto = factory.createBayernIdServiceKonto(formDataHeaders);
...@@ -201,6 +204,34 @@ class ServiceKontoFactoryTest { ...@@ -201,6 +204,34 @@ class ServiceKontoFactoryTest {
} }
} }
@Nested
class TestIsValidTrustLevel {
@Test
void shouldCallHasValue() {
try (var trustLevelMock = mockStatic(TrustLevel.class)) {
isValidTrustLevel();
trustLevelMock.verify(() -> TrustLevel.hasValue(ServiceKontoTestFactory.TRUST_LEVEL));
}
}
@Test
void shouldReturnValue() {
try (var trustLevelMock = mockStatic(TrustLevel.class)) {
trustLevelMock.when(() -> TrustLevel.hasValue(any())).thenReturn(true);
var result = isValidTrustLevel();
assertThat(result).isTrue();
}
}
private boolean isValidTrustLevel() {
return factory.isValidTrustLevel(Map.of(ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, ServiceKontoTestFactory.TRUST_LEVEL));
}
}
@Nested @Nested
class TestBuildBayernIdServiceKonto { class TestBuildBayernIdServiceKonto {
...@@ -212,11 +243,6 @@ class ServiceKontoFactoryTest { ...@@ -212,11 +243,6 @@ class ServiceKontoFactoryTest {
ServiceKontoFactory.KEY_BAYERN_ID_POSTFACH_ID, POSTFACH_ID, ServiceKontoFactory.KEY_BAYERN_ID_POSTFACH_ID, POSTFACH_ID,
ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, TRUST_LEVEL); ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, TRUST_LEVEL);
@BeforeEach
void init() {
doReturn(TRUST_LEVEL).when(factory).getTrustLevel(any());
}
@Test @Test
void shouldSetType() { void shouldSetType() {
var serviceKonto = buildBayernIdServiceKonto(); var serviceKonto = buildBayernIdServiceKonto();
...@@ -262,35 +288,16 @@ class ServiceKontoFactoryTest { ...@@ -262,35 +288,16 @@ class ServiceKontoFactoryTest {
@Nested @Nested
class TestGetTrustLevel { class TestGetTrustLevel {
private static final String TRUST_LEVEL = "STORK-QAA-Level-2"; private final Map<String, Object> formDataHeaders = Map.of(ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL,
ServiceKontoTestFactory.TRUST_LEVEL);
private final Map<String, Object> formDataHeaders = Map.of(ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, TRUST_LEVEL);
@Test
void shouldCallValidateTrustLevel() {
try (var trustLevelMock = mockStatic(TrustLevel.class)) {
trustLevelMock.when(() -> TrustLevel.hasValue(any())).thenReturn(true);
factory.getTrustLevel(formDataHeaders);
trustLevelMock.verify(() -> TrustLevel.hasValue(TRUST_LEVEL));
}
}
@Test @Test
void shouldReturnTrustLevel() { void shouldReturnTrustLevel() {
var trustLevel = factory.getTrustLevel(formDataHeaders); var trustLevel = factory.getTrustLevel(formDataHeaders);
assertThat(trustLevel).isEqualTo(TRUST_LEVEL); assertThat(trustLevel).isEqualTo(ServiceKontoTestFactory.TRUST_LEVEL);
} }
@Test
void shouldThrowExceptionWhenTrustLevelIsInvalid() {
var formDataHeaders = Map.<String, Object>of(ServiceKontoFactory.KEY_BAYERN_ID_TRUST_LEVEL, "unexpected");
assertThrows(UnexpectedTrustLevelException.class, () -> factory.getTrustLevel(formDataHeaders));
}
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment