REGRESSION(r230960): Browser tests under TimeSeriesChart fetchMeasurementSets all...
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / privileged-api-tests.js
1 'use strict';
2
3 const assert = require('assert');
4 require('../tools/js/v3-models.js');
5 const BrowserPrivilegedAPI = require('../public/v3/privileged-api.js').PrivilegedAPI;
6 const NodePrivilegedAPI = require('../tools/js/privileged-api').PrivilegedAPI;
7 const MockRemoteAPI = require('./resources/mock-remote-api.js').MockRemoteAPI;
8
9 describe('BrowserPrivilegedAPI', () => {
10     const requests = MockRemoteAPI.inject(null, BrowserPrivilegedAPI);
11
12     beforeEach(() => {
13         PrivilegedAPI._token = null;
14     });
15
16     describe('requestCSRFToken', () => {
17         it('should generate a new token', () => {
18             PrivilegedAPI.requestCSRFToken();
19             assert.equal(requests.length, 1);
20             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
21         });
22
23         it('should not generate a new token if the existing token had not expired', () => {
24             const tokenRequest = PrivilegedAPI.requestCSRFToken();
25             assert.equal(requests.length, 1);
26             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
27             requests[0].resolve({
28                 token: 'abc',
29                 expiration: Date.now() + 3600 * 1000,
30             });
31             return tokenRequest.then((token) => {
32                 assert.equal(token, 'abc');
33                 PrivilegedAPI.requestCSRFToken();
34                 assert.equal(requests.length, 1);
35             });
36         });
37
38         it('should generate a new token if the existing token had already expired', () => {
39             const tokenRequest = PrivilegedAPI.requestCSRFToken();
40             assert.equal(requests.length, 1);
41             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
42             requests[0].resolve({
43                 token: 'abc',
44                 expiration: Date.now() - 1,
45             });
46             return tokenRequest.then((token) => {
47                 assert.equal(token, 'abc');
48                 PrivilegedAPI.requestCSRFToken();
49                 assert.equal(requests.length, 2);
50                 assert.equal(requests[1].url, '/privileged-api/generate-csrf-token');
51             });
52         });
53     });
54
55     describe('sendRequest', () => {
56
57         it('should generate a new token if no token had been fetched', () => {
58             const promise = PrivilegedAPI.sendRequest('test', {});
59             assert.equal(requests.length, 1);
60             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
61             requests[0].resolve({
62                 token: 'abc',
63                 expiration: Date.now() + 100 * 1000,
64             });
65             return MockRemoteAPI.waitForRequest().then(() => {
66                 assert.equal(requests.length, 2);
67                 assert.equal(requests[1].url, '/privileged-api/test');
68             });
69         });
70
71         it('should not generate a new token if the existing token had not been expired', () => {
72             PrivilegedAPI.sendRequest('test', {});
73             assert.equal(requests.length, 1);
74             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
75             requests[0].resolve({
76                 token: 'abc',
77                 expiration: Date.now() + 3600 * 1000,
78             });
79             return MockRemoteAPI.waitForRequest().then(() => {
80                 assert.equal(requests.length, 2);
81                 assert.equal(requests[1].url, '/privileged-api/test');
82                 PrivilegedAPI.sendRequest('test2', {});
83                 return MockRemoteAPI.waitForRequest();
84             }).then(() => {
85                 assert.equal(requests.length, 3);
86                 assert.equal(requests[2].url, '/privileged-api/test2');
87             });
88         });
89
90         it('should reject immediately when a token generation had failed', () => {
91             const request = PrivilegedAPI.sendRequest('test', {});
92             let caught = false;
93             request.catch(() => { caught = true; });
94             assert.equal(requests.length, 1);
95             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
96             requests[0].reject({status: 'FailedToGenerateToken'});
97             return new Promise((resolve) => setTimeout(resolve, 0)).then(() => {
98                 assert.equal(requests.length, 1);
99                 assert(caught);
100             });
101         });
102
103         it('should re-generate token when it had become invalid', () => {
104             PrivilegedAPI.sendRequest('test', {});
105             assert.equal(requests.length, 1);
106             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
107             requests[0].resolve({
108                 token: 'abc',
109                 expiration: Date.now() + 3600 * 1000,
110             });
111             return MockRemoteAPI.waitForRequest().then(() => {
112                 assert.equal(requests.length, 2);
113                 assert.equal(requests[1].data.token, 'abc');
114                 assert.equal(requests[1].url, '/privileged-api/test');
115                 requests[1].reject('InvalidToken');
116                 return MockRemoteAPI.waitForRequest();
117             }).then(() => {
118                 assert.equal(requests.length, 3);
119                 assert.equal(requests[2].url, '/privileged-api/generate-csrf-token');
120                 requests[2].resolve({
121                     token: 'def',
122                     expiration: Date.now() + 3600 * 1000,
123                 });
124                 return MockRemoteAPI.waitForRequest();
125             }).then(() => {
126                 assert.equal(requests.length, 4);
127                 assert.equal(requests[3].data.token, 'def');
128                 assert.equal(requests[3].url, '/privileged-api/test');
129             });
130         });
131
132         it('should not re-generate token when the re-fetched token was invalid', () => {
133             const request = PrivilegedAPI.sendRequest('test', {});
134             let caught = false;
135             request.catch(() => caught = true);
136             assert.equal(requests.length, 1);
137             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
138             requests[0].resolve({
139                 token: 'abc',
140                 expiration: Date.now() + 3600 * 1000,
141             });
142             return MockRemoteAPI.waitForRequest().then(() => {
143                 assert.equal(requests.length, 2);
144                 assert.equal(requests[1].data.token, 'abc');
145                 assert.equal(requests[1].url, '/privileged-api/test');
146                 requests[1].reject('InvalidToken');
147                 return MockRemoteAPI.waitForRequest();
148             }).then(() => {
149                 assert.equal(requests.length, 3);
150                 assert.equal(requests[2].url, '/privileged-api/generate-csrf-token');
151                 requests[2].resolve({
152                     token: 'def',
153                     expiration: Date.now() + 3600 * 1000,
154                 });
155                 return MockRemoteAPI.waitForRequest();
156             }).then(() => {
157                 assert.equal(requests.length, 4);
158                 assert.equal(requests[3].data.token, 'def');
159                 assert.equal(requests[3].url, '/privileged-api/test');
160                 requests[3].reject('InvalidToken');
161                 return new Promise((resolve) => setTimeout(resolve, 0));
162             }).then(() => {
163                 assert(caught);
164                 assert.equal(requests.length, 4);
165             });
166         });
167
168     });
169
170 });
171
172 describe('NodePrivilegedAPI', () => {
173     let requests = MockRemoteAPI.inject(null, NodePrivilegedAPI);
174     beforeEach(() => {
175         PrivilegedAPI.configure('slave_name', 'password');
176     });
177
178     describe('sendRequest', () => {
179         it('should post slave name and password in data', async () => {
180             const request = PrivilegedAPI.sendRequest('test', {foo: 'bar'});
181
182             assert.equal(requests.length, 1);
183             assert.equal(requests[0].url, '/privileged-api/test');
184             assert.equal(requests[0].method, 'POST');
185             assert.deepEqual(requests[0].data,  {foo: 'bar', slaveName: 'slave_name', slavePassword: 'password'});
186
187             requests[0].resolve({test: 'success'});
188             const result = await request;
189             assert.deepEqual(result, {test: 'success'});
190         });
191     });
192
193 });