-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(security): Create security check based on lts; add test;
- Loading branch information
Patrick Rothweil
committed
Oct 30, 2020
1 parent
7743513
commit 86096b7
Showing
6 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/// <reference types="@types/jest" /> | ||
|
||
jest.mock('execa'); | ||
jest.mock('node-fetch'); | ||
jest.mock('fs-extra'); | ||
|
||
import execa from 'execa'; | ||
import fetch from 'node-fetch'; | ||
import { logMessages } from '../src/log-messages'; | ||
import { getNodeSecurityChecker } from '../src/security'; | ||
|
||
const { Response } = jest.requireActual('node-fetch'); | ||
|
||
|
||
const exampleNodeList = [ | ||
{ version: 'v12.14.0', date: '2019-12-16', npm: '6.13.4', security: true, lts: 'Erbium' }, | ||
{ version: 'v14.7.0', date: '2020-07-29', npm: '6.14.7', security: false, lts: false }, | ||
]; | ||
const nodeVersionListURL = 'https://nodejs.org/dist/index.json'; | ||
|
||
describe('getNodeSecurityChecker', () => { | ||
it('should return success-text if Security version is used', async () => { | ||
(execa as any).mockReturnValue(Promise.resolve({ stdout: '12.14.0' })); | ||
(fetch as any).mockReturnValue(Promise.resolve(new Response(JSON.stringify(exampleNodeList)))); | ||
expect(await getNodeSecurityChecker()).toMatchObject({ | ||
error: false, | ||
text: logMessages.success.nodeVersionSecurity('12.14.0'), | ||
}); | ||
}); | ||
it('should return error-text if none Security version is used', async () => { | ||
(execa as any).mockReturnValue(Promise.resolve({ stdout: '14.7.0' })); | ||
(fetch as any).mockReturnValue(Promise.resolve(new Response(JSON.stringify(exampleNodeList)))); | ||
expect(await getNodeSecurityChecker()).toMatchObject({ | ||
error: true, | ||
text: logMessages.error.nodeVersionNotSecurityError('14.7.0'), | ||
}); | ||
}); | ||
it('should return warning-text if we can not receive node-list', async () => { | ||
(fetch as any).mockReturnValue(Promise.reject()); | ||
expect(await getNodeSecurityChecker()).toMatchObject({ | ||
error: false, | ||
text: logMessages.warning.fetchNodeListErrorNodeSecurity(nodeVersionListURL), | ||
}); | ||
}); | ||
it('should return error-text if we can not receive installed node version', async () => { | ||
(execa as any).mockReturnValue(Promise.reject()); | ||
expect(await getNodeSecurityChecker()).toMatchObject({ | ||
error: true, | ||
text: logMessages.error.readProgramVersionError('node'), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getInstalledVersion } from './get-version'; | ||
import { getNodeList } from './fetch-node-versions'; | ||
import { ILogMessage, INodeVersion } from './const'; | ||
import { logMessages } from './log-messages'; | ||
|
||
export const hasNodeVersionSecurity = (nodeList: INodeVersion[], usedNodeVersion: string) => { | ||
return nodeList.some((nodeVersion) => { | ||
return nodeVersion.version.slice(1) === usedNodeVersion && nodeVersion.security !== false; | ||
}); | ||
}; | ||
|
||
export const getSecurityNodeLog = async (usedNodeVersion: string) => { | ||
const nodeList = await getNodeList('security'); | ||
if (nodeList.error) { | ||
return { error: false, text: nodeList.text }; | ||
} | ||
|
||
return hasNodeVersionSecurity(JSON.parse(nodeList.text), usedNodeVersion) | ||
? { error: false, text: logMessages.success.nodeVersionSecurity(usedNodeVersion) } | ||
: { error: true, text: logMessages.error.nodeVersionNotSecurityError(usedNodeVersion) }; | ||
}; | ||
|
||
export const getNodeSecurityChecker = async () => { | ||
const usedNodeVersion: ILogMessage = await getInstalledVersion('node'); | ||
return usedNodeVersion.error ? usedNodeVersion : getSecurityNodeLog(usedNodeVersion.text); | ||
}; |