Skip to content

Commit a3d4c83

Browse files
committed
getCurrentUser route boilerplate and tests complete
1 parent 83b5507 commit a3d4c83

5 files changed

Lines changed: 62 additions & 13 deletions

File tree

examples_new/microservices/auth/package-lock.json

Lines changed: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples_new/microservices/auth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"author": "",
1919
"license": "ISC",
2020
"dependencies": {
21-
"@chronosrx/common": "^1.0.1",
21+
"@chronosrx/common": "^1.0.2",
2222
"bcryptjs": "^2.4.3",
2323
"cookie-parser": "^1.4.6",
2424
"dotenv": "^16.3.1",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import request from 'supertest';
2+
import { app } from '../app';
3+
4+
// 1) responds with the correct user if cookie exists and is valid
5+
it('responds with the correct user if cookie exists and is valid', async () => {
6+
const username = 'testUser';
7+
const password = 'testPW';
8+
9+
// -- signup a user
10+
const signupResponse = await request(app)
11+
.post('/api/auth/signup')
12+
.send({
13+
username,
14+
password,
15+
})
16+
.expect(201);
17+
18+
// -- get the cookie from that response
19+
const cookie = signupResponse.get('Set-Cookie');
20+
21+
const response = await request(app)
22+
.get('/api/auth/current-user')
23+
.set('Cookie', cookie)
24+
.send({})
25+
.expect(200);
26+
27+
expect(response.body.currentUser.username).toEqual(username);
28+
});
29+
30+
// 2) responds with null if no valid cookie
31+
// -- send request to /current-user
32+
// -- expect null as resposne (response.body.currentUser)
33+
it('responds with the correct user if cookie exists and is valid', async () => {
34+
const response = await request(app)
35+
.get('/api/auth/current-user')
36+
.send({
37+
what: 'colors CAN you see',
38+
})
39+
.expect(200);
40+
41+
expect(response.body.currentUser).toEqual(null);
42+
});

examples_new/microservices/auth/src/controllers/auth-controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ export const logout = async (req: Request, res: Response) => {
8787

8888
res.status(200).send({ message: 'success' });
8989
};
90+
91+
export const getCurrentUser = async (req: Request, res: Response) => {
92+
res.send({});
93+
};
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import express from 'express';
2-
import { signup, login, logout } from '../controllers/auth-controller';
2+
import { signup, login, logout, getCurrentUser } from '../controllers/auth-controller';
3+
import { currentUser } from '@chronosrx/common';
34

45
const router = express.Router();
56

67
router.post('/signup', signup);
78
router.post('/login', login);
89
router.post('/logout', logout);
10+
router.get('/current-user', currentUser, getCurrentUser);
911

1012
export default router;

0 commit comments

Comments
 (0)