mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-13 04:22:40 +00:00
All tests should be done, phew
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -3,30 +3,35 @@ 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 = {};
|
||||||
//Wrap terms in quotes to perform an AND operation
|
if(terms){
|
||||||
$search: _.map(terms.split(' '), (term)=>{
|
query = {$text: {
|
||||||
return `\"${term}\"`;
|
//Wrap terms in quotes to perform an AND operation
|
||||||
}).join(' '),
|
$search: _.map(terms.split(' '), (term)=>{
|
||||||
$caseSensitive : false
|
return `\"${term}\"`;
|
||||||
}};
|
}).join(' '),
|
||||||
|
$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) => {
|
||||||
const opts = _.merge({
|
const opts = _.merge({
|
||||||
limit : 25,
|
limit : 25,
|
||||||
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
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
304
test/api.test.js
304
test/api.test.js
@@ -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,132 +7,241 @@ 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('Connect DB', DB.connect);
|
|
||||||
before('Clear DB', BrewData.removeAll);
|
|
||||||
before('Create session token', () => {
|
before('Create session token', () => {
|
||||||
session_token = Test.getSessionToken(test_user);
|
UserXToken = Test.getSessionToken(UserX);
|
||||||
});
|
UserAToken = Test.getSessionToken(UserA);
|
||||||
before('Create brew', ()=>{
|
|
||||||
return BrewData.create(storedBrew)
|
|
||||||
.then((brew)=>{ storedBrew = brew; });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('CRUD', ()=>{
|
||||||
|
before('Connect DB', DB.connect);
|
||||||
|
before('Clear DB', BrewData.removeAll);
|
||||||
|
before('Populate brews', ()=>{
|
||||||
|
return BrewGen.populateDB(BrewGen.static());
|
||||||
|
});
|
||||||
|
describe('Create', () => {
|
||||||
|
it('creates a new brew', () => {
|
||||||
|
return request(app)
|
||||||
|
.post(`/api/brew`)
|
||||||
|
.send({ text : 'Brew Text' })
|
||||||
|
.expect(200)
|
||||||
|
.then((res) => {
|
||||||
|
const brew = res.body;
|
||||||
|
brew.should.have.property('editId').that.is.a('string');
|
||||||
|
brew.should.have.property('shareId').that.is.a('string');
|
||||||
|
brew.should.have.property('text').equal('Brew Text');
|
||||||
|
brew.should.not.have.property('_id');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Create', () => {
|
it('creates a new brew with a session author', () => {
|
||||||
it('creates a new brew', () => {
|
return request(app)
|
||||||
return request(app)
|
.post(`/api/brew`)
|
||||||
.post(apiPath)
|
.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('editId').that.is.a('string');
|
brew.should.have.property('authors').include(UserX.username);
|
||||||
brew.should.have.property('shareId').that.is.a('string');
|
});
|
||||||
brew.should.have.property('text').equal('Brew Text');
|
});
|
||||||
brew.should.not.have.property('_id');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates a new brew with a session author', () => {
|
describe('Update', () => {
|
||||||
return request(app)
|
it('updates an existing brew', () => {
|
||||||
.post(apiPath)
|
const storedBrew = BrewGen.get('BrewA');
|
||||||
.set('Cookie', `nc_session=${session_token}`)
|
return request(app)
|
||||||
.send({ text : 'Brew Text' })
|
.put(`/api/brew/${storedBrew.editId}`)
|
||||||
.expect(200)
|
.send({ text : 'New Text' })
|
||||||
.then((res) => {
|
.expect(200)
|
||||||
const brew = res.body;
|
.then((res) => {
|
||||||
brew.should.have.property('authors').include(test_user.username);
|
const brew = res.body;
|
||||||
});
|
brew.should.have.property('editId').equal(storedBrew.editId);
|
||||||
});
|
brew.should.have.property('text').equal('New Text');
|
||||||
});
|
brew.should.have.property('authors').include(storedBrew.authors[0]);
|
||||||
|
brew.should.not.have.property('_id');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Update', () => {
|
it('adds the user as author', () => {
|
||||||
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' })
|
.set('Cookie', `nc_session=${UserXToken}`)
|
||||||
.expect(200)
|
.send({ text : 'New Text' })
|
||||||
.then((res) => {
|
.expect(200)
|
||||||
const brew = res.body;
|
.then((res) => {
|
||||||
brew.should.have.property('editId').equal(storedBrew.editId);
|
const brew = res.body;
|
||||||
brew.should.have.property('text').equal('New Text');
|
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]);
|
||||||
brew.should.not.have.property('_id');
|
});
|
||||||
});
|
});
|
||||||
|
it('should throw error on bad edit id', ()=>{
|
||||||
|
const storedBrew = BrewGen.get('BrewA');
|
||||||
|
return request(app)
|
||||||
|
.put(`/api/brew/BADEDITID`)
|
||||||
|
.send({ text : 'New Text' })
|
||||||
|
.expect(404)
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('adds the user as author', () => {
|
describe('Remove', () => {
|
||||||
return request(app)
|
it('should removes a brew', ()=>{
|
||||||
.put(`${apiPath}/${storedBrew.editId}`)
|
const storedBrew = BrewGen.get('BrewA');
|
||||||
.set('Cookie', `nc_session=${session_token}`)
|
return request(app)
|
||||||
.send({ text : 'New Text' })
|
.del(`/api/brew/${storedBrew.editId}`)
|
||||||
.expect(200)
|
.send()
|
||||||
.then((res) => {
|
.expect(200)
|
||||||
const brew = res.body;
|
.then(() => {
|
||||||
brew.should.have.property('authors').include(test_user.username);
|
BrewData.getByEdit(storedBrew.editId)
|
||||||
brew.should.have.property('authors').include('your_dm');
|
.then(() => { throw 'Brew found when one should not have been'; })
|
||||||
});
|
.catch((err) => {
|
||||||
|
err.should.be.instanceof(Error.noBrew);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
it('should throw error on bad edit id', ()=>{
|
})
|
||||||
return request(app)
|
|
||||||
.put(`${apiPath}/BADEDITID`)
|
|
||||||
.send({ text : 'New Text' })
|
|
||||||
.expect(404)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Remove', () => {
|
|
||||||
it('should removes a brew', ()=>{
|
|
||||||
return request(app)
|
|
||||||
.del(`${apiPath}/${storedBrew.editId}`)
|
|
||||||
.send()
|
|
||||||
.expect(200)
|
|
||||||
.then(() => {
|
|
||||||
BrewData.getByEdit(storedBrew.editId)
|
|
||||||
.then(() => { throw 'Brew found when one should not have been'; })
|
|
||||||
.catch((err) => {
|
|
||||||
err.should.be.instanceof(Error.noBrew);
|
|
||||||
})
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
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('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.skip('should use pagniation on the search', ()=>{
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -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}`
|
||||||
|
)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user