| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | var winston = require('winston'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | process.on('uncaughtException', function (err) { | 
					
						
							|  |  |  | 	winston.error('Encountered error while running test suite: ' + err.message); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var	assert = require('assert'), | 
					
						
							| 
									
										
										
										
											2014-10-31 13:22:42 -04:00
										 |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2014-09-24 13:52:49 -04:00
										 |  |  | 	db = require('./mocks/databasemock'); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 13:22:42 -04:00
										 |  |  | var User = require('../src/user'), | 
					
						
							|  |  |  | 	Topics = require('../src/topics'), | 
					
						
							|  |  |  | 	Categories = require('../src/categories'), | 
					
						
							|  |  |  | 	Meta = require('../src/meta'); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('User', function() { | 
					
						
							| 
									
										
										
										
											2014-10-31 13:22:42 -04:00
										 |  |  | 	var	userData, | 
					
						
							|  |  |  | 		testUid, | 
					
						
							|  |  |  | 		testCid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	before(function(done) { | 
					
						
							|  |  |  | 		Categories.create({ | 
					
						
							|  |  |  | 			name: 'Test Category', | 
					
						
							|  |  |  | 			description: 'A test', | 
					
						
							|  |  |  | 			order: 1 | 
					
						
							|  |  |  | 		}, function(err, categoryObj) { | 
					
						
							|  |  |  | 			testCid = categoryObj.cid; | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	beforeEach(function(){ | 
					
						
							|  |  |  | 		userData = { | 
					
						
							| 
									
										
										
										
											2014-03-06 14:51:43 -05:00
										 |  |  | 			username: 'John Smith', | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 			password: 'swordfish', | 
					
						
							|  |  |  | 			email: 'john@example.com', | 
					
						
							|  |  |  | 			callback: undefined | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 13:22:42 -04:00
										 |  |  | 	describe('.create(), when created', function() { | 
					
						
							| 
									
										
										
										
											2014-03-06 14:51:43 -05:00
										 |  |  | 		it('should be created properly', function(done) { | 
					
						
							|  |  |  | 			User.create({username: userData.username, password: userData.password, email: userData.email}, function(error,userId){ | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 				assert.equal(error, null, 'was created with error'); | 
					
						
							|  |  |  | 				assert.ok(userId); | 
					
						
							| 
									
										
										
										
											2014-10-31 13:22:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				testUid = userId; | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 12:41:35 -05:00
										 |  |  | 		it('should have a valid email, if using an email', function() { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 			assert.throws( | 
					
						
							| 
									
										
										
										
											2014-03-06 14:51:43 -05:00
										 |  |  | 				User.create({username: userData.username, password: userData.password, email: 'fakeMail'},function(){}), | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 				Error, | 
					
						
							|  |  |  | 				'does not validate email' | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 15:33:23 -05:00
										 |  |  | 	describe('.isModerator()', function() { | 
					
						
							| 
									
										
										
										
											2014-12-02 15:34:34 -05:00
										 |  |  | 		it('should return false', function(done) { | 
					
						
							| 
									
										
										
										
											2014-12-02 15:33:23 -05:00
										 |  |  | 			User.isModerator(testUid, testCid, function(err, isModerator) { | 
					
						
							|  |  |  | 				assert.equal(isModerator, false); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 15:34:34 -05:00
										 |  |  | 		it('should return two false results', function(done) { | 
					
						
							| 
									
										
										
										
											2014-12-02 15:33:23 -05:00
										 |  |  | 			User.isModerator([testUid, testUid], testCid, function(err, isModerator) { | 
					
						
							|  |  |  | 				assert.equal(isModerator[0], false); | 
					
						
							|  |  |  | 				assert.equal(isModerator[1], false); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 15:34:34 -05:00
										 |  |  | 		it('should return two false results', function(done) { | 
					
						
							| 
									
										
										
										
											2014-12-02 15:33:23 -05:00
										 |  |  | 			User.isModerator(testUid, [testCid, testCid], function(err, isModerator) { | 
					
						
							|  |  |  | 				assert.equal(isModerator[0], false); | 
					
						
							|  |  |  | 				assert.equal(isModerator[1], false); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-31 13:22:42 -04:00
										 |  |  | 	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(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-31 17:20:28 -05:00
										 |  |  | 	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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 	after(function() { | 
					
						
							| 
									
										
										
										
											2013-12-06 13:21:21 -05:00
										 |  |  | 		db.flushdb(); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:32:31 +02:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-10-08 12:32:17 -04:00
										 |  |  | }); |