|
| 1 | +package com.predic8.membrane.examples.withoutinternet.openapi; |
| 2 | + |
| 3 | +import com.predic8.membrane.examples.util.DistributionExtractingTestcase; |
| 4 | +import com.predic8.membrane.examples.util.Process2; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | +import org.junit.jupiter.api.AfterEach; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.io.File; |
| 11 | +import java.io.IOException; |
| 12 | +import java.text.MessageFormat; |
| 13 | + |
| 14 | +import static com.predic8.membrane.core.util.OSUtil.isWindows; |
| 15 | +import static io.restassured.RestAssured.given; |
| 16 | +import static io.restassured.http.ContentType.JSON; |
| 17 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 18 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 19 | +import static org.hamcrest.Matchers.*; |
| 20 | + |
| 21 | +public class OpenApiJwtAuthExampleTest extends DistributionExtractingTestcase { |
| 22 | + |
| 23 | + private Process2 process; |
| 24 | + |
| 25 | + @Override |
| 26 | + protected String getExampleDirName() { |
| 27 | + return "openapi/jwt-auth"; |
| 28 | + } |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + void setup() throws Exception { |
| 32 | + runGenerateJwk(getExampleDir(getExampleDirName())); |
| 33 | + process = startServiceProxyScript(); |
| 34 | + } |
| 35 | + |
| 36 | + @AfterEach |
| 37 | + void stopMembrane() { |
| 38 | + if (process != null) |
| 39 | + process.killScript(); |
| 40 | + } |
| 41 | + |
| 42 | + private static String fetchJwt() { |
| 43 | + // @formatter:off |
| 44 | + return given() |
| 45 | + .when() |
| 46 | + .get("http://localhost:2000/") |
| 47 | + .then() |
| 48 | + .statusCode(200) |
| 49 | + .extract() |
| 50 | + .asString() |
| 51 | + .trim(); |
| 52 | + // @formatter:on |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void shouldListProducts_whenValidJwtProvided() { |
| 57 | + String jwt = fetchJwt(); |
| 58 | + // @formatter:off |
| 59 | + given() |
| 60 | + .header("Authorization", "Bearer " + jwt) |
| 61 | + .when() |
| 62 | + .get("http://localhost:2001/shop/v2/products") |
| 63 | + .then() |
| 64 | + .statusCode(200) |
| 65 | + .contentType(JSON) |
| 66 | + .body("meta.count", greaterThan(0)) |
| 67 | + .body("products", is(not(empty()))) |
| 68 | + .body("products[0].id", notNullValue()); |
| 69 | + // @formatter:on |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void shouldReturnSecurityProblem_whenMissingJwt() { |
| 74 | + // @formatter:off |
| 75 | + given() |
| 76 | + .accept(JSON) |
| 77 | + .when() |
| 78 | + .get("http://localhost:2001/shop/v2/products") |
| 79 | + .then() |
| 80 | + .statusCode(400) |
| 81 | + .body("type", equalTo("https://membrane-api.io/problems/security")) |
| 82 | + .body("detail", containsString("Could not retrieve JWT")); |
| 83 | + // @formatter:on |
| 84 | + } |
| 85 | + |
| 86 | + private static void runGenerateJwk(File dir) throws Exception { |
| 87 | + File jwk = new File(dir, "jwk.json"); |
| 88 | + if (jwk.isFile() && jwk.length() > 0) return; |
| 89 | + |
| 90 | + Process p = createJwkProcess(dir); |
| 91 | + int exit = p.waitFor(); |
| 92 | + |
| 93 | + if (exit != 0) { |
| 94 | + throw new IllegalStateException(MessageFormat.format("generate-jwk failed (exit {0}):\n{1}", exit, new String(p.getInputStream().readAllBytes(), UTF_8))); |
| 95 | + } |
| 96 | + if (!jwk.isFile() || jwk.length() == 0) { |
| 97 | + throw new IllegalStateException("generate-jwk did not create jwk.json in %s".formatted(dir.getAbsolutePath())); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static @NotNull Process createJwkProcess(File dir) throws IOException { |
| 102 | + File sh = new File(dir, "membrane.sh"); |
| 103 | + File bat = new File(dir, "membrane.bat"); |
| 104 | + |
| 105 | + ProcessBuilder pb; |
| 106 | + if (isWindows()) { |
| 107 | + String script = bat.exists() ? "membrane.bat" : "membrane"; |
| 108 | + pb = new ProcessBuilder("cmd", "/c", script, "generate-jwk", "-o", "jwk.json"); |
| 109 | + } else { |
| 110 | + String scriptPath = sh.exists() ? sh.getAbsolutePath() : new File(dir, "membrane").getAbsolutePath(); |
| 111 | + pb = new ProcessBuilder("bash", scriptPath, "generate-jwk", "-o", "./jwk.json"); |
| 112 | + } |
| 113 | + |
| 114 | + pb.directory(dir); |
| 115 | + pb.redirectErrorStream(true); |
| 116 | + |
| 117 | + return pb.start(); |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments