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

Fixing edge cases in the search tests

This commit is contained in:
Scott Tolksdorf
2017-01-27 19:47:45 -05:00
parent a826aaffd9
commit 26bcb3395a
8 changed files with 80 additions and 43 deletions

View File

@@ -21,29 +21,31 @@ module.exports = (Brew) => {
return cmds.search(query, opts, fullAccess); return cmds.search(query, opts, fullAccess);
}, },
search : (queryObj={}, opts={}, fullAccess = true) => { search : (queryObj={}, options={}, fullAccess = true) => {
const pagination = _.defaults(opts.pagination, { const opts = _.merge({
limit : 25, limit : 25,
page : 0 page : 0,
}); sort : {}
const sorting = _.defaults(opts.sorting, { }, options);
'views' : 1
});
let filter = { let filter = {
text : 0 text : 0
}; };
if(!fullAccess){ if(!fullAccess){
filter.editId = 0; filter.editId = 0;
queryObj.published = false; queryObj.published = true;
} }
const searchQuery = Brew const searchQuery = Brew
.find(queryObj) .find(queryObj)
.sort(sorting) .sort(opts.sort)
.select(filter) .select(filter)
.limit(pagination.limit) .limit(opts.limit)
.skip(pagination.page * pagination.limit) .skip(opts.page * opts.limit)
.lean()
.exec(); .exec();
const countQuery = Brew.count(queryObj).exec(); const countQuery = Brew.count(queryObj).exec();
return Promise.all([searchQuery, countQuery]) return Promise.all([searchQuery, countQuery])

View File

@@ -16,7 +16,7 @@ const Middleware = {
return next(); return next();
}, },
admin : (req, res, next) => { admin : (req, res, next) => {
if(req.query.admin_key === config.get('admin:key')){ if(req.headers['x-homebrew-admin'] === config.get('admin:key')){
req.admin = true; req.admin = true;
} }
return next(); return next();

View File

@@ -15,6 +15,7 @@ let brewA = {
authors : ['your_dm'] authors : ['your_dm']
}; };
describe('Admin API', ()=>{ describe('Admin API', ()=>{
before('Connect DB', DB.connect); before('Connect DB', DB.connect);
@@ -35,7 +36,7 @@ describe('Admin API', ()=>{
it('looks up a brew based on the share id', () => { it('looks up a brew based on the share id', () => {
return request(app) return request(app)
.get(`/admin/lookup/${brewA.shareId}`) .get(`/admin/lookup/${brewA.shareId}`)
.query({ admin_key : config.get('admin:key') }) .set('x-homebrew-admin', config.get('admin:key'))
.expect(200) .expect(200)
.then((res) => { .then((res) => {
const brew = res.body; const brew = res.body;
@@ -47,7 +48,7 @@ describe('Admin API', ()=>{
it('looks up a brew based on the edit id', () => { it('looks up a brew based on the edit id', () => {
return request(app) return request(app)
.get(`/admin/lookup/${brewA.editId}`) .get(`/admin/lookup/${brewA.editId}`)
.query({ admin_key : config.get('admin:key') }) .set('x-homebrew-admin', config.get('admin:key'))
.expect(200) .expect(200)
.then((res) => { .then((res) => {
const brew = res.body; const brew = res.body;
@@ -60,7 +61,7 @@ describe('Admin API', ()=>{
const query = brewA.editId.substring(0, brewA.editId.length -2); const query = brewA.editId.substring(0, brewA.editId.length -2);
return request(app) return request(app)
.get(`/admin/lookup/${query}`) .get(`/admin/lookup/${query}`)
.query({ admin_key : config.get('admin:key') }) .set('x-homebrew-admin', config.get('admin:key'))
.expect(200) .expect(200)
.then((res) => { .then((res) => {
const brew = res.body; const brew = res.body;
@@ -72,7 +73,7 @@ describe('Admin API', ()=>{
it('throws an error if it can not find a brew', ()=>{ it('throws an error if it can not find a brew', ()=>{
return request(app) return request(app)
.get(`/admin/lookup/BADID`) .get(`/admin/lookup/BADID`)
.query({ admin_key : config.get('admin:key') }) .set('x-homebrew-admin', config.get('admin:key'))
.expect(404); .expect(404);
}); });
}); });

View File

@@ -1,6 +1,6 @@
const testing = require('./test.init.js'); const Test = require('./test.init.js');
const request = require('supertest-as-promised'); const request = require('supertest-as-promised');
const jwt = require('jwt-simple');
const config = require('nconf'); const config = require('nconf');
const app = require('app.js'); const app = require('app.js');
@@ -24,7 +24,7 @@ describe('Brew API', () => {
before('Connect DB', DB.connect); before('Connect DB', DB.connect);
before('Clear DB', BrewData.removeAll); before('Clear DB', BrewData.removeAll);
before('Create session token', () => { before('Create session token', () => {
session_token = jwt.encode(test_user, config.get('jwt_secret')); session_token = Test.getSessionToken(test_user);
}); });
before('Create brew', ()=>{ before('Create brew', ()=>{
return BrewData.create(storedBrew) return BrewData.create(storedBrew)

View File

@@ -56,6 +56,7 @@ module.exports = {
}, },
populateDB : (brewCollection)=>{ populateDB : (brewCollection)=>{
PopulatedBrews = {};
return Promise.all(_.map(brewCollection, (brewData, id) => { return Promise.all(_.map(brewCollection, (brewData, id) => {
return BrewData.create(brewData) return BrewData.create(brewData)
.then((brew)=>{ .then((brew)=>{
@@ -72,8 +73,7 @@ module.exports = {
const storedBrew = PopulatedBrews[brewId]; const storedBrew = PopulatedBrews[brewId];
if(!storedBrew) return false; if(!storedBrew) return false;
return _.some(this._obj, (brew)=>{ return _.some(this._obj, (brew)=>{
return brew.editId == storedBrew.editId && return brew.shareId == storedBrew.shareId &&
brew.shareId == storedBrew.shareId &&
brew.title == storedBrew.title && brew.title == storedBrew.title &&
brew.views == storedBrew.views; brew.views == storedBrew.views;
}); });

View File

@@ -15,8 +15,6 @@ const requestHandler = (req, res) => {
}; };
console.log(config.get('admin:key'));
const test_user = { const test_user = {
username : 'cool guy' username : 'cool guy'
}; };
@@ -106,7 +104,7 @@ describe('Middleware', () => {
app.use(mw.admin); app.use(mw.admin);
app.use(requestHandler) app.use(requestHandler)
return request(app).get('/') return request(app).get('/')
.query({ admin_key : config.get('admin:key') }) .set('x-homebrew-admin', config.get('admin:key'))
.expect(200) .expect(200)
.then((res) => { .then((res) => {
const req = res.body; const req = res.body;
@@ -119,7 +117,7 @@ describe('Middleware', () => {
app.get(requestHandler); app.get(requestHandler);
app.use(Error.expressHandler); app.use(Error.expressHandler);
return request(app).get('/') return request(app).get('/')
.query({ admin_key : 'BADUSER' }) .set('x-homebrew-admin', 'BADADMIN')
.send() .send()
.expect(401); .expect(401);
}); });

View File

@@ -17,8 +17,12 @@ describe('Brew Search', () => {
describe('Searching', ()=>{ describe('Searching', ()=>{
it.skip('should return a total and a brew array', ()=>{ it('should return a total and a brew array', ()=>{
return BrewData.search()
.then((result) => {
result.total.should.be.a('number');
result.brews.should.be.an('array');
})
}); });
it('should be able to search for all brews', ()=>{ it('should be able to search for all brews', ()=>{
@@ -32,16 +36,35 @@ describe('Brew Search', () => {
}); });
describe('Pagniation', () => { describe('Pagniation', () => {
it.skip('should return the exact number of brews based on limit', () => { it('should return the exact number of brews based on limit', () => {
return BrewData.search({}, {
limit : 2
})
.then((result) => {
result.total.should.be.equal(_.size(BrewGen.static()));
result.brews.length.should.be.equal(2);
})
}); });
it.skip('should return the correct pages when specified', () => { it('should return the correct pages when specified', () => {
return BrewData.search({}, {
limit : 2,
page : 1,
sort : { views : 1 }
})
.then((result) => {
result.brews.should.have.brews('BrewA', 'BrewB');
})
}); });
it.skip('should return a partial list if on the last page', () => { it('should return a partial list if on the last page', () => {
return BrewData.search({}, {
limit : 3,
page : 1
})
.then((result) => {
result.brews.length.should.be.equal(1);
});
}); });
}); });
@@ -59,17 +82,26 @@ describe('Brew Search', () => {
}); });
describe('Permissions', () => { describe('Permissions', () => {
it.skip('should only fetch published brews', () => { it('should only fetch published brews', () => {
return BrewData.search({}, {}, false)
.then((result) => {
result.total.should.be.equal(2);
result.brews.should.have.brews('BrewB', 'BrewD');
})
}); });
it.skip('fetched brews should not have text or editId', () => { it('fetched brews should not have text or editId', () => {
return BrewData.search({}, {}, false)
.then((result) => {
result.brews[0].should.not.have.property('text');
result.brews[0].should.not.have.property('editId');
})
}); });
it.skip('if admin, fetches also non-published brews, with editid', () => { it('if full access, brews should have editid, but no text', () => {
return BrewData.search({}, {}, true)
}); .then((result) => {
it.skip('if author, fetches also non-published brews, with editid', ()=>{ result.brews[0].should.not.have.property('text');
result.brews[0].should.have.property('editId');
})
}); });
}); });

View File

@@ -15,6 +15,10 @@ const Chai = require('chai')
const log = require('loglevel'); const log = require('loglevel');
log.setLevel(config.get('log_level')); log.setLevel(config.get('log_level'));
const jwt = require('jwt-simple');
module.exports = { module.exports = {
should: Chai.should() should: Chai.should(),
getSessionToken : (userInfo) => {
return jwt.encode(userInfo, config.get('jwt_secret'));
}
}; };