|
1 | 1 | /* @flow */ |
2 | 2 |
|
| 3 | +import fs from 'fs'; |
| 4 | +import md5file from 'md5-file'; |
3 | 5 | import MongoBinaryDownload from '../MongoBinaryDownload'; |
4 | 6 |
|
| 7 | +jest.mock('fs'); |
| 8 | +jest.mock('md5-file'); |
| 9 | + |
5 | 10 | describe('MongoBinaryDownload', () => { |
| 11 | + afterEach(() => { |
| 12 | + delete process.env.MONGOMS_SKIP_MD5_CHECK; |
| 13 | + }); |
| 14 | + |
| 15 | + it('skipMD5 attribute can be set via constructor parameter', () => { |
| 16 | + expect(new MongoBinaryDownload({ skipMD5: true }).skipMD5).toBe(true); |
| 17 | + expect(new MongoBinaryDownload({ skipMD5: false }).skipMD5).toBe(false); |
| 18 | + }); |
| 19 | + |
| 20 | + it(`if skipMD5 input parameter is missing, then it checks |
| 21 | +MONGOMS_SKIP_MD5_CHECK environment variable`, () => { |
| 22 | + expect(new MongoBinaryDownload({}).skipMD5).toBe(false); |
| 23 | + process.env.MONGOMS_SKIP_MD5_CHECK = '1'; |
| 24 | + expect(new MongoBinaryDownload({}).skipMD5).toBe(true); |
| 25 | + }); |
| 26 | + |
6 | 27 | it('should use direct download', async () => { |
7 | 28 | process.env['yarn_https-proxy'] = ''; |
8 | 29 | process.env.yarn_proxy = ''; |
@@ -34,4 +55,46 @@ describe('MongoBinaryDownload', () => { |
34 | 55 | expect(callArg1.agent).toBeDefined(); |
35 | 56 | expect(callArg1.agent.options.href).toBe('http://user:pass@proxy:8080/'); |
36 | 57 | }); |
| 58 | + |
| 59 | + it(`checkMD5 returns true if md5 of downloaded mongoDBArchive is |
| 60 | +the same as in the reference result`, () => { |
| 61 | + const someMd5 = 'md5'; |
| 62 | + fs.readFileSync.mockImplementationOnce(() => `${someMd5} fileName`); |
| 63 | + md5file.sync.mockImplementationOnce(() => someMd5); |
| 64 | + const mongoDBArchivePath = '/some/path'; |
| 65 | + const fileWithReferenceMd5 = '/another/path'; |
| 66 | + const du = new MongoBinaryDownload({}); |
| 67 | + // $FlowFixMe |
| 68 | + du.download = jest.fn(() => Promise.resolve(fileWithReferenceMd5)); |
| 69 | + const urlToMongoDBArchivePath = 'some-url'; |
| 70 | + return du.checkMD5(urlToMongoDBArchivePath, mongoDBArchivePath).then(res => { |
| 71 | + expect(res).toBe(true); |
| 72 | + expect(du.download).toBeCalledWith(urlToMongoDBArchivePath); |
| 73 | + expect(fs.readFileSync).toBeCalledWith(fileWithReferenceMd5); |
| 74 | + expect(md5file.sync).toBeCalledWith(mongoDBArchivePath); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it(`checkMD5 throws an error if md5 of downloaded mongoDBArchive is NOT |
| 79 | + the same as in the reference result`, () => { |
| 80 | + fs.readFileSync.mockImplementationOnce(() => 'someMd5 fileName'); |
| 81 | + md5file.sync.mockImplementationOnce(() => 'anotherMd5'); |
| 82 | + const du = new MongoBinaryDownload({}); |
| 83 | + // $FlowFixMe |
| 84 | + du.download = jest.fn(() => Promise.resolve('')); |
| 85 | + expect(du.checkMD5('', '')).rejects.toMatchInlineSnapshot( |
| 86 | + `[Error: MongoBinaryDownload: md5 check is failed]` |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('true value of skipMD5 attribute disables checkMD5 validation', () => { |
| 91 | + expect.assertions(1); |
| 92 | + fs.readFileSync.mockImplementationOnce(() => 'someMd5 fileName'); |
| 93 | + md5file.sync.mockImplementationOnce(() => 'anotherMd5'); |
| 94 | + const du = new MongoBinaryDownload({}); |
| 95 | + du.skipMD5 = true; |
| 96 | + return du.checkMD5('', '').then(res => { |
| 97 | + expect(res).toBe(undefined); |
| 98 | + }); |
| 99 | + }); |
37 | 100 | }); |
0 commit comments