Const, service, and model updates

- Updated enum values to be uppercase
- Updated services to expose service models
- Updated models to only export the mongoose model
- Moved all mongoose static methods over to new services
- Updated tests to refelct new setup

BREAKING

- Status that were uppercased (caps) have caused issues with the
  admin pages
This commit is contained in:
Wyatt Johnson
2017-01-24 12:10:32 -07:00
parent 0994023864
commit a7e9c0c776
54 changed files with 1831 additions and 2499 deletions
+43 -206
View File
@@ -8,18 +8,19 @@ const expect = chai.expect;
chai.should();
chai.use(require('chai-http'));
const Comment = require('../../../../models/comment');
const Asset = require('../../../../models/asset');
const Action = require('../../../../models/action');
const User = require('../../../../models/user');
const CommentModel = require('../../../../models/comment');
const ActionModel = require('../../../../models/action');
const CommentsService = require('../../../../services/comments');
const UsersService = require('../../../../services/users');
const SettingsService = require('../../../../services/settings');
const Setting = require('../../../../models/setting');
const settings = {id: '1', moderation: 'pre', wordlist: {banned: ['bad words'], suspect: ['suspect words']}};
describe('/api/v1/comments', () => {
// Ensure that the settings are always available.
beforeEach(() => Setting.init(settings));
beforeEach(() => SettingsService.init(settings));
describe('#get', () => {
const comments = [{
@@ -34,16 +35,16 @@ describe('/api/v1/comments', () => {
body: 'comment 20',
asset_id: 'asset',
author_id: '456',
status: 'rejected',
status: 'REJECTED',
status_history: [{
type: 'rejected'
type: 'REJECTED'
}]
}, {
body: 'comment 30',
asset_id: '456',
status: 'accepted',
status: 'ACCEPTED',
status_history: [{
type: 'accepted'
type: 'ACCEPTED'
}]
}];
@@ -58,18 +59,18 @@ describe('/api/v1/comments', () => {
}];
const actions = [{
action_type: 'flag',
action_type: 'FLAG',
item_id: 'abc',
item_type: 'comments'
item_type: 'COMMENTS'
}, {
action_type: 'like',
action_type: 'LIKE',
item_id: 'hij',
item_type: 'comments'
item_type: 'COMMENTS'
}];
beforeEach(() => {
return Promise.all([
Comment.create(comments).then((newComments) => {
CommentModel.create(comments).then((newComments) => {
newComments.forEach((comment, i) => {
comments[i].id = comment.id;
});
@@ -77,9 +78,9 @@ describe('/api/v1/comments', () => {
actions[0].item_id = comments[0].id;
actions[1].item_id = comments[1].id;
return Action.create(actions);
return ActionModel.create(actions);
}),
User.createLocalUsers(users)
UsersService.createLocalUsers(users)
]);
});
@@ -119,7 +120,7 @@ describe('/api/v1/comments', () => {
it('should return all the rejected comments', () => {
return chai.request(app)
.get('/api/v1/comments?status=rejected')
.get('/api/v1/comments?status=REJECTED')
.set(passport.inject({roles: ['admin']}))
.then((res) => {
expect(res).to.have.status(200);
@@ -131,7 +132,7 @@ describe('/api/v1/comments', () => {
it('should return all the approved comments', () => {
return chai.request(app)
.get('/api/v1/comments?status=accepted')
.get('/api/v1/comments?status=ACCEPTED')
.set(passport.inject({roles: ['admin']}))
.then((res) => {
expect(res).to.have.status(200);
@@ -142,7 +143,7 @@ describe('/api/v1/comments', () => {
it('should return all the new comments', () => {
return chai.request(app)
.get('/api/v1/comments?status=new')
.get('/api/v1/comments?status=NEW')
.set(passport.inject({roles: ['admin']}))
.then((res) => {
expect(res).to.have.status(200);
@@ -152,7 +153,7 @@ describe('/api/v1/comments', () => {
it('should return all the flagged comments', () => {
return chai.request(app)
.get('/api/v1/comments?action_type=flag')
.get('/api/v1/comments?action_type=FLAG')
.set(passport.inject({roles: ['admin']}))
.then((res) => {
expect(res).to.have.status(200);
@@ -162,174 +163,6 @@ describe('/api/v1/comments', () => {
});
});
});
describe('#post', () => {
let asset_id;
let postmod_asset_id;
beforeEach(() => Promise.all([
Asset.findOrCreateByUrl('https://coralproject.net/section/article-is-the-best').then((asset) => {
// Update the asset id.
asset_id = asset.id;
}),
Asset.findOrCreateByUrl('https://coralproject.net/section/postmod-article-is-the-best').then((asset) => {
// Update the asset id.
postmod_asset_id = asset.id;
return Asset.overrideSettings(postmod_asset_id, {moderation: 'post'});
}),
]));
it('should create a comment', () => {
return chai.request(app)
.post('/api/v1/comments')
.set(passport.inject({roles: []}))
.send({'body': 'Something body.', 'author_id': '123', 'asset_id': asset_id, 'parent_id': ''})
.then((res) => {
expect(res).to.have.status(201);
expect(res.body).to.have.property('id');
expect(res.body).to.have.property('status', 'premod');
});
});
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': asset_id, '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');
});
});
it('should create a comment with no status and a flag if it contains a suspected word', () => {
return chai.request(app).post('/api/v1/comments')
.set(passport.inject({roles: []}))
.send({'body': 'suspect words are the most suspicious', 'author_id': '123', 'asset_id': postmod_asset_id, 'parent_id': ''})
.then((res) => {
expect(res).to.have.status(201);
expect(res.body).to.have.property('id');
expect(res.body).to.have.property('status', null);
return Promise.all([
res.body,
Action.findByType('flag', 'comments')
]);
})
.then(([comment, actions]) => {
expect(actions).to.have.length(1);
let action = actions[0];
expect(action).to.have.property('item_id', comment.id);
expect(action).to.have.property('metadata');
expect(action.metadata).to.have.property('field', 'body');
expect(action.metadata).to.have.property('details', 'Matched suspect word filters.');
});
});
it('should create a comment with a premod status if it\'s asset is has pre-moderation enabled', () => {
return Asset
.findOrCreateByUrl('https://coralproject.net/article1')
.then((asset) => {
return Asset
.overrideSettings(asset.id, {moderation: 'pre'})
.then(() => asset);
})
.then((asset) => {
return chai.request(app).post('/api/v1/comments')
.set(passport.inject({roles: []}))
.send({'body': 'Something body.', 'author_id': '123', 'asset_id': asset.id, 'parent_id': ''});
})
.then((res) => {
expect(res).to.have.status(201);
expect(res.body).to.have.property('id');
expect(res.body).to.have.property('asset_id');
expect(res.body).to.have.property('status', 'premod');
});
});
it('should create a comment with null status if it\'s asset is has post-moderation enabled', () => {
return Asset
.findOrCreateByUrl('https://coralproject.net/article1')
.then((asset) => {
return Asset
.overrideSettings(asset.id, {moderation: 'post'})
.then(() => asset);
})
.then((asset) => {
return chai.request(app).post('/api/v1/comments')
.set(passport.inject({roles: []}))
.send({'body': 'Something body.', 'author_id': '123', 'asset_id': asset.id, 'parent_id': ''});
})
.then((res) => {
expect(res).to.have.status(201);
expect(res.body).to.have.property('id');
expect(res.body).to.have.property('asset_id');
expect(res.body).to.have.property('status', null);
});
});
it('should create a rejected comment if the body is above the character count', () => {
return Asset
.findOrCreateByUrl('https://coralproject.net/article1')
.then((asset) => {
return Asset
.overrideSettings(asset.id, {charCountEnable: true, charCount: 10})
.then(() => asset);
})
.then((asset) => {
return chai.request(app).post('/api/v1/comments')
.set(passport.inject({roles: []}))
.send({'body': 'This is way way way way way too long.', 'author_id': '123', 'asset_id': asset.id, 'parent_id': ''});
})
.then((res) => {
expect(res).to.have.status(201);
expect(res.body).to.have.property('id');
expect(res.body).to.have.property('asset_id');
expect(res.body).to.have.property('status', 'rejected');
});
});
it('shouldn\'t create a comment when the asset has expired commenting', () => {
return Asset.create({
closedAt: new Date().setDate(0),
closedMessage: 'tests said expired!'
})
.then((asset) => {
return chai.request(app).post('/api/v1/comments')
.set(passport.inject({roles: []}))
.send({'body': 'Something body.', 'author_id': '123', 'asset_id': asset.id, 'parent_id': ''});
})
.then((res) => {
expect(res).to.have.status(500);
})
.catch((err) => {
expect(err.response.body).to.not.be.null;
expect(err.response.body).to.have.property('message');
expect(err.response.body.error.metadata.closedMessage).to.be.equal('tests said expired!');
});
});
it('should create a comment when the asset has not expired yet', () => {
return Asset.create({
closedAt: new Date().setDate(32),
closedMessage: 'tests said expired!'
})
.then((asset) => {
return chai.request(app).post('/api/v1/comments')
.set(passport.inject({roles: []}))
.send({'body': 'Something body.', 'author_id': '123', 'asset_id': asset.id, 'parent_id': ''});
})
.then((res) => {
expect(res).to.have.status(201);
});
});
});
});
describe('/api/v1/comments/:comment_id', () => {
@@ -360,21 +193,21 @@ describe('/api/v1/comments/:comment_id', () => {
}];
const actions = [{
action_type: 'flag',
action_type: 'FLAG',
item_id: 'abc',
item_type: 'comment'
item_type: 'COMMENTS'
}, {
action_type: 'like',
action_type: 'LIKE',
item_id: 'hij',
item_type: 'comment'
item_type: 'COMMENTS'
}];
beforeEach(() => {
return Setting.init(settings).then(() => {
return SettingsService.init(settings).then(() => {
return Promise.all([
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
CommentModel.create(comments),
UsersService.createLocalUsers(users),
ActionModel.create(actions)
]);
});
});
@@ -400,7 +233,7 @@ describe('/api/v1/comments/:comment_id', () => {
.set(passport.inject({roles: ['admin']}))
.then((res) => {
expect(res).to.have.status(204);
return Comment.findById('abc');
return CommentsService.findById('abc');
})
.then((comment) => {
expect(comment).to.be.null;
@@ -448,15 +281,17 @@ describe('/api/v1/comments/:comment_id/actions', () => {
body: 'comment 20',
asset_id: 'asset',
author_id: '456',
status: 'REJECTED',
status_history: [{
type: 'rejected'
type: 'REJECTED'
}]
}, {
id: 'hij',
body: 'comment 30',
asset_id: '456',
status: 'ACCEPTED',
status_history: [{
type: 'accepted'
type: 'ACCEPTED'
}]
}];
@@ -471,19 +306,21 @@ describe('/api/v1/comments/:comment_id/actions', () => {
}];
const actions = [{
action_type: 'flag',
action_type: 'FLAG',
item_type: 'COMMENTS',
item_id: 'abc'
}, {
action_type: 'like',
action_type: 'LIKE',
item_type: 'COMMENTS',
item_id: 'hij'
}];
beforeEach(() => {
return Setting.init(settings).then(() => {
return SettingsService.init(settings).then(() => {
return Promise.all([
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
CommentModel.create(comments),
UsersService.createLocalUsers(users),
ActionModel.create(actions)
]);
});
});