Skip to content

Commit 47175da

Browse files
Authentication mocks (#16)
* Updated npm dependencies * Added basic auth test * Added the documentation
1 parent cd5410b commit 47175da

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed

.mocharc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"timeout": 5000
2+
"timeout": 5000,
3+
"exit": true
34
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| /products | GET | Returns a collection of products |
1414
| /products/:sku | GET | Returns the products with the specified sku|
1515
| /restaurants | GET | Returns a collection of restaurant data |
16+
| /auth/login | POST | Returns a token for the user |
1617

1718
## Development
1819
1. Clone this repository `git clone https://github.com/opensource254/mock-api.git`

app.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const postsRouter = require('./routes/posts');
1010
const postWithUserRouter = require('./routes/postwithuser');
1111
const productRouter = require('./routes/products');
1212
const restaurantsRouter = require('./routes/restaurants');
13+
const authRouter = require('./routes/auth');
1314

1415
const app = express();
1516

@@ -26,5 +27,6 @@ app.use('/posts', postsRouter);
2627
app.use('/postwithuser', postWithUserRouter);
2728
app.use('/products', productRouter)
2829
app.use('/restaurants', restaurantsRouter)
30+
app.use('/auth', authRouter)
2931

3032
module.exports = app;

package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"cors": "^2.8.5",
1313
"debug": "~2.6.9",
1414
"express": "~4.16.1",
15+
"faker": "^5.5.3",
1516
"morgan": "~1.9.1"
1617
},
1718
"devDependencies": {

routes/auth.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const router = require('express').Router();
2+
const faker = require('faker');
3+
4+
router.post('/login', (req, res) => {
5+
let admin = {
6+
username: 'admin',
7+
password: 'admin'
8+
};
9+
10+
let user = {
11+
username: 'user',
12+
password: 'user'
13+
};
14+
15+
if (req.body.username === admin.username && req.body.password === admin.password) {
16+
// Create a fake user
17+
let user = {
18+
id: faker.datatype.uuid(),
19+
username: faker.internet.userName(),
20+
role: 'admin',
21+
token: faker.datatype.uuid(),
22+
createdAt: faker.date.past(),
23+
updatedAt: faker.date.past()
24+
};
25+
return res.json({ user });
26+
}
27+
28+
if (req.body.username === user.username && req.body.password === user.password) {
29+
// Create a fake user
30+
let user = {
31+
id: faker.datatype.uuid(),
32+
username: faker.internet.userName(),
33+
role: 'user',
34+
token: faker.datatype.uuid(),
35+
createdAt: faker.date.past(),
36+
updatedAt: faker.date.past()
37+
};
38+
return res.json({ user });
39+
}
40+
41+
res.status(401).json({
42+
message: 'Invalid Credentials'
43+
});
44+
})
45+
46+
module.exports = router;

tests/auth.test.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const chai = require('chai');
2+
const { should } = require('chai')
3+
const chaiHttp = require('chai-http')
4+
const server = require('../app')
5+
const app = chai.use(chaiHttp).request(server).keepOpen()
6+
7+
describe('#Stateless authentication', () => {
8+
it('Should return a simple token on successful authentication', async () => {
9+
const res = await app.post('/auth/login').send({
10+
username: 'admin',
11+
password: 'admin'
12+
})
13+
console.log(res.body);
14+
res.should.have.status(200)
15+
res.body.user.should.have.property('token')
16+
})
17+
18+
it('Should return an error on failed authentication', async () => {
19+
const res = await app.post('/auth/login').send({
20+
username: 'admin',
21+
password: 'wrong'
22+
})
23+
res.should.have.status(401)
24+
res.body.should.have.property('message')
25+
})
26+
27+
it('Should get the user info on successful authentication', async () => {
28+
const res = await app.post('/auth/login').send({
29+
username: 'admin',
30+
password: 'admin'
31+
})
32+
res.should.have.status(200)
33+
res.body.should.have.property('user')
34+
res.body.user.should.have.property('username')
35+
res.body.user.should.have.property('role')
36+
res.body.user.should.have.property('id')
37+
res.body.user.should.have.property('createdAt')
38+
res.body.user.should.have.property('updatedAt')
39+
})
40+
})

0 commit comments

Comments
 (0)