UNPKG

@ringcentral/sdk

Version:

- [Installation](#installation) - [Getting Started](#getting-started) - [API Calls](#api-calls) - [Advanced SDK Configuration & Polyfills](#advanced-sdk-configuration--polyfills) - [Making telephony calls](#making-telephony-calls) - [Call mana

941 lines (940 loc) 123 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var Constants_1 = require("../core/Constants"); var test_1 = require("../test/test"); var globalAny = global; var windowAny = typeof window !== 'undefined' ? window : global; describe('RingCentral.platform.Platform', function () { describe('isTokenValid', function () { it('is not authenticated when token has expired', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _b.sent(); _a = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 2: _a.apply(void 0, [_b.sent()]).toEqual(false); return [2 /*return*/]; } }); }); })); it('is not authenticated after logout', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: (0, test_1.logout)(); platform = sdk.platform(); return [4 /*yield*/, platform.logout()]; case 1: _b.sent(); _a = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 2: _a.apply(void 0, [_b.sent()]).toEqual(false); return [2 /*return*/]; } }); }); })); }); describe('X-User-Agent', function () { it('is added with default value', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, path, request; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); client = sdk.client(); path = "/restapi/v1.0/foo/get"; (0, test_1.apiCall)('get', path, { foo: 'bar' }); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.get(path, null)]; case 1: _a.sent(); (0, test_1.expect)(request.headers.get('x-user-agent')).toEqual("RCJSSDK/".concat(Constants_1.version)); (0, test_1.expect)(platform.userAgent).toEqual("RCJSSDK/".concat(Constants_1.version)); return [2 /*return*/]; } }); }); })); it('is added with app name and version', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, path, request; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); client = sdk.client(); path = "/restapi/v1.0/foo/get"; (0, test_1.apiCall)('get', path, { foo: 'bar' }); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.get(path, null)]; case 1: _a.sent(); (0, test_1.expect)(request.headers.get('x-user-agent')).toContain('TestApp/1.0.0 '); return [2 /*return*/]; } }); }); }, { appName: 'TestApp', appVersion: '1.0.0', })); it('is added with app name', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, path, request; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); client = sdk.client(); path = "/restapi/v1.0/foo/get"; (0, test_1.apiCall)('get', path, { foo: 'bar' }); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.get(path, null)]; case 1: _a.sent(); (0, test_1.expect)(request.headers.get('x-user-agent')).toContain('TestApp '); return [2 /*return*/]; } }); }); }, { appName: 'TestApp', })); it('is added with additional user agent', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, path, request; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); client = sdk.client(); path = "/restapi/v1.0/foo/get"; (0, test_1.apiCall)('get', path, { foo: 'bar' }); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.get(path, null)]; case 1: _a.sent(); (0, test_1.expect)(request.headers.get('x-user-agent')).toContain(' (build.1000; rev.149f00000)'); return [2 /*return*/]; } }); }); }, { additionalUserAgent: '(build.1000; rev.149f00000)', })); it('is added with app name, version and additional user agent', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, path, request; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); client = sdk.client(); path = "/restapi/v1.0/foo/get"; (0, test_1.apiCall)('get', path, { foo: 'bar' }); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.get(path, null)]; case 1: _a.sent(); (0, test_1.expect)(request.headers.get('x-user-agent')).toContain('TestApp/1.0.0 '); (0, test_1.expect)(request.headers.get('x-user-agent')).toContain(' (build.1000; rev.149f00000)'); return [2 /*return*/]; } }); }); }, { appName: 'TestApp', appVersion: '1.0.0', additionalUserAgent: '(build.1000; rev.149f00000)', })); }); describe('authorized', function () { it('initiates refresh if not authorized', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: (0, test_1.tokenRefresh)(); platform = sdk.platform(); _a = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 1: _a.apply(void 0, [(_c.sent()).access_token]).not.toEqual('ACCESS_TOKEN_FROM_REFRESH'); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 2: _c.sent(); return [4 /*yield*/, platform.loggedIn()]; case 3: _c.sent(); _b = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 4: _b.apply(void 0, [(_c.sent()).access_token]).toEqual('ACCESS_TOKEN_FROM_REFRESH'); return [2 /*return*/]; } }); }); })); }); describe('login', function () { it('login with code', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, _a, _b, _c, _d, _e; return __generator(this, function (_f) { switch (_f.label) { case 0: platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _f.sent(); (0, test_1.authentication)(); return [4 /*yield*/, platform.login({ code: 'foo', access_token_ttl: 100, refresh_token_ttl: 100, })]; case 2: _f.sent(); _a = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 3: _a.apply(void 0, [(_f.sent()).access_token]).toEqual('ACCESS_TOKEN'); _b = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 4: _b.apply(void 0, [_f.sent()]).toEqual(true); _c = test_1.expect; return [4 /*yield*/, platform.auth().refreshTokenValid()]; case 5: _c.apply(void 0, [_f.sent()]).toEqual(true); _d = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 6: _d.apply(void 0, [(_f.sent()).expire_time > Date.now() + 30 * 60 * 1000]).toEqual(true); _e = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 7: _e.apply(void 0, [(_f.sent()).refresh_token_expire_time > Date.now() + 6 * 24 * 60 * 60 * 1000]).toEqual(true); return [2 /*return*/]; } }); }); })); it('login with JWT', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _c.sent(); (0, test_1.authentication)(); return [4 /*yield*/, platform.login({ jwt: 'foo', })]; case 2: _c.sent(); _a = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 3: _a.apply(void 0, [(_c.sent()).access_token]).toEqual('ACCESS_TOKEN'); _b = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 4: _b.apply(void 0, [_c.sent()]).toEqual(true); return [2 /*return*/]; } }); }); })); it('login with username/password', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _c.sent(); (0, test_1.authentication)(); return [4 /*yield*/, platform.login({ username: 'foo', password: 'foo', extension: 'foo', })]; case 2: _c.sent(); _a = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 3: _a.apply(void 0, [(_c.sent()).access_token]).toEqual('ACCESS_TOKEN'); _b = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 4: _b.apply(void 0, [_c.sent()]).toEqual(true); return [2 /*return*/]; } }); }); })); it('login with username/password without extension', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _c.sent(); (0, test_1.authentication)(); return [4 /*yield*/, platform.login({ username: 'foo', password: 'foo', })]; case 2: _c.sent(); _a = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 3: _a.apply(void 0, [(_c.sent()).access_token]).toEqual('ACCESS_TOKEN'); _b = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 4: _b.apply(void 0, [_c.sent()]).toEqual(true); return [2 /*return*/]; } }); }); })); it('login with code from usePKCE flow without client secret', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, authData, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _b.sent(); platform.loginUrl({ usePKCE: true }); (0, test_1.authentication)(); return [4 /*yield*/, platform.login({ code: 'foo', access_token_ttl: 100, refresh_token_ttl: 100, })]; case 2: _b.sent(); return [4 /*yield*/, platform.auth().data()]; case 3: authData = _b.sent(); (0, test_1.expect)(authData.access_token).toEqual('ACCESS_TOKEN'); _a = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 4: _a.apply(void 0, [_b.sent()]).toEqual(true); (0, test_1.expect)(authData.code_verifier.length > 0).toEqual(true); return [2 /*return*/]; } }); }); }, { clientSecret: '', })); it('login with code and code_verifier', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, authData, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: (0, test_1.authentication)(); platform = sdk.platform(); return [4 /*yield*/, platform.login({ code: 'test', code_verifier: 'test_code_verifier' })]; case 1: _b.sent(); return [4 /*yield*/, platform.auth().data()]; case 2: authData = _b.sent(); (0, test_1.expect)(authData.code_verifier).toEqual('test_code_verifier'); _a = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 3: _a.apply(void 0, [_b.sent()]).toEqual(true); return [2 /*return*/]; } }); }); }, { clientSecret: '', })); it('login with code and code_verifier with client secret', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, request, authData, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: (0, test_1.authentication)(); platform = sdk.platform(); client = sdk.client(); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.login({ code: 'test', code_verifier: 'test_code_verifier' })]; case 1: _b.sent(); (0, test_1.expect)(request.headers.get('authorization')).not.toEqual(null); return [4 /*yield*/, platform.auth().data()]; case 2: authData = _b.sent(); (0, test_1.expect)(authData.access_token).toEqual('ACCESS_TOKEN'); (0, test_1.expect)(authData.code_verifier).toEqual('test_code_verifier'); _a = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 3: _a.apply(void 0, [_b.sent()]).toEqual(true); return [2 /*return*/]; } }); }); })); it('login with code without clientSecret', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, request, authData; return __generator(this, function (_a) { switch (_a.label) { case 0: (0, test_1.authentication)(); platform = sdk.platform(); client = sdk.client(); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.login({ code: 'test' })]; case 1: _a.sent(); (0, test_1.expect)(request.headers.get('authorization')).toEqual(null); (0, test_1.expect)(request.originalBody || request.body).toContain('client_id=whatever'); return [4 /*yield*/, platform.auth().data()]; case 2: authData = _a.sent(); (0, test_1.expect)(authData.access_token).toEqual('ACCESS_TOKEN'); return [2 /*return*/]; } }); }); }, { clientSecret: '', })); it('login with access_token', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _b.sent(); (0, test_1.authentication)(); return [4 /*yield*/, platform.login({ access_token: 'foo' })]; case 2: _b.sent(); _a = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 3: _a.apply(void 0, [(_b.sent()).access_token]).toEqual('foo'); return [2 /*return*/]; } }); }); })); it('login error', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _a.sent(); (0, test_1.apiCall)('POST', '/restapi/oauth/token', { message: 'expected' }, 400); return [4 /*yield*/, (0, test_1.expectThrows)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, platform.login({ code: 'foo' })]; }); }); }, 'expected')]; case 2: _a.sent(); return [2 /*return*/]; } }); }); })); }); describe('loggedIn', function () { it('returns false if refresh failed', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, res; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _a.sent(); (0, test_1.apiCall)('POST', '/restapi/oauth/token', { message: 'expected' }, 400); return [4 /*yield*/, platform.loggedIn()]; case 2: res = _a.sent(); (0, test_1.expect)(res).toEqual(false); return [2 /*return*/]; } }); }); })); }); describe('sendRequest', function () { it('refreshes token when token was expired', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, path, refreshSpy, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: platform = sdk.platform(); path = '/restapi/xxx'; refreshSpy = (0, test_1.spy)(function () { }); (0, test_1.tokenRefresh)(); (0, test_1.apiCall)('GET', path, {}); _a = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 1: _a.apply(void 0, [(_c.sent()).access_token]).not.toEqual('ACCESS_TOKEN_FROM_REFRESH'); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 2: _c.sent(); return [4 /*yield*/, platform.on(platform.events.refreshSuccess, refreshSpy).get(path)]; case 3: _c.sent(); (0, test_1.expect)(refreshSpy.mock.calls.length === 1).toEqual(true); _b = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 4: _b.apply(void 0, [(_c.sent()).access_token]).toEqual('ACCESS_TOKEN_FROM_REFRESH'); return [2 /*return*/]; } }); }); })); it('tries to refresh the token if Platform returns 401 Unauthorized and re-executes the request', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, path, response, refreshSpy, res, _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: platform = sdk.platform(); path = '/restapi/xxx'; response = { foo: 'bar' }; refreshSpy = (0, test_1.spy)(function () { (0, test_1.apiCall)('GET', path, response, 200); }); (0, test_1.apiCall)('GET', path, { message: 'time not in sync' }, 401, 'Time Not In Sync'); (0, test_1.tokenRefresh)(); platform.on(platform.events.refreshSuccess, refreshSpy); return [4 /*yield*/, platform.get(path)]; case 1: res = _c.sent(); (0, test_1.expect)(refreshSpy.mock.calls.length === 1).toEqual(true); _a = test_1.expect; return [4 /*yield*/, res.json()]; case 2: _a.apply(void 0, [_c.sent()]).toEqual(response); _b = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 3: _b.apply(void 0, [(_c.sent()).access_token]).toEqual('ACCESS_TOKEN_FROM_REFRESH'); return [2 /*return*/]; } }); }); })); it('fails if ajax has status other than 2xx', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, path; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); path = '/restapi/xxx'; (0, test_1.apiCall)('GET', path, { description: 'Fail' }, 400, 'Bad Request'); return [4 /*yield*/, (0, test_1.expectThrows)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, platform.get(path)]; }); }); }, 'Fail')]; case 1: _a.sent(); return [2 /*return*/]; } }); }); })); it('handles rate limit 429', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, path, response, rateLimitSpy, res, e, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: platform = sdk.platform(); path = '/restapi/xxx'; response = { foo: 'bar' }; rateLimitSpy = (0, test_1.spy)(function () { (0, test_1.apiCall)('GET', path, response, 200); }); (0, test_1.apiCall)('GET', path, { message: 'expected' }, 429, 'Rate Limit Exceeded'); platform.on(platform.events.rateLimitError, rateLimitSpy); return [4 /*yield*/, platform.get(path, null, { handleRateLimit: 0.01 })]; case 1: res = _b.sent(); (0, test_1.expect)(rateLimitSpy.mock.calls.length === 1).toEqual(true); e = rateLimitSpy.mock.calls[0][0]; (0, test_1.expect)(e.message).toEqual('expected'); (0, test_1.expect)(e.retryAfter).toEqual(10); _a = test_1.expect; return [4 /*yield*/, res.json()]; case 2: _a.apply(void 0, [_b.sent()]).toEqual(response); return [2 /*return*/]; } }); }); })); it('handles default rate limit 429', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, path, response, rateLimitSpy, res, e, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: platform = sdk.platform(); path = '/restapi/xxx'; response = { foo: 'bar' }; rateLimitSpy = (0, test_1.spy)(function () { (0, test_1.apiCall)('GET', path, response, 200); }); platform['_handleRateLimit'] = 0.01; (0, test_1.apiCall)('GET', path, { message: 'expected' }, 429, 'Rate Limit Exceeded'); platform.on(platform.events.rateLimitError, rateLimitSpy); return [4 /*yield*/, platform.get(path)]; case 1: res = _b.sent(); (0, test_1.expect)(rateLimitSpy.mock.calls.length === 1).toEqual(true); e = rateLimitSpy.mock.calls[0][0]; (0, test_1.expect)(e.message).toEqual('expected'); (0, test_1.expect)(e.retryAfter).toEqual(10); _a = test_1.expect; return [4 /*yield*/, res.json()]; case 2: _a.apply(void 0, [_b.sent()]).toEqual(response); return [2 /*return*/]; } }); }); })); it('emits rate limit 429 errors if they are not handled', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, path, rateLimitSpy; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); path = '/restapi/xxx'; rateLimitSpy = (0, test_1.spy)(function () { }); (0, test_1.apiCall)('GET', path, { message: 'expected' }, 429, 'Rate Limit Exceeded'); platform.on(platform.events.rateLimitError, rateLimitSpy); return [4 /*yield*/, (0, test_1.expectThrows)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, platform.get(path)]; }); }); }, '', function (err) { (0, test_1.expect)(rateLimitSpy.mock.calls.length === 1).toEqual(true); var e = rateLimitSpy.mock.calls[0][0]; (0, test_1.expect)(e.message).toEqual('expected'); (0, test_1.expect)(e.retryAfter).toEqual(60000); (0, test_1.expect)(err).toEqual(e); })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); })); }); describe('refresh', function () { it('handles error in queued AJAX after unsuccessful refresh when token is killed', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, path, successSpy, errorSpy; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); path = '/restapi/xxx'; successSpy = (0, test_1.spy)(function () { }); errorSpy = (0, test_1.spy)(function () { }); (0, test_1.tokenRefresh)(true); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _a.sent(); return [4 /*yield*/, (0, test_1.expectThrows)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, platform .on(platform.events.refreshSuccess, successSpy) .on(platform.events.refreshError, errorSpy) .get(path)]; }); }); }, 'Wrong token')]; case 2: _a.sent(); (0, test_1.expect)(errorSpy.mock.calls.length === 1).toEqual(true); (0, test_1.expect)(successSpy.mock.calls.length === 1).toBe(false); return [2 /*return*/]; } }); }); })); it('handles subsequent refreshes', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); (0, test_1.tokenRefresh)(); (0, test_1.tokenRefresh)(); (0, test_1.tokenRefresh)(); return [4 /*yield*/, platform.refresh()]; case 1: _a.sent(); // first return [4 /*yield*/, platform.refresh()]; case 2: _a.sent(); // second return [4 /*yield*/, Promise.all([ platform.refresh(), platform.refresh(), ])]; case 3: _a.sent(); return [2 /*return*/]; } }); }); })); it('returns error if response is malformed', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); (0, test_1.apiCall)('POST', '/restapi/oauth/token', { message: 'Wrong token', error_description: 'Wrong token', description: 'Wrong token', }, 240); // This weird status was caught on client's machine return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _a.sent(); return [4 /*yield*/, (0, test_1.expectThrows)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, platform.refresh()]; }); }); }, 'Wrong token', function (e) { (0, test_1.expect)(e.originalMessage).toEqual('Malformed OAuth response'); })]; case 2: _a.sent(); return [2 /*return*/]; } }); }); })); it('issues only one refresh request', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, res, _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: (0, test_1.tokenRefresh)(); (0, test_1.apiCall)('GET', '/restapi/v1.0/foo/1', { increment: 1 }); (0, test_1.apiCall)('GET', '/restapi/v1.0/foo/2', { increment: 2 }); (0, test_1.apiCall)('GET', '/restapi/v1.0/foo/3', { increment: 3 }); platform = sdk.platform(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _d.sent(); _b = (_a = Promise).all; return [4 /*yield*/, Promise.all([ platform.get('/restapi/v1.0/foo/1'), platform.get('/restapi/v1.0/foo/2'), platform.get('/restapi/v1.0/foo/3'), ])]; case 2: return [4 /*yield*/, _b.apply(_a, [(_d.sent()).map(function (r) { return r.json(); })])]; case 3: res = _d.sent(); _c = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 4: _c.apply(void 0, [(_d.sent()).access_token]).toEqual('ACCESS_TOKEN_FROM_REFRESH'); (0, test_1.expect)(res[0].increment).toEqual(1); (0, test_1.expect)(res[1].increment).toEqual(2); (0, test_1.expect)(res[2].increment).toEqual(3); return [2 /*return*/]; } }); }); })); it('not skip auth header when auth data with clientSecret', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, request, _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: (0, test_1.tokenRefresh)(); platform = sdk.platform(); client = sdk.client(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _d.sent(); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.refresh()]; case 2: _d.sent(); (0, test_1.expect)(request.headers.get('authorization')).not.toEqual(null); _a = test_1.expect; return [4 /*yield*/, platform.auth().data()]; case 3: _a.apply(void 0, [(_d.sent()).access_token]).toEqual('ACCESS_TOKEN_FROM_REFRESH'); _b = test_1.expect; return [4 /*yield*/, platform.auth().accessTokenValid()]; case 4: _b.apply(void 0, [_d.sent()]).toEqual(true); _c = test_1.expect; return [4 /*yield*/, platform.auth().refreshTokenValid()]; case 5: _c.apply(void 0, [_d.sent()]).toEqual(true); return [2 /*return*/]; } }); }); })); it('skip auth header when auth data without client secret', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, request, authData; return __generator(this, function (_a) { switch (_a.label) { case 0: (0, test_1.tokenRefresh)(); platform = sdk.platform(); client = sdk.client(); return [4 /*yield*/, platform.auth().cancelAccessToken()]; case 1: _a.sent(); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.refresh()]; case 2: _a.sent(); (0, test_1.expect)(request.headers.get('authorization')).toEqual(null); (0, test_1.expect)(request.originalBody || request.body).toContain('client_id=whatever'); return [4 /*yield*/, platform.auth().data()]; case 3: authData = _a.sent(); (0, test_1.expect)(authData.access_token).toEqual('ACCESS_TOKEN_FROM_REFRESH'); return [2 /*return*/]; } }); }); }, { clientSecret: '', })); }); describe('get, post, put, patch, delete', function () { it('sends request using appropriate method', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, test; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); test = function (method) { return __awaiter(void 0, void 0, void 0, function () { var path, res, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: path = "/restapi/v1.0/foo/".concat(method); (0, test_1.apiCall)(method, path, { foo: 'bar' }); return [4 /*yield*/, platform[method](path)]; case 1: res = _b.sent(); _a = test_1.expect; return [4 /*yield*/, res.json()]; case 2: _a.apply(void 0, [(_b.sent()).foo]).toEqual('bar'); return [2 /*return*/]; } }); }); }; return [4 /*yield*/, test('get')]; case 1: _a.sent(); return [4 /*yield*/, test('post')]; case 2: _a.sent(); return [4 /*yield*/, test('put')]; case 3: _a.sent(); return [4 /*yield*/, test('patch')]; case 4: _a.sent(); return [4 /*yield*/, test('delete')]; case 5: _a.sent(); return [2 /*return*/]; } }); }); })); it('send request with user agent option', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, client, path, request; return __generator(this, function (_a) { switch (_a.label) { case 0: platform = sdk.platform(); client = sdk.client(); path = "/restapi/v1.0/foo/get"; (0, test_1.apiCall)('get', path, { foo: 'bar' }); client.on(client.events.requestSuccess, function (_, r) { request = r; }); return [4 /*yield*/, platform.get(path, null, { userAgent: 'TestAgent' })]; case 1: _a.sent(); (0, test_1.expect)(request.headers.get('x-user-agent')).toContain('TestAgent'); return [2 /*return*/]; } }); }); })); }); describe('createUrl', function () { it('builds the URL', (0, test_1.asyncTest)(function (sdk) { return __awaiter(void 0, void 0, void 0, function () { var platform, _a, _b, _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: platform = sdk.platform(); (0, test_1.expect)(platform.createUrl('/restapi/v1.0/foo')).toEqual('/restapi/v1.0/foo'); (0, test_1.expect)(platform.createUrl('/restapi/v1.0/foo', { addServer: true })).toEqual('http://whatever/restapi/v1.0/foo'); _a = test_1.expect; return [4 /*yield*/, platform.signUrl(platform.createUrl('/restapi/v1.0/foo', { addServer: true, }))]; case 1: _a.apply(void 0, [_e.sent()]).toEqual('http://whatever/restapi/v1.0/foo?access_token=ACCESS_TOKEN'); _b = test_1.expect; return [4 /*yield*/, platform.signUrl(platform.createUrl('/restapi/v1.0/foo?bar', { addServer: true,