diff --git a/src/controllers/accounts/profile.js b/src/controllers/accounts/profile.js index 450ee92f60..e067ed3ef5 100644 --- a/src/controllers/accounts/profile.js +++ b/src/controllers/accounts/profile.js @@ -14,7 +14,7 @@ var pagination = require('../../pagination'); var messaging = require('../../messaging'); var translator = require('../../translator'); -var profileController = {}; +var profileController = module.exports; profileController.get = function (req, res, callback) { var lowercaseSlug = req.params.userslug.toLowerCase(); @@ -42,6 +42,7 @@ profileController.get = function (req, res, callback) { userData = _userData; req.session.uids_viewed = req.session.uids_viewed || {}; + if (req.uid !== parseInt(userData.uid, 10) && (!req.session.uids_viewed[userData.uid] || req.session.uids_viewed[userData.uid] < Date.now() - 3600000)) { user.incrementUserFieldBy(userData.uid, 'profileviews', 1); req.session.uids_viewed[userData.uid] = Date.now(); @@ -127,12 +128,9 @@ profileController.get = function (req, res, callback) { plugins.fireHook('filter:user.account', { userData: userData, uid: req.uid }, next); }, - ], function (err, results) { - if (err) { - return callback(err); - } - res.render('account/profile', results.userData); - }); + function (results) { + res.render('account/profile', results.userData); + }, + ], callback); }; -module.exports = profileController; diff --git a/test/controllers.js b/test/controllers.js index 0749c02456..4ae64e612d 100644 --- a/test/controllers.js +++ b/test/controllers.js @@ -8,6 +8,7 @@ var request = require('request'); var db = require('./mocks/databasemock'); var categories = require('../src/categories'); var topics = require('../src/topics'); +var posts = require('../src/posts'); var user = require('../src/user'); var groups = require('../src/groups'); var meta = require('../src/meta'); @@ -1031,6 +1032,151 @@ describe('Controllers', function () { }); }); }); + + it('should load correct user', function (done) { + request(nconf.get('url') + '/api/user/FOO', { jar: jar, json: true }, function (err, res) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + done(); + }); + }); + + it('should redirect', function (done) { + request(nconf.get('url') + '/user/FOO', { jar: jar }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body); + done(); + }); + }); + + it('should 404 if user does not exist', function (done) { + request(nconf.get('url') + '/api/user/doesnotexist', { jar: jar }, function (err, res) { + assert.ifError(err); + assert.equal(res.statusCode, 404); + done(); + }); + }); + + it('should increase profile view', function (done) { + request(nconf.get('url') + '/api/user/foo', { }, function (err, res) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + user.getUserField(fooUid, 'profileviews', function (err, viewcount) { + assert.ifError(err); + assert(viewcount > 0); + done(); + }); + }); + }); + + it('should parse about me', function (done) { + user.setUserFields(fooUid, { picture: '/path/to/picture', aboutme: 'hi i am a bot' }, function (err) { + assert.ifError(err); + request(nconf.get('url') + '/api/user/foo', { json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert.equal(body.aboutme, 'hi i am a bot'); + assert.equal(body.picture, '/path/to/picture'); + done(); + }); + }); + }); + + it('should not return reputation if reputation is disabled', function (done) { + meta.config['reputation:disabled'] = 1; + request(nconf.get('url') + '/api/user/foo', { json: true }, function (err, res, body) { + meta.config['reputation:disabled'] = 0; + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(!body.hasOwnProperty('reputation')); + done(); + }); + }); + + it('should only return posts that are not deleted', function (done) { + var topicData; + var pidToDelete; + async.waterfall([ + function (next) { + topics.post({ uid: fooUid, title: 'visible', content: 'some content', cid: cid }, next); + }, + function (data, next) { + topicData = data.topicData; + topics.reply({ uid: fooUid, content: '1st reply', tid: topicData.tid }, next); + }, + function (postData, next) { + pidToDelete = postData.pid; + topics.reply({ uid: fooUid, content: '2nd reply', tid: topicData.tid }, next); + }, + function (postData, next) { + posts.delete(pidToDelete, fooUid, next); + }, + function (next) { + request(nconf.get('url') + '/api/user/foo', { json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + var contents = body.posts.map(function (p) { + return p.content; + }); + assert(contents.indexOf('1st reply') === -1); + done(); + }); + }, + ], done); + }); + + it('should return selected group title', function (done) { + groups.create({ + name: 'selectedGroup', + }, function (err) { + assert.ifError(err); + groups.join('selectedGroup', fooUid, function (err) { + assert.ifError(err); + request(nconf.get('url') + '/api/user/foo', { json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + assert(body.selectedGroup.name, 'selectedGroup'); + done(); + }); + }); + }); + }); + + it('should 404 if user does not exist', function (done) { + groups.join('administrators', fooUid, function (err) { + assert.ifError(err); + request(nconf.get('url') + '/api/user/doesnotexist/edit', { jar: jar, json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 404); + groups.leave('administrators', fooUid, done); + }); + }); + }); + + it('should render edit/password', function (done) { + request(nconf.get('url') + '/api/user/foo/edit/password', { jar: jar, json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + done(); + }); + }); + + it('should render edit/email', function (done) { + request(nconf.get('url') + '/api/user/foo/edit/email', { jar: jar, json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + done(); + }); + }); + + it('should render edit/username', function (done) { + request(nconf.get('url') + '/api/user/foo/edit/username', { jar: jar, json: true }, function (err, res, body) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + done(); + }); + }); }); describe('account follow page', function () { @@ -1574,6 +1720,14 @@ describe('Controllers', function () { }); }); + it('should load unread page', function (done) { + request(nconf.get('url') + '/api/unread', { jar: jar }, function (err, res) { + assert.ifError(err); + assert.equal(res.statusCode, 200); + done(); + }); + }); + it('should 404 if filter is invalid', function (done) { request(nconf.get('url') + '/api/unread/doesnotexist', { jar: jar }, function (err, res) { assert.ifError(err);