| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var	assert = require('assert'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | var request = require('request'); | 
					
						
							| 
									
										
										
										
											2017-05-23 22:47:40 -04:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | var db = require('./mocks/databasemock'); | 
					
						
							| 
									
										
										
										
											2016-10-16 21:51:42 +03:00
										 |  |  | var user = require('../src/user'); | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | var meta = require('../src/meta'); | 
					
						
							| 
									
										
										
										
											2017-05-27 23:32:55 -04:00
										 |  |  | var helpers = require('./helpers'); | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('authentication', function () { | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 	function loginUser(username, password, callback) { | 
					
						
							|  |  |  | 		var jar = request.jar(); | 
					
						
							|  |  |  | 		request({ | 
					
						
							|  |  |  | 			url: nconf.get('url') + '/api/config', | 
					
						
							|  |  |  | 			json: true, | 
					
						
							|  |  |  | 			jar: jar, | 
					
						
							|  |  |  | 		}, function (err, response, body) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			request.post(nconf.get('url') + '/login', { | 
					
						
							|  |  |  | 				form: { | 
					
						
							|  |  |  | 					username: username, | 
					
						
							|  |  |  | 					password: password, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 				jar: jar, | 
					
						
							|  |  |  | 				headers: { | 
					
						
							|  |  |  | 					'x-csrf-token': body.csrf_token, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, function (err, response, body) { | 
					
						
							|  |  |  | 				callback(err, response, body, jar); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 17:37:16 -04:00
										 |  |  | 	function registerUser(email, username, password, callback) { | 
					
						
							|  |  |  | 		var jar = request.jar(); | 
					
						
							|  |  |  | 		request({ | 
					
						
							|  |  |  | 			url: nconf.get('url') + '/api/config', | 
					
						
							|  |  |  | 			json: true, | 
					
						
							|  |  |  | 			jar: jar, | 
					
						
							|  |  |  | 		}, function (err, response, body) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			request.post(nconf.get('url') + '/register', { | 
					
						
							|  |  |  | 				form: { | 
					
						
							|  |  |  | 					email: email, | 
					
						
							|  |  |  | 					username: username, | 
					
						
							|  |  |  | 					password: password, | 
					
						
							| 
									
										
										
										
											2017-07-20 08:51:04 -04:00
										 |  |  | 					'password-confirm': password, | 
					
						
							| 
									
										
										
										
											2017-05-23 17:37:16 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 				jar: jar, | 
					
						
							|  |  |  | 				headers: { | 
					
						
							|  |  |  | 					'x-csrf-token': body.csrf_token, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, function (err, response, body) { | 
					
						
							|  |  |  | 				callback(err, response, body, jar); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 	var jar = request.jar(); | 
					
						
							| 
									
										
										
										
											2016-12-15 14:47:42 +03:00
										 |  |  | 	var regularUid; | 
					
						
							| 
									
										
										
										
											2016-10-16 17:42:14 +03:00
										 |  |  | 	before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		user.create({ username: 'regular', password: 'regularpwd', email: 'regular@nodebb.org' }, function (err, uid) { | 
					
						
							| 
									
										
										
										
											2016-10-16 17:42:14 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-12-15 14:47:42 +03:00
										 |  |  | 			regularUid = uid; | 
					
						
							| 
									
										
										
										
											2016-10-16 17:36:24 +03:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 18:40:34 -04:00
										 |  |  | 	it('should fail to create user if username is too short', function (done) { | 
					
						
							|  |  |  | 		helpers.registerUser({ | 
					
						
							|  |  |  | 			username: 'a', | 
					
						
							|  |  |  | 			password: '123456', | 
					
						
							|  |  |  | 			'password-confirm': '123456', | 
					
						
							|  |  |  | 			email: 'should@error1.com', | 
					
						
							|  |  |  | 		}, function (err, jar, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 400); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:username-too-short]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to create user if userslug is too short', function (done) { | 
					
						
							|  |  |  | 		helpers.registerUser({ | 
					
						
							|  |  |  | 			username: '----a-----', | 
					
						
							|  |  |  | 			password: '123456', | 
					
						
							|  |  |  | 			'password-confirm': '123456', | 
					
						
							|  |  |  | 			email: 'should@error2.com', | 
					
						
							|  |  |  | 		}, function (err, jar, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 400); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:username-too-short]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to create user if userslug is too short', function (done) { | 
					
						
							|  |  |  | 		helpers.registerUser({ | 
					
						
							|  |  |  | 			username: '     a', | 
					
						
							|  |  |  | 			password: '123456', | 
					
						
							|  |  |  | 			'password-confirm': '123456', | 
					
						
							|  |  |  | 			email: 'should@error3.com', | 
					
						
							|  |  |  | 		}, function (err, jar, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 400); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:username-too-short]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to create user if userslug is too short', function (done) { | 
					
						
							|  |  |  | 		helpers.registerUser({ | 
					
						
							|  |  |  | 			username: 'a      ', | 
					
						
							|  |  |  | 			password: '123456', | 
					
						
							|  |  |  | 			'password-confirm': '123456', | 
					
						
							|  |  |  | 			email: 'should@error4.com', | 
					
						
							|  |  |  | 		}, function (err, jar, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 400); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:username-too-short]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 	it('should register and login a user', function (done) { | 
					
						
							|  |  |  | 		request({ | 
					
						
							|  |  |  | 			url: nconf.get('url') + '/api/config', | 
					
						
							|  |  |  | 			json: true, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			jar: jar, | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 		}, function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			request.post(nconf.get('url') + '/register', { | 
					
						
							|  |  |  | 				form: { | 
					
						
							|  |  |  | 					email: 'admin@nodebb.org', | 
					
						
							|  |  |  | 					username: 'admin', | 
					
						
							|  |  |  | 					password: 'adminpwd', | 
					
						
							| 
									
										
										
										
											2017-07-20 08:51:04 -04:00
										 |  |  | 					'password-confirm': 'adminpwd', | 
					
						
							| 
									
										
										
										
											2017-05-23 22:09:25 -04:00
										 |  |  | 					userLang: 'it', | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 				jar: jar, | 
					
						
							|  |  |  | 				headers: { | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					'x-csrf-token': body.csrf_token, | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 			}, function (err, response, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				request({ | 
					
						
							|  |  |  | 					url: nconf.get('url') + '/api/me', | 
					
						
							|  |  |  | 					json: true, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					jar: jar, | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 				}, function (err, response, body) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(body); | 
					
						
							|  |  |  | 					assert.equal(body.username, 'admin'); | 
					
						
							|  |  |  | 					assert.equal(body.email, 'admin@nodebb.org'); | 
					
						
							| 
									
										
										
										
											2017-05-23 22:09:25 -04:00
										 |  |  | 					user.getSettings(body.uid, function (err, settings) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert.equal(settings.userLang, 'it'); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should logout a user', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-27 23:32:55 -04:00
										 |  |  | 		helpers.logoutUser(jar, function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-05-27 23:32:55 -04:00
										 |  |  | 			request({ | 
					
						
							|  |  |  | 				url: nconf.get('url') + '/api/me', | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 				json: true, | 
					
						
							|  |  |  | 				jar: jar, | 
					
						
							| 
									
										
										
										
											2017-11-16 15:38:26 -07:00
										 |  |  | 			}, function (err, res, body) { | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-11-16 15:38:26 -07:00
										 |  |  | 				assert.equal(res.statusCode, 401); | 
					
						
							| 
									
										
										
										
											2017-05-27 23:32:55 -04:00
										 |  |  | 				assert.equal(body, 'not-authorized'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should login a user', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 		loginUser('regular', 'regularpwd', function (err, response, body, jar) { | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 			assert(body); | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 			request({ | 
					
						
							|  |  |  | 				url: nconf.get('url') + '/api/me', | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 				json: true, | 
					
						
							|  |  |  | 				jar: jar, | 
					
						
							|  |  |  | 			}, function (err, response, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 				assert.equal(body.username, 'regular'); | 
					
						
							|  |  |  | 				assert.equal(body.email, 'regular@nodebb.org'); | 
					
						
							|  |  |  | 				db.getObject('uid:' + regularUid + ':sessionUUID:sessionId', function (err, sessions) { | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 					assert(sessions); | 
					
						
							|  |  |  | 					assert(Object.keys(sessions).length > 0); | 
					
						
							|  |  |  | 					done(); | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 14:47:42 +03:00
										 |  |  | 	it('should revoke all sessions', function (done) { | 
					
						
							|  |  |  | 		var socketAdmin = require('../src/socket.io/admin'); | 
					
						
							|  |  |  | 		db.sortedSetCard('uid:' + regularUid + ':sessions', function (err, count) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(count); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.deleteAllSessions({ uid: 1 }, {}, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 14:47:42 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				db.sortedSetCard('uid:' + regularUid + ':sessions', function (err, count) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!count); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 14:24:13 -05:00
										 |  |  | 	it('should fail to login if ip address if invalid', function (done) { | 
					
						
							|  |  |  | 		var jar = request.jar(); | 
					
						
							|  |  |  | 		request({ | 
					
						
							|  |  |  | 			url: nconf.get('url') + '/api/config', | 
					
						
							|  |  |  | 			json: true, | 
					
						
							|  |  |  | 			jar: jar, | 
					
						
							|  |  |  | 		}, function (err, response, body) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return done(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			request.post(nconf.get('url') + '/login', { | 
					
						
							|  |  |  | 				form: { | 
					
						
							|  |  |  | 					username: 'regular', | 
					
						
							|  |  |  | 					password: 'regularpwd', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 				jar: jar, | 
					
						
							|  |  |  | 				headers: { | 
					
						
							|  |  |  | 					'x-csrf-token': body.csrf_token, | 
					
						
							|  |  |  | 					'x-forwarded-for': '<script>alert("xss")</script>', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, function (err, response, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 500); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-19 16:18:18 -04:00
										 |  |  | 	it('should fail to login if user does not exist', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 		loginUser('doesnotexist', 'nopassword', function (err, response, body) { | 
					
						
							| 
									
										
										
										
											2017-05-19 16:18:18 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 			assert.equal(response.statusCode, 403); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:invalid-login-credentials]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-05-19 16:18:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 	it('should fail to login if username is empty', function (done) { | 
					
						
							|  |  |  | 		loginUser('', 'some password', function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 403); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:invalid-username-or-password]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							| 
									
										
										
										
											2017-05-19 16:18:18 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 15:37:32 -04:00
										 |  |  | 	it('should fail to login if password is empty', function (done) { | 
					
						
							|  |  |  | 		loginUser('someuser', '', function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 403); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:invalid-username-or-password]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to login if username and password are empty', function (done) { | 
					
						
							|  |  |  | 		loginUser('', '', function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 403); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:invalid-username-or-password]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to login if password is longer than 4096', function (done) { | 
					
						
							|  |  |  | 		var longPassword; | 
					
						
							|  |  |  | 		for (var i = 0; i < 5000; i++) { | 
					
						
							|  |  |  | 			longPassword += 'a'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		loginUser('someuser', longPassword, function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 403); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:password-too-long]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to login if local login is disabled', function (done) { | 
					
						
							|  |  |  | 		meta.config.allowLocalLogin = 0; | 
					
						
							|  |  |  | 		loginUser('someuser', 'somepass', function (err, response, body) { | 
					
						
							|  |  |  | 			meta.config.allowLocalLogin = 1; | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 403); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:local-login-disabled]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 17:37:16 -04:00
										 |  |  | 	it('should fail to register if registraton is disabled', function (done) { | 
					
						
							|  |  |  | 		meta.config.registrationType = 'disabled'; | 
					
						
							|  |  |  | 		registerUser('some@user.com', 'someuser', 'somepassword', function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 403); | 
					
						
							|  |  |  | 			assert.equal(body, 'Forbidden'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should return error if invitation is not valid', function (done) { | 
					
						
							|  |  |  | 		meta.config.registrationType = 'invite-only'; | 
					
						
							|  |  |  | 		registerUser('some@user.com', 'someuser', 'somepassword', function (err, response, body) { | 
					
						
							|  |  |  | 			meta.config.registrationType = 'normal'; | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 400); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to register if email is falsy', function (done) { | 
					
						
							|  |  |  | 		registerUser('', 'someuser', 'somepassword', function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 400); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:invalid-email]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to register if username is falsy or too short', function (done) { | 
					
						
							|  |  |  | 		registerUser('some@user.com', '', 'somepassword', function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 400); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:username-too-short]]'); | 
					
						
							|  |  |  | 			registerUser('some@user.com', 'a', 'somepassword', function (err, response, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 400); | 
					
						
							|  |  |  | 				assert.equal(body, '[[error:username-too-short]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to register if username is too long', function (done) { | 
					
						
							|  |  |  | 		registerUser('some@user.com', 'thisisareallylongusername', '123456', function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 400); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:username-too-long]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 22:09:25 -04:00
										 |  |  | 	it('should queue user if ip is used before', function (done) { | 
					
						
							|  |  |  | 		meta.config.registrationType = 'admin-approval-ip'; | 
					
						
							|  |  |  | 		registerUser('another@user.com', 'anotheruser', 'anotherpwd', function (err, response, body) { | 
					
						
							|  |  |  | 			meta.config.registrationType = 'normal'; | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 			assert.equal(body.message, '[[register:registration-added-to-queue]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should be able to login with email', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-27 23:32:55 -04:00
										 |  |  | 		user.create({ username: 'ginger', password: '123456', email: 'ginger@nodebb.org' }, function (err) { | 
					
						
							| 
									
										
										
										
											2017-05-23 22:09:25 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-05-27 23:32:55 -04:00
										 |  |  | 			loginUser('ginger@nodebb.org', '123456', function (err, response) { | 
					
						
							| 
									
										
										
										
											2017-05-23 22:09:25 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should fail to login if login type is username and an email is sent', function (done) { | 
					
						
							|  |  |  | 		meta.config.allowLoginWith = 'username'; | 
					
						
							|  |  |  | 		loginUser('ginger@nodebb.org', '123456', function (err, response, body) { | 
					
						
							|  |  |  | 			meta.config.allowLoginWith = 'username-email'; | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 500); | 
					
						
							|  |  |  | 			assert.equal(body, '[[error:wrong-login-type-username]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should send 200 if not logged in', function (done) { | 
					
						
							|  |  |  | 		var jar = request.jar(); | 
					
						
							|  |  |  | 		request({ | 
					
						
							|  |  |  | 			url: nconf.get('url') + '/api/config', | 
					
						
							|  |  |  | 			json: true, | 
					
						
							|  |  |  | 			jar: jar, | 
					
						
							|  |  |  | 		}, function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			request.post(nconf.get('url') + '/logout', { | 
					
						
							|  |  |  | 				form: {}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 				jar: jar, | 
					
						
							|  |  |  | 				headers: { | 
					
						
							|  |  |  | 					'x-csrf-token': body.csrf_token, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, function (err, res, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				assert.equal(body, 'not-logged-in'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should prevent banned user from logging in', function (done) { | 
					
						
							|  |  |  | 		user.create({ username: 'banme', password: '123456', email: 'ban@me.com' }, function (err, uid) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			user.ban(uid, 0, 'spammer', function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				loginUser('banme', '123456', function (err, res, body) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(res.statusCode, 403); | 
					
						
							|  |  |  | 					assert.equal(body, '[[error:user-banned-reason, spammer]]'); | 
					
						
							|  |  |  | 					user.unban(uid, function (err) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						var expiry = Date.now() + 10000; | 
					
						
							|  |  |  | 						user.ban(uid, expiry, '', function (err) { | 
					
						
							|  |  |  | 							assert.ifError(err); | 
					
						
							|  |  |  | 							loginUser('banme', '123456', function (err, res, body) { | 
					
						
							|  |  |  | 								assert.ifError(err); | 
					
						
							|  |  |  | 								assert.equal(res.statusCode, 403); | 
					
						
							|  |  |  | 								assert.equal(body, '[[error:user-banned-reason-until, ' + (new Date(parseInt(expiry, 10)).toString()) + ', No reason given.]]'); | 
					
						
							|  |  |  | 								done(); | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-23 22:47:40 -04:00
										 |  |  | 	it('should lockout account on 3 failed login attempts', function (done) { | 
					
						
							|  |  |  | 		meta.config.loginAttempts = 3; | 
					
						
							|  |  |  | 		var uid; | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				user.create({ username: 'lockme', password: '123456' }, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (_uid, next) { | 
					
						
							|  |  |  | 				uid = _uid; | 
					
						
							|  |  |  | 				loginUser('lockme', 'abcdef', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (res, body, jar, next) { | 
					
						
							|  |  |  | 				loginUser('lockme', 'abcdef', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (res, body, jar, next) { | 
					
						
							|  |  |  | 				loginUser('lockme', 'abcdef', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (res, body, jar, next) { | 
					
						
							|  |  |  | 				loginUser('lockme', 'abcdef', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (res, body, jar, next) { | 
					
						
							|  |  |  | 				meta.config.loginAttempts = 5; | 
					
						
							| 
									
										
										
										
											2017-05-23 23:03:40 -04:00
										 |  |  | 				assert.equal(res.statusCode, 403); | 
					
						
							|  |  |  | 				assert.equal(body, '[[error:account-locked]]'); | 
					
						
							|  |  |  | 				loginUser('lockme', 'abcdef', next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (res, body, jar, next) { | 
					
						
							| 
									
										
										
										
											2017-05-23 22:47:40 -04:00
										 |  |  | 				assert.equal(res.statusCode, 403); | 
					
						
							|  |  |  | 				assert.equal(body, '[[error:account-locked]]'); | 
					
						
							|  |  |  | 				db.exists('lockout:' + uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (locked, next) { | 
					
						
							|  |  |  | 				assert(locked); | 
					
						
							|  |  |  | 				next(); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		], done); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-16 16:43:38 +03:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 |