Skip to content

Commit

Permalink
Tests vervollständigt, Schönheitsanpassungen
Browse files Browse the repository at this point in the history
  • Loading branch information
GerhardPx committed Apr 17, 2024
1 parent 38d24ab commit 6fa9777
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
}
}
30 changes: 20 additions & 10 deletions wls-common/security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
<version>13.0</version>
<scope>compile</scope>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
Expand All @@ -42,10 +36,6 @@
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
<dependency>
<groupId>de.muenchen.oss.wahllokalsystem.wls-common</groupId>
<artifactId>exception</artifactId>
Expand All @@ -62,6 +52,20 @@
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>

<!-- Validation -->
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>

<!-- Tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand All @@ -78,7 +82,13 @@
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

This file was deleted.

Loading

0 comments on commit 6fa9777

Please sign in to comment.