summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/credential-management/support/fedcm-mock.js
blob: f52bd6e0e70d73782ea1618c3c694599536761e9 (plain)
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
import { RequestTokenStatus, LogoutRpsStatus, FederatedAuthRequest, FederatedAuthRequestReceiver } from '/gen/third_party/blink/public/mojom/webid/federated_auth_request.mojom.m.js';

function toMojoTokenStatus(status) {
  return RequestTokenStatus["k" + status];
}

// A mock service for responding to federated auth requests.
export class MockFederatedAuthRequest {
  constructor() {
    this.receiver_ = new FederatedAuthRequestReceiver(this);
    this.interceptor_ = new MojoInterfaceInterceptor(FederatedAuthRequest.$interfaceName);
    this.interceptor_.oninterfacerequest = e => {
        this.receiver_.$.bindHandle(e.handle);
    }
    this.interceptor_.start();
    this.token_ = null;
    this.selected_identity_provider_config_url_ = null;
    this.status_ = RequestTokenStatus.kError;
    this.logoutRpsStatus_ = LogoutRpsStatus.kError;
    this.returnPending_ = false;
    this.pendingPromiseResolve_ = null;
  }

  // Causes the subsequent `navigator.credentials.get()` to resolve with the token.
  returnToken(selected_identity_provider_config_url, token) {
    this.status_ = RequestTokenStatus.kSuccess;
    this.selected_identity_provider_config_url_ = selected_identity_provider_config_url;
    this.token_ = token;
    this.returnPending_ = false;
  }

  // Causes the subsequent `navigator.credentials.get()` to reject with the error.
  returnError(error) {
    if (error == "Success")
      throw new Error("Success is not a valid error");
    this.status_ = toMojoTokenStatus(error);
    this.selected_identity_provider_config_url_ = null;
    this.token_ = null;
    this.returnPending_ = false;
  }

  // Causes the subsequent `navigator.credentials.get()` to return a pending promise
  // that can be cancelled using `cancelTokenRequest()`.
  returnPendingPromise() {
    this.returnPending_ = true;
  }

  logoutRpsReturn(status) {
    let validated = LogoutRpsStatus[status];
    if (validated === undefined)
      throw new Error("Invalid status: " + status);
    this.logoutRpsStatus_ = validated;
  }

  // Implements
  //   RequestToken(array<IdentityProviderGetParameters> idp_get_params) =>
  //                    (RequestTokenStatus status,
  //                      url.mojom.Url? selected_identity_provider_config_url,
  //                      string? token);
  async requestToken(idp_get_params) {
    if (this.returnPending_) {
      this.pendingPromise_ = new Promise((resolve, reject) => {
        this.pendingPromiseResolve_ = resolve;
      });
      return this.pendingPromise_;
    }
    return Promise.resolve({
      status: this.status_,
      selected_identity_provider_config_url: this.selected_identity_provider_config_url_,
      token: this.token_
    });
  }

  async cancelTokenRequest() {
    this.pendingPromiseResolve_({
      status: toMojoTokenStatus("ErrorCanceled"),
      selected_identity_provider_config_url: null,
      token: null
    });
    this.pendingPromiseResolve_ = null;
  }

  async logoutRps(logout_endpoints) {
    return Promise.resolve({
      status: this.logoutRpsStatus_
    });
  }

  async setIdpSigninStatus(origin, status) {
  }

  async reset() {
    this.token_ = null;
    this.selected_identity_provider_config_url_ = null;
    this.status_ = RequestTokenStatus.kError;
    this.logoutRpsStatus_ = LogoutRpsStatus.kError;
    this.receiver_.$.close();
    this.interceptor_.stop();

    // Clean up and reset mock stubs asynchronously, so that the blink side
    // closes its proxies and notifies JS sensor objects before new test is
    // started.
    await new Promise(resolve => { step_timeout(resolve, 0); });
  }
}