mirror of
https://github.com/stolksdorf/homebrewery.git
synced 2025-12-22 04:51:29 +00:00
Added tests for admin routes, lookup is working
This commit is contained in:
@@ -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
89
test/admin.test.js
Normal 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; });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
@@ -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);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user