replaced eslint:recommended with prettier

This commit is contained in:
Wyatt Johnson
2018-01-11 20:00:34 -07:00
parent d56c19016a
commit 0abc2ca243
649 changed files with 16235 additions and 13008 deletions
+58 -46
View File
@@ -7,7 +7,10 @@ chai.use(require('chai-as-promised'));
const expect = chai.expect;
const events = require('../../../services/events');
const {ACTIONS_NEW, ACTIONS_DELETE} = require('../../../services/events/constants');
const {
ACTIONS_NEW,
ACTIONS_DELETE,
} = require('../../../services/events/constants');
const sinon = require('sinon');
@@ -22,7 +25,7 @@ describe('services.ActionsService', () => {
status_history: [],
parent_id: '',
author_id: '123',
id: '1'
id: '1',
});
mockActions = await ActionModel.create([
@@ -30,31 +33,30 @@ describe('services.ActionsService', () => {
action_type: 'FLAG',
item_id: comment.id,
item_type: 'COMMENTS',
user_id: 'flagginguserid'
user_id: 'flagginguserid',
},
{
action_type: 'FLAG',
item_id: '456',
item_type: 'COMMENTS',
user_id: '1'
user_id: '1',
},
{
action_type: 'FLAG',
item_id: comment.id,
item_type: 'COMMENTS',
user_id: '2'
user_id: '2',
},
{
action_type: 'LIKE',
item_id: comment.id,
item_type: 'COMMENTS',
user_id: '3'
}
user_id: '3',
},
]);
});
describe('#create', () => {
it('creates an action', async () => {
const srcAction = {
action_type: 'LIKE',
@@ -68,7 +70,9 @@ describe('services.ActionsService', () => {
expect(createdAction).has.property('id');
expect(createdAction).has.property('item_id', comment.id);
const retrievedAction = await ActionModel.findOne({id: createdAction.id});
const retrievedAction = await ActionModel.findOne({
id: createdAction.id,
});
expect(retrievedAction).is.not.null;
expect(retrievedAction).has.property('id', createdAction.id);
@@ -93,22 +97,22 @@ describe('services.ActionsService', () => {
expect(spy).to.have.been.calledWith(createdAction);
const retrievedComment = await CommentModel.findOne({id: comment.id});
const retrievedComment = await CommentModel.findOne({ id: comment.id });
expect(retrievedComment).to.have.property('action_counts');
expect(retrievedComment.action_counts).to.have.property('like', 1);
});
});
describe('#delete', () => {
it('deletes an action', async () => {
const deletedAction = await ActionsService.delete(mockActions[0]);
expect(deletedAction).has.property('id', mockActions[0].id);
const retrievedAction = await ActionModel.findOne({id: deletedAction.id});
const retrievedAction = await ActionModel.findOne({
id: deletedAction.id,
});
expect(retrievedAction).is.null;
});
@@ -122,17 +126,16 @@ describe('services.ActionsService', () => {
expect(deletedAction).has.property('id', mockActions[0].id);
expect(spy).to.have.been.calledWith(deletedAction);
const retrievedComment = await CommentModel.findOne({id: comment.id});
const retrievedComment = await CommentModel.findOne({ id: comment.id });
expect(retrievedComment).to.have.property('action_counts');
expect(retrievedComment.action_counts).to.have.property('flag', -1);
});
});
describe('#findById()', () => {
it('should find an action by id', () => {
return ActionsService.findById(mockActions[0].id).then((result) => {
return ActionsService.findById(mockActions[0].id).then(result => {
expect(result).to.not.be.null;
expect(result).to.have.property('action_type', 'FLAG');
});
@@ -141,17 +144,18 @@ describe('services.ActionsService', () => {
describe('#findByItemIdArray()', () => {
it('should find an array of actions from an array of item_ids', () => {
return ActionsService.findByItemIdArray([comment.id, '456']).then((result) => {
expect(result).to.have.length(4);
});
return ActionsService.findByItemIdArray([comment.id, '456']).then(
result => {
expect(result).to.have.length(4);
}
);
});
});
describe('#getActionSummaries()', () => {
it('should return properly formatted summaries from an array of item_ids', () => {
return ActionsService
.getActionSummaries([comment.id, '789'])
.then((summaries) => {
return ActionsService.getActionSummaries([comment.id, '789']).then(
summaries => {
expect(summaries).to.have.length(2);
expect(summaries).to.deep.include({
@@ -159,7 +163,7 @@ describe('services.ActionsService', () => {
count: 1,
item_id: comment.id,
item_type: 'COMMENTS',
current_user: null
current_user: null,
});
expect(summaries).to.deep.include({
@@ -167,39 +171,47 @@ describe('services.ActionsService', () => {
count: 2,
item_id: comment.id,
item_type: 'COMMENTS',
current_user: null
current_user: null,
});
});
}
);
});
it('should include a current user when one is passed', () => {
return ActionsService
.getActionSummaries([comment.id], 'flagginguserid')
.then((summaries) => {
expect(summaries).to.have.length(2);
return ActionsService.getActionSummaries(
[comment.id],
'flagginguserid'
).then(summaries => {
expect(summaries).to.have.length(2);
let summary = summaries.find((s) => s.item_id === comment.id && s.action_type === 'FLAG');
let summary = summaries.find(
s => s.item_id === comment.id && s.action_type === 'FLAG'
);
expect(summary).to.not.be.undefined;
expect(summary.current_user).to.not.be.null;
expect(summary.current_user).to.have.property('item_id', comment.id);
expect(summary.current_user).to.have.property('item_type', 'COMMENTS');
expect(summary.current_user).to.have.property('user_id', 'flagginguserid');
expect(summary.current_user).to.have.property('action_type', 'FLAG');
});
expect(summary).to.not.be.undefined;
expect(summary.current_user).to.not.be.null;
expect(summary.current_user).to.have.property('item_id', comment.id);
expect(summary.current_user).to.have.property('item_type', 'COMMENTS');
expect(summary.current_user).to.have.property(
'user_id',
'flagginguserid'
);
expect(summary.current_user).to.have.property('action_type', 'FLAG');
});
});
it('should not include a current user when one is passed for a user that doesn\'t have an action', () => {
return ActionsService
.getActionSummaries([comment.id], 'flagginguserid2')
.then((summaries) => {
expect(summaries).to.have.length(2);
it("should not include a current user when one is passed for a user that doesn't have an action", () => {
return ActionsService.getActionSummaries(
[comment.id],
'flagginguserid2'
).then(summaries => {
expect(summaries).to.have.length(2);
summaries.forEach((summary) => {
expect(summary).to.not.be.undefined;
expect(summary).to.have.property('current_user', null);
});
summaries.forEach(summary => {
expect(summary).to.not.be.undefined;
expect(summary).to.have.property('current_user', null);
});
});
});
});
});
+83 -72
View File
@@ -12,74 +12,76 @@ chai.should();
const expect = chai.expect;
const settings = {id: '1', moderation: 'PRE', domains: {whitelist: ['new.test.com', 'test.com', 'override.test.com']}};
const defaults = {url:'http://test.com'};
const settings = {
id: '1',
moderation: 'PRE',
domains: { whitelist: ['new.test.com', 'test.com', 'override.test.com'] },
};
const defaults = { url: 'http://test.com' };
describe('services.AssetsService', () => {
let asset;
beforeEach(async () => {
await SettingsService.init(settings);
asset = await AssetModel.findOneAndUpdate({id: '1'}, {$setOnInsert: defaults}, {upsert: true, new: true});
asset = await AssetModel.findOneAndUpdate(
{ id: '1' },
{ $setOnInsert: defaults },
{ upsert: true, new: true }
);
});
describe('#findById', ()=> {
describe('#findById', () => {
it('should find an asset by the id', () => {
return AssetsService.findById(1)
.then((asset) => {
expect(asset).to.have.property('url')
.and.to.equal('http://test.com');
});
return AssetsService.findById(1).then(asset => {
expect(asset)
.to.have.property('url')
.and.to.equal('http://test.com');
});
});
});
describe('#findByUrl', ()=> {
describe('#findByUrl', () => {
beforeEach(() => AssetsService.findOrCreateByUrl('http://test.com'));
it('should find an asset by a url', () => {
return AssetsService
.findByUrl('http://test.com')
.then((asset) => {
expect(asset).to.have.property('url', 'http://test.com');
});
return AssetsService.findByUrl('http://test.com').then(asset => {
expect(asset).to.have.property('url', 'http://test.com');
});
});
it('should return null when a url does not exist', () => {
return AssetsService
.findByUrl('http://new.test.com')
.then((asset) => {
expect(asset).to.be.null;
});
return AssetsService.findByUrl('http://new.test.com').then(asset => {
expect(asset).to.be.null;
});
});
});
describe('#findOrCreateByUrl', ()=> {
describe('#findOrCreateByUrl', () => {
it('should find an asset by a url', () => {
return AssetsService
.findOrCreateByUrl('http://test.com')
.then((asset) => {
expect(asset).to.have.property('url')
.and.to.equal('http://test.com');
});
return AssetsService.findOrCreateByUrl('http://test.com').then(asset => {
expect(asset)
.to.have.property('url')
.and.to.equal('http://test.com');
});
});
it('should return a new asset when the url does not exist and its domain is whitelisted', () => {
return AssetsService
.findOrCreateByUrl('http://new.test.com')
.then((asset) => {
expect(asset).to.have.property('id')
return AssetsService.findOrCreateByUrl('http://new.test.com').then(
asset => {
expect(asset)
.to.have.property('id')
.and.to.not.equal(1);
});
}
);
});
it('should return an error when the url does not exist and its domain is not whitelisted', () => {
return AssetsService
.findOrCreateByUrl('http://bad.test.com')
.then((asset) => {
return AssetsService.findOrCreateByUrl('http://bad.test.com')
.then(asset => {
expect(asset).to.be.null;
})
.catch((error) => {
.catch(error => {
expect(error).to.not.be.null;
});
});
@@ -87,18 +89,21 @@ describe('services.AssetsService', () => {
describe('#overrideSettings', () => {
it('should update the settings', () => {
return AssetsService
.findOrCreateByUrl('https://override.test.com/asset')
.then((asset) => {
return AssetsService.findOrCreateByUrl('https://override.test.com/asset')
.then(asset => {
expect(asset).to.have.property('settings');
expect(asset.settings).to.be.empty;
return AssetsService.overrideSettings(asset.id, {moderation: 'PRE'});
return AssetsService.overrideSettings(asset.id, {
moderation: 'PRE',
});
})
.then(() => {
return AssetsService.findOrCreateByUrl('https://override.test.com/asset');
return AssetsService.findOrCreateByUrl(
'https://override.test.com/asset'
);
})
.then((asset) => {
.then(asset => {
expect(asset).to.have.property('settings');
expect(asset.settings).is.an('object');
expect(asset.settings).to.have.property('moderation', 'PRE');
@@ -106,28 +111,27 @@ describe('services.AssetsService', () => {
});
});
describe('#findOrCreateByUrl', ()=> {
describe('#findOrCreateByUrl', () => {
it('should find an asset by a url', () => {
return AssetsService
.findOrCreateByUrl('http://test.com')
.then((asset) => {
expect(asset).to.have.property('url')
.and.to.equal('http://test.com');
});
return AssetsService.findOrCreateByUrl('http://test.com').then(asset => {
expect(asset)
.to.have.property('url')
.and.to.equal('http://test.com');
});
});
it('should return a new asset when the url does not exist', () => {
return AssetsService
.findOrCreateByUrl('http://new.test.com')
.then((asset) => {
expect(asset).to.have.property('id')
return AssetsService.findOrCreateByUrl('http://new.test.com').then(
asset => {
expect(asset)
.to.have.property('id')
.and.to.not.equal(1);
});
}
);
});
});
describe('#updateURL', () => {
it('should change the url if the asset was found, and there was no conflict', async () => {
let newURL = url.resolve(asset.url, '/new-url');
@@ -135,7 +139,7 @@ describe('services.AssetsService', () => {
await AssetsService.updateURL(asset.id, newURL);
// Check that the url was updated.
let {url: databaseURL} = await AssetsService.findById(asset.id);
let { url: databaseURL } = await AssetsService.findById(asset.id);
expect(databaseURL).to.equal(newURL);
});
@@ -144,47 +148,55 @@ describe('services.AssetsService', () => {
let newURL = url.resolve(asset.url, '/new-url');
// Create a new asset with our new URL.
await AssetModel.findOneAndUpdate({id: '2'}, {$setOnInsert: {url: newURL}}, {upsert: true, new: true});
await AssetModel.findOneAndUpdate(
{ id: '2' },
{ $setOnInsert: { url: newURL } },
{ upsert: true, new: true }
);
return AssetsService.updateURL(asset.id, newURL).should.eventually.be.rejected;
return AssetsService.updateURL(asset.id, newURL).should.eventually.be
.rejected;
});
});
describe('#merge', () => {
it('should error if either the src or the dst is missing', () => {
return AssetsService.merge('not-found', asset.id).should.eventually.be.rejected;
return AssetsService.merge('not-found', asset.id).should.eventually.be
.rejected;
});
it('should merge the assets', async () => {
let newURL = url.resolve(asset.url, '/new-url');
// Create a new asset with our new URL.
await AssetModel.findOneAndUpdate({id: '2'}, {$setOnInsert: {url: newURL}}, {upsert: true, new: true});
await AssetModel.findOneAndUpdate(
{ id: '2' },
{ $setOnInsert: { url: newURL } },
{ upsert: true, new: true }
);
// Create some comments on both assets.
await CommentsService.publicCreate([
{
asset_id: '1',
body: 'This is a comment!',
status: 'ACCEPTED'
status: 'ACCEPTED',
},
{
asset_id: '1',
body: 'This is a comment!',
status: 'ACCEPTED'
status: 'ACCEPTED',
},
{
asset_id: '2',
body: 'This is a comment!',
status: 'ACCEPTED'
status: 'ACCEPTED',
},
{
asset_id: '2',
body: 'This is a comment!',
status: 'ACCEPTED'
}
status: 'ACCEPTED',
},
]);
// Merge all the comments from asset 1 into asset 2, followed by deleting
@@ -192,12 +204,11 @@ describe('services.AssetsService', () => {
await AssetsService.merge('1', '2');
// Check to see if the comments are moved.
expect(await CommentModel.find({asset_id: '1'}).count()).to.equal(0);
expect(await CommentModel.find({asset_id: '2'}).count()).to.equal(4);
expect(await CommentModel.find({ asset_id: '1' }).count()).to.equal(0);
expect(await CommentModel.find({ asset_id: '2' }).count()).to.equal(4);
// Check to see if the asset was removed.
expect(await AssetModel.findOne({id: '1'})).to.equal(null);
expect(await AssetModel.findOne({ id: '1' })).to.equal(null);
});
});
});
+135 -103
View File
@@ -2,12 +2,16 @@ const CommentModel = require('../../../models/comment');
const ActionModel = require('../../../models/action');
const events = require('../../../services/events');
const {COMMENTS_EDIT} = require('../../../services/events/constants');
const { COMMENTS_EDIT } = require('../../../services/events/constants');
const UsersService = require('../../../services/users');
const SettingsService = require('../../../services/settings');
const CommentsService = require('../../../services/comments');
const settings = {id: '1', moderation: 'PRE', wordlist: {banned: ['bad words'], suspect: ['suspect words']}};
const settings = {
id: '1',
moderation: 'PRE',
wordlist: { banned: ['bad words'], suspect: ['suspect words'] },
};
const chai = require('chai');
chai.use(require('sinon-chai'));
@@ -16,84 +20,105 @@ const expect = chai.expect;
const sinon = require('sinon');
describe('services.CommentsService', () => {
const comments = [{
body: 'comment 10',
asset_id: '123',
status_history: [],
parent_id: '',
author_id: '123',
id: '1'
}, {
body: 'comment 20',
asset_id: '123',
status_history: [{
type: 'ACCEPTED'
}],
status: 'ACCEPTED',
parent_id: '',
author_id: '123',
id: '2'
}, {
body: 'comment 30',
asset_id: '456',
status_history: [],
parent_id: '',
author_id: '456',
id: '3'
}, {
body: 'comment 40',
asset_id: '123',
status_history: [{
type: 'REJECTED'
}],
status: 'REJECTED',
parent_id: '',
author_id: '456',
id: '4'
}, {
body: 'comment 50',
asset_id: '1234',
status_history: [{
type: 'PREMOD'
}],
status: 'PREMOD',
parent_id: '',
author_id: '456',
id: '5'
}, {
body: 'comment 60',
asset_id: '1234',
status_history: [{
type: 'PREMOD'
}],
status: 'PREMOD',
parent_id: '',
author_id: '456',
id: '6'
}];
const comments = [
{
body: 'comment 10',
asset_id: '123',
status_history: [],
parent_id: '',
author_id: '123',
id: '1',
},
{
body: 'comment 20',
asset_id: '123',
status_history: [
{
type: 'ACCEPTED',
},
],
status: 'ACCEPTED',
parent_id: '',
author_id: '123',
id: '2',
},
{
body: 'comment 30',
asset_id: '456',
status_history: [],
parent_id: '',
author_id: '456',
id: '3',
},
{
body: 'comment 40',
asset_id: '123',
status_history: [
{
type: 'REJECTED',
},
],
status: 'REJECTED',
parent_id: '',
author_id: '456',
id: '4',
},
{
body: 'comment 50',
asset_id: '1234',
status_history: [
{
type: 'PREMOD',
},
],
status: 'PREMOD',
parent_id: '',
author_id: '456',
id: '5',
},
{
body: 'comment 60',
asset_id: '1234',
status_history: [
{
type: 'PREMOD',
},
],
status: 'PREMOD',
parent_id: '',
author_id: '456',
id: '6',
},
];
const users = [{
id: 'u1',
email: 'stampi@gmail.com',
username: 'Stampi',
password: '1Coral!!'
}, {
email: 'sockmonster@gmail.com',
username: 'Sockmonster',
password: '2Coral!!'
}];
const users = [
{
id: 'u1',
email: 'stampi@gmail.com',
username: 'Stampi',
password: '1Coral!!',
},
{
email: 'sockmonster@gmail.com',
username: 'Sockmonster',
password: '2Coral!!',
},
];
const actions = [{
action_type: 'FLAG',
item_id: '3',
item_type: 'COMMENTS',
user_id: '123'
}, {
action_type: 'LIKE',
item_id: '1',
item_type: 'COMMENTS',
user_id: '456'
}];
const actions = [
{
action_type: 'FLAG',
item_id: '3',
item_type: 'COMMENTS',
user_id: '123',
},
{
action_type: 'LIKE',
item_id: '1',
item_type: 'COMMENTS',
user_id: '456',
},
];
beforeEach(async () => {
await SettingsService.init(settings);
@@ -101,16 +126,15 @@ describe('services.CommentsService', () => {
await Promise.all([
CommentModel.create(comments),
UsersService.createLocalUsers(users),
ActionModel.create(actions)
ActionModel.create(actions),
]);
});
describe('#publicCreate()', () => {
it('creates a new comment', async () => {
const c = await CommentsService.publicCreate({
body: 'This is a comment!',
status: 'ACCEPTED'
status: 'ACCEPTED',
});
expect(c).to.not.be.null;
@@ -120,19 +144,19 @@ describe('services.CommentsService', () => {
});
it('creates many new comments', async () => {
const [
c1,
c2,
c3,
] = await CommentsService.publicCreate([{
body: 'This is a comment!',
status: 'ACCEPTED'
}, {
body: 'This is another comment!'
}, {
body: 'This is a rejected comment!',
status: 'REJECTED'
}]);
const [c1, c2, c3] = await CommentsService.publicCreate([
{
body: 'This is a comment!',
status: 'ACCEPTED',
},
{
body: 'This is another comment!',
},
{
body: 'This is a rejected comment!',
status: 'REJECTED',
},
]);
expect(c1).to.not.be.null;
expect(c1.status).to.be.equal('ACCEPTED');
@@ -161,7 +185,10 @@ describe('services.CommentsService', () => {
expect(retrivedComment).to.have.property('status', 'ACCEPTED');
expect(retrivedComment.status_history).to.have.length(2);
expect(retrivedComment.status_history[1]).to.have.property('type', 'ACCEPTED');
expect(retrivedComment.status_history[1]).to.have.property(
'type',
'ACCEPTED'
);
const editedComment = await CommentsService.edit({
id: originalComment.id,
@@ -172,34 +199,40 @@ describe('services.CommentsService', () => {
expect(editedComment).to.have.property('status', 'PREMOD');
expect(editedComment.status_history).to.have.length(4);
expect(editedComment.status_history[3]).to.have.property('type', 'PREMOD');
expect(editedComment.status_history[3]).to.have.property(
'type',
'PREMOD'
);
retrivedComment = await CommentsService.findById(originalComment.id);
expect(retrivedComment).to.have.property('status', 'PREMOD');
expect(retrivedComment.status_history).to.have.length(4);
expect(retrivedComment.status_history[3]).to.have.property('type', 'PREMOD');
expect(retrivedComment.status_history[3]).to.have.property(
'type',
'PREMOD'
);
});
});
describe('#findById()', () => {
it('should find a comment by id', async () => {
const comment = await CommentsService.findById('1');
expect(comment).to.not.be.null;
expect(comment).to.have.property('body', 'comment 10');
});
});
describe('#findByAssetId()', () => {
it('should find an array of all comments by asset id', async () => {
const comments = await CommentsService.findByAssetId('123');
expect(comments).to.have.length(3);
comments.sort((a, b) => {
if (a.body < b.body) {return -1;}
else {return 1;}
if (a.body < b.body) {
return -1;
} else {
return 1;
}
});
expect(comments[0]).to.have.property('body', 'comment 10');
expect(comments[1]).to.have.property('body', 'comment 20');
@@ -208,7 +241,6 @@ describe('services.CommentsService', () => {
});
describe('#changeStatus', () => {
it('should change the status of a comment from no status', async () => {
let comment_id = comments[0].id;
+12 -19
View File
@@ -3,27 +3,25 @@ const DomainList = require('../../../services/domain_list');
const SettingsService = require('../../../services/settings');
describe('services.DomainList', () => {
const domainLists = {
whitelist: [
'nytimes.com',
'wapo.com'
]
whitelist: ['nytimes.com', 'wapo.com'],
};
let domainList = new DomainList();
const settings = {id: '1', moderation: 'PRE', domainlist: {whitelist: ['nytimes.com', 'wapo.com']}};
const settings = {
id: '1',
moderation: 'PRE',
domainlist: { whitelist: ['nytimes.com', 'wapo.com'] },
};
beforeEach(() => SettingsService.init(settings));
describe('#init', () => {
before(() => domainList.upsert(domainLists));
it('has entries', () => {
expect(domainList.lists.whitelist).to.not.be.empty;
});
});
describe('#parseURL', () => {
@@ -92,30 +90,25 @@ describe('services.DomainList', () => {
['google.Ca:80', 'google.ca'],
['google.Ca:443', 'google.ca'],
].forEach(([domain, hostname]) => {
expect(DomainList.parseURL(domain), `domain ${domain} should be parsed as ${hostname}`).to.equal(hostname);
expect(
DomainList.parseURL(domain),
`domain ${domain} should be parsed as ${hostname}`
).to.equal(hostname);
});
});
});
describe('#match', () => {
const whiteList = DomainList.parseList(domainLists['whitelist']);
it('does match on an included domain', () => {
[
'http://wapo.com',
'nytimes.com'
].forEach((domain) => {
['http://wapo.com', 'nytimes.com'].forEach(domain => {
expect(domainList.match(whiteList, domain)).to.be.true;
});
});
it('does not match on a not included domain', () => {
[
'badsite.com',
'www.badsite.com',
'otherexample.com'
].forEach((domain) => {
['badsite.com', 'www.badsite.com', 'otherexample.com'].forEach(domain => {
expect(domainList.match(whiteList, domain)).to.be.false;
});
});
+34 -19
View File
@@ -3,37 +3,54 @@ const chai = require('chai');
const expect = chai.expect;
describe('services.SettingsService', () => {
beforeEach(() => SettingsService.init({moderation: 'PRE', wordlist: ['donut']}));
beforeEach(() =>
SettingsService.init({ moderation: 'PRE', wordlist: ['donut'] })
);
describe('#retrieve()', () => {
it('should have a moderation field defined', () => {
return SettingsService.retrieve().then((settings) => {
expect(settings).to.have.property('moderation').and.to.equal('PRE');
return SettingsService.retrieve().then(settings => {
expect(settings)
.to.have.property('moderation')
.and.to.equal('PRE');
});
});
it('should have two infoBox fields defined', () => {
return SettingsService.retrieve().then((settings) => {
expect(settings).to.have.property('infoBoxEnable').and.to.equal(false);
expect(settings).to.have.property('infoBoxContent').and.to.equal('');
return SettingsService.retrieve().then(settings => {
expect(settings)
.to.have.property('infoBoxEnable')
.and.to.equal(false);
expect(settings)
.to.have.property('infoBoxContent')
.and.to.equal('');
});
});
});
describe('#update()', () => {
it('should update the settings with a passed object', () => {
const mockSettings = {moderation: 'POST', infoBoxEnable: true, infoBoxContent: 'yeah'};
return SettingsService.update(mockSettings).then((updatedSettings) => {
const mockSettings = {
moderation: 'POST',
infoBoxEnable: true,
infoBoxContent: 'yeah',
};
return SettingsService.update(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('infoBoxContent', 'yeah');
});
});
it('should be ok when receiving an object based off of a mongoose model', async () => {
const mockSettings = {moderation: 'POST', infoBoxEnable: true, infoBoxContent: 'yeah'};
const mockSettings = {
moderation: 'POST',
infoBoxEnable: true,
infoBoxContent: 'yeah',
};
await SettingsService.update(mockSettings);
const settings = await SettingsService.retrieve();
@@ -45,7 +62,7 @@ describe('services.SettingsService', () => {
describe('#get', () => {
it('should return the moderation settings', () => {
return SettingsService.retrieve().then(({moderation}) => {
return SettingsService.retrieve().then(({ moderation }) => {
expect(moderation).not.to.be.null;
});
});
@@ -53,15 +70,13 @@ describe('services.SettingsService', () => {
describe('#merge', () => {
it('should merge a settings object and its overrides', () => {
return SettingsService
.retrieve()
.then((settings) => {
let ovrSett = {moderation: 'POST'};
return SettingsService.retrieve().then(settings => {
let ovrSett = { moderation: 'POST' };
settings.merge(ovrSett);
settings.merge(ovrSett);
expect(settings).to.have.property('moderation', 'POST');
});
expect(settings).to.have.property('moderation', 'POST');
});
});
});
});
+30 -26
View File
@@ -12,14 +12,18 @@ describe('services.TagsService', () => {
let comment, user;
beforeEach(async () => {
await SettingsService.init();
user = await UsersService.createLocalUser('stampi@gmail.com', '1Coral!!', 'Stampi');
user = await UsersService.createLocalUser(
'stampi@gmail.com',
'1Coral!!',
'Stampi'
);
comment = await CommentModel.create({
id: '1',
body: 'comment 10',
asset_id: '123',
status_history: [],
parent_id: null,
author_id: user.id
author_id: user.id,
});
});
@@ -31,43 +35,43 @@ describe('services.TagsService', () => {
await TagsService.add(id, 'COMMENTS', {
tag: {
name
name,
},
assigned_by
assigned_by,
});
const {tags} = await CommentsService.findById(id);
const { tags } = await CommentsService.findById(id);
expect(tags.length).to.equal(1);
expect(tags[0].tag.name).to.equal(name);
expect(tags[0].assigned_by).to.equal(assigned_by);
});
it('can\'t add same tag.id twice', async () => {
it("can't add same tag.id twice", async () => {
const id = comment.id;
const name = 'BEST';
const assigned_by = user.id;
await TagsService.add(id, 'COMMENTS', {
tag: {
name
name,
},
assigned_by
assigned_by,
});
{
let {tags} = await CommentsService.findById(id);
let { tags } = await CommentsService.findById(id);
expect(tags.length).to.equal(1);
}
await TagsService.add(id, 'COMMENTS', {
tag: {
name
name,
},
assigned_by
assigned_by,
});
{
let {tags} = await CommentsService.findById(id);
let { tags } = await CommentsService.findById(id);
expect(tags.length).to.equal(1);
}
});
@@ -81,26 +85,26 @@ describe('services.TagsService', () => {
await TagsService.add(id, 'COMMENTS', {
tag: {
name
name,
},
assigned_by
assigned_by,
});
{
const {tags} = await CommentsService.findById(id);
const { tags } = await CommentsService.findById(id);
expect(tags.length).to.equal(1);
}
// ok now to remove it
await TagsService.remove(id, 'COMMENTS', {
tag: {
name
name,
},
assigned_by
assigned_by,
});
{
const {tags} = await CommentsService.findById(id);
const { tags } = await CommentsService.findById(id);
expect(tags.length).to.equal(0);
}
});
@@ -111,33 +115,33 @@ describe('services.TagsService', () => {
await TagsService.add(id, 'COMMENTS', {
tag: {
name: 'ANOTHER'
name: 'ANOTHER',
},
assigned_by
assigned_by,
});
await TagsService.add(id, 'COMMENTS', {
tag: {
name
name,
},
assigned_by
assigned_by,
});
{
const {tags} = await CommentsService.findById(id);
const { tags } = await CommentsService.findById(id);
expect(tags.length).to.equal(2);
}
// ok now to remove it
await TagsService.remove(id, 'COMMENTS', {
tag: {
name
name,
},
assigned_by
assigned_by,
});
{
const {tags} = await CommentsService.findById(id);
const { tags } = await CommentsService.findById(id);
expect(tags.length).to.equal(1);
expect(tags[0].tag.name).to.equal('ANOTHER');
}
+9 -17
View File
@@ -7,15 +7,17 @@ chai.use(require('chai-as-promised'));
const expect = chai.expect;
describe('services.TokensService', () => {
let user;
beforeEach(async () => {
await SettingsService.init();
user = await UsersService.createLocalUser('sockmonster@gmail.com', '2Coral!!', 'Sockmonster');
user = await UsersService.createLocalUser(
'sockmonster@gmail.com',
'2Coral!!',
'Sockmonster'
);
});
describe('#create', () => {
it('can create the token without error', async () => {
let token = await TokensService.create(user.id, 'Github Token');
expect(token).to.be.an.object;
@@ -29,13 +31,11 @@ describe('services.TokensService', () => {
expect(tokens[0]).to.have.property('id', pat.id);
expect(tokens[0]).to.have.property('name', pat.name);
});
});
describe('#revoke', () => {
it('can revoke a token', async () => {
let {pat: {id}} = await TokensService.create(user.id, 'Github Token');
let { pat: { id } } = await TokensService.create(user.id, 'Github Token');
let tokens = await TokensService.list(user.id);
expect(tokens).to.have.length(1);
@@ -49,24 +49,20 @@ describe('services.TokensService', () => {
expect(tokens[0]).to.have.property('id', id);
expect(tokens[0]).to.have.property('active', false);
});
});
describe('#validate', () => {
it('will allow a valid token', async () => {
// Create a token.
let {pat: {id}} = await TokensService.create(user.id, 'Github Token');
let { pat: { id } } = await TokensService.create(user.id, 'Github Token');
// Validate it.
await TokensService.validate(user.id, id);
});
it('will not allow an invalid token', async () => {
// Create a token.
let {pat: {id}} = await TokensService.create(user.id, 'Github Token');
let { pat: { id } } = await TokensService.create(user.id, 'Github Token');
// Revoke it.
await TokensService.revoke(user.id, id);
@@ -74,23 +70,19 @@ describe('services.TokensService', () => {
// Validate it.
return TokensService.validate(user.id, id).should.eventually.be.rejected;
});
});
describe('#list', () => {
it('lists the tokens for a user', async () => {
let tokens = await TokensService.list(user.id);
expect(tokens).to.have.length(0);
// Create a token.
let {pat: {id}} = await TokensService.create(user.id, 'Github Token');
let { pat: { id } } = await TokensService.create(user.id, 'Github Token');
tokens = await TokensService.list(user.id);
expect(tokens).to.have.length(1);
expect(tokens[0]).to.have.property('id', id);
});
});
});
+111 -70
View File
@@ -9,25 +9,32 @@ chai.use(require('sinon-chai'));
const expect = chai.expect;
describe('services.UsersService', () => {
let mockUsers;
beforeEach(async () => {
const settings = {id: '1', moderation: 'PRE', wordlist: {banned: ['bad words'], suspect: ['suspect words']}};
const settings = {
id: '1',
moderation: 'PRE',
wordlist: { banned: ['bad words'], suspect: ['suspect words'] },
};
await SettingsService.init(settings);
mockUsers = await UsersService.createLocalUsers([{
email: 'stampi@gmail.com',
username: 'Stampi',
password: '1Coral!-',
}, {
email: 'sockmonster@gmail.com',
username: 'Sockmonster',
password: '2Coral!2'
}, {
email: 'marvel@gmail.com',
username: 'Marvel',
password: '3Coral!3'
}]);
mockUsers = await UsersService.createLocalUsers([
{
email: 'stampi@gmail.com',
username: 'Stampi',
password: '1Coral!-',
},
{
email: 'sockmonster@gmail.com',
username: 'Sockmonster',
password: '2Coral!2',
},
{
email: 'marvel@gmail.com',
username: 'Marvel',
password: '3Coral!3',
},
]);
sinon.spy(mailer, 'send');
});
@@ -45,7 +52,7 @@ describe('services.UsersService', () => {
describe('#findByIdArray()', () => {
it('should find an array of users from an array of ids', async () => {
const ids = mockUsers.map((user) => user.id);
const ids = mockUsers.map(user => user.id);
const users = await UsersService.findByIdArray(ids);
expect(users).to.have.length(3);
});
@@ -53,13 +60,17 @@ describe('services.UsersService', () => {
describe('#findPublicByIdArray()', () => {
it('should find an array of users from an array of ids', async () => {
const ids = mockUsers.map((user) => user.id);
const ids = mockUsers.map(user => user.id);
const users = await UsersService.findPublicByIdArray(ids);
expect(users).to.have.length(3);
const sorted = users.sort((a, b) => {
if(a.username < b.username) {return -1;}
if(a.username > b.username) {return 1;}
const sorted = users.sort((a, b) => {
if (a.username < b.username) {
return -1;
}
if (a.username > b.username) {
return 1;
}
return 0;
});
expect(sorted[0]).to.have.property('username', 'Marvel');
@@ -67,74 +78,83 @@ describe('services.UsersService', () => {
});
describe('#findLocalUser', () => {
it('should find a user', () => {
return UsersService
.findLocalUser(mockUsers[0].profiles[0].id)
.then((user) => {
return UsersService.findLocalUser(mockUsers[0].profiles[0].id).then(
user => {
expect(user).to.have.property('username', mockUsers[0].username);
});
}
);
});
});
describe('#createLocalUser', () => {
it('should not create a user with duplicate username', () => {
return UsersService.createLocalUsers([{
email: 'otrostampi@gmail.com',
username: 'StampiTheSecond',
password: '1Coralito!'
}])
.then((user) => {
return UsersService.createLocalUsers([
{
email: 'otrostampi@gmail.com',
username: 'StampiTheSecond',
password: '1Coralito!',
},
])
.then(user => {
expect(user).to.be.null;
})
.catch((error) => {
.catch(error => {
expect(error).to.not.be.null;
});
});
});
describe('#createEmailConfirmToken', () => {
it('should create a token for a valid user', async () => {
const token = await UsersService.createEmailConfirmToken(mockUsers[0], mockUsers[0].profiles[0].id);
const token = await UsersService.createEmailConfirmToken(
mockUsers[0],
mockUsers[0].profiles[0].id
);
expect(token).to.not.be.null;
});
it('should not create a token for a user already verified', async () => {
const token = await UsersService.createEmailConfirmToken(mockUsers[0], mockUsers[0].profiles[0].id);
const token = await UsersService.createEmailConfirmToken(
mockUsers[0],
mockUsers[0].profiles[0].id
);
expect(token).to.not.be.null;
await UsersService.verifyEmailConfirmation(token);
const user = await UsersService.findById(mockUsers[0].id);
return expect(UsersService.createEmailConfirmToken(user, mockUsers[0].profiles[0].id)).to.eventually.be.rejected;
return expect(
UsersService.createEmailConfirmToken(user, mockUsers[0].profiles[0].id)
).to.eventually.be.rejected;
});
});
describe('#verifyEmailConfirmation', () => {
it('should correctly validate a valid token', async () => {
const token = await UsersService.createEmailConfirmToken(mockUsers[0], mockUsers[0].profiles[0].id);
const token = await UsersService.createEmailConfirmToken(
mockUsers[0],
mockUsers[0].profiles[0].id
);
expect(token).to.not.be.null;
return expect(UsersService.verifyEmailConfirmation(token)).to.eventually.not.be.rejected;
return expect(UsersService.verifyEmailConfirmation(token)).to.eventually
.not.be.rejected;
});
it('should correctly reject an invalid token', async () => {
return UsersService
.verifyEmailConfirmation('cats')
.catch((err) => {
expect(err).to.not.be.null;
});
return UsersService.verifyEmailConfirmation('cats').catch(err => {
expect(err).to.not.be.null;
});
});
it('should update the user model when verification is complete', () => {
return UsersService
.createEmailConfirmToken(mockUsers[0], mockUsers[0].profiles[0].id)
.then((token) => {
return UsersService.createEmailConfirmToken(
mockUsers[0],
mockUsers[0].profiles[0].id
)
.then(token => {
expect(token).to.not.be.null;
return UsersService.verifyEmailConfirmation(token);
@@ -142,25 +162,27 @@ describe('services.UsersService', () => {
.then(() => {
return UsersService.findById(mockUsers[0].id);
})
.then((user) => {
.then(user => {
expect(user.profiles[0]).to.have.property('metadata');
expect(user.profiles[0].metadata).to.have.property('confirmed_at');
expect(user.profiles[0].metadata.confirmed_at).to.not.be.null;
});
});
});
describe('#ignoreUser', () => {
it('should add user id to ignoredUsers set', async () => {
const user = mockUsers[0];
const usersToIgnore = [mockUsers[1], mockUsers[2]];
await UsersService.ignoreUsers(user.id, usersToIgnore.map((u) => u.id));
await UsersService.ignoreUsers(user.id, usersToIgnore.map(u => u.id));
const userAfterIgnoring = await UsersService.findById(user.id);
expect(userAfterIgnoring.ignoresUsers.length).to.equal(2);
// ignore same user another time, make sure it's not added to the list.
await UsersService.ignoreUsers(user.id, usersToIgnore.slice(0, 1).map((u) => u.id));
await UsersService.ignoreUsers(
user.id,
usersToIgnore.slice(0, 1).map(u => u.id)
);
const userAfterIgnoring2 = await UsersService.findById(user.id);
expect(userAfterIgnoring2.ignoresUsers.length).to.equal(2);
});
@@ -171,7 +193,7 @@ describe('services.UsersService', () => {
await UsersService.setRole(usersToIgnore[0].id, 'STAFF');
try {
await UsersService.ignoreUsers(user.id, usersToIgnore.map((u) => u.id));
await UsersService.ignoreUsers(user.id, usersToIgnore.map(u => u.id));
} catch (err) {
expect(err.status).to.equal(400);
expect(err.translation_key).to.equal('CANNOT_IGNORE_STAFF');
@@ -180,18 +202,30 @@ describe('services.UsersService', () => {
});
[
{func: 'changeUsername', okStatus: 'REJECTED', notOKStatus: 'UNSET', newStatus: 'CHANGED'},
{func: 'setUsername', okStatus: 'UNSET', notOKStatus: 'REJECTED', newStatus: 'SET'},
].forEach(({func, okStatus, notOKStatus, newStatus}) => {
{
func: 'changeUsername',
okStatus: 'REJECTED',
notOKStatus: 'UNSET',
newStatus: 'CHANGED',
},
{
func: 'setUsername',
okStatus: 'UNSET',
notOKStatus: 'REJECTED',
newStatus: 'SET',
},
].forEach(({ func, okStatus, notOKStatus, newStatus }) => {
describe(`#${func}`, () => {
[
{status: okStatus},
{error: 'EDIT_USERNAME_NOT_AUTHORIZED', status: notOKStatus},
{error: 'EDIT_USERNAME_NOT_AUTHORIZED', status: 'SET'},
{error: 'EDIT_USERNAME_NOT_AUTHORIZED', status: 'APPROVED'},
{error: 'EDIT_USERNAME_NOT_AUTHORIZED', status: 'CHANGED'},
].forEach(({status, error}) => {
it(`${error ? 'should not' : 'should'} let them change the username if they have the status of ${status}`, async () => {
{ status: okStatus },
{ error: 'EDIT_USERNAME_NOT_AUTHORIZED', status: notOKStatus },
{ error: 'EDIT_USERNAME_NOT_AUTHORIZED', status: 'SET' },
{ error: 'EDIT_USERNAME_NOT_AUTHORIZED', status: 'APPROVED' },
{ error: 'EDIT_USERNAME_NOT_AUTHORIZED', status: 'CHANGED' },
].forEach(({ status, error }) => {
it(`${
error ? 'should not' : 'should'
} let them change the username if they have the status of ${status}`, async () => {
const user = mockUsers[0];
// Set the user to the desired status.
@@ -223,11 +257,16 @@ describe('services.UsersService', () => {
await UsersService[func](user.id, 'spock');
throw new Error('edit was processed successfully');
} catch (err) {
expect(err).have.property('translation_key', 'EDIT_USERNAME_NOT_AUTHORIZED');
expect(err).have.property(
'translation_key',
'EDIT_USERNAME_NOT_AUTHORIZED'
);
}
});
it(`${func === 'changeUsername' ? 'should' : 'should not'} refuse changing the username to the same username`, async () => {
it(`${
func === 'changeUsername' ? 'should' : 'should not'
} refuse changing the username to the same username`, async () => {
const user = mockUsers[0];
// Set the user to the desired status.
@@ -238,7 +277,10 @@ describe('services.UsersService', () => {
await UsersService[func](user.id, user.username);
throw new Error('edit was processed successfully');
} catch (err) {
expect(err).have.property('translation_key', 'SAME_USERNAME_PROVIDED');
expect(err).have.property(
'translation_key',
'SAME_USERNAME_PROVIDED'
);
}
} else {
await UsersService[func](user.id, user.username);
@@ -249,12 +291,11 @@ describe('services.UsersService', () => {
describe('#isValidUsername', () => {
it('should not allow non-alphanumeric characters in usernames', () => {
return UsersService
.isValidUsername('hi🖕')
return UsersService.isValidUsername('hi🖕')
.then(() => {
expect(false).to.be.true;
})
.catch((err) => {
.catch(err => {
expect(err).to.be.ok;
});
});
+33 -30
View File
@@ -6,7 +6,6 @@ const chai = require('chai');
const expect = chai.expect;
describe('services.Wordlist', () => {
const wordlists = {
banned: [
'cookies',
@@ -17,18 +16,19 @@ describe('services.Wordlist', () => {
'p**ch',
'p*ch',
],
suspect: [
'do bad things',
]
suspect: ['do bad things'],
};
let wordlist = new Wordlist();
const settings = {id: '1', moderation: 'PRE', wordlist: {banned: ['bad words'], suspect: ['suspect words']}};
const settings = {
id: '1',
moderation: 'PRE',
wordlist: { banned: ['bad words'], suspect: ['suspect words'] },
};
beforeEach(() => SettingsService.init(settings));
describe('#regexp', () => {
before(() => wordlist.upsert(wordlists));
it('does match on a bad word', () => {
@@ -40,8 +40,8 @@ describe('services.Wordlist', () => {
'how to do bad things',
'How To do bad things!',
'This stuff is $hit!',
'That\'s a p**ch!',
].forEach((word) => {
"That's a p**ch!",
].forEach(word => {
expect(wordlist.regexp.banned.test(word)).to.be.true;
});
});
@@ -54,16 +54,14 @@ describe('services.Wordlist', () => {
'how to not do really bad things?',
'i have $100 dollars.',
'I have bad $ hit lling',
'That\'s a p***ch!',
].forEach((word) => {
"That's a p***ch!",
].forEach(word => {
expect(wordlist.regexp.banned.test(word)).to.be.false;
});
});
});
describe('#scan', () => {
it('does match on a bad word', () => {
[
'how to do really bad things',
@@ -73,8 +71,8 @@ describe('services.Wordlist', () => {
'how to do bad things',
'How To do bad things!',
'This stuff is $hit!',
'That\'s a p**ch!',
].forEach((word) => {
"That's a p**ch!",
].forEach(word => {
expect(wordlist.scan('body', word)).to.not.be.undefined;
});
});
@@ -87,43 +85,48 @@ describe('services.Wordlist', () => {
'how to not do really bad things?',
'i have $100 dollars.',
'I have bad $ hit lling',
'That\'s a p***ch!',
].forEach((word) => {
"That's a p***ch!",
].forEach(word => {
expect(wordlist.scan('body', word)).to.be.deep.equal({});
});
});
});
describe('#filter', () => {
before(() => wordlist.upsert(wordlists));
it('matches on bodies containing bad words', () => {
let errors = wordlist.filter({
content: 'how to do really bad things?'
}, 'content');
let errors = wordlist.filter(
{
content: 'how to do really bad things?',
},
'content'
);
expect(errors).to.have.property('banned', Errors.ErrContainsProfanity);
});
it('does not match on bodies not containing bad words', () => {
let errors = wordlist.filter({
content: 'how to not do really bad things?'
}, 'content');
let errors = wordlist.filter(
{
content: 'how to not do really bad things?',
},
'content'
);
expect(errors).to.not.have.property('banned');
});
it('does not match on bodies not containing the bad word field', () => {
let errors = wordlist.filter({
author: 'how to do really bad things?',
content: 'how to be a great person?'
}, 'content');
let errors = wordlist.filter(
{
author: 'how to do really bad things?',
content: 'how to be a great person?',
},
'content'
);
expect(errors).to.not.have.property('banned');
});
});
});