mirror of
https://github.com/stolksdorf/homebrewery.git
synced 2025-12-20 09:01:30 +00:00
Fixing edge cases in the search tests
This commit is contained in:
@@ -21,29 +21,31 @@ module.exports = (Brew) => {
|
||||
return cmds.search(query, opts, fullAccess);
|
||||
},
|
||||
|
||||
search : (queryObj={}, opts={}, fullAccess = true) => {
|
||||
const pagination = _.defaults(opts.pagination, {
|
||||
search : (queryObj={}, options={}, fullAccess = true) => {
|
||||
const opts = _.merge({
|
||||
limit : 25,
|
||||
page : 0
|
||||
});
|
||||
const sorting = _.defaults(opts.sorting, {
|
||||
'views' : 1
|
||||
});
|
||||
page : 0,
|
||||
sort : {}
|
||||
}, options);
|
||||
|
||||
let filter = {
|
||||
text : 0
|
||||
};
|
||||
|
||||
if(!fullAccess){
|
||||
filter.editId = 0;
|
||||
queryObj.published = false;
|
||||
queryObj.published = true;
|
||||
}
|
||||
|
||||
const searchQuery = Brew
|
||||
.find(queryObj)
|
||||
.sort(sorting)
|
||||
.sort(opts.sort)
|
||||
.select(filter)
|
||||
.limit(pagination.limit)
|
||||
.skip(pagination.page * pagination.limit)
|
||||
.limit(opts.limit)
|
||||
.skip(opts.page * opts.limit)
|
||||
.lean()
|
||||
.exec();
|
||||
|
||||
const countQuery = Brew.count(queryObj).exec();
|
||||
|
||||
return Promise.all([searchQuery, countQuery])
|
||||
|
||||
@@ -16,7 +16,7 @@ const Middleware = {
|
||||
return 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;
|
||||
}
|
||||
return next();
|
||||
|
||||
@@ -15,6 +15,7 @@ let brewA = {
|
||||
authors : ['your_dm']
|
||||
};
|
||||
|
||||
|
||||
describe('Admin API', ()=>{
|
||||
before('Connect DB', DB.connect);
|
||||
|
||||
@@ -35,7 +36,7 @@ describe('Admin API', ()=>{
|
||||
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') })
|
||||
.set('x-homebrew-admin', config.get('admin:key'))
|
||||
.expect(200)
|
||||
.then((res) => {
|
||||
const brew = res.body;
|
||||
@@ -47,7 +48,7 @@ describe('Admin API', ()=>{
|
||||
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') })
|
||||
.set('x-homebrew-admin', config.get('admin:key'))
|
||||
.expect(200)
|
||||
.then((res) => {
|
||||
const brew = res.body;
|
||||
@@ -60,7 +61,7 @@ describe('Admin API', ()=>{
|
||||
const query = brewA.editId.substring(0, brewA.editId.length -2);
|
||||
return request(app)
|
||||
.get(`/admin/lookup/${query}`)
|
||||
.query({ admin_key : config.get('admin:key') })
|
||||
.set('x-homebrew-admin', config.get('admin:key'))
|
||||
.expect(200)
|
||||
.then((res) => {
|
||||
const brew = res.body;
|
||||
@@ -72,7 +73,7 @@ describe('Admin API', ()=>{
|
||||
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') })
|
||||
.set('x-homebrew-admin', config.get('admin:key'))
|
||||
.expect(404);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const testing = require('./test.init.js');
|
||||
const Test = require('./test.init.js');
|
||||
const request = require('supertest-as-promised');
|
||||
const jwt = require('jwt-simple');
|
||||
|
||||
const config = require('nconf');
|
||||
|
||||
const app = require('app.js');
|
||||
@@ -24,7 +24,7 @@ describe('Brew API', () => {
|
||||
before('Connect DB', DB.connect);
|
||||
before('Clear DB', BrewData.removeAll);
|
||||
before('Create session token', () => {
|
||||
session_token = jwt.encode(test_user, config.get('jwt_secret'));
|
||||
session_token = Test.getSessionToken(test_user);
|
||||
});
|
||||
before('Create brew', ()=>{
|
||||
return BrewData.create(storedBrew)
|
||||
|
||||
@@ -56,6 +56,7 @@ module.exports = {
|
||||
},
|
||||
|
||||
populateDB : (brewCollection)=>{
|
||||
PopulatedBrews = {};
|
||||
return Promise.all(_.map(brewCollection, (brewData, id) => {
|
||||
return BrewData.create(brewData)
|
||||
.then((brew)=>{
|
||||
@@ -72,8 +73,7 @@ module.exports = {
|
||||
const storedBrew = PopulatedBrews[brewId];
|
||||
if(!storedBrew) return false;
|
||||
return _.some(this._obj, (brew)=>{
|
||||
return brew.editId == storedBrew.editId &&
|
||||
brew.shareId == storedBrew.shareId &&
|
||||
return brew.shareId == storedBrew.shareId &&
|
||||
brew.title == storedBrew.title &&
|
||||
brew.views == storedBrew.views;
|
||||
});
|
||||
|
||||
@@ -15,8 +15,6 @@ const requestHandler = (req, res) => {
|
||||
};
|
||||
|
||||
|
||||
console.log(config.get('admin:key'));
|
||||
|
||||
const test_user = {
|
||||
username : 'cool guy'
|
||||
};
|
||||
@@ -106,7 +104,7 @@ describe('Middleware', () => {
|
||||
app.use(mw.admin);
|
||||
app.use(requestHandler)
|
||||
return request(app).get('/')
|
||||
.query({ admin_key : config.get('admin:key') })
|
||||
.set('x-homebrew-admin', config.get('admin:key'))
|
||||
.expect(200)
|
||||
.then((res) => {
|
||||
const req = res.body;
|
||||
@@ -119,7 +117,7 @@ describe('Middleware', () => {
|
||||
app.get(requestHandler);
|
||||
app.use(Error.expressHandler);
|
||||
return request(app).get('/')
|
||||
.query({ admin_key : 'BADUSER' })
|
||||
.set('x-homebrew-admin', 'BADADMIN')
|
||||
.send()
|
||||
.expect(401);
|
||||
});
|
||||
|
||||
@@ -17,8 +17,12 @@ describe('Brew Search', () => {
|
||||
|
||||
|
||||
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', ()=>{
|
||||
@@ -32,16 +36,35 @@ describe('Brew Search', () => {
|
||||
});
|
||||
|
||||
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', () => {
|
||||
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.skip('if author, fetches also non-published brews, with editid', ()=>{
|
||||
|
||||
it('if full access, brews should have editid, but no text', () => {
|
||||
return BrewData.search({}, {}, true)
|
||||
.then((result) => {
|
||||
result.brews[0].should.not.have.property('text');
|
||||
result.brews[0].should.have.property('editId');
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -15,6 +15,10 @@ const Chai = require('chai')
|
||||
const log = require('loglevel');
|
||||
log.setLevel(config.get('log_level'));
|
||||
|
||||
const jwt = require('jwt-simple');
|
||||
module.exports = {
|
||||
should: Chai.should()
|
||||
should: Chai.should(),
|
||||
getSessionToken : (userInfo) => {
|
||||
return jwt.encode(userInfo, config.get('jwt_secret'));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user