forked from chrisleekr/binance-trading-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: secure frontend with password (chrisleekr#260)
- Loading branch information
1 parent
1c45699
commit 6173ca9
Showing
56 changed files
with
21,289 additions
and
27,984 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
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
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,86 @@ | ||
/* eslint-disable global-require */ | ||
|
||
describe('webserver/configure.js', () => { | ||
const mockHandlers = { | ||
handleAuth: null, | ||
handle404: null | ||
}; | ||
|
||
let cacheMock; | ||
let loggerMock; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks().resetModules(); | ||
|
||
mockHandlers.handleAuth = jest.fn().mockResolvedValue(true); | ||
mockHandlers.handle404 = jest.fn().mockResolvedValue(true); | ||
|
||
jest.mock('../handlers', () => ({ | ||
handleAuth: mockHandlers.handleAuth, | ||
handle404: mockHandlers.handle404 | ||
})); | ||
}); | ||
|
||
describe('when jwt token is not cached', () => { | ||
beforeEach(async () => { | ||
const { logger, cache } = require('../../../helpers'); | ||
|
||
loggerMock = logger; | ||
cacheMock = cache; | ||
cacheMock.get = jest.fn().mockReturnValue(null); | ||
cacheMock.set = jest.fn().mockReturnValue(true); | ||
|
||
const { configureWebServer } = require('../configure'); | ||
await configureWebServer('app', loggerMock); | ||
}); | ||
|
||
it('triggers cache.get', () => { | ||
expect(cacheMock.get).toHaveBeenCalledWith('auth-jwt-secret'); | ||
}); | ||
|
||
it('triggers cache.set', () => { | ||
expect(cacheMock.set).toHaveBeenCalledWith( | ||
'auth-jwt-secret', | ||
expect.any(String) | ||
); | ||
}); | ||
|
||
[ | ||
{ | ||
handlerFunc: 'handleAuth' | ||
}, | ||
{ | ||
handlerFunc: 'handle404' | ||
} | ||
].forEach(t => { | ||
it(`triggers ${t.handlerFunc}`, () => { | ||
expect(mockHandlers[t.handlerFunc]).toHaveBeenCalledWith( | ||
loggerMock, | ||
'app' | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when jwt token is cached', () => { | ||
beforeEach(async () => { | ||
const { logger, cache } = require('../../../helpers'); | ||
|
||
loggerMock = logger; | ||
cacheMock = cache; | ||
cacheMock.get = jest.fn().mockReturnValue('uuid'); | ||
cacheMock.set = jest.fn().mockReturnValue(true); | ||
|
||
const { configureWebServer } = require('../configure'); | ||
await configureWebServer('app', loggerMock); | ||
}); | ||
|
||
it('triggers cache.get', () => { | ||
expect(cacheMock.get).toHaveBeenCalledWith('auth-jwt-secret'); | ||
}); | ||
|
||
it('does not trigger cache.set', () => { | ||
expect(cacheMock.set).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,28 @@ | ||
const { v4: uuidv4 } = require('uuid'); | ||
|
||
const { cache } = require('../../helpers'); | ||
|
||
const { handleAuth, handle404 } = require('./handlers'); | ||
|
||
const configureJWTToken = async () => { | ||
let jwtSecret = await cache.get('auth-jwt-secret'); | ||
|
||
if (jwtSecret === null) { | ||
jwtSecret = uuidv4(); | ||
await cache.set('auth-jwt-secret', jwtSecret); | ||
} | ||
|
||
return jwtSecret; | ||
}; | ||
|
||
const configureWebServer = async (app, funcLogger) => { | ||
const logger = funcLogger.child({ server: 'webserver' }); | ||
|
||
// Firstly get(or set) JWT secret | ||
await configureJWTToken(); | ||
|
||
handleAuth(logger, app); | ||
handle404(logger, app); | ||
}; | ||
|
||
module.exports = { configureWebServer }; |
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,11 @@ | ||
const handle404 = async (_logger, app) => { | ||
// catch 404 and forward to error handler | ||
app.get('*', (_req, res) => { | ||
res.send( | ||
{ success: false, status: 404, message: 'Route not found.', data: {} }, | ||
404 | ||
); | ||
}); | ||
}; | ||
|
||
module.exports = { handle404 }; |
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,24 @@ | ||
/* eslint-disable global-require */ | ||
describe('webserver/handlers/404', () => { | ||
const appMock = {}; | ||
|
||
let resSendMock; | ||
|
||
beforeEach(async () => { | ||
resSendMock = jest.fn().mockResolvedValue(true); | ||
appMock.get = jest.fn().mockImplementation((_path, func) => { | ||
func(null, { send: resSendMock }); | ||
}); | ||
|
||
const { handle404 } = require('../404'); | ||
|
||
await handle404(null, appMock); | ||
}); | ||
|
||
it('triggers res.send', () => { | ||
expect(resSendMock).toHaveBeenCalledWith( | ||
{ success: false, status: 404, message: 'Route not found.', data: {} }, | ||
404 | ||
); | ||
}); | ||
}); |
Oops, something went wrong.