generated from wednesday-solutions/nodejs-hapi-template
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from wednesday-solutions/feat/fix-tests-for-n…
…ode20 Feat/fix tests for node20
- Loading branch information
Showing
14 changed files
with
2,375 additions
and
1,732 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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
FROM node:20 | ||
ARG ENVIRONMENT_NAME | ||
ARG BUILD_NAME | ||
ENV NODE_OPTIONS=--openssl-legacy-provider | ||
|
||
RUN mkdir -p /app-build | ||
ADD . /app-build | ||
|
@@ -14,12 +13,11 @@ RUN yarn build:$BUILD_NAME | |
FROM node:20-alpine | ||
ARG ENVIRONMENT_NAME | ||
ARG BUILD_NAME | ||
ENV NODE_OPTIONS=--openssl-legacy-provider | ||
|
||
RUN mkdir -p /dist | ||
RUN apk add yarn | ||
RUN yarn global add [email protected] | ||
RUN yarn add shelljs bull dotenv pg [email protected] | ||
RUN yarn add shelljs dotenv pg [email protected] | ||
ADD scripts/migrate-and-run.sh / | ||
ADD package.json / | ||
ADD . / | ||
|
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,37 @@ | ||
module.exports = function(api) { | ||
api.cache(true); // Caches the computed configuration | ||
|
||
const presets = ['@babel/preset-env', '@babel/preset-flow']; | ||
const plugins = [ | ||
'@babel/plugin-proposal-throw-expressions', | ||
'@babel/plugin-proposal-class-properties', | ||
'@babel/transform-runtime' | ||
]; | ||
|
||
// Environment-specific configuration | ||
const env = process.env.BABEL_ENV || process.env.NODE_ENV; | ||
if (env !== 'test') { | ||
plugins.push([ | ||
'module-resolver', | ||
{ | ||
root: ['./src'], | ||
alias: { | ||
'@server': './server', | ||
'@root': '', | ||
'@utils': './server/utils', | ||
'@middleware': './server/middleware', | ||
'@services': './server/services', | ||
'@daos': './server/daos', | ||
'@database': './server/database', | ||
'@gql': './server/gql', | ||
'@config': './config' | ||
} | ||
} | ||
]); | ||
} | ||
|
||
return { | ||
presets, | ||
plugins | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import md5 from 'md5'; | ||
import { getUserByEmailPassword, createUserBySignup } from '../auth'; | ||
import db from '@database/models'; | ||
import { checkPassword, createPassword } from '@server/utils/passwordUtils'; | ||
|
||
describe('getUserBySign tests', () => { | ||
const email = '[email protected]'; | ||
const password = 1234; | ||
const md5Password = md5(password); | ||
const user = { email, password: md5Password }; | ||
const password = '1234'; | ||
const hashedPassword = createPassword(password); | ||
const user = { email, password: hashedPassword }; | ||
let mock; | ||
|
||
beforeEach(() => { | ||
|
@@ -25,9 +25,12 @@ describe('creatUserBySignup tests', () => { | |
const firstName = 'abc'; | ||
const lastName = 'x'; | ||
const email = '[email protected]'; | ||
const password = 1234; | ||
const password = '1234'; | ||
const mock = jest.spyOn(db.users, 'create'); | ||
await createUserBySignup(firstName, lastName, email, password); | ||
expect(mock).toHaveBeenCalledWith({ firstName, lastName, email, password: md5(password) }); | ||
expect(mock).toHaveBeenCalledWith({ firstName, lastName, email, password: expect.any(String) }); | ||
expect(mock.mock.calls[0][0].password.length).toEqual(161); | ||
const hashedPassword = mock.mock.calls[0][0].password; | ||
expect(checkPassword(password, hashedPassword)); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,5 +1,13 @@ | ||
import md5 from 'md5'; | ||
import crypto from 'crypto'; | ||
|
||
export const createPassword = password => md5(password); | ||
export const createPassword = password => { | ||
const salt = crypto.randomBytes(16).toString('hex'); // Generate a random salt | ||
const hashedPassword = crypto.scryptSync(password, salt, 64).toString('hex'); // Generate a hash | ||
return `${salt}:${hashedPassword}`; | ||
}; | ||
|
||
export const checkPassword = (password, hash) => md5(password) === hash; | ||
export const checkPassword = (passwordToVerify, storedHash) => { | ||
const [salt, key] = storedHash.split(':'); | ||
const derivedKey = crypto.scryptSync(passwordToVerify, salt, 64).toString('hex'); | ||
return key === derivedKey; | ||
}; |
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 |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import { createPassword, checkPassword } from '../passwordUtils'; | ||
import md5 from 'md5'; | ||
import crypto from 'crypto'; | ||
|
||
describe('createPassword tests', () => { | ||
it('should ensure that it create correct password', () => { | ||
const password = 1234; | ||
const md5Password = md5(password); | ||
expect(createPassword(password)).toEqual(md5Password); | ||
const password = '1234'; | ||
const salt = crypto.randomBytes(16).toString('hex'); | ||
const hashedPassword = crypto.scryptSync(password, salt, 64).toString('hex'); | ||
expect(createPassword(password).length).toEqual(`${salt}:${hashedPassword}`.length); | ||
}); | ||
}); | ||
|
||
describe('check password tests', () => { | ||
const password = 1234; | ||
const md5Password = md5(1234); | ||
const password = '1234'; | ||
const salt = crypto.randomBytes(16).toString('hex'); | ||
const hashedPassword = `${salt}:${crypto.scryptSync(password, salt, 64).toString('hex')}`; | ||
|
||
it('should ensure it returns true when password is correct', () => { | ||
expect(checkPassword(password, md5Password)).toBeTruthy(); | ||
it('should ensure it returns true when password is correct', async () => { | ||
expect(await checkPassword(password, hashedPassword)).toBeTruthy(); | ||
}); | ||
it('should ensure it returns false when password is incorrect', () => { | ||
expect(checkPassword(123, md5Password)).toBeFalsy(); | ||
expect(checkPassword('123', md5Password)).toBeFalsy(); | ||
expect(checkPassword('', md5Password)).toBeFalsy(); | ||
it('should ensure it returns false when password is incorrect', async () => { | ||
expect(await checkPassword('123', hashedPassword)).toBeFalsy(); | ||
expect(await checkPassword('', hashedPassword)).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.
8057248
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report
Test suite run success
177 tests passing in 55 suites.
Report generated by 🧪jest coverage report action from 8057248