|
| 1 | +import { makeSnykRequest } from '../../../src/lib/request/request'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as nock from 'nock'; |
| 4 | +import * as _ from 'lodash'; |
| 5 | +import * as path from 'path'; |
| 6 | +import { |
| 7 | + NotFoundError, |
| 8 | + ApiError, |
| 9 | + ApiAuthenticationError, |
| 10 | + GenericError, |
| 11 | +} from '../../../src/lib/customErrors/apiError'; |
| 12 | + |
| 13 | +const fixturesFolderPath = path.resolve(__dirname, '../..') + '/fixtures/'; |
| 14 | +beforeEach(() => { |
| 15 | + return nock('https://api.snyk.io') |
| 16 | + .persist() |
| 17 | + .get(/\/xyz/) |
| 18 | + .reply(404, '404') |
| 19 | + .get(/\/customtoken/) |
| 20 | + .reply(200, function() { |
| 21 | + return this.req.headers.authorization; |
| 22 | + }) |
| 23 | + .post(/\/xyz/) |
| 24 | + .reply(404, '404') |
| 25 | + .get(/\/apierror/) |
| 26 | + .reply(500, '500') |
| 27 | + .post(/\/apierror/) |
| 28 | + .reply(500, '500') |
| 29 | + .get(/\/genericerror/) |
| 30 | + .reply(512, '512') |
| 31 | + .post(/\/genericerror/) |
| 32 | + .reply(512, '512') |
| 33 | + .get(/\/apiautherror/) |
| 34 | + .reply(401, '401') |
| 35 | + .post(/\/apiautherror/) |
| 36 | + .reply(401, '401') |
| 37 | + .post(/^(?!.*xyz).*$/) |
| 38 | + .reply(200, (uri, requestBody) => { |
| 39 | + switch (uri) { |
| 40 | + case '/rest/': |
| 41 | + return requestBody; |
| 42 | + break; |
| 43 | + default: |
| 44 | + } |
| 45 | + }) |
| 46 | + .get(/^(?!.*xyz).*$/) |
| 47 | + .reply(200, (uri) => { |
| 48 | + switch (uri) { |
| 49 | + case '/rest/': |
| 50 | + return fs.readFileSync( |
| 51 | + fixturesFolderPath + 'apiResponses/general-doc.json', |
| 52 | + ); |
| 53 | + break; |
| 54 | + default: |
| 55 | + } |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +const OLD_ENV = process.env; |
| 60 | +beforeEach(() => { |
| 61 | + jest.resetModules(); // this is important - it clears the cache |
| 62 | + process.env = { ...OLD_ENV }; |
| 63 | + delete process.env.SNYK_TOKEN; |
| 64 | +}); |
| 65 | + |
| 66 | +afterEach(() => { |
| 67 | + process.env = OLD_ENV; |
| 68 | +}); |
| 69 | + |
| 70 | +describe('Test Snyk Utils make request properly', () => { |
| 71 | + it('Test GET command on /', async () => { |
| 72 | + const response = await makeSnykRequest( |
| 73 | + { verb: 'GET', url: '/', useRESTApi: true }, |
| 74 | + 'token123', |
| 75 | + ); |
| 76 | + const fixturesJSON = JSON.parse( |
| 77 | + fs |
| 78 | + .readFileSync(fixturesFolderPath + 'apiResponses/general-doc.json') |
| 79 | + .toString(), |
| 80 | + ); |
| 81 | + expect(response.data).toEqual(fixturesJSON); |
| 82 | + }); |
| 83 | + it('Test POST command on /', async () => { |
| 84 | + const bodyToSend = { |
| 85 | + testbody: {}, |
| 86 | + }; |
| 87 | + const response = await makeSnykRequest( |
| 88 | + { |
| 89 | + verb: 'POST', |
| 90 | + url: '/', |
| 91 | + body: JSON.stringify(bodyToSend), |
| 92 | + useRESTApi: true, |
| 93 | + }, |
| 94 | + 'token123', |
| 95 | + ); |
| 96 | + expect(response.data).toEqual(bodyToSend); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('Test Snyk Utils error handling/classification', () => { |
| 101 | + it('Test NotFoundError on GET command', async () => { |
| 102 | + try { |
| 103 | + await makeSnykRequest( |
| 104 | + { verb: 'GET', url: '/xyz', body: '', useRESTApi: true }, |
| 105 | + 'token123', |
| 106 | + ); |
| 107 | + } catch (err) { |
| 108 | + expect(err.data).toEqual(404); |
| 109 | + expect(err).toBeInstanceOf(NotFoundError); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + it('Test NotFoundError on POST command', async () => { |
| 114 | + try { |
| 115 | + const bodyToSend = { |
| 116 | + testbody: {}, |
| 117 | + }; |
| 118 | + await makeSnykRequest( |
| 119 | + { |
| 120 | + verb: 'POST', |
| 121 | + url: '/xyz', |
| 122 | + body: JSON.stringify(bodyToSend), |
| 123 | + useRESTApi: true, |
| 124 | + }, |
| 125 | + 'token123', |
| 126 | + ); |
| 127 | + } catch (err) { |
| 128 | + expect(err.data).toEqual(404); |
| 129 | + expect(err).toBeInstanceOf(NotFoundError); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + it('Test ApiError on GET command', async () => { |
| 134 | + try { |
| 135 | + await makeSnykRequest( |
| 136 | + { verb: 'GET', url: '/apierror', useRESTApi: true }, |
| 137 | + 'token123', |
| 138 | + ); |
| 139 | + } catch (err) { |
| 140 | + expect(err.data).toEqual(500); |
| 141 | + expect(err).toBeInstanceOf(ApiError); |
| 142 | + } |
| 143 | + }); |
| 144 | + it('Test ApiError on POST command', async () => { |
| 145 | + try { |
| 146 | + const bodyToSend = { |
| 147 | + testbody: {}, |
| 148 | + }; |
| 149 | + await makeSnykRequest( |
| 150 | + { |
| 151 | + verb: 'POST', |
| 152 | + url: '/apierror', |
| 153 | + body: JSON.stringify(bodyToSend), |
| 154 | + useRESTApi: true, |
| 155 | + }, |
| 156 | + 'token123', |
| 157 | + ); |
| 158 | + } catch (err) { |
| 159 | + expect(err.data).toEqual(500); |
| 160 | + expect(err).toBeInstanceOf(ApiError); |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + it('Test ApiAuthenticationError on GET command', async () => { |
| 165 | + try { |
| 166 | + await makeSnykRequest( |
| 167 | + { verb: 'GET', url: '/apiautherror', useRESTApi: true }, |
| 168 | + 'token123', |
| 169 | + ); |
| 170 | + } catch (err) { |
| 171 | + expect(err.data).toEqual(401); |
| 172 | + expect(err).toBeInstanceOf(ApiAuthenticationError); |
| 173 | + } |
| 174 | + }); |
| 175 | + it('Test ApiAuthenticationError on POST command', async () => { |
| 176 | + try { |
| 177 | + const bodyToSend = { |
| 178 | + testbody: {}, |
| 179 | + }; |
| 180 | + await makeSnykRequest( |
| 181 | + { |
| 182 | + verb: 'POST', |
| 183 | + url: '/apiautherror', |
| 184 | + body: JSON.stringify(bodyToSend), |
| 185 | + useRESTApi: true, |
| 186 | + }, |
| 187 | + 'token123', |
| 188 | + ); |
| 189 | + } catch (err) { |
| 190 | + expect(err.data).toEqual(401); |
| 191 | + expect(err).toBeInstanceOf(ApiAuthenticationError); |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + it('Test GenericError on GET command', async () => { |
| 196 | + try { |
| 197 | + await makeSnykRequest( |
| 198 | + { verb: 'GET', url: '/genericerror', useRESTApi: true }, |
| 199 | + 'token123', |
| 200 | + ); |
| 201 | + } catch (err) { |
| 202 | + expect(err.data).toEqual(512); |
| 203 | + expect(err).toBeInstanceOf(GenericError); |
| 204 | + } |
| 205 | + }); |
| 206 | + it('Test GenericError on POST command', async () => { |
| 207 | + try { |
| 208 | + const bodyToSend = { |
| 209 | + testbody: {}, |
| 210 | + }; |
| 211 | + await makeSnykRequest( |
| 212 | + { |
| 213 | + verb: 'POST', |
| 214 | + url: '/genericerror', |
| 215 | + body: JSON.stringify(bodyToSend), |
| 216 | + useRESTApi: true, |
| 217 | + }, |
| 218 | + 'token123', |
| 219 | + ); |
| 220 | + } catch (err) { |
| 221 | + expect(err.data).toEqual(512); |
| 222 | + expect(err).toBeInstanceOf(GenericError); |
| 223 | + } |
| 224 | + }); |
| 225 | +}); |
0 commit comments