summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/credential-management/support/fedcm-mock.js
blob: 271dd9cd944a2770f27b119331dc77d919ba9cf5 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { RequestTokenStatus, LogoutRpsStatus, DisconnectStatus, 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.disconnectStatus_ = DisconnectStatus.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;
  }

  // Causes the subsequent `FederatedCredential.disconnect` to reject with this
  // status.
  disconnectReturn(status) {
    let validated = DisconnectStatus[status];
    if (validated === undefined)
      throw new Error("Invalid status: " + status);
    this.disconnectStatus_ = 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;
  }

  // Implements
  //   RequestUserInfo(IdentityProviderGetParameters idp_get_param) =>
  //                    (RequestUserInfoStatus status, array<IdentityUserInfo>? user_info);
  async requestUserInfo(idp_get_param) {
    return Promise.resolve({
      status: "",
      user_info: ""
    });
  }

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

  async disconnect(provider, client_id, account_id) {
    return Promise.resolve({
      status: this.disconnectStatus_
    });
  }

  async setIdpSigninStatus(origin, status) {
  }

  async registerIdP(configURL) {
  }

  async unregisterIdP(configURL) {
  }

  async resolveTokenRequest(token) {
  }

  async closeModalDialogView() {
  }

  async preventSilentAccess() {
  }

  async reset() {
    this.token_ = null;
    this.selected_identity_provider_config_url_ = null;
    this.status_ = RequestTokenStatus.kError;
    this.logoutRpsStatus_ = LogoutRpsStatus.kError;
    this.disconnectStatus_ = DisconnectStatus.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); });
  }
}