diff --git a/wls-common/exception/src/main/java/de/muenchen/oss/wahllokalsystem/wls/common/exception/errorhandler/WlsResponseErrorHandler.java b/wls-common/exception/src/main/java/de/muenchen/oss/wahllokalsystem/wls/common/exception/errorhandler/WlsResponseErrorHandler.java index 0dc8e8015..a18a35f5f 100644 --- a/wls-common/exception/src/main/java/de/muenchen/oss/wahllokalsystem/wls/common/exception/errorhandler/WlsResponseErrorHandler.java +++ b/wls-common/exception/src/main/java/de/muenchen/oss/wahllokalsystem/wls/common/exception/errorhandler/WlsResponseErrorHandler.java @@ -43,6 +43,13 @@ public void handleError(@NonNull final ClientHttpResponse response) throws WlsEx throw createdException; } + public TechnischeWlsException createFalseObjectReferenceException (String Id,final Throwable cause){ + return TechnischeWlsException + .withCode(ExceptionKonstanten.CODE_ALLGEMEIN_UNBEKANNT) + .inService(Id).withCause(cause) + .buildWithMessage(buildUndefinedErrorMessageWithCauseMessages(cause)); + } + private TechnischeWlsException createUnknownTechnischeWlsExceptionWithCause(final Throwable cause) { return TechnischeWlsException.withCode(ExceptionKonstanten.CODE_ALLGEMEIN_UNBEKANNT).inService(ExceptionKonstanten.SERVICE_UNBEKANNT).withCause(cause) .buildWithMessage(buildUndefinedErrorMessageWithCauseMessages(cause)); @@ -76,11 +83,4 @@ private WlsException createException(final WlsExceptionDTO wahlExceptionDTO) { private WlsException completeWithDTOData(final CodeIsSet startedWlsExceptionCreation, final WlsExceptionDTO dtoData) { return startedWlsExceptionCreation.inService(dtoData.service()).buildWithMessage(dtoData.message()); } - - public TechnischeWlsException createFalseObjectReferenceException (String Id,final Throwable cause){ - return TechnischeWlsException - .withCode(ExceptionKonstanten.CODE_ALLGEMEIN_UNBEKANNT) - .inService(Id).withCause(cause) - .buildWithMessage(buildUndefinedErrorMessageWithCauseMessages(cause)); - } } diff --git a/wls-common/security/pom.xml b/wls-common/security/pom.xml index 84167bce1..9c2bd7367 100644 --- a/wls-common/security/pom.xml +++ b/wls-common/security/pom.xml @@ -24,12 +24,6 @@ 13.0 compile - - - - org.springframework.boot - spring-boot-starter-logging - org.springframework.security spring-security-oauth2-client @@ -42,10 +36,6 @@ jakarta.persistence jakarta.persistence-api - - jakarta.validation - jakarta.validation-api - de.muenchen.oss.wahllokalsystem.wls-common exception @@ -62,6 +52,20 @@ lombok provided + + + + org.springframework.boot + spring-boot-starter-logging + + + + + jakarta.validation + jakarta.validation-api + + + org.junit.jupiter junit-jupiter @@ -78,7 +82,13 @@ mockito-junit-jupiter test + + org.springframework.boot + spring-boot-test + test + + diff --git a/wls-common/security/src/main/java/de/muenchen/wls/common/security/DummyBezirkIdPermissionEvaluatorImpl.java b/wls-common/security/src/main/java/de/muenchen/wls/common/security/DummyBezirkIdPermissionEvaluatorImpl.java index 8337d0c14..e12b26971 100644 --- a/wls-common/security/src/main/java/de/muenchen/wls/common/security/DummyBezirkIdPermissionEvaluatorImpl.java +++ b/wls-common/security/src/main/java/de/muenchen/wls/common/security/DummyBezirkIdPermissionEvaluatorImpl.java @@ -17,9 +17,4 @@ public boolean tokenUserBezirkIdMatches(String bezirkId, Authentication auth) { LOG.info("tokenUserBezirkIdMatches {}, {}", bezirkId, auth.getPrincipal()); return true; } - - private String loadBezirkID(String username) { - LOG.debug("#loadBezirkID {}", username); - return "123"; - } } diff --git a/wls-common/security/src/test/java/de/muenchen/wls/common/security/BezirkIDPermissionEvaluatorImplTest.java b/wls-common/security/src/test/java/de/muenchen/wls/common/security/BezirkIDPermissionEvaluatorImplTest.java new file mode 100644 index 000000000..d9b02ba19 --- /dev/null +++ b/wls-common/security/src/test/java/de/muenchen/wls/common/security/BezirkIDPermissionEvaluatorImplTest.java @@ -0,0 +1,61 @@ +package de.muenchen.wls.common.security; + +import de.muenchen.wls.common.security.testultils.LoggerExtension; +import lombok.val; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; + +import java.util.HashMap; + +class BezirkIDPermissionEvaluatorImplTest { + + private final BezirkIDPermissionEvaluatorImpl unitUnderTest= new BezirkIDPermissionEvaluatorImpl(); + + @Nested + class TestTokenUserBezirkIdMatches { + + + @RegisterExtension + public LoggerExtension loggerExtension = new LoggerExtension(); + + @Test + void warnOnAuthenticationIsNull() { + unitUnderTest.tokenUserBezirkIdMatches("1234",null); + Assertions.assertThat(loggerExtension.getFormattedMessages().contains("No authentication object for bezirkId=1234")).isTrue(); + } + + @Test + void errorWhileChecking() { + AuthenticationManager authManager = authentication -> authentication; + UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("username", "password"); + Authentication auth = authManager.authenticate(authRequest); + SecurityContext context = SecurityContextHolder.createEmptyContext(); + context.setAuthentication(auth); + SecurityContextHolder.setContext(context); + unitUnderTest.tokenUserBezirkIdMatches("1234", auth); + Assertions.assertThat(loggerExtension.getFormattedMessages().contains("Error while checking bezirkId.")).isTrue(); + } + + @Test + void bezirkIDMatches() { + val map = new HashMap<>(); + map.put("bezirkID", "1234"); + map.put("wahlbezirkID", "1234"); + AuthenticationManager authManager = authentication -> authentication; + UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("username", "password"); + authRequest.setDetails(map); + Authentication auth = authManager.authenticate(authRequest); + SecurityContext context = SecurityContextHolder.createEmptyContext(); + context.setAuthentication(auth); + SecurityContextHolder.setContext(context); + Assertions.assertThat(unitUnderTest.tokenUserBezirkIdMatches("1234", auth)).isTrue(); + } + } +} diff --git a/wls-common/security/src/test/java/de/muenchen/wls/common/security/BezirkIDUndWaehlerverzeichnisNummerTest.java b/wls-common/security/src/test/java/de/muenchen/wls/common/security/BezirkIDUndWaehlerverzeichnisNummerTest.java new file mode 100644 index 000000000..7f9ab9192 --- /dev/null +++ b/wls-common/security/src/test/java/de/muenchen/wls/common/security/BezirkIDUndWaehlerverzeichnisNummerTest.java @@ -0,0 +1,36 @@ +package de.muenchen.wls.common.security; + +import de.muenchen.wls.common.security.domain.BezirkIDUndWaehlerverzeichnisNummer; +import lombok.val; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +class BezirkIDUndWaehlerverzeichnisNummerTest { + + @Test + void returnsHash() { + val unitUnderTest = new BezirkIDUndWaehlerverzeichnisNummer(); + unitUnderTest.setWaehlerverzeichnisNummer(Long.valueOf("1")); + unitUnderTest.setWahlbezirkID("2"); + Assertions.assertThat(unitUnderTest.hashCode()).isEqualTo(2512); + } + + @Test + void returnsString() { + val unitUnderTest = new BezirkIDUndWaehlerverzeichnisNummer(); + unitUnderTest.setWaehlerverzeichnisNummer(Long.valueOf("1")); + unitUnderTest.setWahlbezirkID("2"); + Assertions.assertThat(unitUnderTest.toString()).isEqualTo("BezirkIDUndWaehlerverzeichnisNummer{" + "wahlbezirkID='" + 2 + '\'' +", waehlerverzeichnisNummer=" + 1 +'}'); + } + + @Test + void equals() { + val unitUnderTest = new BezirkIDUndWaehlerverzeichnisNummer(); + unitUnderTest.setWaehlerverzeichnisNummer(Long.valueOf("1")); + unitUnderTest.setWahlbezirkID("2"); + val falseObject = new BezirkIDUndWaehlerverzeichnisNummer(); + falseObject.setWaehlerverzeichnisNummer(Long.valueOf("2")); + falseObject.setWahlbezirkID("3"); + Assertions.assertThat(unitUnderTest.equals(falseObject)).isFalse(); + } +} diff --git a/wls-common/security/src/test/java/de/muenchen/wls/common/security/BezirkUndWahlIDTest.java b/wls-common/security/src/test/java/de/muenchen/wls/common/security/BezirkUndWahlIDTest.java new file mode 100644 index 000000000..f591ae242 --- /dev/null +++ b/wls-common/security/src/test/java/de/muenchen/wls/common/security/BezirkUndWahlIDTest.java @@ -0,0 +1,36 @@ +package de.muenchen.wls.common.security; + +import de.muenchen.wls.common.security.domain.BezirkUndWahlID; +import lombok.val; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +class BezirkIDUndWahlIDTest { + + @Test + void returnsHash() { + val unitUnderTest = new BezirkUndWahlID(); + unitUnderTest.setWahlID("1"); + unitUnderTest.setWahlbezirkID("2"); + Assertions.assertThat(unitUnderTest.hashCode()).isEqualTo(2530); + } + + @Test + void returnsString() { + val unitUnderTest = new BezirkUndWahlID(); + unitUnderTest.setWahlID("1"); + unitUnderTest.setWahlbezirkID("2"); + Assertions.assertThat(unitUnderTest.toString()).isEqualTo("de.muenchen.wls.common.security.domain.BezirkUndWahlID{" + "wahlID='" + 1 + '\'' +", wahlbezirkID='" + 2 + '\'' +'}'); + } + + @Test + void equals() { + val unitUnderTest = new BezirkUndWahlID(); + unitUnderTest.setWahlID("1"); + unitUnderTest.setWahlbezirkID("2"); + val falseObject = new BezirkUndWahlID(); + falseObject.setWahlID("2"); + falseObject.setWahlbezirkID("3"); + Assertions.assertThat(unitUnderTest.equals(falseObject)).isFalse(); + } +} diff --git a/wls-common/security/src/test/java/de/muenchen/wls/common/security/DummyBezirkIdPermissionEvaluatorImplTest.java b/wls-common/security/src/test/java/de/muenchen/wls/common/security/DummyBezirkIdPermissionEvaluatorImplTest.java new file mode 100644 index 000000000..1dd3166e7 --- /dev/null +++ b/wls-common/security/src/test/java/de/muenchen/wls/common/security/DummyBezirkIdPermissionEvaluatorImplTest.java @@ -0,0 +1,41 @@ +package de.muenchen.wls.common.security; +import de.muenchen.wls.common.security.testultils.LoggerExtension; +import lombok.val; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; + +import java.util.HashMap; + +class DummyBezirkIdPermissionEvaluatorImplTest { + + private final DummyBezirkIdPermissionEvaluatorImpl unitUnderTest= new DummyBezirkIdPermissionEvaluatorImpl(); + + @Nested + class TestLoggingEvents { + + @RegisterExtension + public LoggerExtension loggerExtension = new LoggerExtension(); + + @Test + void logInfoMatch() { + val map = new HashMap<>(); + map.put("bezirkID", "1234"); + map.put("wahlbezirkID", "1234"); + AuthenticationManager authManager = authentication -> authentication; + UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken("username", "password"); + authRequest.setDetails(map); + Authentication auth = authManager.authenticate(authRequest); + SecurityContext context = SecurityContextHolder.createEmptyContext(); + context.setAuthentication(auth); + SecurityContextHolder.setContext(context); + Assertions.assertThat(unitUnderTest.tokenUserBezirkIdMatches("1234", auth)).isTrue(); + } + } +} diff --git a/wls-common/security/src/test/java/de/muenchen/wls/common/security/EncryptionBuilderTest.java b/wls-common/security/src/test/java/de/muenchen/wls/common/security/EncryptionBuilderTest.java new file mode 100644 index 000000000..77b416690 --- /dev/null +++ b/wls-common/security/src/test/java/de/muenchen/wls/common/security/EncryptionBuilderTest.java @@ -0,0 +1,70 @@ +package de.muenchen.wls.common.security; + +import de.muenchen.wls.common.security.testultils.LoggerExtension; +import lombok.val; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.extension.RegisterExtension; + +import javax.crypto.NoSuchPaddingException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + + +class EncryptionBuilderTest { + + @RegisterExtension + public LoggerExtension loggerExtension = new LoggerExtension(); + + @Nested + class decrypt { + + @Test + void sucessful() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException { + val aByte = new byte[16]; + val unitUnderTest = new EncryptionBuilder(aByte); + Assertions.assertThat(unitUnderTest.decryptValue("Efl8HLaoqguJ-CkS4r_m_QFD22PuZrDN_59pkXaAFR4=")).isEqualTo("376526723AFDAB3D"); + } + + @Test + void throwBadPadding() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException { + val aByte = new byte[16]; + val random = new SecureRandom(); + random.nextBytes(aByte); + val unitUnderTest = new EncryptionBuilder(aByte); + try { + unitUnderTest.decryptValue("Efl8HLaoqguJ-CkS4r_m_QFD22PuZrDN_59pkXaAFR4="); + } catch (Exception e) { + Assertions.assertThat(loggerExtension.getFormattedMessages().size()).isEqualTo(1); + } + + } + } + + @Nested + class encrypt { + + @Test + void successful() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException { + val aByte = new byte[16]; + val unitUnderTest = new EncryptionBuilder(aByte); + Assertions.assertThat(unitUnderTest.encryptValue("376526723AFDAB3D")).isEqualTo("Efl8HLaoqguJ-CkS4r_m_QFD22PuZrDN_59pkXaAFR4="); + } + + @Test + void throwBadPadding() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException { + val aByte = new byte[16]; + val random = new SecureRandom(); + random.nextBytes(aByte); + val unitUnderTest = new EncryptionBuilder(aByte); + try { + unitUnderTest.encryptValue("376526723AFDAB3D"); + } catch (Exception e) { + Assertions.assertThat(loggerExtension.getFormattedMessages().size()).isEqualTo(1); + } + } + + } +} diff --git a/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/testultils/LoggerExtension.java b/wls-common/security/src/test/java/de/muenchen/wls/common/security/testultils/LoggerExtension.java similarity index 95% rename from wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/testultils/LoggerExtension.java rename to wls-common/security/src/test/java/de/muenchen/wls/common/security/testultils/LoggerExtension.java index 5a45b9804..b7db6fcd4 100644 --- a/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/testultils/LoggerExtension.java +++ b/wls-common/security/src/test/java/de/muenchen/wls/common/security/testultils/LoggerExtension.java @@ -1,4 +1,4 @@ -package de.muenchen.wls.common.wls.security.testultils; +package de.muenchen.wls.common.security.testultils; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; diff --git a/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/BezirkIDPermissionEvaluatorImplTest.java b/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/BezirkIDPermissionEvaluatorImplTest.java deleted file mode 100644 index 2bb7ce989..000000000 --- a/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/BezirkIDPermissionEvaluatorImplTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.muenchen.wls.common.wls.security; - -import de.muenchen.wls.common.security.BezirkIDPermissionEvaluatorImpl; -import de.muenchen.wls.common.wls.security.testultils.LoggerExtension; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -class BezirkIDPermissionEvaluatorImplTest { - - private final BezirkIDPermissionEvaluatorImpl unitUnderTest= new BezirkIDPermissionEvaluatorImpl(); - - @Nested - class TestLoggingEvents { - - @RegisterExtension - public LoggerExtension loggerExtension = new LoggerExtension(); - - @Test - void warnOnAuthenticationIsNull() { - unitUnderTest.tokenUserBezirkIdMatches("1234",null); - Assertions.assertThat(loggerExtension.getFormattedMessages().size()).isEqualTo(1); - } - } -} diff --git a/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/DummyBezirkIdPermissionEvaluatorImplTest.java b/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/DummyBezirkIdPermissionEvaluatorImplTest.java deleted file mode 100644 index 7a772727d..000000000 --- a/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/DummyBezirkIdPermissionEvaluatorImplTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.muenchen.wls.common.wls.security; -import de.muenchen.wls.common.security.DummyBezirkIdPermissionEvaluatorImpl; -import de.muenchen.wls.common.wls.security.testultils.LoggerExtension; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -class DummyBezirkIdPermissionEvaluatorImplTest { - - private final DummyBezirkIdPermissionEvaluatorImpl unitUnderTest= new DummyBezirkIdPermissionEvaluatorImpl(); - - @Nested - class TestLoggingEvents { - - @RegisterExtension - public LoggerExtension loggerExtension = new LoggerExtension(); - - @Test - void logInfoMatch() { - unitUnderTest.tokenUserBezirkIdMatches("1234",null); - Assertions.assertThat(loggerExtension.getFormattedMessages().size()).isEqualTo(1); - } - } -} diff --git a/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/EncryptionBuilderTest.java b/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/EncryptionBuilderTest.java deleted file mode 100644 index ec162c47f..000000000 --- a/wls-common/security/src/test/java/de/muenchen/wls/common/wls/security/EncryptionBuilderTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package de.muenchen.wls.common.wls.security; - -import de.muenchen.wls.common.security.EncryptionBuilder; -import lombok.val; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; - -import javax.crypto.NoSuchPaddingException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.Arrays; -import java.util.Base64; - - -@ExtendWith(MockitoExtension.class) -class EncryptionBuilderTest { - byte[] toBeDecrypted = "Mzc2NTI2NzIzQUZEQUIzRD==".getBytes(); - byte [] toBeEncrypted = "376526723AFDAB3D".getBytes(); - - @Mock - private EncryptionBuilder mockedCreator; - - @Test - void decrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException { - Mockito.when(mockedCreator.decryptValue("Mzc2NTI2NzIzQUZEQUIzRD==")).thenReturn("376526723AFDAB3D"); - val aByte = new byte[16]; - val secureRandom = new SecureRandom(); - secureRandom.nextBytes(aByte); - val unitUnderTest = new EncryptionBuilder(aByte); - Assertions.assertThat(unitUnderTest.decryptValue("Mzc2NTI2NzIzQUZEQUIzRD==").contains((Arrays.toString(Base64.getUrlDecoder().decode(toBeDecrypted))))).isTrue(); - } - - @Test - void encrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException { - Mockito.when(mockedCreator.encryptValue("376526723AFDAB3D")).thenReturn("Mzc2NTI2NzIzQUZEQUIzRD=="); - val aByte = new byte[16]; - val secureRandom = new SecureRandom(); - secureRandom.nextBytes(aByte); - val unitUnderTest = new EncryptionBuilder(aByte); - Assertions.assertThat(unitUnderTest.encryptValue("376526723AFDAB3D").contains(Arrays.toString(Base64.getUrlEncoder().encode(toBeEncrypted)))).isTrue(); - } -}