Skip to content

Commit

Permalink
Merge pull request #20035 from dimas-b/inject-wiremock
Browse files Browse the repository at this point in the history
Support injecting OIDC WireMockServer into tests
  • Loading branch information
sberyozkin authored Sep 13, 2021
2 parents 0a9bdbc + fa444fe commit b79308a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.it.keycloak;

import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static org.hamcrest.Matchers.equalTo;

import java.time.Instant;
Expand All @@ -10,8 +11,12 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.oidc.server.OidcWireMock;
import io.quarkus.test.oidc.server.OidcWiremockTestResource;
import io.restassured.RestAssured;
import io.smallrye.jwt.build.Jwt;
Expand All @@ -20,6 +25,9 @@
@QuarkusTestResource(OidcWiremockTestResource.class)
public class BearerTokenAuthorizationTest {

@OidcWireMock
WireMockServer wireMockServer;

@Test
public void testSecureAccessSuccessPreferredUsername() {
for (String username : Arrays.asList("alice", "admin")) {
Expand Down Expand Up @@ -98,6 +106,19 @@ public void testBearerTokenWrongAudience() {
.header("WWW-Authenticate", equalTo("Bearer"));
}

@Test
public void testInvalidBearerToken() {
wireMockServer.stubFor(WireMock.post("/auth/realms/quarkus/protocol/openid-connect/token/introspect")
.withRequestBody(matching(".*token=invalid_token.*"))
.willReturn(WireMock.aResponse().withStatus(400)));

RestAssured.given().auth().oauth2("invalid_token").when()
.get("/api/users/me/bearer")
.then()
.statusCode(401)
.header("WWW-Authenticate", equalTo("Bearer"));
}

private String getAccessToken(String userName, Set<String> groups) {
return Jwt.preferredUserName(userName)
.groups(groups)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.test.oidc.server;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.github.tomakehurst.wiremock.WireMockServer;

/**
* Used to specify that the field should be injected with the {@link WireMockServer}
* server that provides mock HTTP API for OIDC clients.
* <p>
* Note: for this injection to work the test must use {@link OidcWiremockTestResource}.
* </p>
* <p>
* The main purpose of injecting the {@link WireMockServer} is for tests to be able
* to mock extra URLs not covered by {@link OidcWiremockTestResource}.
* </p>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface OidcWireMock {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import io.smallrye.jwt.build.Jwt;

/**
* Provides a mock OIDC server to tests.
*
* @see OidcWireMock
*/
public class OidcWiremockTestResource implements QuarkusTestResourceLifecycleManager {

private static final Logger LOG = Logger.getLogger(OidcWiremockTestResource.class);
Expand Down Expand Up @@ -192,6 +197,12 @@ private String generateJwtToken(String userName, Set<String> groups) {
.sign("privateKey.jwk");
}

@Override
public void inject(TestInjector testInjector) {
testInjector.injectIntoFields(server,
new TestInjector.AnnotatedAndMatchesType(OidcWireMock.class, WireMockServer.class));
}

@Override
public synchronized void stop() {
if (server != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.quarkus.test.oidc.server;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import com.github.tomakehurst.wiremock.WireMockServer;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.common.TestResourceManager;

/**
* Validates the injection of {@link WireMockServer} objects into test instances by {@link OidcWiremockTestResource}.
*/
class OidcWiremockTestResourceInjectionTest {

@Test
void testWireMockServerInjection() {
TestResourceManager manager = new TestResourceManager(CustomTest.class);
manager.start();

CustomTest test = new CustomTest();
manager.inject(test);
assertNotNull(test.server);
}

@QuarkusTestResource(OidcWiremockTestResource.class)
public static class CustomTest {
@OidcWireMock
WireMockServer server;
}
}

0 comments on commit b79308a

Please sign in to comment.