Skip to content

Commit 8ef118d

Browse files
Merge pull request #1 from oslabs-beta/microservice
Created auth service and event-bus for new microservice example app
2 parents ad4e996 + cd91a19 commit 8ef118d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+9852
-0
lines changed

examples_new/microservices/auth/package-lock.json

Lines changed: 5915 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "auth",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "ts-node-dev ./src/index.ts",
8+
"test": "jest --watchAll --no-cache"
9+
},
10+
"jest": {
11+
"preset": "ts-jest",
12+
"testEnvironment": "node",
13+
"setupFilesAfterEnv": [
14+
"./src/test/setup.ts"
15+
]
16+
},
17+
"keywords": [],
18+
"author": "",
19+
"license": "ISC",
20+
"dependencies": {
21+
"@chronosrx/common": "^1.0.2",
22+
"axios": "^1.6.2",
23+
"bcryptjs": "^2.4.3",
24+
"cookie-parser": "^1.4.6",
25+
"dotenv": "^16.3.1",
26+
"express": "^4.18.2",
27+
"express-async-errors": "^3.1.1",
28+
"jsonwebtoken": "^9.0.2",
29+
"mongoose": "^8.0.3",
30+
"ts-node-dev": "^2.0.0"
31+
},
32+
"devDependencies": {
33+
"@types/bcryptjs": "^2.4.6",
34+
"@types/cookie-parser": "^1.4.6",
35+
"@types/express": "^4.17.21",
36+
"@types/jest": "^29.5.11",
37+
"@types/jsonwebtoken": "^9.0.5",
38+
"@types/supertest": "^2.0.16",
39+
"jest": "^29.7.0",
40+
"mongodb-memory-server": "^9.1.1",
41+
"supertest": "^6.3.3",
42+
"ts-jest": "^29.1.1"
43+
}
44+
}
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 currentUser of null when no cookie included in request', 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+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import request from 'supertest';
2+
import { app } from '../app';
3+
4+
// Mongo Memory Server - Users collection always starts out empty**
5+
6+
// 1) Fails with bad request error either username or password are not provided
7+
it('fails if either username or password are not provided', async () => {
8+
await request(app)
9+
.post('/api/auth/login')
10+
.send({
11+
username: 'AYYYYY',
12+
})
13+
.expect(400);
14+
15+
await request(app)
16+
.post('/api/auth/login')
17+
.send({
18+
password: 'CUH',
19+
})
20+
.expect(400);
21+
});
22+
23+
// 2) Fails with bad request error if user does not exist in the database
24+
it('user does not exist', async () => {
25+
await request(app)
26+
.post('/api/auth/login')
27+
.send({
28+
username: 'nonexistentuser',
29+
password: 'test',
30+
})
31+
.expect(400);
32+
});
33+
34+
// 3) Fails with BadRequest Error if passwords do not match
35+
it('fails if passwords do not match', async () => {
36+
await request(app)
37+
.post('/api/auth/signup')
38+
.send({
39+
username: 'test',
40+
password: 'rightTest',
41+
})
42+
.expect(201);
43+
44+
await request(app)
45+
.post('/api/auth/login')
46+
.send({
47+
username: 'test',
48+
password: 'wrongTest',
49+
})
50+
.expect(400);
51+
});
52+
53+
// 4) Succeeds if username and password match a user in the database
54+
it('Users has correct password', async () => {
55+
await request(app)
56+
.post('/api/auth/signup')
57+
.send({
58+
username: 'test',
59+
password: 'test123',
60+
})
61+
.expect(201);
62+
63+
const response = await request(app)
64+
.post('/api/auth/login')
65+
.send({
66+
username: 'test',
67+
password: 'test123',
68+
})
69+
.expect(200);
70+
71+
expect(response.get('Set-Cookie')).toBeDefined();
72+
expect(response.get('Set-Cookie')[0].split('=')[0]).toEqual('token');
73+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import request from 'supertest';
2+
import { app } from '../app';
3+
4+
// Mongo Memory Server - Users collection always starts out empty**
5+
6+
// 1) Clears the cookie on logout
7+
// Means that 'Set-Cookie' header is defined
8+
//200 means request is successful, 201 means request is successful and result generated
9+
it('Clears the cookie on logout', async () => {
10+
await request(app)
11+
.post('/api/auth/logout')
12+
.send({
13+
username: 'test',
14+
password: 'test',
15+
})
16+
.expect(200);
17+
18+
const response = await request(app).post('/api/auth/logout').send().expect(200);
19+
20+
console.log(response.get('Set-Cookie'));
21+
expect(response.get('Set-Cookie')).toBeDefined();
22+
// expect(response.get('Set-Cookie')[0].split('=')[0]).toEqual('token');
23+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import request from 'supertest';
2+
import { app } from '../app';
3+
import { User } from '../models/user';
4+
5+
it('fails with 400 if no username/password provided', async () => {
6+
await request(app)
7+
.post('/api/auth/signup')
8+
.send({
9+
password: 'wheremyname',
10+
})
11+
.expect(400);
12+
13+
await request(app)
14+
.post('/api/auth/signup')
15+
.send({
16+
username: 'wheremypassword',
17+
})
18+
.expect(400);
19+
});
20+
21+
it('fails with 400 with invalid password', async () => {
22+
await request(app)
23+
.post('/api/auth/signup')
24+
.send({
25+
username: 'validuser',
26+
password: 'b',
27+
})
28+
.expect(400);
29+
});
30+
31+
it('Does not allow duplicate username signup ', async () => {
32+
await request(app)
33+
.post('/api/auth/signup')
34+
.send({
35+
username: 'test',
36+
password: 'test',
37+
})
38+
.expect(201);
39+
40+
await request(app)
41+
.post('/api/auth/signup')
42+
.send({
43+
username: 'test',
44+
password: 'test',
45+
})
46+
.expect(400);
47+
});
48+
49+
it('creates a user with valid inputs', async () => {
50+
await request(app)
51+
.post('/api/auth/signup')
52+
.send({
53+
username: 'test',
54+
password: 'test',
55+
})
56+
.expect(201);
57+
58+
const users = await User.find({});
59+
expect(users[0].username).toEqual('test');
60+
});
61+
62+
it('Sets a cookie on successful signup', async () => {
63+
const response = await request(app)
64+
.post('/api/auth/signup')
65+
.send({
66+
username: 'test',
67+
password: 'test',
68+
})
69+
.expect(201);
70+
71+
expect(response.get('Set-Cookie')).toBeDefined();
72+
expect(response.get('Set-Cookie')[0].split('=')[0]).toEqual('token');
73+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import express from 'express';
2+
import 'express-async-errors';
3+
import dotenv from 'dotenv';
4+
dotenv.config();
5+
import { NotFoundError, errorHandler } from '@chronosrx/common';
6+
import authRouter from './routes/auth-router';
7+
import eventRouter from './routes/event-router';
8+
import cookieParser from 'cookie-parser';
9+
10+
const app = express();
11+
12+
app.use(express.json());
13+
app.use(cookieParser());
14+
15+
// app.get('/', (req, res) => {
16+
// console.log('💥 Test Route');
17+
// res.status(200).send({ msg: '💥 Test route' });
18+
// });
19+
20+
app.use('/api/auth', authRouter);
21+
app.use('/events', eventRouter);
22+
23+
app.use('*', (req, res) => {
24+
throw new NotFoundError();
25+
});
26+
27+
app.use(errorHandler);
28+
29+
export { app };

0 commit comments

Comments
 (0)