0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-17 14:32:41 +00:00

All tests should be done, phew

This commit is contained in:
Scott Tolksdorf
2017-01-28 12:25:26 -05:00
parent 26bcb3395a
commit 75111acefb
6 changed files with 289 additions and 117 deletions

View File

@@ -6,10 +6,25 @@ const mw = require('./middleware.js');
//Search //Search
router.get('/api/brew', (req, res, next) => { router.get('/api/brew', (req, res, next) => {
const opts = _.pick(req.query, ['limit', 'sort', 'page']);
//TODO BrewData.termSearch(req.query.terms, opts, req.admin)
.then((result) => {
return res.status(200).json(result);
})
.catch(next);
});
//User
router.get('/api/user/:username', (req, res, next) => {
const fullAccess = req.admin ||
!!(req.account && req.params.username == req.account.username);
BrewData.userSearch(req.params.username, fullAccess)
.then((result) => {
return res.status(200).json(result);
})
.catch(next);
}); });
//Get //Get

View File

@@ -3,22 +3,25 @@ const _ = require('lodash');
module.exports = (Brew) => { module.exports = (Brew) => {
const cmds = { const cmds = {
termSearch : (terms='', opts, fullAccess) => { termSearch : (terms='', opts, fullAccess) => {
const query = {$text: { let query = {};
if(terms){
query = {$text: {
//Wrap terms in quotes to perform an AND operation //Wrap terms in quotes to perform an AND operation
$search: _.map(terms.split(' '), (term)=>{ $search: _.map(terms.split(' '), (term)=>{
return `\"${term}\"`; return `\"${term}\"`;
}).join(' '), }).join(' '),
$caseSensitive : false $caseSensitive : false
}}; }};
}
return cmds.search(query, opts, fullAccess); return cmds.search(query, opts, fullAccess);
}, },
userSearch : (username, opts, fullAccess) => { userSearch : (username, fullAccess) => {
const query = { const query = {
authors : username authors : username
}; };
return cmds.search(query, opts, fullAccess); return cmds.search(query, {}, fullAccess);
}, },
search : (queryObj={}, options={}, fullAccess = true) => { search : (queryObj={}, options={}, fullAccess = true) => {
@@ -27,6 +30,8 @@ module.exports = (Brew) => {
page : 0, page : 0,
sort : {} sort : {}
}, options); }, options);
opts.limit = _.toNumber(opts.limit);
opts.page = _.toNumber(opts.page);
let filter = { let filter = {
text : 0 text : 0

View File

@@ -16,6 +16,7 @@ const Middleware = {
return next(); return next();
}, },
admin : (req, res, next) => { admin : (req, res, next) => {
req.admin = false;
if(req.headers['x-homebrew-admin'] === config.get('admin:key')){ if(req.headers['x-homebrew-admin'] === config.get('admin:key')){
req.admin = true; req.admin = true;
} }
@@ -44,6 +45,7 @@ const Middleware = {
}, },
//TODO: REMOVE
//Loaders //Loaders
loadBrew : (req, res, next) => { loadBrew : (req, res, next) => {
BrewData.getByEdit(req.params.editId) BrewData.getByEdit(req.params.editId)

View File

@@ -1,4 +1,5 @@
const Test = require('./test.init.js'); const Test = require('./test.init.js');
const _ = require('lodash');
const request = require('supertest-as-promised'); const request = require('supertest-as-promised');
const config = require('nconf'); const config = require('nconf');
@@ -6,36 +7,30 @@ const config = require('nconf');
const app = require('app.js'); const app = require('app.js');
const DB = require('db.js'); const DB = require('db.js');
const BrewData = require('brew.data.js'); const BrewData = require('brew.data.js');
const BrewGen = require('./brew.gen.js');
const Error = require('error.js'); const Error = require('error.js');
const apiPath = '/api/brew';
let session_token; const UserX = { username : 'userX' };
const test_user = { const UserA = { username : 'userA' };
username : 'cool guy' let UserXToken, UserAToken;
};
let storedBrew = {
title : 'good title',
text : 'original text',
authors : ['your_dm']
};
describe('Brew API', () => { describe('Brew API', () => {
before('Create session token', () => {
UserXToken = Test.getSessionToken(UserX);
UserAToken = Test.getSessionToken(UserA);
});
describe('CRUD', ()=>{
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('Populate brews', ()=>{
session_token = Test.getSessionToken(test_user); return BrewGen.populateDB(BrewGen.static());
}); });
before('Create brew', ()=>{
return BrewData.create(storedBrew)
.then((brew)=>{ storedBrew = brew; });
});
describe('Create', () => { describe('Create', () => {
it('creates a new brew', () => { it('creates a new brew', () => {
return request(app) return request(app)
.post(apiPath) .post(`/api/brew`)
.send({ text : 'Brew Text' }) .send({ text : 'Brew Text' })
.expect(200) .expect(200)
.then((res) => { .then((res) => {
@@ -49,47 +44,50 @@ describe('Brew API', () => {
it('creates a new brew with a session author', () => { it('creates a new brew with a session author', () => {
return request(app) return request(app)
.post(apiPath) .post(`/api/brew`)
.set('Cookie', `nc_session=${session_token}`) .set('Cookie', `nc_session=${UserXToken}`)
.send({ text : 'Brew Text' }) .send({ text : 'Brew Text' })
.expect(200) .expect(200)
.then((res) => { .then((res) => {
const brew = res.body; const brew = res.body;
brew.should.have.property('authors').include(test_user.username); brew.should.have.property('authors').include(UserX.username);
}); });
}); });
}); });
describe('Update', () => { describe('Update', () => {
it('updates an existing brew', () => { it('updates an existing brew', () => {
const storedBrew = BrewGen.get('BrewA');
return request(app) return request(app)
.put(`${apiPath}/${storedBrew.editId}`) .put(`/api/brew/${storedBrew.editId}`)
.send({ text : 'New Text' }) .send({ text : 'New Text' })
.expect(200) .expect(200)
.then((res) => { .then((res) => {
const brew = res.body; const brew = res.body;
brew.should.have.property('editId').equal(storedBrew.editId); brew.should.have.property('editId').equal(storedBrew.editId);
brew.should.have.property('text').equal('New Text'); brew.should.have.property('text').equal('New Text');
brew.should.have.property('authors').include('your_dm'); brew.should.have.property('authors').include(storedBrew.authors[0]);
brew.should.not.have.property('_id'); brew.should.not.have.property('_id');
}); });
}); });
it('adds the user as author', () => { it('adds the user as author', () => {
const storedBrew = BrewGen.get('BrewA');
return request(app) return request(app)
.put(`${apiPath}/${storedBrew.editId}`) .put(`/api/brew/${storedBrew.editId}`)
.set('Cookie', `nc_session=${session_token}`) .set('Cookie', `nc_session=${UserXToken}`)
.send({ text : 'New Text' }) .send({ text : 'New Text' })
.expect(200) .expect(200)
.then((res) => { .then((res) => {
const brew = res.body; const brew = res.body;
brew.should.have.property('authors').include(test_user.username); brew.should.have.property('authors').include(UserX.username);
brew.should.have.property('authors').include('your_dm'); brew.should.have.property('authors').include(storedBrew.authors[0]);
}); });
}); });
it('should throw error on bad edit id', ()=>{ it('should throw error on bad edit id', ()=>{
const storedBrew = BrewGen.get('BrewA');
return request(app) return request(app)
.put(`${apiPath}/BADEDITID`) .put(`/api/brew/BADEDITID`)
.send({ text : 'New Text' }) .send({ text : 'New Text' })
.expect(404) .expect(404)
}); });
@@ -97,8 +95,9 @@ describe('Brew API', () => {
describe('Remove', () => { describe('Remove', () => {
it('should removes a brew', ()=>{ it('should removes a brew', ()=>{
const storedBrew = BrewGen.get('BrewA');
return request(app) return request(app)
.del(`${apiPath}/${storedBrew.editId}`) .del(`/api/brew/${storedBrew.editId}`)
.send() .send()
.expect(200) .expect(200)
.then(() => { .then(() => {
@@ -110,28 +109,139 @@ describe('Brew API', () => {
}); });
}); });
}); });
})
describe('Search', () => { describe('Search', () => {
it.skip('should be able to search for brews with given terms', ()=>{ before('Connect DB', DB.connect);
before('Clear DB', BrewData.removeAll);
before('Populate brews', ()=>{
return BrewGen.populateDB(BrewGen.static());
}); });
it.skip('should exclude unpublished brews and have no editIdsh', ()=>{
it('should be able to search for all published brews', ()=>{
return request(app)
.get(`/api/brew`)
.query({})
.send()
.expect(200)
.then((res) => {
const result = res.body;
result.total.should.be.equal(2);
result.brews.should.have.brews('BrewB','BrewD');
result.brews[0].should.not.have.property('editId');
}); });
it.skip('should sort the search', ()=>{
}); });
it.skip('should use pagniation on the search', ()=>{
it('should be able to search for brews with given terms', ()=>{
return request(app)
.get(`/api/brew`)
.query({
terms : '5e ranger'
})
.send()
.expect(200)
.then((res) => {
const result = res.body;
result.total.should.be.equal(1);
result.brews.should.have.brews('BrewD');
});
});
it('should be able to sort the search', ()=>{
return request(app)
.get(`/api/brew`)
.query({
sort : { views : 1}
})
.send()
.expect(200)
.then((res) => {
const result = res.body;
result.total.should.be.equal(2);
result.brews[0].should.be.brew('BrewD');
result.brews[1].should.be.brew('BrewB');
});
});
it('should use pagniation on the search', ()=>{
return request(app)
.get(`/api/brew`)
.query({
limit : 1,
page : 1,
sort : { views : -1}
})
.send()
.expect(200)
.then((res) => {
const result = res.body;
result.total.should.be.equal(2);
result.brews[0].should.be.brew('BrewD');
})
});
it('should return all brews and editIds if admin', ()=>{
return request(app)
.get(`/api/brew`)
.query({})
.set('x-homebrew-admin', config.get('admin:key'))
.send()
.expect(200)
.then((res) => {
const result = res.body;
const brewCount = _.size(BrewGen.static());
result.total.should.be.equal(brewCount);
result.brews.length.should.be.equal(brewCount);
result.brews[0].should.have.property('editId');
});
}); });
}); });
describe('User', () => { describe('User', () => {
it.skip('should be able to query brews for a specific user', ()=>{ before('Connect DB', DB.connect);
before('Clear DB', BrewData.removeAll);
before('Populate brews', ()=>{
return BrewGen.populateDB(BrewGen.static());
}); });
it.skip('should return full access to brews if loggedin user is queried user', ()=>{
it('should be able to query brews for a specific user', ()=>{
return request(app)
.get(`/api/user/userA`)
.send()
.expect(200)
.then((res) => {
const result = res.body;
result.total.should.be.equal(1);
result.brews.length.should.be.equal(1);
result.brews.should.have.brews('BrewB');
result.brews[0].should.not.have.property('editId');
});
});
it('should have full access if loggedin user is queried user', ()=>{
return request(app)
.get(`/api/user/userA`)
.set('Cookie', `nc_session=${UserAToken}`)
.send()
.expect(200)
.then((res) => {
const result = res.body;
result.total.should.be.equal(3);
result.brews.length.should.be.equal(3);
result.brews.should.have.brews('BrewA', 'BrewB', 'BrewC');
result.brews[0].should.have.property('editId');
});
});
it('should have full access if admin', ()=>{
return request(app)
.get(`/api/user/userA`)
.set('x-homebrew-admin', config.get('admin:key'))
.send()
.expect(200)
.then((res) => {
const result = res.body;
result.total.should.be.equal(3);
result.brews.length.should.be.equal(3);
result.brews.should.have.brews('BrewA', 'BrewB', 'BrewC');
result.brews[0].should.have.property('editId');
});
}); });
}); });

View File

@@ -4,6 +4,8 @@ const BrewData = require('../server/brew.data.js');
let PopulatedBrews = {}; let PopulatedBrews = {};
module.exports = { module.exports = {
//TODO: Add in a generator for old brews to test the old rendering code
random : (num = 20)=>{ random : (num = 20)=>{
return _.times(num, ()=>{ return _.times(num, ()=>{
//TODO: Build better generator //TODO: Build better generator
@@ -66,6 +68,10 @@ module.exports = {
); );
}, },
get : (brewId) => {
return PopulatedBrews[brewId]
},
chaiPlugin : (chai, utils) => { chaiPlugin : (chai, utils) => {
chai.Assertion.addMethod('brews', function(...brewIds){ chai.Assertion.addMethod('brews', function(...brewIds){
new chai.Assertion(this._obj).to.be.instanceof(Array); new chai.Assertion(this._obj).to.be.instanceof(Array);
@@ -84,5 +90,22 @@ module.exports = {
`expect #{this} to not have brews ${brewIds.join(', ')}` `expect #{this} to not have brews ${brewIds.join(', ')}`
) )
}); });
chai.Assertion.addMethod('brew', function(brewId){
new chai.Assertion(this._obj).to.be.instanceof(Object);
const brew = this._obj;
const storedBrew = PopulatedBrews[brewId];
const valid = storedBrew &&
brew.shareId == storedBrew.shareId &&
brew.title == storedBrew.title &&
brew.views == storedBrew.views;
this.assert(
valid,
`expect #{this} to be brew ${brewId}`,
`expect #{this} to not be brew ${brewId}`
)
});
} }
}; };

View File

@@ -70,14 +70,27 @@ describe('Brew Search', () => {
}); });
describe('Sorting', ()=>{ describe('Sorting', ()=>{
it.skip('should sort ASC', () => { it('should sort ASC', () => {
return BrewData.search({}, {
sort : { views : 1 }
})
.then((result) => {
result.brews[0].should.be.brew('BrewC');
result.brews[1].should.be.brew('BrewD');
result.brews[2].should.be.brew('BrewB');
result.brews[3].should.be.brew('BrewA');
})
}); });
it.skip('should sort DESC', () => { it('should sort DESC', () => {
return BrewData.search({}, {
}); sort : { views : -1 }
it.skip('should sort based on multiple fields', () => { })
.then((result) => {
result.brews[0].should.be.brew('BrewA');
result.brews[1].should.be.brew('BrewB');
result.brews[2].should.be.brew('BrewD');
result.brews[3].should.be.brew('BrewC');
})
}); });
}); });
@@ -146,8 +159,12 @@ describe('Brew Search', () => {
result.brews.should.have.brews('BrewB'); result.brews.should.have.brews('BrewB');
}); });
}); });
it.skip('should not worry about the case of the terms', () => { it('should not worry about the case of the terms', () => {
return BrewData.termSearch('FANCY')
.then((result) => {
result.total.should.be.equal(2);
result.brews.should.have.brews('BrewA', 'BrewB');
});
}); });
}); });