Skip to content

Commit cbb73ff

Browse files
author
Hans Kristian Flaatten
committed
feat(api): add authentication example server
1 parent ce4d9b3 commit cbb73ff

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

examples/server.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const express = require('express');
4+
const mongo = require('../test/support/mongo');
5+
const redis = require('../test/support/redis');
6+
7+
const app = module.exports = express();
8+
const auth = require('../');
9+
10+
app.use(auth({
11+
mongo: mongo.users,
12+
redis,
13+
}));
14+
15+
app.get('/', (req, res) => {
16+
res.end(`Hello ${req.user.name}`);
17+
});
18+
19+
app.use((err, req, res, next) => {
20+
res.status(err.code).json(err.toJSON());
21+
});

test/xeptance/server.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const request = require('supertest');
4+
let app;
5+
6+
before(() => {
7+
app = request(require('../../examples/server')); // eslint-disable-line global-require
8+
});
9+
10+
describe('Acceptance Server', () => {
11+
it('accepts unauthenticated user', done => {
12+
app.get('/')
13+
.set('x-forwarded-for', '123.456.789')
14+
.expect(200)
15+
.expect('X-User-Auth', 'false')
16+
.expect('X-RateLimit-Limit', '100')
17+
.expect('X-RateLimit-Remaining', '99')
18+
.expect('X-RateLimit-Reset', /[0-9]{10}/)
19+
.expect('Hello guest (123.456.789)', done);
20+
});
21+
22+
it('authenticates user with valid Authorization header', done => {
23+
app.get('/')
24+
.set('Authorization', 'Token foo_app1_dev')
25+
.expect(200)
26+
.expect('X-User-Auth', 'true')
27+
.expect('X-User-Provider', 'FOO')
28+
.expect('X-RateLimit-Limit', '500')
29+
.expect('X-RateLimit-Remaining', '499')
30+
.expect('X-RateLimit-Reset', /[0-9]{10}/)
31+
.expect('Hello FOO (foo_app1)', done);
32+
});
33+
34+
it('authenticates user with valid api_key query param', done => {
35+
app.get('/?api_key=foo_app1_dev')
36+
.expect(200)
37+
.expect('X-User-Auth', 'true')
38+
.expect('X-User-Provider', 'FOO')
39+
.expect('X-RateLimit-Limit', '500')
40+
.expect('X-RateLimit-Remaining', '499')
41+
.expect('X-RateLimit-Reset', /[0-9]{10}/)
42+
.expect('Hello FOO (foo_app1)', done);
43+
});
44+
});

0 commit comments

Comments
 (0)