-
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.
test: First functional tests for Restify framework
BREAKING CHANGE: Middleware's second parameter is an object now, it contains definition of storage, framework, etc.
- Loading branch information
1 parent
db84e18
commit 8d6c778
Showing
8 changed files
with
987 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const got = require('got'); | ||
|
||
const url = 'http://localhost:9001'; | ||
let server; | ||
|
||
beforeAll(() => { | ||
return require('../../examples/restify').then((srv) => server = srv); | ||
}); | ||
|
||
describe('Restify', () => { | ||
it('should restrict /secret path', async () => { | ||
const secretPromise = got.get(url + '/secret'); | ||
await expect(secretPromise).rejects.toHaveProperty('statusCode', 403); | ||
}); | ||
|
||
it('should restrict /secret path with wrong token', async () => { | ||
const secretPromise = got.get(url + '/secret', { | ||
headers: {'X-Session-Token': 'my-fake-token'} | ||
}); | ||
await expect(secretPromise).rejects.toHaveProperty('statusCode', 403); | ||
}); | ||
|
||
it('should login and show data from /secret path', async () => { | ||
// login | ||
const loginResponse = await got.post(url + '/login', { | ||
body: {name: 'Bob'}, | ||
json: true, | ||
}); | ||
const token = loginResponse.headers['x-session-token']; | ||
|
||
// secret | ||
const secretPromise = got.get(url + '/secret', { | ||
headers: {'X-Session-Token': token} | ||
}); | ||
await expect(secretPromise).resolves.toHaveProperty('statusCode', 200); | ||
}); | ||
|
||
it('should login, logout and restrict /secret path', async () => { | ||
// login | ||
const loginResponse = await got.post(url + '/login', { | ||
body: {name: 'Bob'}, | ||
json: true, | ||
}); | ||
const token = loginResponse.headers['x-session-token']; | ||
|
||
// logout | ||
await got.post(url + '/logout', { | ||
headers: {'X-Session-Token': token} | ||
}); | ||
|
||
// secret | ||
const secretPromise = got.get(url + '/secret', { | ||
headers: {'X-Session-Token': token} | ||
}); | ||
await expect(secretPromise).rejects.toHaveProperty('statusCode', 403); | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
server.close(); | ||
}); |
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,67 @@ | ||
const restify = require('restify'); | ||
const auzy = require('../index'); | ||
|
||
const users = [{ | ||
id: 1, | ||
name: 'Bob', | ||
email: 'bob@mail.com', | ||
}]; | ||
|
||
const auzyConfig = { | ||
session: { | ||
sessionName: 'X-Session-Token', | ||
// ttl: 60 * 60 * 24 * 30 * 6, | ||
alwaysSend: true, | ||
receiveSessionId: (req, sessionName) => req.header(sessionName), | ||
sendSessionId: (res, sessionName, sessionId) => res.header(sessionName, sessionId), | ||
loadUser: (sessionData) => { | ||
const index = users.findIndex(user => user.id === sessionData.userId); | ||
if (index === -1) { | ||
return null; | ||
} | ||
return users[index]; | ||
}, | ||
}, | ||
}; | ||
const auzyEnvironment = { | ||
framework: 'restify', | ||
}; | ||
|
||
const server = restify.createServer(); | ||
server.use(restify.plugins.bodyParser()); | ||
server.use(auzy(auzyConfig, auzyEnvironment)); | ||
|
||
server.post('/login', async (req, res, next) => { | ||
const index = users.findIndex(user => user.name === req.body.name); | ||
if (index !== -1) { | ||
const user = users[index]; | ||
await req.session.authenticate({userId: user.id}); | ||
res.send({name: req.user.name}); | ||
} else { | ||
res.send(404, {error: 'User not found'}); | ||
} | ||
next(); | ||
}); | ||
|
||
server.get('/secret', (req, res, next) => { | ||
if (req.user) { | ||
res.send({email: req.user.email}); | ||
} else { | ||
res.send(403, {error: 'Restricted area'}); | ||
} | ||
next(); | ||
}); | ||
|
||
server.post('/logout', (req, res, next) => { | ||
req.session.destroy(); | ||
res.send(200); | ||
next(); | ||
}); | ||
|
||
const launchPromise = new Promise((resolve) => { | ||
server.listen(9001, () => { | ||
resolve(server); | ||
}); | ||
}); | ||
|
||
module.exports = launchPromise; |
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,21 +1,48 @@ | ||
'use strict'; | ||
|
||
module.exports = (config, storage = null) => { | ||
const SessionRequestHandler = require('./SessionRequestHandler'); | ||
|
||
const defaultMiddleware = (storageObject, config) => { | ||
return async (req, res, next) => { | ||
req.session = new SessionRequestHandler(req, res, storageObject, config.session); | ||
await req.session.loadSession(); | ||
next(); | ||
}; | ||
}; | ||
|
||
const koaMiddleware = (storageObject, config) => { | ||
return async (ctx, next) => { | ||
ctx.session = new SessionRequestHandler(ctx.request, ctx.response, storageObject, config.session); | ||
await ctx.session.loadSession(); | ||
await next(); | ||
}; | ||
}; | ||
|
||
const frameworkMiddleware = { | ||
default: defaultMiddleware, | ||
restify: defaultMiddleware, | ||
express: defaultMiddleware, | ||
connect: defaultMiddleware, | ||
koa: koaMiddleware, | ||
}; | ||
|
||
module.exports = (config, {storage = null, framework = 'default'}) => { | ||
let storageObject; | ||
if (typeof storage === 'string' || storage instanceof String) { | ||
const auzyStorage = require(`auzy-storage-${storage}`); | ||
storageObject = new auzyStorage(config.storage); | ||
} else { | ||
// Object storage is non-persistent storage for sessions | ||
// Object storage is a non-persistent storage for sessions | ||
const ObjectStorage = require('./ObjectStorage'); | ||
const objectStorage = new ObjectStorage(config.storage); | ||
storageObject = storage || objectStorage; | ||
} | ||
const SessionRequestHandler = require('./SessionRequestHandler'); | ||
|
||
return async (req, res, next) => { | ||
req.session = new SessionRequestHandler(req, res, storageObject, config.session); | ||
await req.session.loadSession(); | ||
next(); | ||
}; | ||
if (!frameworkMiddleware[framework]) { | ||
const middleware = `Framework ${framework} is not supported, please specify one of ` + | ||
`this: ${Object.keys(frameworkMiddleware).join(', ')}`; | ||
throw new Error(middleware); | ||
} | ||
|
||
return frameworkMiddleware[framework](storageObject, config); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.