1
0
mirror of https://github.com/stolksdorf/homebrewery.git synced 2025-12-22 10:21:29 +00:00

Added tests for admin routes, lookup is working

This commit is contained in:
Scott Tolksdorf
2017-01-10 14:44:48 -05:00
parent cd2eb5fdce
commit e77532acef
3 changed files with 96 additions and 13 deletions

View File

@@ -29,26 +29,19 @@ router.delete('/admin/invalid', mw.adminOnly, (req, res, next)=>{
.catch(next); .catch(next);
}); });
router.get('/admin/lookup/:search', (req, res) => { router.get('/admin/lookup/:search', mw.adminOnly, (req, res, next) => {
//search for mathcing edit id //search for mathcing edit id
//search for matching share id //search for matching share id
// search for partial match // search for partial match
BrewData.get({editId : req.params.search})
.then((brew) => {
})
BrewData.get({ $or:[ BrewData.get({ $or:[
{editId : { "$regex": req.params.search, "$options": "i" }}, {editId : { "$regex": req.params.search, "$options": "i" }},
{shareId : { "$regex": req.params.search, "$options": "i" }}, {shareId : { "$regex": req.params.search, "$options": "i" }},
]}) ]})
.then((brews) => { .then((brews) => {
console.log(brews);
return res.json(brews); return res.json(brews);
}) })
.catch(next);
});
})
module.exports = router; module.exports = router;

89
test/admin.test.js Normal file
View File

@@ -0,0 +1,89 @@
const testing = require('./test.init.js');
const request = require('supertest-as-promised');
const config = require('nconf');
const app = require('app.js');
const DB = require('db.js');
const BrewData = require('brew.data.js');
const Error = require('error.js');
let brewA = {
title : 'good title',
text : 'original text',
authors : ['your_dm']
};
describe('Admin API', ()=>{
before('Connect DB', DB.connect);
describe('Brew Lookup', ()=>{
before('Clear DB', BrewData.removeAll);
before('Create brew', ()=>{
return BrewData.create(brewA)
.then((brew)=>{ brewA = brew; });
});
it('throws an error if not admin', ()=>{
return request(app)
.get(`/admin/lookup/${brewA.editId}`)
.expect(401);
});
it('looks up a brew based on the share id', () => {
return request(app)
.get(`/admin/lookup/${brewA.shareId}`)
.query({ admin_key : config.get('admin:key') })
.expect(200)
.then((res) => {
const brew = res.body;
brew.should.have.property('editId').equal(brewA.editId);
brew.should.have.property('shareId').equal(brewA.shareId);
brew.should.have.property('text').equal(brewA.text);
});
});
it('looks up a brew based on the edit id', () => {
return request(app)
.get(`/admin/lookup/${brewA.editId}`)
.query({ admin_key : config.get('admin:key') })
.expect(200)
.then((res) => {
const brew = res.body;
brew.should.have.property('editId').equal(brewA.editId);
brew.should.have.property('shareId').equal(brewA.shareId);
brew.should.have.property('text').equal(brewA.text);
});
});
it('looks up a brew based on a partial id', () => {
const query = brewA.editId.substring(0, brewA.editId.length -2);
return request(app)
.get(`/admin/lookup/${query}`)
.query({ admin_key : config.get('admin:key') })
.expect(200)
.then((res) => {
const brew = res.body;
brew.should.have.property('editId').equal(brewA.editId);
brew.should.have.property('shareId').equal(brewA.shareId);
brew.should.have.property('text').equal(brewA.text);
});
});
it('throws an error if it can not find a brew', ()=>{
return request(app)
.get(`/admin/lookup/BADID`)
.query({ admin_key : config.get('admin:key') })
.expect(404);
});
});
describe('Invalid Brew', ()=>{
before('Clear DB', BrewData.removeAll);
before('Create brew', ()=>{
return BrewData.create(brewA)
.then((brew)=>{ brewA = brew; });
});
});
});

View File

@@ -105,8 +105,8 @@ describe('Middleware', () => {
it('should detect when you use the admin key', () => { it('should detect when you use the admin key', () => {
app.use(mw.admin); app.use(mw.admin);
app.use(requestHandler) app.use(requestHandler)
return request(app).get(`/?admin_key=${config.get('admin:key')}`) return request(app).get('/')
.send() .query({ admin_key : config.get('admin:key') })
.expect(200) .expect(200)
.then((res) => { .then((res) => {
const req = res.body; const req = res.body;
@@ -118,7 +118,8 @@ describe('Middleware', () => {
app.use(mw.adminOnly); app.use(mw.adminOnly);
app.get(requestHandler); app.get(requestHandler);
app.use(Error.expressHandler); app.use(Error.expressHandler);
return request(app).get(`/?admin_key=BADKEY`) return request(app).get('/')
.query({ admin_key : 'BADUSER' })
.send() .send()
.expect(401); .expect(401);
}); });