-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: asjervanasten <[email protected]>
- Loading branch information
1 parent
9fd53f9
commit 7bb6cf3
Showing
10 changed files
with
228 additions
and
75 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
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
99 changes: 99 additions & 0 deletions
99
core/src/main/java/org/microshed/testing/jwt/JwtConfigExtension.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,99 @@ | ||
package org.microshed.testing.jwt; | ||
|
||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionConfigurationException; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.microshed.testing.internal.InternalLogger; | ||
import org.microshed.testing.jupiter.MicroShedTestExtension; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
|
||
public class JwtConfigExtension implements BeforeEachCallback, AfterEachCallback { | ||
|
||
private static final InternalLogger LOG = InternalLogger.get(JwtConfigExtension.class); | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) throws Exception { | ||
configureJwt(context); | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
removeJwt(context); | ||
} | ||
|
||
private void configureJwt(ExtensionContext context) throws Exception { | ||
// Check if RestAssured is being used | ||
Class<?> restAssuredClass = tryLoad("io.restassured.RestAssured"); | ||
if (restAssuredClass == null) { | ||
LOG.debug("RESTAssured not found!"); | ||
return; | ||
} | ||
|
||
LOG.debug("RESTAssured found!"); | ||
|
||
// Check if the test method has the @JwtConfig annotation | ||
Method testMethod = context.getTestMethod().orElse(null); | ||
if (testMethod != null) { | ||
JwtConfig jwtConfig = testMethod.getAnnotation(JwtConfig.class); | ||
if (jwtConfig != null) { | ||
// Configure RestAssured with the values from @JwtConfig for each test method | ||
LOG.info("JWTConfig on method: " + testMethod.getName()); | ||
// Get the RequestSpecBuilder class | ||
Class<?> requestSpecBuilderClass = Class.forName("io.restassured.builder.RequestSpecBuilder"); | ||
// Create an instance of RequestSpecBuilder | ||
Object requestSpecBuilder = requestSpecBuilderClass.newInstance(); | ||
// Get the requestSpecification field | ||
Field requestSpecificationField = restAssuredClass.getDeclaredField("requestSpecification"); | ||
requestSpecificationField.setAccessible(true); | ||
|
||
// Get the header method of RequestSpecBuilder | ||
Method headerMethod = requestSpecBuilderClass.getDeclaredMethod("addHeader", String.class, String.class); | ||
|
||
try { | ||
String jwt = JwtBuilder.buildJwt(jwtConfig.subject(), jwtConfig.issuer(), jwtConfig.claims()); | ||
headerMethod.invoke(requestSpecBuilder, "Authorization", "Bearer " + jwt); | ||
LOG.debug("Using provided JWT auth header: " + jwt); | ||
} catch (Exception e) { | ||
throw new ExtensionConfigurationException("Error while building JWT for method " + testMethod.getName() + " with JwtConfig: " + jwtConfig, e); | ||
} | ||
|
||
// Set the updated requestSpecification | ||
requestSpecificationField.set(null, requestSpecBuilderClass.getMethod("build").invoke(requestSpecBuilder)); | ||
} | ||
} | ||
} | ||
|
||
private void removeJwt(ExtensionContext context) throws Exception { | ||
// Check if RestAssured is being used | ||
Class<?> restAssuredClass = tryLoad("io.restassured.RestAssured"); | ||
if (restAssuredClass == null) { | ||
LOG.debug("RESTAssured not found!"); | ||
return; | ||
} | ||
|
||
// Check if the test method has the @JwtConfig annotation | ||
Method testMethod = context.getTestMethod().orElse(null); | ||
if (testMethod != null) { | ||
|
||
LOG.debug("Method was annotated with: " + testMethod.getName()); | ||
// Get the requestSpecification field | ||
Field requestSpecificationField = restAssuredClass.getDeclaredField("requestSpecification"); | ||
requestSpecificationField.setAccessible(true); | ||
|
||
// Removes all requestSpec | ||
requestSpecificationField.set(null, null); | ||
} | ||
} | ||
|
||
private static Class<?> tryLoad(String clazz) { | ||
try { | ||
return Class.forName(clazz, false, MicroShedTestExtension.class.getClassLoader()); | ||
} catch (ClassNotFoundException | LinkageError e) { | ||
return null; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.