Skip to content

Truly independent web browser

4.0 KB
Raw Blame
<!DOCTYPE html>
<title>Credential Management API: get() basics.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
  promise_test(async (t) => {
    await promise_rejects_dom(
      t,
      "NotSupportedError",
      navigator.credentials.get()
    );

    await promise_rejects_dom(
      t,
      "NotSupportedError",
      navigator.credentials.get({})
    );

    await promise_rejects_dom(
      t,
      "NotSupportedError",
      navigator.credentials.get({ x: "y" })
    );

    await promise_rejects_dom(
      t,
      "NotSupportedError",
      navigator.credentials.get({ x: "y", y: "z" })
    );

    await promise_rejects_dom(
      t,
      "NotSupportedError",
      navigator.credentials.get({ x: "y" })
    );

    await promise_rejects_dom(
      t,
      "NotSupportedError",
      navigator.credentials.get({ mediation: "required" })
    );

    const abortController = new AbortController();
    const { signal } = abortController;
    await promise_rejects_dom(
      t,
      "NotSupportedError",
      navigator.credentials.get({ signal })
    );

    await promise_rejects_dom(
      t,
      "NotSupportedError",
      navigator.credentials.get({ signal, mediation: "required" })
    );
  }, "Calling navigator.credentials.get() without a valid matching interface.");

  promise_test(async (t) => {
    const invalidCombinations = [
      { password: true, publicKey: { challenge: new Uint8Array() } },
      { password: true, otp: { transport: ["sms"] } },
      { password: true, identity: { providers: [] } },
      { federated: { providers: ['https://idp.example.com'] }, publicKey: { challenge: new Uint8Array() } },
      { federated: { providers: ['https://idp.example.com'] }, otp: { transport: ["sms"] } },
      { federated: { providers: ['https://idp.example.com'] }, identity: { providers: [] } },
      { publicKey: { challenge: new Uint8Array() }, otp: { transport: ["sms"] } },
      { publicKey: { challenge: new Uint8Array() }, identity: { providers: [] } },
      { otp: { transport: ["sms"] }, identity: { providers: [] } },
    ];

    for (const options of invalidCombinations) {
      await promise_rejects_dom(
        t,
        "NotSupportedError",
        navigator.credentials.get(options)
      );
    }
  }, "Calling navigator.credentials.get() with invalid combinations of credential types.");

  promise_test(async (t) => {
    // Valid combination (password + federated) is allowed by the spec. It should reach the
    // fetching phase, where it will fail with NotAllowedError due to lack of user activation,
    // but crucially NOT NotSupportedError.
    await promise_rejects_dom(
      t,
      "NotAllowedError",
      navigator.credentials.get({
        password: true,
        federated: { providers: ['https://idp.example.com'] }
      })
    );
  }, "Calling navigator.credentials.get() with valid combination (password + federated).");

  promise_test(function(t) {
    const controller = new AbortController();
    controller.abort("custom reason");

    return promise_rejects_exactly(t, "custom reason",
        navigator.credentials.get({ signal: controller.signal }));
  }, "navigator.credentials.get() aborted with custom reason");

  promise_test(async function(t) {
    const reasons = [{}, [], new Error("custom error")];

    for (let reason of reasons) {
        const result = navigator.credentials.get({ signal: AbortSignal.abort(reason) });
        await promise_rejects_exactly(t, reason, result);
    }
  }, "navigator.credentials.get() aborted with different objects");

  promise_test(function(t) {
    const error = new Error("custom error");
    const controller = new AbortController();

    const result = navigator.credentials.get({ signal: controller.signal });
    controller.abort(error); // aborted after the promise is created

    return promise_rejects_exactly(t, error, result);
  }, "navigator.credentials.get() rejects when aborted after the promise creation");
</script>