-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c16371c
commit 1ef65be
Showing
4 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
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
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
62 changes: 62 additions & 0 deletions
62
ehealthid-rp/src/test/java/com/oviva/ehealthid/relyingparty/testenv/EnvironmentTest.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,62 @@ | ||
package com.oviva.ehealthid.relyingparty.testenv; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
class EnvironmentTest { | ||
|
||
@Test | ||
void test_envVariable() { | ||
|
||
var authHeader = "mySpecialS3cr3t"; | ||
var env = Map.of("GEMATIK_AUTH_HEADER", authHeader); | ||
|
||
Environment.getenv = env::get; | ||
|
||
var got = Environment.gematikAuthHeader(); | ||
|
||
assertEquals(authHeader, got); | ||
} | ||
|
||
@Test | ||
void test_envFile(@TempDir Path tempDir) throws IOException { | ||
|
||
var authHeader = "t0k3n"; | ||
|
||
var f = tempDir.resolve("env.properties"); | ||
Files.write( | ||
f, | ||
""" | ||
GEMATIK_AUTH_HEADER=%s | ||
""" | ||
.formatted(authHeader) | ||
.getBytes(StandardCharsets.UTF_8)); | ||
|
||
Environment.getenv = s -> null; | ||
Environment.envPath = () -> f; | ||
|
||
var got = Environment.gematikAuthHeader(); | ||
|
||
assertEquals(authHeader, got); | ||
} | ||
|
||
@Test | ||
void test_envFile_notFound(@TempDir Path tempDir) throws IOException { | ||
|
||
var f = tempDir.resolve("does_not_exist"); | ||
|
||
Environment.getenv = s -> null; | ||
Environment.envPath = () -> f; | ||
|
||
var got = Environment.gematikAuthHeader(); | ||
|
||
assertNull(got); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...t/java/com/oviva/ehealthid/relyingparty/testenv/GematikHeaderDecoratorHttpClientTest.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,108 @@ | ||
package com.oviva.ehealthid.relyingparty.testenv; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.oviva.ehealthid.fedclient.api.HttpClient; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
class GematikHeaderDecoratorHttpClientTest { | ||
|
||
@Test | ||
void noDecoration() { | ||
|
||
var env = Map.of("GEMATIK_AUTH_HEADER", "myheader"); | ||
Environment.getenv = env::get; | ||
|
||
var httpClient = mock(HttpClient.class); | ||
var client = new GematikHeaderDecoratorHttpClient(httpClient); | ||
|
||
var uri = URI.create("https://example.com"); | ||
var req = new HttpClient.Request(uri, "GET", List.of(), null); | ||
|
||
var res = client.call(req); | ||
|
||
var captor = ArgumentCaptor.forClass(HttpClient.Request.class); | ||
verify(httpClient).call(captor.capture()); | ||
|
||
var decoratedReq = captor.getValue(); | ||
|
||
var hasAuthHeader = | ||
decoratedReq.headers().stream().anyMatch(h -> "X-Authorization".equals(h.name())); | ||
|
||
assertFalse(hasAuthHeader); | ||
} | ||
|
||
@Test | ||
void worksIfHeaderMissing() { | ||
|
||
var env = Map.of("GEMATIK_AUTH_HEADER", ""); | ||
Environment.getenv = env::get; | ||
|
||
var httpClient = mock(HttpClient.class); | ||
var client = new GematikHeaderDecoratorHttpClient(httpClient); | ||
|
||
var uri = URI.create("https://gsi-ref-mtls.dev.gematik.solutions/PAR_Auth"); | ||
var req = new HttpClient.Request(uri, "GET", List.of(), null); | ||
|
||
// when | ||
var res = client.call(req); | ||
|
||
// then | ||
var captor = ArgumentCaptor.forClass(HttpClient.Request.class); | ||
verify(httpClient).call(captor.capture()); | ||
|
||
var decoratedReq = captor.getValue(); | ||
|
||
var hasAuthHeader = | ||
decoratedReq.headers().stream().anyMatch(h -> "X-Authorization".equals(h.name())); | ||
|
||
assertFalse(hasAuthHeader); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource( | ||
strings = { | ||
"https://gsi-ref.dev.gematik.solutions/.well-known/openid-federation", | ||
"https://gsi-ref-mtls.dev.gematik.solutions/PAR_Auth", | ||
"https://gsi.dev.gematik.solutions/.well-known/openid-federation", | ||
}) | ||
void decoratesRequest(String rawUri) { | ||
|
||
var authHeader = "mySpecialS3cr3t"; | ||
var env = Map.of("GEMATIK_AUTH_HEADER", authHeader); | ||
Environment.getenv = env::get; | ||
|
||
var mockRes = mock(HttpClient.Response.class); | ||
var httpClient = mock(HttpClient.class); | ||
when(httpClient.call(any())).thenReturn(mockRes); | ||
var client = new GematikHeaderDecoratorHttpClient(httpClient); | ||
|
||
var uri = URI.create(rawUri); | ||
var req = new HttpClient.Request(uri, "GET", List.of(), null); | ||
|
||
// when | ||
var res = client.call(req); | ||
|
||
// then | ||
var captor = ArgumentCaptor.forClass(HttpClient.Request.class); | ||
verify(httpClient).call(captor.capture()); | ||
|
||
var decoratedReq = captor.getValue(); | ||
|
||
var header = | ||
decoratedReq.headers().stream() | ||
.filter(h -> "X-Authorization".equals(h.name())) | ||
.findFirst() | ||
.orElseThrow(); | ||
|
||
assertEquals(authHeader, header.value()); | ||
assertEquals(mockRes, res); | ||
} | ||
} |