Fix tests.

This commit is contained in:
gaba
2017-05-08 12:57:37 -07:00
parent 6310528804
commit c4c0ee79cd
8 changed files with 106 additions and 54 deletions
+14
View File
@@ -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,
+2 -2
View File
@@ -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);
};
/**
+27 -15
View File
@@ -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: {
+27 -16
View File
@@ -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
});
});
}
+1
View File
@@ -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);
+11
View File
@@ -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 () => {
+21 -18
View File
@@ -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;
});
});
});
});
+3 -3
View File
@@ -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;