-
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.
correcting the test cases for auth middleware
- Loading branch information
1 parent
17afef8
commit 1eda61f
Showing
7 changed files
with
105 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
import request from 'supertest'; | ||
import app from '../app'; | ||
import * as auth from '../services/auth.service'; | ||
const mcheckUserLoggedIn = jest | ||
.spyOn(auth, 'checkUserLoggedIn') | ||
.mockImplementation((req, res, next) => { | ||
req.user = 'john.doe'; //for testing bypassing the auth | ||
if (req.user) { | ||
next(); | ||
} | ||
return res.status(401).send({ | ||
error: 'You are not logged in..!!' | ||
}); | ||
}); | ||
import app from '../app'; //This should be in the last or after the mock | ||
|
||
describe('GET - Respond when requesting for healthcheck', () => { | ||
test('Should return success message', async () => { | ||
const res = await request(app).get('/v1/healthcheck'); | ||
expect(res.status).toEqual(200); | ||
expect(mcheckUserLoggedIn).toHaveBeenCalled(); | ||
expect(res.text).toEqual('Hello from Kutir, I am alive..!!'); | ||
}); | ||
}); |
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,13 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { logger } from '../utils/logger'; | ||
|
||
export function checkUserLoggedIn(req: Request, res: Response, next: NextFunction) { | ||
logger.debug(`Current user is ${req.user}`); | ||
const isUserLoggedIn = req.isAuthenticated() && req.user; | ||
if (!isUserLoggedIn) { | ||
return res.status(401).send({ | ||
error: 'You are not logged in..!!' | ||
}); | ||
} | ||
next(); | ||
} |
File renamed without changes.