|
| 1 | +import {fetchAsJson as httpFetchAsJson} from '../sources/httpUtils'; |
| 2 | +import {DEFAULT_HEADERS, DEFAULT_NPM_REGISTRY_URL, fetchAsJson} from '../sources/npmRegistryUtils'; |
| 3 | + |
| 4 | +jest.mock(`../sources/httpUtils`); |
| 5 | + |
| 6 | +describe(`npm registry utils fetchAsJson`, () => { |
| 7 | + const OLD_ENV = process.env; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + process.env = {...OLD_ENV}; // Make a copy |
| 11 | + jest.resetAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + process.env = OLD_ENV; // Restore old environment |
| 16 | + }); |
| 17 | + |
| 18 | + it(`throw usage error if COREPACK_ENABLE_NETWORK env is set to 0`, async () => { |
| 19 | + process.env.COREPACK_ENABLE_NETWORK = `0`; |
| 20 | + |
| 21 | + await expect(fetchAsJson(`package-name`)).rejects.toThrowError(); |
| 22 | + }); |
| 23 | + |
| 24 | + it(`loads from DEFAULT_NPM_REGISTRY_URL by default`, async () => { |
| 25 | + await fetchAsJson(`package-name`); |
| 26 | + |
| 27 | + expect(httpFetchAsJson).toBeCalled(); |
| 28 | + expect(httpFetchAsJson).lastCalledWith(`${DEFAULT_NPM_REGISTRY_URL}/package-name`, {headers: DEFAULT_HEADERS}); |
| 29 | + }); |
| 30 | + |
| 31 | + it(`loads from custom COREPACK_NPM_REGISTRY if set`, async () => { |
| 32 | + process.env.COREPACK_NPM_REGISTRY = `https://registry.example.org`; |
| 33 | + await fetchAsJson(`package-name`); |
| 34 | + |
| 35 | + expect(httpFetchAsJson).toBeCalled(); |
| 36 | + expect(httpFetchAsJson).lastCalledWith(`${process.env.COREPACK_NPM_REGISTRY}/package-name`, {headers: DEFAULT_HEADERS}); |
| 37 | + }); |
| 38 | + |
| 39 | + it(`adds authorization header with bearer token if COREPACK_NPM_TOKEN is set`, async () => { |
| 40 | + process.env.COREPACK_NPM_TOKEN = `foo`; |
| 41 | + |
| 42 | + await fetchAsJson(`package-name`); |
| 43 | + |
| 44 | + expect(httpFetchAsJson).toBeCalled(); |
| 45 | + expect(httpFetchAsJson).lastCalledWith(`${DEFAULT_NPM_REGISTRY_URL}/package-name`, {headers: { |
| 46 | + ...DEFAULT_HEADERS, |
| 47 | + authorization: `Bearer ${process.env.COREPACK_NPM_TOKEN}`, |
| 48 | + }}); |
| 49 | + }); |
| 50 | + |
| 51 | + it(`only adds authorization header with bearer token if COREPACK_NPM_TOKEN and COREPACK_NPM_USERNAME are set`, async () => { |
| 52 | + process.env.COREPACK_NPM_TOKEN = `foo`; |
| 53 | + process.env.COREPACK_NPM_USERNAME = `bar`; |
| 54 | + process.env.COREPACK_NPM_PASSWORD = `foobar`; |
| 55 | + |
| 56 | + await fetchAsJson(`package-name`); |
| 57 | + |
| 58 | + expect(httpFetchAsJson).toBeCalled(); |
| 59 | + expect(httpFetchAsJson).lastCalledWith(`${DEFAULT_NPM_REGISTRY_URL}/package-name`, {headers: { |
| 60 | + ...DEFAULT_HEADERS, |
| 61 | + authorization: `Bearer ${process.env.COREPACK_NPM_TOKEN}`, |
| 62 | + }}); |
| 63 | + }); |
| 64 | + |
| 65 | + |
| 66 | + it(`adds authorization header with basic auth if COREPACK_NPM_USERNAME and COREPACK_NPM_PASSWORD are set`, async () => { |
| 67 | + process.env.COREPACK_NPM_USERNAME = `foo`; |
| 68 | + process.env.COREPACK_NPM_PASSWORD = `bar`; |
| 69 | + |
| 70 | + const encodedCreds = Buffer.from(`${process.env.COREPACK_NPM_USERNAME}:${process.env.COREPACK_NPM_PASSWORD}`, `utf8`).toString(`base64`); |
| 71 | + |
| 72 | + await fetchAsJson(`package-name`); |
| 73 | + |
| 74 | + expect(httpFetchAsJson).toBeCalled(); |
| 75 | + expect(httpFetchAsJson).lastCalledWith(`${DEFAULT_NPM_REGISTRY_URL}/package-name`, {headers: { |
| 76 | + ...DEFAULT_HEADERS, |
| 77 | + authorization: `Basic ${encodedCreds}`, |
| 78 | + }}); |
| 79 | + }); |
| 80 | + |
| 81 | + it(`does not add authorization header if COREPACK_NPM_USERNAME is set and COREPACK_NPM_PASSWORD is not.`, async () => { |
| 82 | + process.env.COREPACK_NPM_USERNAME = `foo`; |
| 83 | + |
| 84 | + await fetchAsJson(`package-name`); |
| 85 | + |
| 86 | + expect(httpFetchAsJson).toBeCalled(); |
| 87 | + expect(httpFetchAsJson).lastCalledWith(`${DEFAULT_NPM_REGISTRY_URL}/package-name`, {headers: DEFAULT_HEADERS}); |
| 88 | + }); |
| 89 | +}); |
0 commit comments