|
| 1 | +/* eslint-disable */ |
| 2 | + |
| 3 | +const context = require('../index'); // eslint-disable-line |
| 4 | +const { whoami } = require('./whoami'); |
| 5 | +const { APIKeyContext } = require('./index'); |
| 6 | + |
| 7 | +jest.mock('../../api/helper', () => { // eslint-disable-line |
| 8 | + const apiHelper = require.requireActual('../../api/helper'); |
| 9 | + |
| 10 | + let response; |
| 11 | + let validateParams; |
| 12 | + |
| 13 | + const __setResponse = (x) => { |
| 14 | + response = x; |
| 15 | + }; |
| 16 | + |
| 17 | + const __setValidateParams = (x) => { |
| 18 | + validateParams = x; |
| 19 | + }; |
| 20 | + |
| 21 | + const __reset = () => { |
| 22 | + response = null; |
| 23 | + validateParams = null; |
| 24 | + }; |
| 25 | + |
| 26 | + const sendHttpRequest = (userOptions, context, flag) => { // eslint-disable-line |
| 27 | + if (validateParams) { |
| 28 | + validateParams(userOptions, context, flag); |
| 29 | + } |
| 30 | + |
| 31 | + if (response) { |
| 32 | + return response(); |
| 33 | + } else { // eslint-disable-line |
| 34 | + return apiHelper.sendHttpRequest(userOptions, context, flag); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + return { |
| 39 | + __setResponse, |
| 40 | + __setValidateParams, |
| 41 | + __reset, |
| 42 | + sendHttpRequest, |
| 43 | + }; |
| 44 | +}); |
| 45 | + |
| 46 | +const apiHelper = require('../../api/helper'); |
| 47 | + |
| 48 | +beforeEach(() => { |
| 49 | + apiHelper.__reset(); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('whoami tests', () => { |
| 53 | + |
| 54 | + describe('api key context', () => { |
| 55 | + |
| 56 | + describe('positive', () => { |
| 57 | + |
| 58 | + it('should return account information in case context is valid', () => { |
| 59 | + const context = new APIKeyContext({ |
| 60 | + name: 'test-context', |
| 61 | + url: 'http://test', |
| 62 | + token: 'test-token', |
| 63 | + }); |
| 64 | + |
| 65 | + apiHelper.__setValidateParams((userOptions, context, flag) => { |
| 66 | + expect(userOptions) |
| 67 | + .toEqual({ |
| 68 | + 'method': 'GET', |
| 69 | + 'url': '/api/user/', |
| 70 | + }); |
| 71 | + expect(flag) |
| 72 | + .toEqual(true); |
| 73 | + }); |
| 74 | + |
| 75 | + apiHelper.__setResponse(() => { |
| 76 | + return Promise.resolve({ |
| 77 | + activeAccountName: 'account-name-1', |
| 78 | + account: [ |
| 79 | + { |
| 80 | + name: 'account-name-1', |
| 81 | + runtimeEnvironment: 'runtime-environment-1', |
| 82 | + }, |
| 83 | + { |
| 84 | + name: 'account-name-2', |
| 85 | + runtimeEnvironment: 'runtime-environment-2', |
| 86 | + }, |
| 87 | + ], |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + return whoami(context) |
| 92 | + .then((accountInfo) => { |
| 93 | + expect(accountInfo) |
| 94 | + .toEqual({ |
| 95 | + name: 'account-name-1', |
| 96 | + runtimeEnvironment: 'runtime-environment-1', |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + }); |
| 101 | + |
| 102 | + }); |
| 103 | + |
| 104 | + describe('negative', () => { |
| 105 | + |
| 106 | + it('should fail in case context is not valid', () => { |
| 107 | + const context = new APIKeyContext({ |
| 108 | + name: 'test-context', |
| 109 | + url: 'http://test', |
| 110 | + token: 'test-token', |
| 111 | + }); |
| 112 | + |
| 113 | + apiHelper.__setResponse(() => { |
| 114 | + return Promise.reject(new Error('http request error')); |
| 115 | + }); |
| 116 | + |
| 117 | + return whoami(context) |
| 118 | + .then(() => { |
| 119 | + throw new Error('should have failed'); |
| 120 | + }, (err) => { |
| 121 | + expect(err.toString()) |
| 122 | + .toEqual('Error: http request error'); |
| 123 | + }); |
| 124 | + |
| 125 | + }); |
| 126 | + |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | +}); |
0 commit comments