|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const querystring = require('querystring'); |
| 5 | +const { newClient } = require('./support/client'); |
| 6 | + |
| 7 | +// Capture the final axios request options a method builds, with NO network: |
| 8 | +// stub _send (the single point every method funnels through) and inspect what |
| 9 | +// it was handed. These tests lock in the correctness fixes from the audit. |
| 10 | +function capture (invoke) { |
| 11 | + const api = newClient({ api_key: 'k', api_secret: 's' }); |
| 12 | + let captured = null; |
| 13 | + api._send = function (requestOptions, callback) { |
| 14 | + captured = requestOptions; |
| 15 | + if (callback) callback(null, {}); |
| 16 | + return Promise.resolve({}); |
| 17 | + }; |
| 18 | + invoke(api); |
| 19 | + return captured; |
| 20 | +} |
| 21 | + |
| 22 | +describe('Request shaping', function () { |
| 23 | + describe('audit fixes', function () { |
| 24 | + it('getTests sends count (not limit) as the page size', function () { |
| 25 | + const r = capture(api => api.getTests(0, 25)); |
| 26 | + assert.strictEqual(r.method, 'GET'); |
| 27 | + assert.match(r.url, /\/v1\/tests\/$/); |
| 28 | + assert.deepStrictEqual(r.params, { offset: 0, count: 25 }); |
| 29 | + assert.ok(!('limit' in r.params), 'should not send a limit param'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('getDevice puts the id in the path, not a query param', function () { |
| 33 | + const r = capture(api => api.getDevice('A123')); |
| 34 | + assert.strictEqual(r.url, 'https://api.testingbot.com/v1/devices/A123'); |
| 35 | + assert.ok(!r.params, 'should not send a query param'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('takeScreenshot uses snake_case option names', function () { |
| 39 | + const r = capture(api => |
| 40 | + api.takeScreenshot('https://x', [{ browserName: 'chrome' }], '1280x1024', 5, true, 'https://cb') |
| 41 | + ); |
| 42 | + assert.strictEqual(r.headers['Content-Type'], 'application/json'); |
| 43 | + assert.strictEqual(r.data.wait_time, 5); |
| 44 | + assert.strictEqual(r.data.fullpage, true); |
| 45 | + assert.strictEqual(r.data.callback_url, 'https://cb'); |
| 46 | + assert.ok(!('waitTime' in r.data) && !('fullPage' in r.data) && !('callbackURL' in r.data)); |
| 47 | + }); |
| 48 | + |
| 49 | + it('createUserInTeam sends flat top-level fields (no user[...] wrapper)', function () { |
| 50 | + const r = capture(api => api.createUserInTeam({ email: 'a@b.com', password: 'pw' })); |
| 51 | + const parsed = querystring.parse(r.data); |
| 52 | + assert.strictEqual(parsed.email, 'a@b.com'); |
| 53 | + assert.strictEqual(parsed.password, 'pw'); |
| 54 | + assert.ok(!r.data.includes('user'), 'should not nest under user[...]'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('updateUserInfo wraps the body under user[...]', function () { |
| 58 | + const r = capture(api => api.updateUserInfo({ first_name: 'Jo' })); |
| 59 | + const parsed = querystring.parse(r.data); |
| 60 | + assert.strictEqual(parsed['user[first_name]'], 'Jo'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('getStorageFile strips the tb:// prefix and encodes the appkey', function () { |
| 64 | + const r = capture(api => api.getStorageFile('tb://abc123')); |
| 65 | + assert.strictEqual(r.url, 'https://api.testingbot.com/v1/storage/abc123'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('createSession merges caller capabilities over the defaults', function () { |
| 69 | + const r = capture(api => api.createSession({ capabilities: { browserName: 'firefox' } })); |
| 70 | + assert.strictEqual(r.url, 'https://cloud.testingbot.com/session'); |
| 71 | + assert.strictEqual(r.data.capabilities.browserName, 'firefox'); |
| 72 | + assert.strictEqual(r.data.capabilities.browserVersion, 'latest'); |
| 73 | + assert.strictEqual(r.data.capabilities.platform, 'WIN10'); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('Tier-1 methods', function () { |
| 78 | + it('getIpRanges -> GET /v1/configuration/ip-ranges', function () { |
| 79 | + const r = capture(api => api.getIpRanges()); |
| 80 | + assert.strictEqual(r.method, 'GET'); |
| 81 | + assert.match(r.url, /\/v1\/configuration\/ip-ranges$/); |
| 82 | + }); |
| 83 | + |
| 84 | + it('getTunnelById -> GET /v1/tunnel/:id', function () { |
| 85 | + const r = capture(api => api.getTunnelById(42)); |
| 86 | + assert.strictEqual(r.method, 'GET'); |
| 87 | + assert.strictEqual(r.url, 'https://api.testingbot.com/v1/tunnel/42'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('deleteActiveTunnel -> DELETE /v1/tunnel', function () { |
| 91 | + const r = capture(api => api.deleteActiveTunnel()); |
| 92 | + assert.strictEqual(r.method, 'DELETE'); |
| 93 | + assert.strictEqual(r.url, 'https://api.testingbot.com/v1/tunnel'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('getUserClientKey -> GET /v1/team-management/users/:id/client-key', function () { |
| 97 | + const r = capture(api => api.getUserClientKey(7)); |
| 98 | + assert.strictEqual(r.url, 'https://api.testingbot.com/v1/team-management/users/7/client-key'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('getUserKeys -> GET /v1/user/keys', function () { |
| 102 | + const r = capture(api => api.getUserKeys()); |
| 103 | + assert.match(r.url, /\/v1\/user\/keys$/); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments