-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd07fe6
commit e5c9845
Showing
10 changed files
with
699 additions
and
70 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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,90 @@ | ||
const Boom = require('boom') | ||
|
||
export class AppsHandler { | ||
constructor(app) { | ||
this.app = app | ||
this.route = '/apps' | ||
} | ||
|
||
async validatePost(ctx, next) { | ||
ctx.checkHeader('user-email').notEmpty().isEmail() | ||
ctx.checkBody('bundleId').notEmpty().match(/^[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)+$/i) | ||
ctx.checkBody('key').notEmpty().len(1, 255) | ||
if (ctx.errors) { | ||
const error = Boom.badData('wrong arguments', ctx.errors) | ||
ctx.status = 422 | ||
ctx.body = error.output.payload | ||
ctx.body.data = error.data | ||
return | ||
} | ||
await next() | ||
} | ||
|
||
async get(ctx) { | ||
const apps = await this.app.db.App.findAll() | ||
ctx.body = { apps } | ||
ctx.status = 200 | ||
} | ||
|
||
async post(ctx) { | ||
const body = ctx.request.body | ||
body.createdBy = ctx.request.header['user-email'] | ||
const app = await this.app.db.App.create(body) | ||
ctx.body = { app } | ||
ctx.status = 201 | ||
} | ||
} | ||
|
||
export class AppHandler { | ||
constructor(app) { | ||
this.app = app | ||
this.route = '/apps/:id' | ||
} | ||
|
||
async validatePut(ctx, next) { | ||
ctx.checkBody('bundleId').notEmpty().match(/^[a-z0-9]+\.[a-z0-9]+(\.[a-z0-9]+)+$/i) | ||
ctx.checkBody('key').notEmpty().len(1, 255) | ||
if (ctx.errors) { | ||
const error = Boom.badData('wrong arguments', ctx.errors) | ||
ctx.status = 422 | ||
ctx.body = error.output.payload | ||
ctx.body.data = error.data | ||
return | ||
} | ||
await next() | ||
} | ||
|
||
async get(ctx) { | ||
const app = await this.app.db.App.findById(this.params.id) | ||
if (!app) { | ||
ctx.status = 404 | ||
return | ||
} | ||
ctx.body = { app } | ||
ctx.status = 200 | ||
} | ||
|
||
async put(ctx) { | ||
const body = ctx.request.body | ||
const app = await this.app.db.App.findById(this.params.id) | ||
if (!app) { | ||
ctx.status = 404 | ||
return | ||
} | ||
|
||
const updatedApp = await app.updateAttributes(body) | ||
ctx.body = { app: updatedApp } | ||
ctx.status = 200 | ||
} | ||
|
||
async delete(ctx) { | ||
const app = await this.app.db.App.findById(this.params.id) | ||
if (!app) { | ||
ctx.status = 404 | ||
return | ||
} | ||
|
||
await app.destroy() | ||
ctx.status = 204 | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import App from './app' | ||
|
||
const models = [ | ||
const models = { | ||
App, | ||
] | ||
} | ||
export default models |
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,145 @@ | ||
import { expect } from '../../common' | ||
import uuid from 'uuid' | ||
|
||
describe('Handlers', () => { | ||
describe('Apps Handler', () => { | ||
describe('GET', () => { | ||
it('should return 200 and an empty list of apps if there are no apps', async function () { | ||
await this.app.db.App.destroy({ truncate: true }) | ||
const res = await this.request.get('/apps') | ||
expect(res.status).to.equal(200) | ||
|
||
const body = res.body | ||
expect(body).to.be.an('object') | ||
|
||
expect(body.apps).to.exist() | ||
expect(body.apps).to.have.length(0) | ||
}) | ||
|
||
it('should return 200 and a list of apps', async function () { | ||
const app = { | ||
key: uuid.v4(), | ||
bundleId: 'com.app.my', | ||
createdBy: 'someone@somewhere.com', | ||
} | ||
await this.app.db.App.create(app) | ||
const res = await this.request.get('/apps') | ||
expect(res.status).to.equal(200) | ||
|
||
const body = res.body | ||
expect(body).to.be.an('object') | ||
|
||
expect(body.apps).to.exist() | ||
expect(body.apps).to.have.length.at.least(1) | ||
|
||
const myApp = body.apps.filter(a => a.key === app.key)[0] | ||
expect(myApp).to.exist() | ||
expect(myApp.key).to.equal(app.key) | ||
expect(myApp.bundleId).to.equal(app.bundleId) | ||
expect(myApp.createdBy).to.equal(app.createdBy) | ||
}) | ||
}) | ||
|
||
describe('POST', () => { | ||
let app | ||
let userEmail | ||
|
||
beforeEach(() => { | ||
app = { | ||
key: uuid.v4(), | ||
bundleId: 'com.app.my', | ||
} | ||
userEmail = 'someone@somewhere.com' | ||
}) | ||
|
||
it('should return 201 and the created app', async function () { | ||
const res = await this.request.post('/apps').send(app).set('user-email', userEmail) | ||
expect(res.status).to.equal(201) | ||
|
||
const body = res.body | ||
expect(body).to.be.an('object') | ||
|
||
expect(body.app).to.exist() | ||
expect(body.app).to.be.an('object') | ||
|
||
expect(body.app.key).to.equal(app.key) | ||
expect(body.app.bundleId).to.equal(app.bundleId) | ||
expect(body.app.createdBy).to.equal(userEmail) | ||
}) | ||
|
||
describe('Should fail if missing', () => { | ||
it('user-email header', async function () { | ||
const res = await this.request.post('/apps').send(app) | ||
expect(res.status).to.equal(422) | ||
|
||
const body = res.body | ||
expect(body).to.be.an('object') | ||
|
||
expect(body.data).to.exist() | ||
expect(body.data).to.have.length(1) | ||
expect(body.data[0]).to.have.property('user-email') | ||
expect(body.data[0]['user-email']).to.contain('empty') | ||
}) | ||
|
||
const tests = [ | ||
{ args: 'key' }, | ||
{ args: 'bundleId' }, | ||
] | ||
|
||
tests.forEach((test) => { | ||
it(test.args, async function () { | ||
delete app[test.args] | ||
const res = await this.request.post('/apps').send(app).set('user-email', userEmail) | ||
expect(res.status).to.equal(422) | ||
|
||
const body = res.body | ||
expect(body).to.be.an('object') | ||
|
||
expect(body.data).to.exist() | ||
expect(body.data).to.have.length(1) | ||
expect(body.data[0]).to.have.property(test.args) | ||
expect(body.data[0][test.args]).to.contain('empty') | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Should fail if invalid', () => { | ||
it('user-email header', async function () { | ||
const res = await this.request.post('/apps').send(app).set('user-email', 'not an email') | ||
expect(res.status).to.equal(422) | ||
|
||
const body = res.body | ||
expect(body).to.be.an('object') | ||
|
||
expect(body.data).to.exist() | ||
expect(body.data).to.have.length(1) | ||
expect(body.data[0]).to.have.property('user-email') | ||
expect(body.data[0]['user-email']).to.contain('email format') | ||
}) | ||
|
||
const tests = [ | ||
{ args: 'key', invalidParam: '', reason: 'empty' }, | ||
// { args: 'key', invalidParam: 'a'.repeat(256), reason: 'too long' }, | ||
{ args: 'bundleId', invalidParam: '', reason: 'empty' }, | ||
{ args: 'bundleId', invalidParam: 'a.s', reason: 'bad format.' }, | ||
] | ||
|
||
tests.forEach((test) => { | ||
it(test.args, async function () { | ||
app[test.args] = test.invalidParam | ||
const res = await this.request.post('/apps').send(app).set('user-email', userEmail) | ||
expect(res.status).to.equal(422) | ||
|
||
const body = res.body | ||
expect(body).to.be.an('object') | ||
|
||
expect(body.data).to.exist() | ||
expect(body.data).to.have.length(1) | ||
expect(body.data[0]).to.have.property(test.args) | ||
expect(body.data[0][test.args]).to.contain(test.reason) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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