mirror of
https://github.com/wassname/talk.git
synced 2026-07-03 06:54:33 +08:00
Test was failing as I was not returning an error.
This commit is contained in:
@@ -82,7 +82,7 @@ const ErrNoCommentFound = new APIError('comment does not exist', {
|
||||
});
|
||||
|
||||
// ErrNoCommentFound is returned when trying to add a tag to a comment that does not exist.
|
||||
const ErrorTagNotAllowed = new APIError('tag not allowed', {
|
||||
const ErrTagNotAllowed = new APIError('tag not allowed', {
|
||||
translation_key: 'TAG_NOT_ALLOWED',
|
||||
status: 400
|
||||
});
|
||||
@@ -174,7 +174,7 @@ module.exports = {
|
||||
ErrMissingPassword,
|
||||
ErrMissingToken,
|
||||
ErrNoCommentFound,
|
||||
ErrorTagNotAllowed,
|
||||
ErrTagNotAllowed,
|
||||
ErrEmailTaken,
|
||||
ErrSpecialChars,
|
||||
ErrMissingUsername,
|
||||
|
||||
@@ -92,6 +92,15 @@ const CommentSchema = new Schema({
|
||||
}
|
||||
});
|
||||
|
||||
// Add the indexes on the comment tag.
|
||||
CommentSchema.index({
|
||||
'id': 1,
|
||||
'tags.id': 1
|
||||
}, {
|
||||
unique: true,
|
||||
background: false
|
||||
});
|
||||
|
||||
// Comment model.
|
||||
const Comment = mongoose.model('Comment', CommentSchema);
|
||||
|
||||
|
||||
+22
-7
@@ -76,21 +76,26 @@ module.exports = class CommentsService {
|
||||
});
|
||||
}
|
||||
else if (!ALLOWED_TAGS.includes(name) || settings.tags.findIndex((t) => {return t.id === name & t.models.include('COMMENTS');}) === -1) {
|
||||
return Promise.reject(errors.ErrorTagNotAllowed);
|
||||
return Promise.reject(errors.ErrTagNotAllowed);
|
||||
}
|
||||
});
|
||||
|
||||
return CommentModel.findOneAndUpdate({id}, {
|
||||
return CommentModel.findOneAndUpdate({id, 'tags.id': {$ne: name}}, {
|
||||
$push: {
|
||||
tags: {
|
||||
id: name,
|
||||
added_by: added_by
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
new: false,
|
||||
upsert: false
|
||||
})
|
||||
.then(({nModified}) => {
|
||||
switch (nModified) {
|
||||
case 0:
|
||||
return Promise.reject(errors.ErrNoCommentFound);
|
||||
case 1:
|
||||
return;
|
||||
default:
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -102,12 +107,22 @@ module.exports = class CommentsService {
|
||||
* @param {String} tag_id the id of the tag to remove
|
||||
*/
|
||||
static removeTag(id, tag_id) {
|
||||
return CommentModel.findOneAndUpdate({id}, {
|
||||
return CommentModel.findOneAndUpdate({id, 'tags.id': tag_id}, {
|
||||
$pull: {
|
||||
tags: {
|
||||
id: tag_id
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
.then(({nModified}) => {
|
||||
switch(nModified) {
|
||||
case 0:
|
||||
return Promise.reject(errors.ErrNoCommentFound);
|
||||
case 1:
|
||||
return;
|
||||
default:
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ describe('graph.mutations.ignoreUser', () => {
|
||||
|
||||
// @TODO (bengo) - test a user can't ignore themselves
|
||||
it('users can ignoreUser', async () => {
|
||||
UsersService.findLocalUser('usernameB@example.com');
|
||||
const user = await UsersService.createLocalUser('usernameA@example.com', 'password', 'usernameA');
|
||||
const userToIgnore = await UsersService.createLocalUser('usernameB@example.com', 'password', 'usernameB');
|
||||
const context = new Context({user});
|
||||
|
||||
@@ -236,7 +236,10 @@ describe('services.CommentsService', () => {
|
||||
const tagName = 'BEST';
|
||||
const userId = users[0].id;
|
||||
|
||||
await expect(CommentsService.addTag(commentId, tagName, userId, 'PUBLIC')).to.be.rejected;
|
||||
CommentsService.addTag(commentId, tagName, userId)
|
||||
.catch((error) => {
|
||||
expect(error).to.not.be.null;
|
||||
});
|
||||
});
|
||||
it('can\'t add same tag.id twice', async () => {
|
||||
const commentId = comments[0].id;
|
||||
@@ -244,10 +247,10 @@ describe('services.CommentsService', () => {
|
||||
const userId = users[0].id;
|
||||
|
||||
// first time
|
||||
await CommentsService.addTag(commentId, tagName, userId, 'PUBLIC');
|
||||
await CommentsService.addTag(commentId, tagName, userId);
|
||||
|
||||
// second time should fail
|
||||
await expect(CommentsService.addTag(commentId, tagName, userId, 'PUBLIC')).to.be.rejected;
|
||||
await expect(CommentsService.addTag(commentId, tagName, userId)).to.be.rejected;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -255,7 +258,7 @@ describe('services.CommentsService', () => {
|
||||
it('removes a tag', async () => {
|
||||
const commentId = comments[0].id;
|
||||
const tagName = 'BEST';
|
||||
await CommentsService.addTag(commentId, tagName, users[0].id, 'PUBLIC');
|
||||
await CommentsService.addTag(commentId, tagName, users[0].id);
|
||||
const {tags} = await CommentsService.findById(commentId);
|
||||
expect(tags.length).to.equal(1);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user