You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/tests/user.js

177 lines
4.3 KiB
JavaScript

'use strict';
/*global require, process, before, beforeEach, after*/
11 years ago
var winston = require('winston');
process.on('uncaughtException', function (err) {
winston.error('Encountered error while running test suite: ' + err.message);
});
var assert = require('assert'),
async = require('async'),
db = require('./mocks/databasemock');
11 years ago
var User = require('../src/user'),
Topics = require('../src/topics'),
Categories = require('../src/categories'),
Meta = require('../src/meta');
11 years ago
describe('User', function() {
var userData,
testUid,
testCid;
before(function(done) {
Categories.create({
name: 'Test Category',
description: 'A test',
order: 1
}, function(err, categoryObj) {
testCid = categoryObj.cid;
done();
});
});
11 years ago
beforeEach(function(){
userData = {
11 years ago
username: 'John Smith',
11 years ago
password: 'swordfish',
email: '[email protected]',
callback: undefined
};
});
describe('.create(), when created', function() {
11 years ago
it('should be created properly', function(done) {
User.create({username: userData.username, password: userData.password, email: userData.email}, function(error,userId){
11 years ago
assert.equal(error, null, 'was created with error');
assert.ok(userId);
testUid = userId;
11 years ago
done();
});
});
it('should have a valid email, if using an email', function() {
11 years ago
assert.throws(
11 years ago
User.create({username: userData.username, password: userData.password, email: 'fakeMail'},function(){}),
11 years ago
Error,
'does not validate email'
);
});
});
describe('.isModerator()', function() {
it('should return false', function(done) {
User.isModerator(testUid, testCid, function(err, isModerator) {
assert.equal(isModerator, false);
done();
});
});
it('should return two false results', function(done) {
User.isModerator([testUid, testUid], testCid, function(err, isModerator) {
assert.equal(isModerator[0], false);
assert.equal(isModerator[1], false);
done();
});
});
it('should return two false results', function(done) {
User.isModerator(testUid, [testCid, testCid], function(err, isModerator) {
assert.equal(isModerator[0], false);
assert.equal(isModerator[1], false);
done();
});
});
});
describe('.isReadyToPost()', function() {
it('should error when a user makes two posts in quick succession', function(done) {
Meta.config = Meta.config || {};
Meta.config.postDelay = '10';
async.series([
async.apply(Topics.post, {
uid: testUid,
title: 'Topic 1',
content: 'lorem ipsum',
cid: testCid
}),
async.apply(Topics.post, {
uid: testUid,
title: 'Topic 2',
content: 'lorem ipsum',
cid: testCid
})
], function(err) {
assert(err);
done();
});
});
it('should allow a post if the last post time is > 10 seconds', function(done) {
User.setUserField(testUid, 'lastposttime', +new Date()-(11*1000), function() {
Topics.post({
uid: testUid,
title: 'Topic 3',
content: 'lorem ipsum',
cid: testCid
}, function(err) {
assert.ifError(err);
done();
});
});
});
it('should error when a new user posts if the last post time is 10 < 30 seconds', function(done) {
Meta.config.newbiePostDelay = 30;
Meta.config.newbiePostDelayThreshold = 3;
User.setUserField(testUid, 'lastposttime', +new Date()-(20*1000), function() {
Topics.post({
uid: testUid,
title: 'Topic 4',
content: 'lorem ipsum',
cid: testCid
}, function(err) {
assert(err);
done();
});
});
});
it('should not error if a non-newbie user posts if the last post time is 10 < 30 seconds', function(done) {
User.setUserFields(testUid, {
lastposttime: +new Date()-(20*1000),
reputation: 10
}, function() {
Topics.post({
uid: testUid,
title: 'Topic 5',
content: 'lorem ipsum',
cid: testCid
}, function(err) {
assert.ifError(err);
done();
});
});
});
});
10 years ago
describe('.search()', function() {
it('should return an object containing an array of matching users', function(done) {
User.search({query: 'john'}, function(err, searchData) {
assert.ifError(err);
assert.equal(Array.isArray(searchData.users) && searchData.users.length > 0, true);
assert.equal(searchData.users[0].username, 'John Smith');
done();
});
});
});
11 years ago
after(function() {
db.flushdb();
11 years ago
});
});