forked from quarkusio/quarkus
/
CodeFlowDevModeTestCase.java
116 lines (87 loc) · 4.76 KB
/
CodeFlowDevModeTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package io.quarkus.oidc.test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import io.quarkus.test.QuarkusDevModeTest;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.keycloak.server.KeycloakTestResourceLifecycleManager;
@QuarkusTestResource(KeycloakTestResourceLifecycleManager.class)
public class CodeFlowDevModeTestCase {
private static Class<?>[] testClasses = {
ProtectedResource.class,
UnprotectedResource.class,
CustomTenantConfigResolver.class,
CustomTokenStateManager.class
};
@RegisterExtension
static final QuarkusDevModeTest test = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(testClasses)
.addAsResource("application-dev-mode.properties", "application.properties"));
@Test
public void testAccessAndRefreshTokenInjectionDevMode() throws IOException, InterruptedException {
// Default tenant is disabled, check that having TenantConfigResolver is enough
useTenantConfigResolver();
try (final WebClient webClient = createWebClient()) {
// Default tenant is disabled and client-id is wrong
HtmlPage page = webClient.getPage("http://localhost:8080/unprotected");
assertEquals("unprotected", page.getBody().asText());
try {
webClient.getPage("http://localhost:8080/protected");
fail("Exception is expected because the tenant is disabled and invalid client_id is used");
} catch (FailingHttpStatusCodeException ex) {
// Reported by Quarkus
assertEquals(401, ex.getStatusCode());
}
// Enable the default tenant
test.modifyResourceFile("application.properties", s -> s.replace("tenant-enabled=false", "tenant-enabled=true"));
// Default tenant is enabled, client-id is wrong
try {
webClient.getPage("http://localhost:8080/protected");
fail("Exception is expected because the tenant is disabled and invalid client_id is used");
} catch (FailingHttpStatusCodeException ex) {
// Reported by Keycloak
assertEquals(400, ex.getStatusCode());
assertTrue(ex.getResponse().getContentAsString().contains("Client not found"));
}
// Now set the correct client-id
test.modifyResourceFile("application.properties", s -> s.replace("client-dev", "quarkus-web-app"));
page = webClient.getPage("http://localhost:8080/protected");
assertEquals("Sign in to quarkus", page.getTitleText());
HtmlForm loginForm = page.getForms().get(0);
loginForm.getInputByName("username").setValueAttribute("alice");
loginForm.getInputByName("password").setValueAttribute("alice");
page = loginForm.getInputByName("login").click();
assertEquals("alice", page.getBody().asText());
assertEquals("custom", page.getWebClient().getCookieManager().getCookie("q_session").getValue().split("\\|")[3]);
webClient.getCookieManager().clearCookies();
}
}
private void useTenantConfigResolver() throws IOException, InterruptedException {
try (final WebClient webClient = createWebClient()) {
HtmlPage page = webClient.getPage("http://localhost:8080/protected/tenant/tenant-config-resolver");
assertEquals("Sign in to quarkus", page.getTitleText());
HtmlForm loginForm = page.getForms().get(0);
loginForm.getInputByName("username").setValueAttribute("alice");
loginForm.getInputByName("password").setValueAttribute("alice");
page = loginForm.getInputByName("login").click();
assertEquals("tenant-config-resolver:alice", page.getBody().asText());
webClient.getCookieManager().clearCookies();
}
}
private WebClient createWebClient() {
WebClient webClient = new WebClient();
webClient.setCssErrorHandler(new SilentCssErrorHandler());
return webClient;
}
}