Merge pull request #50 from coralproject/auth

Users
This commit is contained in:
David Erwin
2016-11-10 11:46:38 -05:00
committed by GitHub
13 changed files with 986 additions and 179 deletions
+6 -1
View File
@@ -6,7 +6,12 @@ const path = require('path');
const app = express();
// Middleware declarations.
app.use(morgan('dev'));
// Add the logging middleware only if we aren't testing.
if (app.get('env') !== 'test') {
app.use(morgan('dev'));
}
app.use(bodyParser.json());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
Executable
+29
View File
@@ -0,0 +1,29 @@
#!/usr/bin/env node
/**
* Setup the debug paramater.
*/
process.env.DEBUG = process.env.TALK_DEBUG;
/**
* Module dependencies.
*/
const program = require('commander');
const pkg = require('../package.json');
//==============================================================================
// Setting up the program command line arguments.
//==============================================================================
program
.version(pkg.version)
.command('settings', 'work with the application settings')
.command('users', 'work with the application auth')
.parse(process.argv);
// If there is no command listed, output help.
if (!process.argv.slice(2).length) {
program.outputHelp();
}
+42
View File
@@ -0,0 +1,42 @@
#!/usr/bin/env node
/**
* Setup the debug paramater.
*/
process.env.DEBUG = process.env.TALK_DEBUG;
/**
* Module dependencies.
*/
const program = require('commander');
//==============================================================================
// Setting up the program command line arguments.
//==============================================================================
program
.command('init')
.description('initilizes the talk settings')
.action(() => {
const mongoose = require('../mongoose');
const Setting = require('../models/setting');
const defaults = {id: '1', moderation: 'pre'};
Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true})
.then(() => {
console.log('Created settings object.');
mongoose.disconnect();
}).catch((err) => {
console.error(`failed to create the settings object ${JSON.stringify(err)}`);
throw new Error(err); // just to be safe
});
});
program.parse(process.argv);
// If there is no command listed, output help.
if (!process.argv.slice(2).length) {
program.outputHelp();
}
Executable
+408
View File
@@ -0,0 +1,408 @@
#!/usr/bin/env node
/**
* Setup the debug paramater.
*/
process.env.DEBUG = process.env.TALK_DEBUG;
/**
* Module dependencies.
*/
const program = require('commander');
const pkg = require('../package.json');
const prompt = require('prompt');
/**
* Prompts for input and registers a user based on those.
*/
function createUser(options) {
const User = require('../models/user');
const mongoose = require('../mongoose');
return new Promise((resolve, reject) => {
if (options.flag_mode) {
return resolve({
email: options.email,
password: options.password,
displayName: options.name,
});
}
prompt.start();
prompt.get([
{
name: 'email',
description: 'Email',
format: 'email',
required: true
},
{
name: 'password',
description: 'Password',
hidden: true,
required: true
},
{
name: 'confirmPassword',
description: 'Confirm Password',
hidden: true,
required: true
},
{
name: 'displayName',
description: 'Display Name',
required: true
}
], (err, result) => {
if (err) {
return reject(err);
}
if (result.password !== result.confirmPassword) {
return reject(new Error('Passwords do not match'));
}
resolve(result);
});
})
.then((result) => {
return User.createLocalUser(result.email.trim(), result.password.trim(), result.displayName.trim());
}).then((user) => {
console.log(`Created user ${user.id}.`);
mongoose.disconnect();
}).catch((err) => {
console.error(err);
mongoose.disconnect();
});
}
/**
* Deletes a user.
*/
function deleteUser(userID) {
const User = require('../models/user');
const mongoose = require('../mongoose');
User
.findOneAndRemove({
id: userID
})
.then(() => {
console.log('Deleted user');
mongoose.disconnect();
})
.catch((err) => {
console.error(err);
mongoose.disconnect();
});
}
/**
* Changes the password for a user.
*/
function passwd(userID) {
const User = require('../models/user');
const mongoose = require('../mongoose');
prompt.start();
prompt.get([
{
name: 'password',
description: 'Password',
hidden: true,
required: true
},
{
name: 'confirmPassword',
description: 'Confirm Password',
hidden: true,
required: true
}
], (err, result) => {
if (err) {
console.error(err);
mongoose.disconnect();
return;
}
if (result.password !== result.confirmPassword) {
console.error(new Error('Password mismatch'));
mongoose.disconnect();
return;
}
User
.changePassword(userID, result.password)
.then(() => {
console.log('Password changed.');
mongoose.disconnect();
})
.catch((err) => {
console.error(err);
mongoose.disconnect();
});
});
}
/**
* Updates the user from the options array.
*/
function updateUser(userID, options) {
const User = require('../models/user');
const mongoose = require('../mongoose');
const updates = [];
if (options.email && typeof options.email === 'string' && options.email.length > 0) {
let q = User.update({
'id': userID,
'profiles.provider': 'local'
}, {
$set: {
'profiles.$.id': options.email
}
});
updates.push(q);
}
if (options.name && typeof options.name === 'string' && options.name.length > 0) {
let q = User.update({
'id': userID
}, {
$set: {
displayName: options.name
}
});
updates.push(q);
}
Promise
.all(updates.map((q) => q.exec()))
.then(() => {
console.log(`User ${userID} updated.`);
mongoose.disconnect();
})
.catch((err) => {
console.error(err);
mongoose.disconnect();
});
}
/**
* Lists all the users registered in the database.
*/
function listUsers() {
const Table = require('cli-table');
const User = require('../models/user');
const mongoose = require('../mongoose');
User
.find()
.then((users) => {
let table = new Table({
head: [
'ID',
'Display Name',
'Profiles',
'Roles',
'State'
]
});
users.forEach((user) => {
table.push([
user.id,
user.displayName,
user.profiles.map((p) => p.provider).join(', '),
user.roles.join(', '),
user.disabled ? 'Disabled' : 'Enabled'
]);
});
console.log(table.toString());
mongoose.disconnect();
})
.catch((err) => {
console.error(err);
mongoose.disconnect();
});
}
/**
* Merges two users using the specified ID's.
* @param {String} dstUserID id of the user to which is the target of the merge
* @param {String} srcUserID id of the user to which is the source of the merge
*/
function mergeUsers(dstUserID, srcUserID) {
const User = require('../models/user');
const mongoose = require('../mongoose');
User
.mergeUsers(dstUserID, srcUserID)
.then(() => {
console.log(`User ${srcUserID} was merged into user ${dstUserID}.`);
mongoose.disconnect();
}).catch((err) => {
console.error(err);
mongoose.disconnect();
});
}
/**
* Adds a role to a user
* @param {String} userUD id of the user to add the role to
* @param {String} role the role to add
*/
function addRole(userID, role) {
const User = require('../models/user');
const mongoose = require('../mongoose');
User
.addRoleToUser(userID, role)
.then(() => {
console.log(`Added the ${role} role to User ${userID}.`);
mongoose.disconnect();
})
.catch((err) => {
console.error(err);
mongoose.disconnect();
});
}
/**
* Removes a role from a user
* @param {String} userUD id of the user to remove the role from
* @param {String} role the role to remove
*/
function removeRole(userID, role) {
const User = require('../models/user');
const mongoose = require('../mongoose');
User
.removeRoleFromUser(userID, role)
.then(() => {
console.log(`Removed the ${role} role from User ${userID}.`);
mongoose.disconnect();
})
.catch((err) => {
console.error(err);
mongoose.disconnect();
});
}
/**
* Disable a given user.
* @param {String} userID the ID of a user to disable
*/
function disableUser(userID) {
const User = require('../models/user');
const mongoose = require('../mongoose');
User
.disableUser(userID)
.then(() => {
console.log(`User ${userID} was disabled.`);
mongoose.disconnect();
})
.catch((err) => {
console.error(err);
mongoose.disconnect();
});
}
/**
* Enabled a given user.
* @param {String} userID the ID of a user to enable
*/
function enableUser(userID) {
const User = require('../models/user');
const mongoose = require('../mongoose');
User
.enableUser(userID)
.then(() => {
console.log(`User ${userID} was enabled.`);
mongoose.disconnect();
})
.catch((err) => {
console.error(err);
mongoose.disconnect();
});
}
//==============================================================================
// Setting up the program command line arguments.
//==============================================================================
program
.version(pkg.version);
program
.command('create')
.option('--email [email]', 'Email to use')
.option('--password [password]', 'Password to use')
.option('--name [name]', 'Name to use')
.option('-f, --flag_mode', 'Source from flags instead of prompting')
.description('create a new user')
.action(createUser);
program
.command('delete <userID>')
.description('delete a user')
.action(deleteUser);
program
.command('passwd <userID>')
.description('change a password for a user')
.action(passwd);
program
.command('update <userID>')
.option('--email [email]', 'Email to use')
.option('--name [name]', 'Name to use')
.description('update a user')
.action(updateUser);
program
.command('list')
.description('list all the users in the database')
.action(listUsers);
program
.command('merge <dstUserID> <srcUserID>')
.description('merge srcUser into the dstUser')
.action(mergeUsers);
program
.command('addrole <userID> <role>')
.description('adds a role to a given user')
.action(addRole);
program
.command('removerole <userID> <role>')
.description('removes a role from a given user')
.action(removeRole);
program
.command('disable <userID>')
.description('disable a given user from logging in')
.action(disableUser);
program
.command('enable <userID>')
.description('enable a given user from logging in')
.action(enableUser);
program.parse(process.argv);
// If there is no command listed, output help.
if (!process.argv.slice(2).length) {
program.outputHelp();
}
-12
View File
@@ -1,12 +0,0 @@
const mongoose = require('../mongoose');
const Setting = require('../models/setting');
const defaults = {id: '1', moderation: 'pre'};
Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true})
.then(() => {
console.log('Created settings object.');
mongoose.disconnect();
}).catch((err) => {
console.error(`failed to create the settings object ${JSON.stringify(err)}`);
throw new Error(err); // just to be safe
});
+13 -15
View File
@@ -40,7 +40,7 @@ server.on('listening', onListening);
*/
function normalizePort(val) {
var port = parseInt(val, 10);
let port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
@@ -64,23 +64,21 @@ function onError(error) {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
let bind = typeof port === 'string'
? `Pipe ${ port}`
: `Port ${ port}`;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
console.error(`${bind} requires elevated privileges`);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
console.error(`${bind} is already in use`);
break;
default:
throw error;
}
throw error;
}
/**
@@ -88,9 +86,9 @@ function onError(error) {
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
let addr = server.address();
let bind = typeof addr === 'string'
? `pipe ${ addr}`
: `port ${ addr.port}`;
debug(`Listening on ${ bind}`);
}
+295 -23
View File
@@ -1,45 +1,317 @@
const mongoose = require('../mongoose');
const uuid = require('uuid');
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');
const UserProfileSchema = new Schema({
const SALT_ROUNDS = 10;
const UserSchema = new mongoose.Schema({
id: {
type: String,
default: uuid.v4,
unique: true
unique: true,
required: true
},
display_name: String,
auth_user_id: String
displayName: String,
disabled: Boolean,
password: String,
profiles: [{
id: {
type: String,
required: true
},
provider: {
type: String,
required: true
}
}],
roles: [String]
});
// Add the indixies on the user profile data.
UserSchema.index({
'profiles.id': 1,
'profiles.provider': 1
}, {
timestamps: {
createdAt: 'created_at',
updatedAt: 'updated_at'
}
unique: true,
background: false
});
/**
* Finds a user by the id.
* @param {String} id identifier of the user (uuid)
* toJSON overrides to remove the password field from the json
* output.
*/
UserSchema.options.toJSON = {};
UserSchema.options.toJSON.hide = 'password profiles roles disabled';
UserSchema.options.toJSON.transform = (doc, ret, options) => {
if (options.hide) {
options.hide.split(' ').forEach((prop) => {
delete ret[prop];
});
}
return ret;
};
/**
* Finds a user given their email address that we have for them in the system
* and ensures that the retuned user matches the password passed in as well.
* @param {string} email - email to look up the user by
* @param {string} password - password to match against the found user
* @param {Function} done [description]
*/
UserSchema.statics.findLocalUser = function(email, password) {
return User.findOne({
profiles: {
$elemMatch: {
id: email,
provider: 'local'
}
}
})
.then((user) => {
if (!user) {
return false;
}
return new Promise((resolve, reject) => {
bcrypt.compare(password, user.password, (err, res) => {
if (err) {
return reject(err);
}
if (!res) {
return resolve(false);
}
return resolve(user);
});
});
});
};
/**
* Merges two users together by taking all the profiles on a given user and
* pushing them into the source user followed by deleting the destination user's
* user account. This will not merge the roles associated with the source user.
* @param {String} dstUserID id of the user to which is the target of the merge
* @param {String} srcUserID id of the user to which is the source of the merge
* @return {Promise} resolves when the users are merged
*/
UserSchema.statics.mergeUsers = function(dstUserID, srcUserID) {
let srcUser, dstUser;
return Promise.all([
User.findOne({id: dstUserID}).exec(),
User.findOne({id: srcUserID}).exec()
]).then((users) => {
dstUser = users[0];
srcUser = users[1];
srcUser.profiles.forEach((profile) => {
dstUser.profiles.push(profile);
});
return srcUser.remove();
}).then(() => dstUser.save());
};
/**
* Finds a user given a social profile and if the user does not exist, creates
* them.
* @param {Object} profile - User social/external profile
* @param {Function} done [description]
*/
UserSchema.statics.findOrCreateExternalUser = function(profile) {
return User.findOne({
profiles: {
$elemMatch: {
id: profile.id,
provider: profile.provider
}
}
})
.then((user) => {
if (user) {
return user;
}
// The user was not found, lets create them!
user = new User({
displayName: profile.displayName,
roles: [],
profiles: [
{
id: profile.id,
provider: profile.provider
}
]
});
return user.save();
});
};
UserSchema.statics.changePassword = function(id, password) {
return new Promise((resolve, reject) => {
bcrypt.hash(password, SALT_ROUNDS, (err, hashedPassword) => {
if (err) {
return reject(err);
}
resolve(hashedPassword);
});
})
.then((hashedPassword) => {
return User.update({id}, {
$set: {
password: hashedPassword
}
});
});
};
/**
* Creates local users.
* @param {Array} users Users to create
* @return {Promise} Resolves with the users that were created
*/
UserSchema.statics.createLocalUsers = function(users) {
return Promise.all(users.map((user) => {
return User
.createLocalUser(user.email, user.password, user.displayName);
}));
};
/**
* Creates the local user with a given email, password, and name.
* @param {String} email email of the new user
* @param {String} password plaintext password of the new user
* @param {String} displayName name of the display user
* @param {Function} done callback
*/
UserSchema.statics.createLocalUser = function(email, password, displayName) {
if (!email) {
return Promise.reject('email is required');
}
if (!password) {
return Promise.reject('password is required');
}
if (!displayName) {
return Promise.reject('displayName is required');
}
return new Promise((resolve, reject) => {
bcrypt.hash(password, SALT_ROUNDS, (err, hashedPassword) => {
if (err) {
return reject(err);
}
let user = new User({
displayName: displayName,
password: hashedPassword,
roles: [],
profiles: [
{
id: email,
provider: 'local'
}
]
});
user.save((err) => {
if (err) {
return reject(err);
}
return resolve(user);
});
});
});
};
/**
* Disables a given user account.
* @param {String} id id of a user
* @param {Function} done callback after the operation is complete
*/
UserSchema.statics.disableUser = function(id) {
return User.update({
id: id
}, {
$set: {
disabled: true
}
});
};
/**
* Enables a given user account.
* @param {String} id id of a user
* @param {Function} done callback after the operation is complete
*/
UserSchema.statics.enableUser = function(id) {
return User.update({
id: id
}, {
$set: {
disabled: false
}
});
};
/**
* Adds a role to a user.
* @param {String} id id of a user
* @param {String} role role to add
* @param {Function} done callback after the operation is complete
*/
UserSchema.statics.addRoleToUser = function(id, role) {
return User.update({
id: id
}, {
$addToSet: {
roles: role
}
});
};
/**
* Removes a role from a user.
* @param {String} id id of a user
* @param {String} role role to remove
* @param {Function} done callback after the operation is complete
*/
UserSchema.statics.removeRoleFromUser = function(id, role) {
return User.update({
id: id
}, {
$pull: {
roles: role
}
});
};
/**
* Finds a user with the id.
* @param {String} id user id (uuid)
*/
UserProfileSchema.statics.findById = function(id) {
return UserProfile.findOne({id});
UserSchema.statics.findById = function(id) {
return User.findOne({id});
};
/**
* Finds users in an array of idd.
* @param {String} idd array of user identifiers (uuid)
* @param {Array} ids array of user identifiers (uuid)
*/
UserProfileSchema.statics.findByIdArray = function(ids) {
return UserProfile.find({
UserSchema.statics.findByIdArray = function(ids) {
return User.find({
'id': {$in: ids}
});
};
// TO DO: methods
// modifications to user as statics
// find by auth user id
const User = mongoose.model('User', UserSchema);
const UserProfile = mongoose.model('UserProfile', UserProfileSchema);
module.exports = UserProfile;
module.exports = User;
module.exports.Schema = UserSchema;
+7 -3
View File
@@ -1,4 +1,5 @@
const mongoose = require('mongoose');
const debug = require('debug')('talk:db');
const enabled = require('debug').enabled;
const url = process.env.TALK_MONGO_URL || 'mongodb://localhost';
@@ -11,11 +12,14 @@ if (enabled('talk:db')) {
try {
mongoose.connect(url, (err) => {
if (err) {throw err;}
console.log('Connected to MongoDB!');
if (err) {
throw err;
}
debug('Connected to MongoDB!');
});
} catch (err) {
console.log('Cannot stablish a connection with MongoDB');
console.error('Cannot stablish a connection with MongoDB', err);
}
module.exports = mongoose;
+6 -2
View File
@@ -7,7 +7,7 @@
"start": "./bin/www",
"build": "webpack --config webpack.config.js --bail",
"build-watch": "webpack --config webpack.config.dev.js --watch",
"lint": "eslint .",
"lint": "eslint bin/* .",
"pretest": "npm install",
"test": "mocha tests --recursive",
"test-watch": "mocha tests --recursive -w",
@@ -17,7 +17,8 @@
"pre-git": {
"commit-msg": [],
"pre-commit": [
"npm run lint"
"npm run lint",
"npm test"
],
"pre-push": [
"npm test"
@@ -43,13 +44,16 @@
},
"homepage": "https://github.com/coralproject/talk#readme",
"dependencies": {
"bcrypt": "^0.8.7",
"body-parser": "^1.15.2",
"commander": "^2.9.0",
"debug": "^2.2.0",
"ejs": "^2.5.2",
"eslint-config-postcss": "^2.0.2",
"express": "^4.14.0",
"mongoose": "^4.6.5",
"morgan": "^1.7.0",
"prompt": "^1.0.0",
"uuid": "^2.0.3"
},
"devDependencies": {
+12 -11
View File
@@ -41,11 +41,13 @@ describe('Comment: models', () => {
}];
const users = [{
id: '123',
display_name: 'Ana',
email: 'stampi@gmail.com',
displayName: 'Stampi',
password: '1Coral!'
}, {
id: '456',
display_name: 'Maria',
email: 'sockmonster@gmail.com',
displayName: 'Sockmonster',
password: '2Coral!'
}];
const actions = [{
@@ -61,13 +63,12 @@ describe('Comment: models', () => {
}];
beforeEach(() => {
return Setting.create(settings).then(() => {
return Comment.create(comments);
}).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
});
return Promise.all([
Setting.create(settings),
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
]);
});
describe('#findById()', () => {
+37 -8
View File
@@ -6,12 +6,18 @@ const expect = require('chai').expect;
describe('User: models', () => {
let mockUsers;
beforeEach(() => {
return User.create([{
display_name: 'Stampi',
return User.createLocalUsers([{
email: 'stampi@gmail.com',
displayName: 'Stampi',
password: '1Coral!'
}, {
display_name: 'Sockmonster',
email: 'sockmonster@gmail.com',
displayName: 'Sockmonster',
password: '2Coral!'
}, {
display_name: 'Marvel',
email: 'marvel@gmail.com',
displayName: 'Marvel',
password: '3Coral!'
}]).then((users) => {
mockUsers = users;
});
@@ -19,10 +25,12 @@ describe('User: models', () => {
describe('#findById()', () => {
it('should find a user by id', () => {
return User.findById(mockUsers[0].id).then((result) => {
expect(result).to.have.property('display_name')
.and.to.equal('Stampi');
});
return User
.findById(mockUsers[0].id)
.then((user) => {
expect(user).to.have.property('displayName')
.and.to.equal('Stampi');
});
});
});
@@ -34,4 +42,25 @@ describe('User: models', () => {
});
});
});
describe('#findLocalUser', () => {
it('should find a user when we give the right credentials', () => {
return User
.findLocalUser(mockUsers[0].profiles[0].id, '1Coral!')
.then((user) => {
expect(user).to.have.property('displayName')
.and.to.equal(mockUsers[0].displayName);
});
});
it('should not find the user when we give the wrong credentials', () => {
return User
.findLocalUser(mockUsers[0].profiles[0].id, '1Coral!<nope>')
.then((user) => {
expect(user).to.equal(false);
});
});
});
});
+107 -89
View File
@@ -39,11 +39,13 @@ describe('Get /comments', () => {
}];
const users = [{
id: '123',
display_name: 'Ana',
displayName: 'Ana',
email: 'ana@gmail.com',
password: '123'
}, {
id: '456',
display_name: 'Maria',
displayName: 'Maria',
email: 'maria@gmail.com',
password: '123'
}];
const actions = [{
@@ -55,11 +57,11 @@ describe('Get /comments', () => {
}];
beforeEach(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
});
return Promise.all([
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
]);
});
it('should return all the comments', function(done){
@@ -93,11 +95,13 @@ describe('Get moderation queues rejected, pending, flags', () => {
}];
const users = [{
id: '123',
display_name: 'Ana',
displayName: 'Ana',
email: 'ana@gmail.com',
password: '123'
}, {
id: '456',
display_name: 'Maria',
displayName: 'Maria',
email: 'maria@gmail.com',
password: '123'
}];
const actions = [{
@@ -111,11 +115,11 @@ describe('Get moderation queues rejected, pending, flags', () => {
}];
beforeEach(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
});
return Promise.all([
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
]);
});
it('should return all the rejected comments', function(done){
@@ -179,11 +183,13 @@ describe('Get moderation queues rejected, pending, flags', () => {
describe('Post /comments', () => {
const users = [{
id: '123',
display_name: 'Ana',
displayName: 'Ana',
email: 'ana@gmail.com',
password: '123'
}, {
id: '456',
display_name: 'Maria',
displayName: 'Maria',
email: 'maria@gmail.com',
password: '123'
}];
const actions = [{
@@ -195,9 +201,10 @@ describe('Post /comments', () => {
}];
beforeEach(() => {
return User.create(users).then(() => {
return Action.create(actions);
});
return Promise.all([
User.createLocalUsers(users),
Action.create(actions)
]);
});
it('it should create a comment', function(done) {
@@ -230,11 +237,13 @@ describe('Get /:comment_id', () => {
}];
const users = [{
id: '123',
display_name: 'Ana',
displayName: 'Ana',
email: 'ana@gmail.com',
password: '123'
}, {
id: '456',
display_name: 'Maria',
displayName: 'Maria',
email: 'maria@gmail.com',
password: '123'
}];
const actions = [{
@@ -248,11 +257,11 @@ describe('Get /:comment_id', () => {
}];
beforeEach(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
});
return Promise.all([
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
]);
});
it('should return the right comment for the comment_id', function(done){
@@ -287,11 +296,13 @@ describe('Put /:comment_id', () => {
}];
const users = [{
id: '123',
display_name: 'Ana',
displayName: 'Ana',
email: 'ana@gmail.com',
password: '123'
}, {
id: '456',
display_name: 'Maria',
displayName: 'Maria',
email: 'maria@gmail.com',
password: '123'
}];
const actions = [{
@@ -303,11 +314,11 @@ describe('Put /:comment_id', () => {
}];
beforeEach(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
});
return Promise.all([
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
]);
});
it('it should update comment', function(done) {
@@ -342,11 +353,13 @@ describe('Remove /:comment_id', () => {
}];
const users = [{
id: '123',
display_name: 'Ana',
displayName: 'Ana',
email: 'ana@gmail.com',
password: '123'
}, {
id: '456',
display_name: 'Maria',
displayName: 'Maria',
email: 'maria@gmail.com',
password: '123'
}];
const actions = [{
@@ -358,27 +371,32 @@ describe('Remove /:comment_id', () => {
}];
beforeEach(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
});
return Promise.all([
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
]);
});
it('it should remove comment', function(done) {
chai.request(app)
it('it should remove comment', () => {
return chai.request(app)
.delete('/api/v1/comments/abc')
.end(function(err, res){
expect(err).to.be.null;
.then((res) => {
expect(res).to.have.status(201);
Comment.findById('abc').then((comment) => {
expect(comment).to.be.empty;
});
done();
return Comment.findById('abc');
})
.then((comment) => {
expect(comment).to.be.null;
});
});
});
process.on('unhandledRejection', (reason) => {
console.error('Reason: ');
console.error(reason);
});
describe('Post /:comment_id/status', () => {
const comments = [{
@@ -401,11 +419,13 @@ describe('Post /:comment_id/status', () => {
}];
const users = [{
id: '123',
display_name: 'Ana',
displayName: 'Ana',
email: 'ana@gmail.com',
password: '123'
}, {
id: '456',
display_name: 'Maria',
displayName: 'Maria',
email: 'maria@gmail.com',
password: '123'
}];
const actions = [{
@@ -417,23 +437,21 @@ describe('Post /:comment_id/status', () => {
}];
beforeEach(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
});
return Promise.all([
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
]);
});
it('it should update status', function(done) {
chai.request(app)
it('it should update status', function() {
return chai.request(app)
.post('/api/v1/comments/abc/status')
.send({'status': 'accepted'})
.end(function(err, res){
expect(err).to.be.null;
.send({status: 'accepted'})
.then((res) => {
expect(res).to.have.status(200);
expect(res).to.have.body;
expect(res.body).to.have.property('status', 'accepted');
done();
});
});
});
@@ -460,11 +478,13 @@ describe('Post /:comment_id/actions', () => {
}];
const users = [{
id: '123',
display_name: 'Ana',
displayName: 'Ana',
email: 'ana@gmail.com',
password: '123'
}, {
id: '456',
display_name: 'Maria',
displayName: 'Maria',
email: 'maria@gmail.com',
password: '123'
}];
const actions = [{
@@ -476,26 +496,24 @@ describe('Post /:comment_id/actions', () => {
}];
beforeEach(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
});
return Promise.all([
Comment.create(comments),
User.createLocalUsers(users),
Action.create(actions)
]);
});
it('it should update actions', function(done) {
chai.request(app)
it('it should update actions', () => {
return chai.request(app)
.post('/api/v1/comments/abc/actions')
.send({'user_id': '456', 'action_type': 'flag'})
.end(function(err, res){
expect(err).to.be.null;
.then((res) => {
expect(res).to.have.status(200);
expect(res).to.have.body;
expect(res.body).to.have.property('item_type', 'comment');
expect(res.body).to.have.property('action_type', 'flag');
expect(res.body).to.have.property('item_id', 'abc');
expect(res.body).to.have.property('user_id', '456');
done();
});
});
});
+24 -15
View File
@@ -47,11 +47,13 @@ describe('api/stream: routes', () => {
}];
const users = [{
id: '123',
display_name: 'John',
displayName: 'Ana',
email: 'ana@gmail.com',
password: '123'
}, {
id: '456',
display_name: 'Paul',
displayName: 'Maria',
email: 'maria@gmail.com',
password: '123'
}];
const actions = [{
@@ -63,24 +65,31 @@ describe('api/stream: routes', () => {
}];
beforeEach(() => {
return Setting.create(settings).then(() => {
return Comment.create(comments).then(() => {
return User.create(users);
}).then(() => {
return Action.create(actions);
return User
.createLocalUsers(users)
.then(users => {
comments[0].author_id = users[0].id;
comments[1].author_id = users[1].id;
return Promise.all([
Comment.create(comments),
Action.create(actions),
Setting.create(settings)
]);
});
});
});
it('should return a stream with comments, users and actions', function(done){
chai.request(app)
it('should return a stream with comments, users and actions', () => {
return chai.request(app)
.get('/api/v1/stream')
.query({'asset_id': 'asset'})
.end(function(err, res){
expect(err).to.be.null;
.then(res => {
expect(res).to.have.status(200);
expect(res.body.length).to.equal(3);
done();
});
});
});