Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
cbiesinger committed Jun 6, 2023
1 parent 6f5a25a commit 1f696b8
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/src/web/fedcm/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AddType application/json .json
17 changes: 17 additions & 0 deletions common/src/web/fedcm/accounts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"accounts": [{
"id": "1234",
"given_name": "John",
"name": "John Doe",
"email": "john_doe@idp.example",
"picture": "https://idp.example/profile/123",
"approved_clients": ["123", "456", "789"]
}, {
"id": "5678",
"given_name": "Aisha",
"name": "Aisha Ahmad",
"email": "aisha@idp.example",
"picture": "https://idp.example/profile/567",
"approved_clients": []
}]
}
4 changes: 4 additions & 0 deletions common/src/web/fedcm/client_metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"privacy_policy_url": "https://rp.example/privacy_policy.html",
"terms_of_service_url": "https://rp.example/terms_of_service.html"
}
19 changes: 19 additions & 0 deletions common/src/web/fedcm/fedcm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<script>

let configURL = `https://{location.host}/fedcm/fedcm.json`;
let promise = null;

function triggerFedCm() {
promise = navigator.credentials.get({
identity: {
providers: [{
configURL: configURL,
clientId: '1',
}]
}
});
return promise;
}

</script>
6 changes: 6 additions & 0 deletions common/src/web/fedcm/fedcm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"accounts_endpoint": "accounts.json",
"client_metadata_endpoint": "client_metadata.json",
"id_assertion_endpoint": "token.php",
"signin_url": "https://idp.example/signin"
}
4 changes: 4 additions & 0 deletions common/src/web/fedcm/token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php header("Content-Type: application/json") ?>
{
"token": "<?php echo $_POST["account_id"]; ?>"
}
73 changes: 73 additions & 0 deletions java/src/org/openqa/selenium/remote/FedCmDialogImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.federatedcredentialmanagement.FederatedCredentialManagementAccount;
import org.openqa.selenium.federatedcredentialmanagement.FederatedCredentialManagementDialog;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.ExecuteMethod;

class FedCmDialogImpl implements FederatedCredentialManagementDialog {
private final ExecuteMethod executeMethod;

FedCmDialogImpl(ExecuteMethod executeMethod) {
this.executeMethod = executeMethod;
}

@Override
public void cancelDialog() {
executeMethod.execute(DriverCommand.CANCEL_DIALOG, null);
}

@Override
public void selectAccount(int index) {
executeMethod.execute(DriverCommand.SELECT_ACCOUNT, ImmutableMap.of("accountIndex", index));
}

@Override
public String getDialogType() {
return (String) executeMethod.execute(DriverCommand.GET_FEDCM_DIALOG_TYPE, null);
}

@Override
public String getTitle() {
Map<String, Object> result = (Map<String, Object>) executeMethod.execute(DriverCommand.GET_FEDCM_TITLE, null);
return (String) result.getOrDefault("title", null);
}

@Override
public String getSubtitle() {
Map<String, Object> result = (Map<String, Object>) executeMethod.execute(DriverCommand.GET_FEDCM_TITLE, null);
return (String) result.getOrDefault("subtitle", null);
}

@Override
public List<FederatedCredentialManagementAccount> getAccounts() {
List<Map<String, String>> list = (List<Map<String, String>>) executeMethod.execute(DriverCommand.GET_ACCOUNTS, null);
ArrayList<FederatedCredentialManagementAccount> accounts = new ArrayList<FederatedCredentialManagementAccount>();
for (Map<String, String> map : list) {
accounts.add(new FederatedCredentialManagementAccount(map));
}
return accounts;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "JUNIT5_DEPS", "java_selenium_test_suite")

java_selenium_test_suite(
name = "LargeTests",
size = "large",
srcs = glob(["*.java"]),
browsers = [
"chrome",
],
deps = [
"//java/src/org/openqa/selenium/chrome",
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/remote",
"//java/test/org/openqa/selenium/environment",
"//java/test/org/openqa/selenium/testing:annotations",
"//java/test/org/openqa/selenium/testing:test-base",
"//java/test/org/openqa/selenium/testing/drivers",
artifact("org.junit.jupiter:junit-jupiter-api"),
artifact("org.assertj:assertj-core"),
] + JUNIT5_DEPS,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.federatedcredentialmanagement;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.assertj.core.api.Fail.fail;
import static org.assertj.core.api.InstanceOfAssertFactories.MAP;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.federatedcredentialmanagement.FederatedCredentialManagementDialog;
import org.openqa.selenium.federatedcredentialmanagement.HasFederatedCredentialManagement;
import org.openqa.selenium.testing.JupiterTestBase;
import org.openqa.selenium.testing.drivers.Browser;

class FederatedCredentialManagementTest extends JupiterTestBase {

private JavascriptExecutor jsAwareDriver;
private HasFederatedCredentialManagement fedcmDriver;

@BeforeEach
public void setup() {
ChromeOptions options = (ChromeOptions) Browser.CHROME.getCapabilities();
options.addArguments("user-agent=foo;bar");
localDriver = seleniumExtension.createNewDriver(options);

assumeThat(localDriver).isInstanceOf(HasFederatedCredentialManagement.class);
jsAwareDriver = (JavascriptExecutor) localDriver;
fedcmDriver = (HasFederatedCredentialManagement) localDriver;
localDriver.get(appServer.whereIs("/fedcm/fedcm.html"));
}

private Object triggerFedCm() {
return jsAwareDriver.executeScript("triggerFedCm()");
}

@Test
void testDismissDialog() {
fedcmDriver.setDelayEnabled(false);

//assertThat(response).asInstanceOf(MAP).containsEntry("status", "OK");
Object response = triggerFedCm();


FederatedCredentialManagementDialog dialog = fedcmDriver.getFederatedCredentialManagementDialog();

// Attempt to use the credential to get an assertion.
assertThat(response)
.extracting("credential.rawId")
.asInstanceOf(MAP)
.containsEntry("status", "OK");
}

}

0 comments on commit 1f696b8

Please sign in to comment.