mirror of
https://github.com/wassname/talk.git
synced 2026-09-12 13:01:11 +08:00
Added asset settings + improved tests for routes
This commit is contained in:
+18
-1
@@ -1,7 +1,8 @@
|
|||||||
const mongoose = require('../mongoose');
|
const mongoose = require('../mongoose');
|
||||||
const uuid = require('uuid');
|
|
||||||
const Schema = mongoose.Schema;
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
|
const uuid = require('uuid');
|
||||||
|
|
||||||
const AssetSchema = new Schema({
|
const AssetSchema = new Schema({
|
||||||
id: {
|
id: {
|
||||||
type: String,
|
type: String,
|
||||||
@@ -22,6 +23,10 @@ const AssetSchema = new Schema({
|
|||||||
type: Date,
|
type: Date,
|
||||||
default: null
|
default: null
|
||||||
},
|
},
|
||||||
|
settings: {
|
||||||
|
type: Schema.Types.Mixed,
|
||||||
|
default: null
|
||||||
|
},
|
||||||
title: String,
|
title: String,
|
||||||
description: String,
|
description: String,
|
||||||
image: String,
|
image: String,
|
||||||
@@ -90,6 +95,18 @@ AssetSchema.statics.findOrCreateByUrl = (url) => Asset.findOneAndUpdate({url}, {
|
|||||||
setDefaultsOnInsert: true
|
setDefaultsOnInsert: true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the settings for the asset.
|
||||||
|
* @param {[type]} id [description]
|
||||||
|
* @param {[type]} settings [description]
|
||||||
|
* @return {[type]} [description]
|
||||||
|
*/
|
||||||
|
AssetSchema.statics.overrideSettings = (id, settings) => Asset.update({id}, {
|
||||||
|
$set: {
|
||||||
|
settings
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds assets matching keywords on the model. If `value` is an empty string,
|
* Finds assets matching keywords on the model. If `value` is an empty string,
|
||||||
* then it will not even perform a text search query.
|
* then it will not even perform a text search query.
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ const SALT_ROUNDS = 10;
|
|||||||
|
|
||||||
// USER_ROLES is the array of roles that is permissible as a user role.
|
// USER_ROLES is the array of roles that is permissible as a user role.
|
||||||
const USER_ROLES = [
|
const USER_ROLES = [
|
||||||
|
'',
|
||||||
'admin',
|
'admin',
|
||||||
'moderator'
|
'moderator'
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -81,4 +81,19 @@ router.post('/:asset_id/scrape', (req, res, next) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.put('/:asset_id/settings', (req, res, next) => {
|
||||||
|
|
||||||
|
// Override the settings for the asset.
|
||||||
|
Asset
|
||||||
|
.overrideSettings(req.params.asset_id, req.body)
|
||||||
|
.then(() => {
|
||||||
|
|
||||||
|
res.status(204).end();
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
next(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -30,16 +30,36 @@ router.get('/', (req, res, next) => {
|
|||||||
Setting.getModerationSetting()
|
Setting.getModerationSetting()
|
||||||
])
|
])
|
||||||
.then(([asset, settings]) => {
|
.then(([asset, settings]) => {
|
||||||
// Get the sitewide moderation setting and return the appropriate comments
|
|
||||||
|
// Merge the asset specific settings with the returned settings object in
|
||||||
|
// the event that the asset that was returned also had settings.
|
||||||
|
if (asset.settings) {
|
||||||
|
settings = Object.assign(settings, asset.settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch the appropriate comments stream.
|
||||||
let comments;
|
let comments;
|
||||||
if (settings.moderation === 'pre') {
|
|
||||||
|
if (settings.moderation === 'post') {
|
||||||
comments = Comment.findAcceptedByAssetId(asset.id);
|
comments = Comment.findAcceptedByAssetId(asset.id);
|
||||||
} else {
|
} else {
|
||||||
comments = Comment.findAcceptedAndNewByAssetId(asset.id);
|
comments = Comment.findAcceptedAndNewByAssetId(asset.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all([comments, asset, settings]);
|
return Promise.all([
|
||||||
|
|
||||||
|
// This is the promised component... Fetch the comments based on the
|
||||||
|
// moderation settings.
|
||||||
|
comments,
|
||||||
|
|
||||||
|
// Send back the reference to the asset.
|
||||||
|
asset,
|
||||||
|
|
||||||
|
// Send back the settings to the stream.
|
||||||
|
settings
|
||||||
|
]);
|
||||||
})
|
})
|
||||||
|
// Get all the users and actions for those comments.
|
||||||
.then(([comments, asset, settings]) => {
|
.then(([comments, asset, settings]) => {
|
||||||
|
|
||||||
// Get the user id's from the author id's as a unique array that gets
|
// Get the user id's from the author id's as a unique array that gets
|
||||||
@@ -73,14 +93,16 @@ router.get('/', (req, res, next) => {
|
|||||||
// The users who wrote those comments
|
// The users who wrote those comments
|
||||||
users,
|
users,
|
||||||
|
|
||||||
// The actions on the above items
|
// And all actions about the asset, comments, and users.
|
||||||
actions,
|
actions,
|
||||||
|
|
||||||
// And the relevant settings
|
// Pass back the settings that we loaded.
|
||||||
settings
|
settings
|
||||||
]);
|
]);
|
||||||
})
|
})
|
||||||
.then(([asset, comments, users, actions, settings]) => {
|
.then(([asset, comments, users, actions, settings]) => {
|
||||||
|
|
||||||
|
// Send back the payload containing all this data.
|
||||||
res.json({
|
res.json({
|
||||||
assets: [asset],
|
assets: [asset],
|
||||||
comments,
|
comments,
|
||||||
|
|||||||
@@ -335,6 +335,28 @@ paths:
|
|||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/Error'
|
$ref: '#/definitions/Error'
|
||||||
|
|
||||||
|
/asset/{asset_id}/settings:
|
||||||
|
put:
|
||||||
|
parameters:
|
||||||
|
- name: asset_id
|
||||||
|
in: path
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
format: uuid
|
||||||
|
- name: body
|
||||||
|
in: body
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/Settings'
|
||||||
|
responses:
|
||||||
|
204:
|
||||||
|
description: The asset settings were updated.
|
||||||
|
404:
|
||||||
|
description: The asset was not found.
|
||||||
|
500:
|
||||||
|
description: An error occured.
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/Error'
|
||||||
|
|
||||||
/stream:
|
/stream:
|
||||||
get:
|
get:
|
||||||
|
|||||||
+27
-1
@@ -1,5 +1,10 @@
|
|||||||
const Asset = require('../../models/asset');
|
const Asset = require('../../models/asset');
|
||||||
const expect = require('chai').expect;
|
|
||||||
|
const chai = require('chai');
|
||||||
|
const expect = chai.expect;
|
||||||
|
|
||||||
|
// Use the chai should.
|
||||||
|
chai.should();
|
||||||
|
|
||||||
describe('Asset: model', () => {
|
describe('Asset: model', () => {
|
||||||
|
|
||||||
@@ -53,6 +58,27 @@ describe('Asset: model', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#overrideSettings', () => {
|
||||||
|
it('should update the settings', () => {
|
||||||
|
return Asset
|
||||||
|
.findOrCreateByUrl('https://override.test.com/asset')
|
||||||
|
.then((asset) => {
|
||||||
|
expect(asset).to.have.property('settings');
|
||||||
|
expect(asset.settings).to.be.null;
|
||||||
|
|
||||||
|
return Asset.overrideSettings(asset.id, {moderation: 'pre'});
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
return Asset.findOrCreateByUrl('https://override.test.com/asset');
|
||||||
|
})
|
||||||
|
.then((asset) => {
|
||||||
|
expect(asset).to.have.property('settings');
|
||||||
|
expect(asset.settings).is.an('object');
|
||||||
|
expect(asset.settings).to.have.property('moderation', 'pre');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#findOrCreateByUrl', ()=> {
|
describe('#findOrCreateByUrl', ()=> {
|
||||||
it('should find an asset by a url', () => {
|
it('should find an asset by a url', () => {
|
||||||
return Asset.findOrCreateByUrl('http://test.com')
|
return Asset.findOrCreateByUrl('http://test.com')
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ describe('Setting: model', () => {
|
|||||||
expect(settings).to.have.property('moderation').and.to.equal('pre');
|
expect(settings).to.have.property('moderation').and.to.equal('pre');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have two infoBox fields defined', () => {
|
it('should have two infoBox fields defined', () => {
|
||||||
return Setting.getSettings().then(settings => {
|
return Setting.getSettings().then(settings => {
|
||||||
expect(settings).to.have.property('infoBoxEnable').and.to.equal(false);
|
expect(settings).to.have.property('infoBoxEnable').and.to.equal(false);
|
||||||
@@ -26,6 +27,7 @@ describe('Setting: model', () => {
|
|||||||
it('should update the settings with a passed object', () => {
|
it('should update the settings with a passed object', () => {
|
||||||
const mockSettings = {moderation: 'post', infoBoxEnable: true, infoBoxContent: 'yeah'};
|
const mockSettings = {moderation: 'post', infoBoxEnable: true, infoBoxContent: 'yeah'};
|
||||||
return Setting.updateSettings(mockSettings).then(updatedSettings => {
|
return Setting.updateSettings(mockSettings).then(updatedSettings => {
|
||||||
|
expect(updatedSettings).to.be.an('object');
|
||||||
expect(updatedSettings).to.have.property('moderation').and.to.equal('post');
|
expect(updatedSettings).to.have.property('moderation').and.to.equal('post');
|
||||||
expect(updatedSettings).to.have.property('infoBoxEnable', true);
|
expect(updatedSettings).to.have.property('infoBoxEnable', true);
|
||||||
expect(updatedSettings).to.have.property('infoBoxContent', 'yeah');
|
expect(updatedSettings).to.have.property('infoBoxContent', 'yeah');
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ chai.use(require('chai-http'));
|
|||||||
|
|
||||||
const Asset = require('../../../../models/asset');
|
const Asset = require('../../../../models/asset');
|
||||||
|
|
||||||
describe('/assets', () => {
|
describe('/api/v1/assets', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
return Asset.create([
|
return Asset.create([
|
||||||
@@ -27,7 +27,7 @@ describe('/assets', () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('GET', () => {
|
describe('#get', () => {
|
||||||
|
|
||||||
it('should return all assets without a search query', () => {
|
it('should return all assets without a search query', () => {
|
||||||
return chai.request(app)
|
return chai.request(app)
|
||||||
|
|||||||
@@ -6,32 +6,47 @@ chai.use(require('chai-http'));
|
|||||||
|
|
||||||
const User = require('../../../../models/user');
|
const User = require('../../../../models/user');
|
||||||
|
|
||||||
describe('POST /auth/local', () => {
|
describe('/api/v1/auth', () => {
|
||||||
|
describe('#get', () => {
|
||||||
|
it('should return nothing when no user is logged in', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.get('/api/v1/auth')
|
||||||
|
.then((res) => {
|
||||||
|
expect(res.status).to.be.equal(204);
|
||||||
|
expect(res.body).to.be.empty;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('/api/v1/auth/local', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
return User.createLocalUser('maria@gmail.com', 'password!', 'Maria');
|
return User.createLocalUser('maria@gmail.com', 'password!', 'Maria');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should send back the user on a successful login', () => {
|
describe('#post', () => {
|
||||||
return chai.request(app)
|
it('should send back the user on a successful login', () => {
|
||||||
.post('/api/v1/auth/local')
|
return chai.request(app)
|
||||||
.send({email: 'maria@gmail.com', password: 'password!'})
|
.post('/api/v1/auth/local')
|
||||||
.catch((res) => {
|
.send({email: 'maria@gmail.com', password: 'password!'})
|
||||||
expect(res).to.have.status(200);
|
.catch((res) => {
|
||||||
expect(res).to.be.json;
|
expect(res).to.have.status(200);
|
||||||
expect(res.body).to.have.property('user');
|
expect(res).to.be.json;
|
||||||
expect(res.body.user).to.have.property('displayName', 'Maria');
|
expect(res.body).to.have.property('user');
|
||||||
});
|
expect(res.body.user).to.have.property('displayName', 'Maria');
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should not send back the user on a unsuccessful login', () => {
|
it('should not send back the user on a unsuccessful login', () => {
|
||||||
return chai.request(app)
|
return chai.request(app)
|
||||||
.post('/api/v1/auth/local')
|
.post('/api/v1/auth/local')
|
||||||
.send({email: 'maria@gmail.com', password: 'password!3'})
|
.send({email: 'maria@gmail.com', password: 'password!3'})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
expect(err).to.not.be.null;
|
expect(err).to.not.be.null;
|
||||||
expect(err.response).to.have.status(401);
|
expect(err.response).to.have.status(401);
|
||||||
expect(err.response.body).to.have.property('message', 'not authorized');
|
expect(err.response.body).to.have.property('message', 'not authorized');
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+156
-303
@@ -16,11 +16,7 @@ const User = require('../../../../models/user');
|
|||||||
const Setting = require('../../../../models/setting');
|
const Setting = require('../../../../models/setting');
|
||||||
const settings = {id: '1', moderation: 'pre'};
|
const settings = {id: '1', moderation: 'pre'};
|
||||||
|
|
||||||
beforeEach(() => {
|
describe('/api/v1/comments', () => {
|
||||||
return Setting.create(settings);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Get /comments', () => {
|
|
||||||
const comments = [{
|
const comments = [{
|
||||||
id: 'abc',
|
id: 'abc',
|
||||||
body: 'comment 10',
|
body: 'comment 10',
|
||||||
@@ -32,61 +28,11 @@ describe('Get /comments', () => {
|
|||||||
asset_id: 'asset',
|
asset_id: 'asset',
|
||||||
author_id: '456'
|
author_id: '456'
|
||||||
}, {
|
}, {
|
||||||
id: 'hij',
|
id: 'def-rejected',
|
||||||
body: 'comment 30',
|
|
||||||
asset_id: '456'
|
|
||||||
}];
|
|
||||||
|
|
||||||
const users = [{
|
|
||||||
displayName: 'Ana',
|
|
||||||
email: 'ana@gmail.com',
|
|
||||||
password: '123'
|
|
||||||
}, {
|
|
||||||
displayName: 'Maria',
|
|
||||||
email: 'maria@gmail.com',
|
|
||||||
password: '123'
|
|
||||||
}];
|
|
||||||
|
|
||||||
const actions = [{
|
|
||||||
action_type: 'flag',
|
|
||||||
item_id: 'abc'
|
|
||||||
}, {
|
|
||||||
action_type: 'like',
|
|
||||||
item_id: 'hij'
|
|
||||||
}];
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
return Promise.all([
|
|
||||||
Comment.create(comments),
|
|
||||||
User.createLocalUsers(users),
|
|
||||||
Action.create(actions)
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return all the comments', () => {
|
|
||||||
return chai.request(app)
|
|
||||||
.get('/api/v1/comments')
|
|
||||||
.set(passport.inject({roles: ['admin']}))
|
|
||||||
.then((res) => {
|
|
||||||
|
|
||||||
expect(res).to.have.status(200);
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Get comments by status and action', () => {
|
|
||||||
const comments = [{
|
|
||||||
id: 'abc',
|
|
||||||
body: 'comment 10',
|
|
||||||
asset_id: 'asset',
|
|
||||||
author_id: '123',
|
|
||||||
status: 'rejected'
|
|
||||||
}, {
|
|
||||||
id: 'def',
|
|
||||||
body: 'comment 20',
|
body: 'comment 20',
|
||||||
asset_id: 'asset',
|
asset_id: 'asset',
|
||||||
author_id: '456'
|
author_id: '456',
|
||||||
|
status: 'rejected'
|
||||||
}, {
|
}, {
|
||||||
id: 'hij',
|
id: 'hij',
|
||||||
body: 'comment 30',
|
body: 'comment 30',
|
||||||
@@ -117,109 +63,100 @@ describe('Get comments by status and action', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
Comment.create(comments),
|
Comment.create(comments),
|
||||||
User.createLocalUsers(users),
|
|
||||||
Action.create(actions)
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return all the rejected comments', () => {
|
|
||||||
return chai.request(app)
|
|
||||||
.get('/api/v1/comments?status=rejected')
|
|
||||||
.set(passport.inject({roles: ['admin']}))
|
|
||||||
.then((res) => {
|
|
||||||
expect(res).to.have.status(200);
|
|
||||||
expect(res.body[0]).to.have.property('id', 'abc');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return all the approved comments', () => {
|
|
||||||
return chai.request(app)
|
|
||||||
.get('/api/v1/comments?status=accepted')
|
|
||||||
.set(passport.inject({roles: ['admin']}))
|
|
||||||
.then((res) => {
|
|
||||||
expect(res).to.have.status(200);
|
|
||||||
expect(res.body[0]).to.have.property('id', 'hij');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return all the new comments', () => {
|
|
||||||
return chai.request(app)
|
|
||||||
.get('/api/v1/comments?status=new')
|
|
||||||
.set(passport.inject({roles: ['admin']}))
|
|
||||||
.then((res) => {
|
|
||||||
expect(res).to.have.status(200);
|
|
||||||
expect(res.body[0]).to.have.property('id', 'def');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return all the flagged comments', () => {
|
|
||||||
return chai.request(app)
|
|
||||||
.get('/api/v1/comments?action_type=flag')
|
|
||||||
.set(passport.inject({roles: ['admin']}))
|
|
||||||
.then((res) => {
|
|
||||||
expect(res).to.have.status(200);
|
|
||||||
|
|
||||||
expect(res.body.length).to.equal(1);
|
|
||||||
expect(res.body[0]).to.have.property('id', 'abc');
|
|
||||||
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Post /comments', () => {
|
|
||||||
const users = [{
|
|
||||||
displayName: 'Ana',
|
|
||||||
email: 'ana@gmail.com',
|
|
||||||
password: '123'
|
|
||||||
}, {
|
|
||||||
displayName: 'Maria',
|
|
||||||
email: 'maria@gmail.com',
|
|
||||||
password: '123'
|
|
||||||
}];
|
|
||||||
|
|
||||||
const actions = [{
|
|
||||||
action_type: 'flag',
|
|
||||||
item_id: 'abc'
|
|
||||||
}, {
|
|
||||||
action_type: 'like',
|
|
||||||
item_id: 'hij'
|
|
||||||
}];
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
return Promise.all([
|
|
||||||
User.createLocalUsers(users),
|
User.createLocalUsers(users),
|
||||||
Action.create(actions),
|
Action.create(actions),
|
||||||
wordlist.insert([
|
wordlist.insert([
|
||||||
'bad words'
|
'bad words'
|
||||||
])
|
]),
|
||||||
|
Setting.create(settings)
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a comment', () => {
|
describe('#get', () => {
|
||||||
return chai.request(app)
|
it('should return all the comments', () => {
|
||||||
.post('/api/v1/comments')
|
return chai.request(app)
|
||||||
.set(passport.inject({roles: []}))
|
.get('/api/v1/comments')
|
||||||
.send({'body': 'Something body.', 'author_id': '123', 'asset_id': '1', 'parent_id': ''})
|
.set(passport.inject({roles: ['admin']}))
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
expect(res).to.have.status(201);
|
|
||||||
expect(res.body).to.have.property('id');
|
expect(res).to.have.status(200);
|
||||||
});
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return all the rejected comments', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.get('/api/v1/comments?status=rejected')
|
||||||
|
.set(passport.inject({roles: ['admin']}))
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(200);
|
||||||
|
expect(res.body[0]).to.have.property('id', 'def-rejected');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return all the approved comments', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.get('/api/v1/comments?status=accepted')
|
||||||
|
.set(passport.inject({roles: ['admin']}))
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(200);
|
||||||
|
expect(res.body).to.have.length(1);
|
||||||
|
expect(res.body[0]).to.have.property('id', 'hij');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return all the new comments', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.get('/api/v1/comments?status=new')
|
||||||
|
.set(passport.inject({roles: ['admin']}))
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(200);
|
||||||
|
expect(res.body).to.have.length(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return all the flagged comments', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.get('/api/v1/comments?action_type=flag')
|
||||||
|
.set(passport.inject({roles: ['admin']}))
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(200);
|
||||||
|
|
||||||
|
expect(res.body).to.have.length(1);
|
||||||
|
expect(res.body[0]).to.have.property('id', 'abc');
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a comment with a rejected status if it contains a bad word', () => {
|
describe('#post', () => {
|
||||||
return chai.request(app)
|
|
||||||
.post('/api/v1/comments')
|
it('should create a comment', () => {
|
||||||
.set(passport.inject({roles: []}))
|
return chai.request(app)
|
||||||
.send({'body': 'bad words are the baddest', 'author_id': '123', 'asset_id': '1', 'parent_id': ''})
|
.post('/api/v1/comments')
|
||||||
.then((res) => {
|
.set(passport.inject({roles: []}))
|
||||||
expect(res).to.have.status(201);
|
.send({'body': 'Something body.', 'author_id': '123', 'asset_id': '1', 'parent_id': ''})
|
||||||
expect(res.body).to.have.property('id');
|
.then((res) => {
|
||||||
expect(res.body).to.have.property('status', 'rejected');
|
expect(res).to.have.status(201);
|
||||||
});
|
expect(res.body).to.have.property('id');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create a comment with a rejected status if it contains a bad word', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.post('/api/v1/comments')
|
||||||
|
.set(passport.inject({roles: []}))
|
||||||
|
.send({'body': 'bad words are the baddest', 'author_id': '123', 'asset_id': '1', 'parent_id': ''})
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(201);
|
||||||
|
expect(res.body).to.have.property('id');
|
||||||
|
expect(res.body).to.have.property('status', 'rejected');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Get /:comment_id', () => {
|
describe('/api/v1/comments/:comment_id', () => {
|
||||||
const comments = [{
|
const comments = [{
|
||||||
id: 'abc',
|
id: 'abc',
|
||||||
body: 'comment 10',
|
body: 'comment 10',
|
||||||
@@ -264,79 +201,65 @@ describe('Get /:comment_id', () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the right comment for the comment_id', () => {
|
describe('#get', () => {
|
||||||
return chai.request(app)
|
|
||||||
.get('/api/v1/comments/abc')
|
|
||||||
.set(passport.inject({roles: ['admin']}))
|
|
||||||
.then((res) => {
|
|
||||||
expect(res).to.have.status(200);
|
|
||||||
expect(res).to.have.property('body');
|
|
||||||
expect(res.body).to.have.property('body', 'comment 10');
|
|
||||||
|
|
||||||
});
|
it('should return the right comment for the comment_id', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.get('/api/v1/comments/abc')
|
||||||
|
.set(passport.inject({roles: ['admin']}))
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(200);
|
||||||
|
expect(res).to.have.property('body');
|
||||||
|
expect(res.body).to.have.property('body', 'comment 10');
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#delete', () => {
|
||||||
|
it('it should remove comment', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.delete('/api/v1/comments/abc')
|
||||||
|
.set(passport.inject({roles: ['admin']}))
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(204);
|
||||||
|
|
||||||
|
return Comment.findById('abc');
|
||||||
|
})
|
||||||
|
.then((comment) => {
|
||||||
|
expect(comment).to.be.null;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#put', () => {
|
||||||
|
it('it should update status', function() {
|
||||||
|
return chai.request(app)
|
||||||
|
.put('/api/v1/comments/abc/status')
|
||||||
|
.set(passport.inject({roles: ['admin']}))
|
||||||
|
.send({status: 'accepted'})
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(204);
|
||||||
|
expect(res.body).to.be.empty;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it should not allow a non-admin to update status', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.put('/api/v1/comments/abc/status')
|
||||||
|
.set(passport.inject({roles: []}))
|
||||||
|
.send({status: 'accepted'})
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.be.empty;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
expect(err).to.have.property('status', 401);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Remove /:comment_id', () => {
|
describe('/api/v1/comments/:comment_id/actions', () => {
|
||||||
|
|
||||||
const comments = [{
|
|
||||||
id: 'abc',
|
|
||||||
body: 'comment 10',
|
|
||||||
asset_id: 'asset',
|
|
||||||
author_id: '123'
|
|
||||||
}, {
|
|
||||||
id: 'def',
|
|
||||||
body: 'comment 20',
|
|
||||||
asset_id: 'asset',
|
|
||||||
author_id: '456'
|
|
||||||
}, {
|
|
||||||
id: 'hij',
|
|
||||||
body: 'comment 30',
|
|
||||||
asset_id: '456'
|
|
||||||
}];
|
|
||||||
|
|
||||||
const users = [{
|
|
||||||
displayName: 'Ana',
|
|
||||||
email: 'ana@gmail.com',
|
|
||||||
password: '123'
|
|
||||||
}, {
|
|
||||||
displayName: 'Maria',
|
|
||||||
email: 'maria@gmail.com',
|
|
||||||
password: '123'
|
|
||||||
}];
|
|
||||||
|
|
||||||
const actions = [{
|
|
||||||
action_type: 'flag',
|
|
||||||
item_id: 'abc'
|
|
||||||
}, {
|
|
||||||
action_type: 'like',
|
|
||||||
item_id: 'hij'
|
|
||||||
}];
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
return Promise.all([
|
|
||||||
Comment.create(comments),
|
|
||||||
User.createLocalUsers(users),
|
|
||||||
Action.create(actions)
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('it should remove comment', () => {
|
|
||||||
return chai.request(app)
|
|
||||||
.delete('/api/v1/comments/abc')
|
|
||||||
.set(passport.inject({roles: ['admin']}))
|
|
||||||
.then((res) => {
|
|
||||||
expect(res).to.have.status(204);
|
|
||||||
|
|
||||||
return Comment.findById('abc');
|
|
||||||
})
|
|
||||||
.then((comment) => {
|
|
||||||
expect(comment).to.be.null;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Put /:comment_id/status', () => {
|
|
||||||
|
|
||||||
const comments = [{
|
const comments = [{
|
||||||
id: 'abc',
|
id: 'abc',
|
||||||
@@ -383,90 +306,20 @@ describe('Put /:comment_id/status', () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('it should update status', function() {
|
describe('#post', () => {
|
||||||
return chai.request(app)
|
it('it should update actions', () => {
|
||||||
.put('/api/v1/comments/abc/status')
|
return chai.request(app)
|
||||||
.set(passport.inject({roles: ['admin']}))
|
.post('/api/v1/comments/abc/actions')
|
||||||
.send({status: 'accepted'})
|
.set(passport.inject({id: '456', roles: ['admin']}))
|
||||||
.then((res) => {
|
.send({'user_id': '456', 'action_type': 'flag'})
|
||||||
expect(res).to.have.status(204);
|
.then((res) => {
|
||||||
expect(res.body).to.be.empty;
|
expect(res).to.have.status(201);
|
||||||
});
|
expect(res).to.have.body;
|
||||||
});
|
expect(res.body).to.have.property('item_type', 'comment');
|
||||||
|
expect(res.body).to.have.property('action_type', 'flag');
|
||||||
it('it should not allow a non-admin to update status', () => {
|
expect(res.body).to.have.property('item_id', 'abc');
|
||||||
return chai.request(app)
|
expect(res.body).to.have.property('user_id', '456');
|
||||||
.put('/api/v1/comments/abc/status')
|
});
|
||||||
.set(passport.inject({roles: []}))
|
});
|
||||||
.send({status: 'accepted'})
|
|
||||||
.then((res) => {
|
|
||||||
expect(res).to.be.empty;
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
expect(err).to.have.property('status', 401);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Post /:comment_id/actions', () => {
|
|
||||||
|
|
||||||
const comments = [{
|
|
||||||
id: 'abc',
|
|
||||||
body: 'comment 10',
|
|
||||||
asset_id: 'asset',
|
|
||||||
author_id: '123',
|
|
||||||
status: ''
|
|
||||||
}, {
|
|
||||||
id: 'def',
|
|
||||||
body: 'comment 20',
|
|
||||||
asset_id: 'asset',
|
|
||||||
author_id: '456',
|
|
||||||
status: 'rejected'
|
|
||||||
}, {
|
|
||||||
id: 'hij',
|
|
||||||
body: 'comment 30',
|
|
||||||
asset_id: '456',
|
|
||||||
status: 'accepted'
|
|
||||||
}];
|
|
||||||
|
|
||||||
const users = [{
|
|
||||||
displayName: 'Ana',
|
|
||||||
email: 'ana@gmail.com',
|
|
||||||
password: '123'
|
|
||||||
}, {
|
|
||||||
displayName: 'Maria',
|
|
||||||
email: 'maria@gmail.com',
|
|
||||||
password: '123'
|
|
||||||
}];
|
|
||||||
|
|
||||||
const actions = [{
|
|
||||||
action_type: 'flag',
|
|
||||||
item_id: 'abc'
|
|
||||||
}, {
|
|
||||||
action_type: 'like',
|
|
||||||
item_id: 'hij'
|
|
||||||
}];
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
return Promise.all([
|
|
||||||
Comment.create(comments),
|
|
||||||
User.createLocalUsers(users),
|
|
||||||
Action.create(actions)
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('it should update actions', () => {
|
|
||||||
return chai.request(app)
|
|
||||||
.post('/api/v1/comments/abc/actions')
|
|
||||||
.set(passport.inject({id: '456', roles: ['admin']}))
|
|
||||||
.send({'user_id': '456', 'action_type': 'flag'})
|
|
||||||
.then((res) => {
|
|
||||||
expect(res).to.have.status(201);
|
|
||||||
expect(res).to.have.body;
|
|
||||||
expect(res.body).to.have.property('item_type', 'comment');
|
|
||||||
expect(res.body).to.have.property('action_type', 'flag');
|
|
||||||
expect(res.body).to.have.property('item_id', 'abc');
|
|
||||||
expect(res.body).to.have.property('user_id', '456');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -15,11 +15,7 @@ const User = require('../../../../models/user');
|
|||||||
const Setting = require('../../../../models/setting');
|
const Setting = require('../../../../models/setting');
|
||||||
const settings = {id: '1', moderation: 'pre'};
|
const settings = {id: '1', moderation: 'pre'};
|
||||||
|
|
||||||
beforeEach(() => {
|
describe('/api/v1/queue', () => {
|
||||||
return Setting.create(settings);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Get moderation queues rejected, pending, flags', () => {
|
|
||||||
const comments = [{
|
const comments = [{
|
||||||
id: 'abc',
|
id: 'abc',
|
||||||
body: 'comment 10',
|
body: 'comment 10',
|
||||||
@@ -62,19 +58,22 @@ describe('Get moderation queues rejected, pending, flags', () => {
|
|||||||
return Promise.all([
|
return Promise.all([
|
||||||
Comment.create(comments),
|
Comment.create(comments),
|
||||||
User.createLocalUsers(users),
|
User.createLocalUsers(users),
|
||||||
Action.create(actions)
|
Action.create(actions),
|
||||||
|
Setting.create(settings)
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return all the pending comments', function(done){
|
describe('#get', () => {
|
||||||
chai.request(app)
|
it('should return all the pending comments', function(done){
|
||||||
.get('/api/v1/queue/comments/pending')
|
chai.request(app)
|
||||||
.set(passport.inject({roles: ['admin']}))
|
.get('/api/v1/queue/comments/pending')
|
||||||
.end(function(err, res){
|
.set(passport.inject({roles: ['admin']}))
|
||||||
expect(err).to.be.null;
|
.end(function(err, res){
|
||||||
expect(res).to.have.status(200);
|
expect(err).to.be.null;
|
||||||
expect(res.body[0]).to.have.property('id', 'def');
|
expect(res).to.have.status(200);
|
||||||
done();
|
expect(res.body[0]).to.have.property('id', 'def');
|
||||||
});
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,49 +10,42 @@ chai.use(require('chai-http'));
|
|||||||
const Setting = require('../../../../models/setting');
|
const Setting = require('../../../../models/setting');
|
||||||
const defaults = {id: '1', moderation: 'pre'};
|
const defaults = {id: '1', moderation: 'pre'};
|
||||||
|
|
||||||
describe('GET /settings', () => {
|
describe('/api/v1/settings', () => {
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => Setting.create(defaults));
|
||||||
return Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true});
|
|
||||||
|
describe('#get', () => {
|
||||||
|
|
||||||
|
it('should return a settings object', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.get('/api/v1/settings')
|
||||||
|
.set(passport.inject({
|
||||||
|
roles: ['admin']
|
||||||
|
}))
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(200);
|
||||||
|
expect(res).to.be.json;
|
||||||
|
expect(res.body).to.have.property('moderation', 'pre');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a settings object', () => {
|
describe('#put', () => {
|
||||||
return chai.request(app)
|
|
||||||
.get('/api/v1/settings')
|
|
||||||
.set(passport.inject({
|
|
||||||
roles: ['admin']
|
|
||||||
}))
|
|
||||||
.then((res) => {
|
|
||||||
expect(res).to.have.status(200);
|
|
||||||
expect(res).to.be.json;
|
|
||||||
expect(res.body).to.have.property('moderation', 'pre');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// update the settings.
|
it('should update the settings', () => {
|
||||||
describe('update settings', () => {
|
return chai.request(app)
|
||||||
it('should respond ok to a PUT', () => {
|
.put('/api/v1/settings')
|
||||||
return Setting
|
.set(passport.inject({roles: ['admin']}))
|
||||||
.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true})
|
.send({moderation: 'post'})
|
||||||
.then(() => {
|
.then((res) => {
|
||||||
return chai.request(app)
|
expect(res).to.have.status(204);
|
||||||
.put('/api/v1/settings')
|
|
||||||
.set(passport.inject({
|
|
||||||
roles: ['admin']
|
|
||||||
}))
|
|
||||||
.send({moderation: 'post'});
|
|
||||||
})
|
|
||||||
.then(res => {
|
|
||||||
expect(res).to.have.status(204);
|
|
||||||
|
|
||||||
return Setting.getSettings();
|
return Setting.getSettings();
|
||||||
})
|
})
|
||||||
.then(settings => {
|
.then((settings) => {
|
||||||
|
expect(settings).to.have.property('moderation', 'post');
|
||||||
// confirm updated settings in db
|
});
|
||||||
expect(settings).to.have.property('moderation');
|
});
|
||||||
expect(settings.moderation).to.equal('post');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -13,9 +13,12 @@ const Asset = require('../../../../models/asset');
|
|||||||
|
|
||||||
const Setting = require('../../../../models/setting');
|
const Setting = require('../../../../models/setting');
|
||||||
|
|
||||||
describe('api/stream: routes', () => {
|
describe('/api/v1/stream', () => {
|
||||||
|
|
||||||
const settings = {id: '1', moderation: 'pre'};
|
const settings = {
|
||||||
|
id: '1',
|
||||||
|
moderation: 'pre'
|
||||||
|
};
|
||||||
|
|
||||||
const comments = [{
|
const comments = [{
|
||||||
id: 'abc',
|
id: 'abc',
|
||||||
@@ -35,7 +38,7 @@ describe('api/stream: routes', () => {
|
|||||||
asset_id: 'asset',
|
asset_id: 'asset',
|
||||||
author_id: '456',
|
author_id: '456',
|
||||||
parent_id: '',
|
parent_id: '',
|
||||||
status: ''
|
status: 'accepted'
|
||||||
}, {
|
}, {
|
||||||
id: 'hij',
|
id: 'hij',
|
||||||
body: 'comment 40',
|
body: 'comment 40',
|
||||||
@@ -65,15 +68,26 @@ describe('api/stream: routes', () => {
|
|||||||
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
User.createLocalUsers(users),
|
User.createLocalUsers(users),
|
||||||
Asset.findOrCreateByUrl('http://test.com')
|
Asset.findOrCreateByUrl('http://test.com'),
|
||||||
|
Asset
|
||||||
|
.findOrCreateByUrl('http://coralproject.net/asset2')
|
||||||
|
.then((asset) => {
|
||||||
|
return Asset
|
||||||
|
.overrideSettings(asset.id, {moderation: 'post'})
|
||||||
|
.then(() => asset);
|
||||||
|
})
|
||||||
])
|
])
|
||||||
.then(([users, asset]) => {
|
.then(([users, asset1, asset2]) => {
|
||||||
|
|
||||||
comments[0].author_id = users[0].id;
|
comments[0].author_id = users[0].id;
|
||||||
comments[1].author_id = users[1].id;
|
comments[1].author_id = users[1].id;
|
||||||
|
comments[2].author_id = users[0].id;
|
||||||
|
comments[3].author_id = users[1].id;
|
||||||
|
|
||||||
comments[0].asset_id = asset.id;
|
comments[0].asset_id = asset1.id;
|
||||||
comments[1].asset_id = asset.id;
|
comments[1].asset_id = asset1.id;
|
||||||
|
comments[2].asset_id = asset2.id;
|
||||||
|
comments[3].asset_id = asset2.id;
|
||||||
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
Comment.create(comments),
|
Comment.create(comments),
|
||||||
@@ -83,17 +97,32 @@ describe('api/stream: routes', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return a stream with comments, users and actions for an existing asset', () => {
|
describe('#get', () => {
|
||||||
return chai.request(app)
|
it('should return a stream with comments, users and actions for an existing asset', () => {
|
||||||
.get('/api/v1/stream')
|
return chai.request(app)
|
||||||
.query({'asset_url': 'http://test.com'})
|
.get('/api/v1/stream')
|
||||||
.then(res => {
|
.query({'asset_url': 'http://test.com'})
|
||||||
expect(res).to.have.status(200);
|
.then(res => {
|
||||||
expect(res.body.assets[0]).to.have.property('url');
|
expect(res).to.have.status(200);
|
||||||
expect(res.body.comments[0]).to.have.property('body');
|
expect(res.body.assets.length).to.equal(1);
|
||||||
expect(res.body.users[0]).to.have.property('displayName');
|
expect(res.body.comments.length).to.equal(2);
|
||||||
expect(res.body.actions[0]).to.have.property('action_type');
|
expect(res.body.users.length).to.equal(2);
|
||||||
expect(res.body.settings).to.have.property('moderation');
|
expect(res.body.actions.length).to.equal(1);
|
||||||
});
|
expect(res.body.settings).to.have.property('moderation', 'pre');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should merge the settings when the asset contains settings to override it with', () => {
|
||||||
|
return chai.request(app)
|
||||||
|
.get('/api/v1/stream')
|
||||||
|
.query({'asset_url': 'http://coralproject.net/asset2'})
|
||||||
|
.then((res) => {
|
||||||
|
expect(res).to.have.status(200);
|
||||||
|
expect(res.body.assets.length).to.equal(1);
|
||||||
|
expect(res.body.comments.length).to.equal(1);
|
||||||
|
expect(res.body.users.length).to.equal(1);
|
||||||
|
expect(res.body.settings).to.have.property('moderation', 'post');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user