Skip to content

Commit 80c1fb9

Browse files
committed
Get keycloak working, but test still fails
1 parent 33fe8c3 commit 80c1fb9

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

security-oauth2/src/test/groovy/io/micronaut/security/oauth2/e2e/OpenIdAuthorizationCodeSpec.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class OpenIdAuthorizationCodeSpec extends GebEmbeddedServerSpecification {
5555

5656
@IgnoreIf({ System.getProperty(Keycloak.SYS_TESTCONTAINERS) != null && !Boolean.valueOf(System.getProperty(Keycloak.SYS_TESTCONTAINERS)) })
5757
void "test a full login"() {
58+
5859
when:
5960
browser.go "/oauth/login/keycloak"
6061

test-suite-utils/src/main/groovy/io/micronaut/security/testutils/Keycloak.groovy

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
1-
/*
2-
* Copyright 2017-2021 original authors
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
161
package io.micronaut.security.testutils
172

183
import dasniko.testcontainers.keycloak.KeycloakContainer
194
import org.testcontainers.Testcontainers
20-
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy
21-
22-
import java.time.Duration
235

246
class Keycloak {
257
static final String SYS_TESTCONTAINERS = "testcontainers"
@@ -48,33 +30,58 @@ class Keycloak {
4830
Integer.valueOf(issuer.substring(issuer.indexOf("localhost:") + "localhost:".length(), issuer.indexOf("/realms")))
4931
}
5032

33+
private static exec(String... parts) {
34+
println "Running command: " + parts.join(" ")
35+
def result = keycloak.execInContainer(parts)
36+
println "OUT: $result.stdout"
37+
println "ERR: $result.stderr"
38+
println "EXIT: $result.exitCode"
39+
assert result.exitCode == 0
40+
}
41+
5142
static void init() {
5243
if (keycloak == null) {
5344

5445
keycloak = new KeycloakContainer()
5546
.withAdminUsername("admin")
5647
.withAdminPassword("admin")
5748
.withExposedPorts(8080)
58-
.withEnv(Map.of(
59-
"KEYCLOAK_USER", "user",
60-
"KEYCLOAK_PASSWORD", "password",
61-
"DB_VENDOR", "H2")
62-
)
63-
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*powered by Quarkus.*").withStartupTimeout(Duration.ofMinutes(5)))
49+
.withContextPath("/auth")
50+
.withEnv(DB_VENDOR: "H2")
6451

6552
keycloak.start()
66-
keycloak.execInContainer("/opt/keycloak/bin/kcreg.sh config credentials " +
67-
"--server http://localhost:8080/auth " +
68-
"--realm master --user user --password password"
69-
.split(" "))
70-
keycloak.execInContainer("/opt/keycloak/bin/kcreg.sh " +
71-
"create -s clientId=$CLIENT_ID " +
72-
"-s redirectUris=[\"http://${TestContainersUtils.host}*\"] " +
73-
"-s secret=$clientSecret"
74-
.split(" "))
53+
// Login
54+
exec("/opt/keycloak/bin/kcadm.sh",
55+
"config", "credentials",
56+
"--server", "http://localhost:8080/auth",
57+
"--realm", "master",
58+
"--user", "admin",
59+
"--password", "admin")
60+
// Add user
61+
exec("/opt/keycloak/bin/kcadm.sh",
62+
"create", "users",
63+
"-s", "username=user",
64+
"-s", "enabled=true",
65+
"-o",
66+
"--fields", "id,username")
67+
// Set user password
68+
exec("/opt/keycloak/bin/kcadm.sh",
69+
"set-password",
70+
"--username", "user",
71+
"--new-password", "password")
72+
// Add client
73+
exec("/opt/keycloak/bin/kcreg.sh",
74+
"create",
75+
"--server", "http://localhost:8080/auth",
76+
"--realm", "master",
77+
"--user", "admin",
78+
"--password", "admin",
79+
"-s", "clientId=$CLIENT_ID",
80+
"-s", "redirectUris=[\"http://${redirectUriHost}*\"]",
81+
"-s", "secret=$clientSecret")
7582
int port = keycloak.getMappedPort(8080)
7683
Testcontainers.exposeHostPorts(port)
77-
issuer = "http://" + getHost() + ":" + port + "/realms/master"
84+
issuer = "http://$host:$port/auth/realms/master"
7885
}
7986
}
8087

0 commit comments

Comments
 (0)