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