Skip to content

Commit d58c901

Browse files
committed
[java] Update and fix test
1 parent 98d9693 commit d58c901

File tree

9 files changed

+201
-58
lines changed

9 files changed

+201
-58
lines changed

common/src/web/fedcm/fedcm.html

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
<!DOCTYPE html>
2-
<script>
2+
<html>
3+
<head>
4+
<title>FedCM Example</title>
5+
</head>
6+
<body>
7+
<button id="triggerButton" onclick="triggerFedCm()">Trigger FedCM</button>
8+
<div id="result"></div>
39

4-
let configURL = `http://${location.host}/fedcm/fedcm.json`;
5-
let promise = null;
10+
<script>
11+
// Use a relative path for the configURL
12+
let configURL = `https://${location.host}/fedcm/config.json`;
13+
console.log(configURL)
14+
let result = null;
615

7-
function triggerFedCm() {
8-
console.log(configURL);
9-
promise = navigator.credentials.get({
10-
identity: {
11-
providers: [{
12-
configURL: configURL,
13-
clientId: '1',
14-
}]
15-
}
16-
});
17-
return promise;
18-
}
19-
20-
</script>
16+
async function triggerFedCm() {
17+
console.log("Config URL:", configURL);
18+
try {
19+
let promise = await navigator.credentials.get({
20+
identity: {
21+
providers: [{
22+
configURL: configURL,
23+
clientId: '1',
24+
}]
25+
}
26+
});
27+
result = promise;
28+
document.getElementById('result').innerText = JSON.stringify(result);
29+
} catch (error) {
30+
console.error("FedCM Error:", error);
31+
result = { error: error.message };
32+
document.getElementById('result').innerText = JSON.stringify(result);
33+
}
34+
}
35+
</script>
36+
</body>
37+
</html>

common/src/web/fedcm/login.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Login</title>
5+
</head>
6+
<body>
7+
<h1>Login Page</h1>
8+
<p>Login successful! This is a placeholder for the login process.</p>
9+
<a href="/">Return to Home</a>
10+
</body>
11+
</html>

common/src/web/fedcm/signin.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Sign In</title>
5+
</head>
6+
<body>
7+
<h1>Sign In Page</h1>
8+
<p>This is a placeholder for the sign-in page.</p>
9+
<form action="/signin" method="post">
10+
<label for="username">Username:</label>
11+
<input type="text" id="username" name="username" required>
12+
<br><br>
13+
<label for="password">Password:</label>
14+
<input type="password" id="password" name="password" required>
15+
<br><br>
16+
<button type="submit">Sign In</button>
17+
</form>
18+
</body>
19+
</html>

java/src/org/openqa/selenium/federatedcredentialmanagement/FederatedCredentialManagementDialog.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public interface FederatedCredentialManagementDialog {
5252
/** Returns the subtitle of the dialog or null if none. */
5353
String getSubtitle();
5454

55+
void clickDialog();
56+
5557
/**
5658
* Returns the accounts shown in the account chooser.
5759
*

java/src/org/openqa/selenium/remote/FedCmDialogImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public String getDialogType() {
4545
return (String) executeMethod.execute(DriverCommand.GET_FEDCM_DIALOG_TYPE, null);
4646
}
4747

48+
@Override
49+
public void clickDialog() {
50+
executeMethod.execute(
51+
DriverCommand.CLICK_DIALOG, Map.of("dialogButton", "ConfirmIdpLoginContinue"));
52+
}
53+
4854
@Override
4955
public String getTitle() {
5056
Map<String, Object> result =
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.environment.webserver;
19+
20+
21+
import java.io.UncheckedIOException;
22+
import java.util.Map;
23+
import org.openqa.selenium.remote.http.Contents;
24+
import org.openqa.selenium.remote.http.HttpHandler;
25+
import org.openqa.selenium.remote.http.HttpRequest;
26+
import org.openqa.selenium.remote.http.HttpResponse;
27+
28+
class FedCmConfigHandler implements HttpHandler {
29+
30+
@Override
31+
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
32+
HttpResponse response = new HttpResponse();
33+
response.setHeader("Content-Type", "application/json");
34+
response.setHeader("Cache-Control", "no-store");
35+
36+
response.setContent(
37+
Contents.asJson(
38+
Map.of(
39+
"accounts_endpoint", "accounts.json",
40+
"client_metadata_endpoint", "client_metadata.json",
41+
"id_assertion_endpoint", "id_assertion.json",
42+
"signin_url", "signin",
43+
"login_url", "login")));
44+
45+
return response;
46+
}
47+
}

java/test/org/openqa/selenium/environment/webserver/HandlersForTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public HandlersForTests(String hostname, int port, Path tempPageDir) {
5656
.setContent(Contents.string("<h1>authorized</h1>", UTF_8)))
5757
.with(new BasicAuthenticationFilter("test", "test")),
5858
Route.get("/.well-known/web-identity").to(WellKnownWebIdentityHandler::new),
59+
Route.get("/fedcm/config.json").to(FedCmConfigHandler::new),
5960
Route.get("/echo").to(EchoHandler::new),
6061
Route.get("/cookie").to(CookieHandler::new),
61-
Route.post("/fedcm/id_assertion").to(FedCmIdAssertion::new),
62+
Route.post("/fedcm/id_assertion.json").to(FedCmIdAssertion::new),
6263
Route.get("/encoding").to(EncodingHandler::new),
6364
Route.matching(req -> req.getUri().startsWith("/generated/"))
6465
.to(() -> new GeneratedJsTestHandler("/generated")),

java/test/org/openqa/selenium/environment/webserver/WellKnownWebIdentityHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
3535
HttpResponse response = new HttpResponse();
3636
response.setHeader("Content-Type", "application/json");
3737
response.setHeader("Cache-Control", "no-store");
38-
String targetLocation = UrlPath.relativeToContext(req, "/fedcm/fedcm.json");
38+
String targetLocation = UrlPath.relativeToContext(req, "https://idp.com");
3939

4040
response.setContent(Contents.string(String.format(RESPONSE_STRING, targetLocation), UTF_8));
4141

java/test/org/openqa/selenium/federatedcredentialmanagement/FederatedCredentialManagementTest.java

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,59 @@
1919

2020
import static org.assertj.core.api.Assertions.assertThat;
2121
import static org.assertj.core.api.Assumptions.assumeThat;
22-
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
23-
import static org.junit.jupiter.api.Assertions.assertEquals;
2422
import static org.junit.jupiter.api.Assertions.assertNull;
2523
import static org.junit.jupiter.api.Assertions.assertThrows;
2624
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
2725

2826
import java.net.MalformedURLException;
2927
import java.net.URL;
3028
import java.time.Duration;
29+
import java.util.List;
30+
import org.junit.jupiter.api.AfterEach;
3131
import org.junit.jupiter.api.BeforeEach;
3232
import org.junit.jupiter.api.Test;
33-
import org.openqa.selenium.InvalidSelectorException;
34-
import org.openqa.selenium.JavascriptException;
35-
import org.openqa.selenium.JavascriptExecutor;
33+
import org.openqa.selenium.By;
34+
import org.openqa.selenium.NoAlertPresentException;
35+
import org.openqa.selenium.WebDriver;
36+
import org.openqa.selenium.WebElement;
37+
import org.openqa.selenium.chrome.ChromeDriver;
3638
import org.openqa.selenium.chrome.ChromeOptions;
39+
import org.openqa.selenium.environment.InProcessTestEnvironment;
40+
import org.openqa.selenium.environment.webserver.AppServer;
3741
import org.openqa.selenium.support.ui.WebDriverWait;
38-
import org.openqa.selenium.testing.JupiterTestBase;
39-
4042
import org.openqa.selenium.testing.NeedsSecureServer;
4143

4244
@NeedsSecureServer
43-
class FederatedCredentialManagementTest extends JupiterTestBase {
45+
class FederatedCredentialManagementTest {
4446

45-
private JavascriptExecutor jsAwareDriver;
4647
private HasFederatedCredentialManagement fedcmDriver;
48+
private WebDriver localDriver;
49+
InProcessTestEnvironment environment = new InProcessTestEnvironment(true);
50+
AppServer appServer = environment.getAppServer();
4751

4852
@BeforeEach
4953
public void setup() {
5054
ChromeOptions options = (ChromeOptions) CHROME.getCapabilities();
51-
// options.setAcceptInsecureCerts(true);
55+
options.setAcceptInsecureCerts(true);
5256
options.addArguments(
5357
String.format("host-resolver-rules=MAP localhost:443 localhost:%d", getSecurePort()));
5458
options.addArguments("ignore-certificate-errors");
55-
localDriver = seleniumExtension.createNewDriver(options);
59+
options.addArguments("--enable-fedcm-without-well-known-enforcement");
60+
localDriver = new ChromeDriver(options);
5661

5762
assumeThat(localDriver).isInstanceOf(HasFederatedCredentialManagement.class);
58-
jsAwareDriver = (JavascriptExecutor) localDriver;
5963
fedcmDriver = (HasFederatedCredentialManagement) localDriver;
6064
localDriver.get(appServer.whereIsSecure("/fedcm/fedcm.html"));
6165
}
6266

63-
private Object triggerFedCm() {
64-
return jsAwareDriver.executeScript("triggerFedCm()");
67+
@AfterEach
68+
public void teardown() {
69+
localDriver.quit();
70+
appServer.stop();
6571
}
6672

6773
private void waitForDialog() {
68-
WebDriverWait wait = new WebDriverWait(localDriver, Duration.ofSeconds(5));
74+
WebDriverWait wait = new WebDriverWait(localDriver, Duration.ofSeconds(20));
6975
wait.until(
7076
driver ->
7177
((HasFederatedCredentialManagement) driver).getFederatedCredentialManagementDialog()
@@ -87,51 +93,85 @@ void testDismissDialog() {
8793
fedcmDriver.setDelayEnabled(false);
8894
assertNull(fedcmDriver.getFederatedCredentialManagementDialog());
8995

90-
Object response = triggerFedCm();
96+
WebElement triggerButton = localDriver.findElement(By.id("triggerButton"));
97+
triggerButton.click();
9198

9299
waitForDialog();
93100

94101
FederatedCredentialManagementDialog dialog =
95102
fedcmDriver.getFederatedCredentialManagementDialog();
96103

97-
assertEquals("Sign in to localhost with localhost", dialog.getTitle());
98-
assertEquals("AccountChooser", dialog.getDialogType());
99-
104+
assertThat(dialog.getTitle()).isEqualTo("Sign in to localhost with localhost");
105+
assertThat(dialog.getDialogType()).isEqualTo("AccountChooser");
100106
dialog.cancelDialog();
101107

102-
// Check that the dialog was indeed closed (i.e. the promise now resolves).
103-
assertThrows(
104-
JavascriptException.class,
105-
() -> {
106-
try {
107-
jsAwareDriver.executeScript("await promise");
108-
} catch (InvalidSelectorException ex) {
109-
// Due to a bug in Chromedriver (https://crbug.com/1454586), we may
110-
// get an invalid selector exception here instead of a JavascriptException.
111-
// Turn it into a JavascriptException to make the test pass for now.
112-
throw new JavascriptException(ex.getMessage(), ex);
113-
}
114-
});
108+
// Check that the dialog was indeed closed. Unable to get the dialog type since the dialog was
109+
// closed.
110+
assertThrows(NoAlertPresentException.class, dialog::getDialogType);
115111
}
116112

117113
@Test
118114
void testSelectAccount() {
119-
fedcmDriver.setDelayEnabled(false);
120115
assertNull(fedcmDriver.getFederatedCredentialManagementDialog());
121116

122-
Object response = triggerFedCm();
117+
WebElement triggerButton = localDriver.findElement(By.id("triggerButton"));
118+
triggerButton.click();
123119

124120
waitForDialog();
125121

126122
FederatedCredentialManagementDialog dialog =
127123
fedcmDriver.getFederatedCredentialManagementDialog();
128124

129-
assertEquals("Sign in to localhost with localhost", dialog.getTitle());
130-
assertEquals("AccountChooser", dialog.getDialogType());
125+
assertThat(dialog.getTitle()).isEqualTo("Sign in to localhost with localhost");
126+
assertThat(dialog.getDialogType()).isEqualTo("AccountChooser");
131127

132-
dialog.selectAccount(0);
128+
List<FederatedCredentialManagementAccount> accountList = dialog.getAccounts();
129+
assertThat(accountList.size()).isEqualTo(2);
130+
dialog.selectAccount(1);
131+
}
132+
133+
@Test
134+
void testGetAccounts() {
135+
assertNull(fedcmDriver.getFederatedCredentialManagementDialog());
136+
137+
WebElement triggerButton = localDriver.findElement(By.id("triggerButton"));
138+
triggerButton.click();
139+
140+
waitForDialog();
141+
142+
FederatedCredentialManagementDialog dialog =
143+
fedcmDriver.getFederatedCredentialManagementDialog();
133144

134-
response = jsAwareDriver.executeScript("return await promise");
135-
assertThat(response).asInstanceOf(MAP).containsEntry("token", "a token");
145+
assertThat(dialog.getTitle()).isEqualTo("Sign in to localhost with localhost");
146+
assertThat(dialog.getDialogType()).isEqualTo("AccountChooser");
147+
148+
List<FederatedCredentialManagementAccount> accountList = dialog.getAccounts();
149+
assertThat(accountList.size()).isEqualTo(2);
150+
151+
FederatedCredentialManagementAccount account1 = accountList.get(0);
152+
153+
assertThat(account1.getName()).isEqualTo("John Doe");
154+
assertThat(account1.getEmail()).isEqualTo("[email protected]");
155+
assertThat(account1.getAccountid()).isEqualTo("1234");
156+
assertThat(account1.getGivenName()).isEqualTo("John");
157+
assertThat(account1.getIdpConfigUrl()).contains("/fedcm/config.json");
158+
assertThat(account1.getPictureUrl()).isEqualTo("https://idp.example/profile/123");
159+
assertThat(account1.getLoginState()).isEqualTo("SignUp");
160+
assertThat(account1.getTermsOfServiceUrl())
161+
.isEqualTo("https://rp.example/terms_of_service.html");
162+
assertThat(account1.getPrivacyPolicyUrl()).isEqualTo("https://rp.example/privacy_policy.html");
163+
164+
FederatedCredentialManagementAccount account2 = accountList.get(1);
165+
166+
assertThat(account2.getName()).isEqualTo("Aisha Ahmad");
167+
assertThat(account2.getEmail()).isEqualTo("[email protected]");
168+
assertThat(account2.getAccountid()).isEqualTo("5678");
169+
assertThat(account2.getGivenName()).isEqualTo("Aisha");
170+
assertThat(account2.getIdpConfigUrl()).contains("/fedcm/config.json");
171+
assertThat(account2.getPictureUrl()).isEqualTo("https://idp.example/profile/567");
172+
assertThat(account2.getLoginState()).isEqualTo("SignUp");
173+
assertThat(account2.getTermsOfServiceUrl())
174+
.isEqualTo("https://rp.example/terms_of_service.html");
175+
assertThat(account2.getPrivacyPolicyUrl()).isEqualTo("https://rp.example/privacy_policy.html");
136176
}
137177
}

0 commit comments

Comments
 (0)