From c4c0ee79cd48a3972bb2f2e8e1d690131b48c9ca Mon Sep 17 00:00:00 2001 From: gaba Date: Mon, 8 May 2017 12:57:37 -0700 Subject: [PATCH] Fix tests. --- errors.js | 14 ++++++ graph/mutators/comment.js | 4 +- models/user.js | 42 +++++++++++------- services/comments.js | 43 ++++++++++++------- services/users.js | 1 + test/server/graph/mutations/ignoreUser.js | 11 +++++ .../graph/mutations/removeCommentTag.js | 39 +++++++++-------- test/server/services/comments.js | 6 +-- 8 files changed, 106 insertions(+), 54 deletions(-) diff --git a/errors.js b/errors.js index 67ae0693a..9f12c7835 100644 --- a/errors.js +++ b/errors.js @@ -75,6 +75,18 @@ const ErrMissingToken = new APIError('token is required', { status: 400 }); +// ErrNoCommentFound is returned when trying to add a tag to a comment that does not exist. +const ErrNoCommentFound = new APIError('comment does not exist', { + translation_key: 'COMMENT_NOT_FOUND', + status: 400 +}); + +// ErrNoCommentFound is returned when trying to add a tag to a comment that does not exist. +const ErrorTagNotAllowed = new APIError('tag not allowed', { + translation_key: 'TAG_NOT_ALLOWED', + status: 400 +}); + // ErrAssetCommentingClosed is returned when a comment or action is attempted on // a stream where commenting has been closed. class ErrAssetCommentingClosed extends APIError { @@ -161,6 +173,8 @@ module.exports = { ErrMissingEmail, ErrMissingPassword, ErrMissingToken, + ErrNoCommentFound, + ErrorTagNotAllowed, ErrEmailTaken, ErrSpecialChars, ErrMissingUsername, diff --git a/graph/mutators/comment.js b/graph/mutators/comment.js index 427ab4c63..3413c6f4e 100644 --- a/graph/mutators/comment.js +++ b/graph/mutators/comment.js @@ -28,7 +28,7 @@ const createComment = ({user, loaders: {Comments}, pubsub}, {body, asset_id, par .then(async (comment) => { if (user.hasRoles('ADMIN') || user.hasRoles('MODERATOR')) { - await CommentsService.addTag(comment.id, 'STAFF', user); + await CommentsService.addTag(comment.id, 'STAFF', user.id); } // If the loaders are present, clear the caches for these values because we @@ -209,7 +209,7 @@ const setCommentStatus = ({user, loaders: {Comments}}, {id, status}) => { * @param {String} tag name of the tag */ const addCommentTag = ({user, loaders: {Comments}}, {id, tag}) => { - return CommentsService.addTag(id, tag, user); + return CommentsService.addTag(id, tag, user.id); }; /** diff --git a/models/user.js b/models/user.js index 73e04c392..688c37c2c 100644 --- a/models/user.js +++ b/models/user.js @@ -1,5 +1,6 @@ const mongoose = require('../services/mongoose'); const bcrypt = require('bcrypt'); +const Schema = mongoose.Schema; const uuid = require('uuid'); // USER_ROLES is the array of roles that is permissible as a user role. @@ -16,9 +17,32 @@ const USER_STATUS = [ 'APPROVED' // Indicates that the users' username has been approved ]; +// /** +// * The Mongo schema for a User Tag. +// * @type {Schema} +// */ +// const TagSchema = new Schema({ +// +// // This is the actual 'tag' and we only permit tags that are in Setting.tags. +// id: String, +// +// // The User ID of the user that added the tag. +// added_by: { +// type: String, +// default: null +// }, +// +// created_at: { +// type: Date, +// default: Date +// } +// }, { +// _id: false +// }); + // ProfileSchema is the mongoose schema defined as the representation of a // User's profile stored in MongoDB. -const ProfileSchema = new mongoose.Schema({ +const ProfileSchema = new Schema({ // ID provides the identifier for the user profile, in the case of a local // provider, the id would be an email, in the case of a social provider, @@ -41,7 +65,7 @@ const ProfileSchema = new mongoose.Schema({ // used by the `local` provider to indicate when the email address was // confirmed. metadata: { - type: mongoose.Schema.Types.Mixed + type: Schema.Types.Mixed } }, { _id: false @@ -49,7 +73,7 @@ const ProfileSchema = new mongoose.Schema({ // UserSchema is the mongoose schema defined as the representation of a User in // MongoDB. -const UserSchema = new mongoose.Schema({ +const UserSchema = new Schema({ // This ID represents the most unique identifier for a user, it is generated // when the user is created as a random uuid. @@ -111,18 +135,6 @@ const UserSchema = new mongoose.Schema({ default: false }, - tags: [{ - id: { - type: String, - unique: true - }, - public: Boolean, - text: [{ - type: mongoose.Schema.Types.Mixed, - default: null - }] - }], - // User's settings settings: { bio: { diff --git a/services/comments.js b/services/comments.js index aae5c8248..db4dd18c1 100644 --- a/services/comments.js +++ b/services/comments.js @@ -4,6 +4,9 @@ const ActionModel = require('../models/action'); const ActionsService = require('./actions'); const SettingModel = require('../models/setting'); const SettingsService = require('./settings'); +const UsersService = require('./users'); + +const errors = require('../errors'); const STATUSES = [ 'ACCEPTED', @@ -58,29 +61,37 @@ module.exports = class CommentsService { return SettingsService.retrieve() .then((settings) => { - // Moderators or ADMIN can add any tag automatically. - if (added_by != null && (added_by.hasRoles('ADMIN') || added_by.hasRoles('MODERATOR'))) { - SettingModel.findOneAndUpdate({id: settings.id}, { - $push: { - tags: { - id: name, - models: ['COMMENTS'] + UsersService.findById(added_by) + .then((user) => { + + // Moderators or ADMIN can add any tag automatically. + if (user != null && (user.hasRoles('ADMIN') || user.hasRoles('MODERATOR'))) { + SettingModel.findOneAndUpdate({id: settings.id}, { + $push: { + tags: { + id: name, + models: ['COMMENTS'] + } } - } - }); - } - else if (!ALLOWED_TAGS.includes(name) || settings.tags.findIndex((t) => {return t.id === name & t.models.include('COMMENTS');}) === -1) { - return Promise.reject(new Error('tag not allowed')); - } + }); + } + 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 CommentModel.findOneAndUpdate({id}, { $push: { tags: { id: name, - added_by: added_by.id + added_by: added_by } - } - }); + }, + }, + { + new: false, + upsert: false + }); }); } diff --git a/services/users.js b/services/users.js index 14301ccf4..c33b18780 100644 --- a/services/users.js +++ b/services/users.js @@ -339,6 +339,7 @@ module.exports = class UsersService { if (err.message.match('Username')) { return reject(errors.ErrUsernameTaken); } + console.log('DEBUG FUCKING ERROR', err); return reject(errors.ErrEmailTaken); } return reject(err); diff --git a/test/server/graph/mutations/ignoreUser.js b/test/server/graph/mutations/ignoreUser.js index 54d6305a1..94243a690 100644 --- a/test/server/graph/mutations/ignoreUser.js +++ b/test/server/graph/mutations/ignoreUser.js @@ -32,11 +32,18 @@ describe('graph.mutations.ignoreUser', () => { // @TODO (bengo) - test a user can't ignore themselves it('users can ignoreUser', async () => { + UsersService.findLocalUser('usernameB@example.com') + .then((user) => { + console.log('--------- debug user', user); + }); const user = await UsersService.createLocalUser('usernameA@example.com', 'password', 'usernameA'); + console.log('--------------- debug aca -2'); const userToIgnore = await UsersService.createLocalUser('usernameB@example.com', 'password', 'usernameB'); + console.log('--------------- debug aca -1'); const context = new Context({user}); const ignoreUserResponse = await graphql(schema, ignoreUserMutation, {}, context, {id: userToIgnore.id}); if (ignoreUserResponse.errors && ignoreUserResponse.errors.length) { + console.log('--------------- debug aca 0'); console.error(ignoreUserResponse.errors); } expect(ignoreUserResponse.errors).to.be.empty; @@ -44,13 +51,17 @@ describe('graph.mutations.ignoreUser', () => { // now check my ignored users const myIgnoredUsersResponse = await graphql(schema, getMyIgnoredUsersQuery, {}, context, {}); if (myIgnoredUsersResponse.errors && myIgnoredUsersResponse.errors.length) { + console.log('debug aca 1'); console.error(myIgnoredUsersResponse.errors); } expect(myIgnoredUsersResponse.errors).to.be.empty; const myIgnoredUsers = myIgnoredUsersResponse.data.myIgnoredUsers; expect(myIgnoredUsers.length).to.equal(1); + console.log('debug aca 2'); expect(myIgnoredUsers[0].id).to.equal(userToIgnore.id); + console.log('debug aca 3'); expect(myIgnoredUsers[0].username).to.equal(userToIgnore.username); + console.log('debug aca 4'); }); it('users cannot ignore themselves', async () => { diff --git a/test/server/graph/mutations/removeCommentTag.js b/test/server/graph/mutations/removeCommentTag.js index 663e0de9a..760887a44 100644 --- a/test/server/graph/mutations/removeCommentTag.js +++ b/test/server/graph/mutations/removeCommentTag.js @@ -34,7 +34,7 @@ describe('graph.mutations.removeCommentTag', () => { const context = new Context({user}); // add a tag first - await CommentsService.addTag(comment.id, 'BEST', user); + await CommentsService.addTag(comment.id, 'BEST', user.id); const response = await graphql(schema, query, {}, context, {id: comment.id, tag: 'BEST'}); if (response.errors && response.errors.length) { console.error(response.errors); @@ -59,25 +59,28 @@ describe('graph.mutations.removeCommentTag', () => { models: ['COMMENTS'] } } - }); - Object.entries({ - 'anonymous': undefined, - 'regular commenter': new UserModel({}), - 'banned moderator': new UserModel({roles: ['MODERATOR'], status: 'BANNED'}) - }).forEach(([ userDescription, user ]) => { - it(userDescription, async function () { - const context = new Context({user}); + }) + .then(() => { + Object.entries({ + 'anonymous': undefined, + 'regular commenter': new UserModel({}), + 'banned moderator': new UserModel({roles: ['MODERATOR'], status: 'BANNED'}) + }).forEach(([ userDescription, user ]) => { + it(userDescription, async function () { + const context = new Context({user}); - // add a tag first - await CommentsService.addTag(comment.id, 'BEST', user); - const response = await graphql(schema, query, {}, context, {id: comment.id, tag: 'BEST'}); - if (response.errors && response.errors.length) { - console.error(response.errors); - } - expect(response.errors).to.be.empty; + // add a tag first + await CommentsService.addTag(comment.id, 'BEST', user.id); - expect(response.data.removeCommentTag.errors).to.deep.equal([{'translation_key':'NOT_AUTHORIZED'}]); - expect(response.data.removeCommentTag.comment).to.be.null; + const response = await graphql(schema, query, {}, context, {id: comment.id, tag: 'BEST'}); + if (response.errors && response.errors.length) { + console.error(response.errors); + } + expect(response.errors).to.be.empty; + + expect(response.data.removeCommentTag.errors).to.deep.equal([{'translation_key':'NOT_AUTHORIZED'}]); + expect(response.data.removeCommentTag.comment).to.be.null; + }); }); }); }); diff --git a/test/server/services/comments.js b/test/server/services/comments.js index ed908d1b2..e01fc7bad 100644 --- a/test/server/services/comments.js +++ b/test/server/services/comments.js @@ -227,8 +227,8 @@ describe('services.CommentsService', () => { await CommentsService.addTag(commentId, tagName, userId, 'PUBLIC'); const {tags} = await CommentsService.findById(commentId); expect(tags.length).to.equal(1); - expect(tags[0].name).to.equal(tagName); - expect(tags[0].assigned_by).to.equal(userId); + expect(tags[0].id).to.equal(tagName); + expect(tags[0].added_by).to.equal(userId); expect(tags[0].created_at).to.be.an.instanceof(Date); }); it('can\'t add a tag to comment id that doesn\'t exist', async () => { @@ -238,7 +238,7 @@ describe('services.CommentsService', () => { await expect(CommentsService.addTag(commentId, tagName, userId, 'PUBLIC')).to.be.rejected; }); - it('can\'t add same tag.name twice', async () => { + it('can\'t add same tag.id twice', async () => { const commentId = comments[0].id; const tagName = 'BEST'; const userId = users[0].id;