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.
841 lines
30 KiB
JavaScript
841 lines
30 KiB
JavaScript
10 years ago
|
'use strict';
|
||
|
|
||
8 years ago
|
var assert = require('assert');
|
||
8 years ago
|
var async = require('async');
|
||
8 years ago
|
var path = require('path');
|
||
|
var nconf = require('nconf');
|
||
11 years ago
|
|
||
8 years ago
|
var db = require('./mocks/databasemock');
|
||
8 years ago
|
var helpers = require('./helpers');
|
||
8 years ago
|
var Groups = require('../src/groups');
|
||
|
var User = require('../src/user');
|
||
11 years ago
|
|
||
8 years ago
|
describe('Groups', function () {
|
||
8 years ago
|
var adminUid;
|
||
|
var testUid;
|
||
8 years ago
|
before(function (done) {
|
||
8 years ago
|
Groups.resetCache();
|
||
8 years ago
|
async.series([
|
||
8 years ago
|
function (next) {
|
||
11 years ago
|
// Create a group to play around with
|
||
10 years ago
|
Groups.create({
|
||
|
name: 'Test',
|
||
8 years ago
|
description: 'Foobar!',
|
||
10 years ago
|
}, next);
|
||
11 years ago
|
},
|
||
8 years ago
|
function (next) {
|
||
|
Groups.create({
|
||
|
name: 'PrivateNoJoin',
|
||
|
description: 'Private group',
|
||
|
private: 1,
|
||
8 years ago
|
disableJoinRequests: 1,
|
||
8 years ago
|
}, next);
|
||
|
},
|
||
|
function (next) {
|
||
|
Groups.create({
|
||
|
name: 'PrivateCanJoin',
|
||
|
description: 'Private group',
|
||
|
private: 1,
|
||
8 years ago
|
disableJoinRequests: 0,
|
||
8 years ago
|
}, next);
|
||
|
},
|
||
8 years ago
|
function (next) {
|
||
11 years ago
|
// Create a new user
|
||
10 years ago
|
User.create({
|
||
11 years ago
|
username: 'testuser',
|
||
8 years ago
|
email: '[email protected]',
|
||
10 years ago
|
}, next);
|
||
11 years ago
|
},
|
||
8 years ago
|
function (next) {
|
||
|
User.create({
|
||
|
username: 'admin',
|
||
8 years ago
|
email: '[email protected]',
|
||
8 years ago
|
password: '123456',
|
||
8 years ago
|
}, next);
|
||
|
},
|
||
8 years ago
|
function (next) {
|
||
11 years ago
|
// Also create a hidden group
|
||
|
Groups.join('Hidden', 'Test', next);
|
||
8 years ago
|
},
|
||
8 years ago
|
], function (err, results) {
|
||
|
assert.ifError(err);
|
||
|
testUid = results[3];
|
||
|
adminUid = results[4];
|
||
|
Groups.join('administrators', adminUid, done);
|
||
|
});
|
||
11 years ago
|
});
|
||
|
|
||
8 years ago
|
describe('.list()', function () {
|
||
|
it('should list the groups present', function (done) {
|
||
|
Groups.getGroupsFromSet('groups:createtime', 0, 0, -1, function (err, groups) {
|
||
8 years ago
|
assert.ifError(err);
|
||
8 years ago
|
assert.equal(groups.length, 6);
|
||
11 years ago
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
describe('.get()', function () {
|
||
|
before(function (done) {
|
||
8 years ago
|
Groups.join('Test', testUid, done);
|
||
11 years ago
|
});
|
||
|
|
||
8 years ago
|
it('with no options, should show group information', function (done) {
|
||
|
Groups.get('Test', {}, function (err, groupObj) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.equal(typeof groupObj, 'object');
|
||
|
assert(Array.isArray(groupObj.members));
|
||
|
assert.strictEqual(groupObj.name, 'Test');
|
||
|
assert.strictEqual(groupObj.description, 'Foobar!');
|
||
|
assert.strictEqual(groupObj.memberCount, 1);
|
||
|
assert.equal(typeof groupObj.members[0], 'object');
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
describe('.search()', function () {
|
||
8 years ago
|
var socketGroups = require('../src/socket.io/groups');
|
||
|
|
||
|
it('should return the groups when search query is empty', function (done) {
|
||
8 years ago
|
socketGroups.search({ uid: adminUid }, { query: '' }, function (err, groups) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
assert.equal(3, groups.length);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should return the "Test" group when searched for', function (done) {
|
||
8 years ago
|
socketGroups.search({ uid: adminUid }, { query: 'test' }, function (err, groups) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
assert.equal(1, groups.length);
|
||
|
assert.strictEqual('Test', groups[0].name);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should return the "Test" group when searched for and sort by member count', function (done) {
|
||
8 years ago
|
Groups.search('test', { filterHidden: true, sort: 'count' }, function (err, groups) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
assert.equal(1, groups.length);
|
||
|
assert.strictEqual('Test', groups[0].name);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should return the "Test" group when searched for and sort by creation time', function (done) {
|
||
8 years ago
|
Groups.search('test', { filterHidden: true, sort: 'date' }, function (err, groups) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.equal(1, groups.length);
|
||
|
assert.strictEqual('Test', groups[0].name);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should return all users if no query', function (done) {
|
||
|
User.create({
|
||
|
username: 'newuser',
|
||
8 years ago
|
email: '[email protected]',
|
||
8 years ago
|
}, function (err, uid) {
|
||
|
assert.ifError(err);
|
||
|
Groups.join('Test', uid, function (err) {
|
||
|
assert.ifError(err);
|
||
8 years ago
|
socketGroups.searchMembers({ uid: adminUid }, { groupName: 'Test', query: '' }, function (err, data) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
assert.equal(data.users.length, 2);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should search group members', function (done) {
|
||
8 years ago
|
socketGroups.searchMembers({ uid: adminUid }, { groupName: 'Test', query: 'test' }, function (err, data) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
assert.strictEqual('testuser', data.users[0].username);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
11 years ago
|
});
|
||
|
|
||
8 years ago
|
describe('.isMember()', function () {
|
||
|
it('should return boolean true when a user is in a group', function (done) {
|
||
|
Groups.isMember(1, 'Test', function (err, isMember) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual(isMember, true);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should return boolean false when a user is not in a group', function (done) {
|
||
|
Groups.isMember(2, 'Test', function (err, isMember) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual(isMember, false);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
describe('.isMemberOfGroupList', function () {
|
||
|
it('should report that a user is part of a groupList, if they are', function (done) {
|
||
|
Groups.isMemberOfGroupList(1, 'Hidden', function (err, isMember) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual(isMember, true);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should report that a user is not part of a groupList, if they are not', function (done) {
|
||
|
Groups.isMemberOfGroupList(2, 'Hidden', function (err, isMember) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual(isMember, false);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
describe('.exists()', function () {
|
||
|
it('should verify that the test group exists', function (done) {
|
||
|
Groups.exists('Test', function (err, exists) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual(exists, true);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should verify that a fake group does not exist', function (done) {
|
||
|
Groups.exists('Derp', function (err, exists) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual(exists, false);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
9 years ago
|
|
||
8 years ago
|
it('should check if group exists using an array', function (done) {
|
||
|
Groups.exists(['Test', 'Derp'], function (err, groupsExists) {
|
||
8 years ago
|
assert.ifError(err);
|
||
9 years ago
|
assert.strictEqual(groupsExists[0], true);
|
||
|
assert.strictEqual(groupsExists[1], false);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
11 years ago
|
});
|
||
|
|
||
8 years ago
|
describe('.create()', function () {
|
||
|
it('should create another group', function (done) {
|
||
10 years ago
|
Groups.create({
|
||
|
name: 'foo',
|
||
8 years ago
|
description: 'bar',
|
||
8 years ago
|
}, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
Groups.get('foo', {}, done);
|
||
|
});
|
||
|
});
|
||
9 years ago
|
|
||
8 years ago
|
it('should fail to create group with duplicate group name', function (done) {
|
||
8 years ago
|
Groups.create({ name: 'foo' }, function (err) {
|
||
9 years ago
|
assert(err);
|
||
|
assert.equal(err.message, '[[error:group-already-exists]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should fail to create group if slug is empty', function (done) {
|
||
8 years ago
|
Groups.create({ name: '>>>>' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-group-name]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail if group name is invalid', function (done) {
|
||
8 years ago
|
Groups.create({ name: 'not/valid' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-group-name]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
8 years ago
|
|
||
|
it('should fail if group name is invalid', function (done) {
|
||
8 years ago
|
Groups.create({ name: 'not:valid' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-group-name]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
11 years ago
|
});
|
||
|
|
||
8 years ago
|
describe('.hide()', function () {
|
||
|
it('should mark the group as hidden', function (done) {
|
||
|
Groups.hide('foo', function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
|
||
8 years ago
|
Groups.get('foo', {}, function (err, groupObj) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual(true, groupObj.hidden);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
describe('.update()', function () {
|
||
|
before(function (done) {
|
||
10 years ago
|
Groups.create({
|
||
|
name: 'updateTestGroup',
|
||
10 years ago
|
description: 'bar',
|
||
|
system: 0,
|
||
8 years ago
|
hidden: 0,
|
||
10 years ago
|
}, done);
|
||
10 years ago
|
});
|
||
10 years ago
|
|
||
8 years ago
|
it('should change an aspect of a group', function (done) {
|
||
10 years ago
|
Groups.update('updateTestGroup', {
|
||
8 years ago
|
description: 'baz',
|
||
8 years ago
|
}, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
|
||
8 years ago
|
Groups.get('updateTestGroup', {}, function (err, groupObj) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual('baz', groupObj.description);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
10 years ago
|
|
||
8 years ago
|
it('should rename a group if the name was updated', function (done) {
|
||
10 years ago
|
Groups.update('updateTestGroup', {
|
||
8 years ago
|
name: 'updateTestGroup?',
|
||
8 years ago
|
}, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
10 years ago
|
|
||
8 years ago
|
Groups.get('updateTestGroup?', {}, function (err, groupObj) {
|
||
8 years ago
|
assert.ifError(err);
|
||
10 years ago
|
assert.strictEqual('updateTestGroup?', groupObj.name);
|
||
10 years ago
|
assert.strictEqual('updatetestgroup', groupObj.slug);
|
||
10 years ago
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
11 years ago
|
});
|
||
|
|
||
8 years ago
|
describe('.destroy()', function () {
|
||
|
before(function (done) {
|
||
10 years ago
|
Groups.join('foobar?', 1, done);
|
||
11 years ago
|
});
|
||
|
|
||
8 years ago
|
it('should destroy a group', function (done) {
|
||
|
Groups.destroy('foobar?', function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
|
||
8 years ago
|
Groups.get('foobar?', {}, function (err) {
|
||
10 years ago
|
assert(err, 'Group still exists!');
|
||
11 years ago
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
11 years ago
|
|
||
8 years ago
|
it('should also remove the members set', function (done) {
|
||
|
db.exists('group:foo:members', function (err, exists) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
|
||
|
assert.strictEqual(false, exists);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
11 years ago
|
});
|
||
|
|
||
8 years ago
|
describe('.join()', function () {
|
||
|
before(function (done) {
|
||
8 years ago
|
Groups.leave('Test', testUid, done);
|
||
11 years ago
|
});
|
||
|
|
||
8 years ago
|
it('should add a user to a group', function (done) {
|
||
8 years ago
|
Groups.join('Test', testUid, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
|
||
8 years ago
|
Groups.isMember(testUid, 'Test', function (err, isMember) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual(true, isMember);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
11 years ago
|
|
||
8 years ago
|
describe('.leave()', function () {
|
||
|
it('should remove a user from a group', function (done) {
|
||
8 years ago
|
Groups.leave('Test', testUid, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
|
||
8 years ago
|
Groups.isMember(testUid, 'Test', function (err, isMember) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
assert.strictEqual(false, isMember);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
describe('.leaveAllGroups()', function () {
|
||
|
it('should remove a user from all groups', function (done) {
|
||
8 years ago
|
Groups.leaveAllGroups(testUid, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
11 years ago
|
|
||
11 years ago
|
var groups = ['Test', 'Hidden'];
|
||
8 years ago
|
async.every(groups, function (group, next) {
|
||
8 years ago
|
Groups.isMember(testUid, group, function (err, isMember) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
next(!isMember);
|
||
11 years ago
|
});
|
||
8 years ago
|
}, function (result) {
|
||
11 years ago
|
assert(result);
|
||
|
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
10 years ago
|
|
||
8 years ago
|
describe('.show()', function () {
|
||
|
it('should make a group visible', function (done) {
|
||
|
Groups.show('Test', function (err) {
|
||
9 years ago
|
assert.ifError(err);
|
||
|
assert.equal(arguments.length, 1);
|
||
8 years ago
|
db.isSortedSetMember('groups:visible:createtime', 'Test', function (err, isMember) {
|
||
9 years ago
|
assert.ifError(err);
|
||
|
assert.strictEqual(isMember, true);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
describe('.hide()', function () {
|
||
|
it('should make a group hidden', function (done) {
|
||
|
Groups.hide('Test', function (err) {
|
||
9 years ago
|
assert.ifError(err);
|
||
|
assert.equal(arguments.length, 1);
|
||
8 years ago
|
db.isSortedSetMember('groups:visible:createtime', 'Test', function (err, isMember) {
|
||
9 years ago
|
assert.ifError(err);
|
||
|
assert.strictEqual(isMember, false);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
|
||
|
describe('socket methods', function () {
|
||
|
var socketGroups = require('../src/socket.io/groups');
|
||
|
var meta = require('../src/meta');
|
||
|
|
||
|
|
||
|
it('should error if data is null', function (done) {
|
||
8 years ago
|
socketGroups.before({ uid: 0 }, 'groups.join', null, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-data]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should not error if data is valid', function (done) {
|
||
8 years ago
|
socketGroups.before({ uid: 0 }, 'groups.join', {}, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should return error if not logged in', function (done) {
|
||
8 years ago
|
socketGroups.join({ uid: 0 }, {}, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-uid]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should return error if group name is special', function (done) {
|
||
8 years ago
|
socketGroups.join({ uid: adminUid }, { groupName: 'administrators' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:not-allowed]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should error if group does not exist', function (done) {
|
||
8 years ago
|
socketGroups.join({ uid: adminUid }, { groupName: 'doesnotexist' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:no-group]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should join test group', function (done) {
|
||
|
meta.config.allowPrivateGroups = 0;
|
||
8 years ago
|
socketGroups.join({ uid: adminUid }, { groupName: 'Test' }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.isMember(adminUid, 'Test', function (err, isMember) {
|
||
|
assert.ifError(err);
|
||
|
assert(isMember);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should error if not logged in', function (done) {
|
||
8 years ago
|
socketGroups.leave({ uid: 0 }, {}, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-uid]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should return error if group name is special', function (done) {
|
||
8 years ago
|
socketGroups.leave({ uid: adminUid }, { groupName: 'administrators' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:cant-remove-self-as-admin]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should leave test group', function (done) {
|
||
8 years ago
|
socketGroups.leave({ uid: adminUid }, { groupName: 'Test' }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.isMember('Test', adminUid, function (err, isMember) {
|
||
|
assert.ifError(err);
|
||
|
assert(!isMember);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to join if group is private and join requests are disabled', function (done) {
|
||
|
meta.config.allowPrivateGroups = 1;
|
||
8 years ago
|
socketGroups.join({ uid: testUid }, { groupName: 'PrivateNoJoin' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:join-requests-disabled]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should join if user is admin', function (done) {
|
||
8 years ago
|
socketGroups.join({ uid: adminUid }, { groupName: 'PrivateCanJoin' }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.isMember(adminUid, 'PrivateCanJoin', function (err, isMember) {
|
||
|
assert.ifError(err);
|
||
|
assert(isMember);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should request membership for regular user', function (done) {
|
||
8 years ago
|
socketGroups.join({ uid: testUid }, { groupName: 'PrivateCanJoin' }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.isPending(testUid, 'PrivateCanJoin', function (err, isPending) {
|
||
|
assert.ifError(err);
|
||
|
assert(isPending);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
it('should accept membership of user', function (done) {
|
||
8 years ago
|
socketGroups.accept({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: testUid }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.isMember(testUid, 'PrivateCanJoin', function (err, isMember) {
|
||
|
assert.ifError(err);
|
||
|
assert(isMember);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should grant ownership to user', function (done) {
|
||
8 years ago
|
socketGroups.grant({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: testUid }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.ownership.isOwner(testUid, 'PrivateCanJoin', function (err, isOwner) {
|
||
|
assert.ifError(err);
|
||
|
assert(isOwner);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should rescind ownership from user', function (done) {
|
||
8 years ago
|
socketGroups.rescind({ uid: adminUid }, { groupName: 'PrivateCanJoin', toUid: testUid }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.ownership.isOwner(testUid, 'PrivateCanJoin', function (err, isOwner) {
|
||
|
assert.ifError(err);
|
||
|
assert(!isOwner);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should kick user from group', function (done) {
|
||
8 years ago
|
socketGroups.kick({ uid: adminUid }, { groupName: 'PrivateCanJoin', uid: testUid }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.isMember(testUid, 'PrivateCanJoin', function (err, isMember) {
|
||
|
assert.ifError(err);
|
||
|
assert(!isMember);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
8 years ago
|
});
|
||
|
|
||
8 years ago
|
describe('admin socket methods', function () {
|
||
|
var socketGroups = require('../src/socket.io/admin/groups');
|
||
|
|
||
|
it('should fail to create group with invalid data', function (done) {
|
||
8 years ago
|
socketGroups.create({ uid: adminUid }, null, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-data]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to create group if group name is privilege group', function (done) {
|
||
8 years ago
|
socketGroups.create({ uid: adminUid }, { name: 'cid:1:privileges:read' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-group-name]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should create a group', function (done) {
|
||
8 years ago
|
socketGroups.create({ uid: adminUid }, { name: 'newgroup', description: 'group created by admin' }, function (err, groupData) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
assert.equal(groupData.name, 'newgroup');
|
||
|
assert.equal(groupData.description, 'group created by admin');
|
||
|
assert.equal(groupData.ownerUid, adminUid);
|
||
|
assert.equal(groupData.private, true);
|
||
|
assert.equal(groupData.memberCount, 1);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to join with invalid data', function (done) {
|
||
8 years ago
|
socketGroups.join({ uid: adminUid }, null, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-data]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should add user to group', function (done) {
|
||
8 years ago
|
socketGroups.join({ uid: adminUid }, { uid: testUid, groupName: 'newgroup' }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.isMember(testUid, 'newgroup', function (err, isMember) {
|
||
|
assert.ifError(err);
|
||
|
assert(isMember);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to if user is already member', function (done) {
|
||
8 years ago
|
socketGroups.join({ uid: adminUid }, { uid: testUid, groupName: 'newgroup' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:group-already-member]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('it should fail with invalid data', function (done) {
|
||
8 years ago
|
socketGroups.leave({ uid: adminUid }, null, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-data]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('it should fail if admin tries to remove self', function (done) {
|
||
8 years ago
|
socketGroups.leave({ uid: adminUid }, { uid: adminUid, groupName: 'administrators' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:cant-remove-self-as-admin]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail if user is not member', function (done) {
|
||
8 years ago
|
socketGroups.leave({ uid: adminUid }, { uid: 3, groupName: 'newgroup' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:group-not-member]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should remove user from group', function (done) {
|
||
8 years ago
|
socketGroups.leave({ uid: adminUid }, { uid: testUid, groupName: 'newgroup' }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.isMember(testUid, 'newgroup', function (err, isMember) {
|
||
|
assert.ifError(err);
|
||
|
assert(!isMember);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail with invalid data', function (done) {
|
||
8 years ago
|
socketGroups.update({ uid: adminUid }, null, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:invalid-data]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should update group', function (done) {
|
||
|
var data = {
|
||
|
groupName: 'newgroup',
|
||
|
values: {
|
||
|
name: 'renamedgroup',
|
||
|
description: 'cat group',
|
||
|
userTitle: 'cats',
|
||
|
userTitleEnabled: 1,
|
||
|
disableJoinRequests: 1,
|
||
|
hidden: 1,
|
||
8 years ago
|
private: 0,
|
||
|
},
|
||
8 years ago
|
};
|
||
8 years ago
|
socketGroups.update({ uid: adminUid }, data, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.get('renamedgroup', {}, function (err, groupData) {
|
||
|
assert.ifError(err);
|
||
|
assert.equal(groupData.name, 'renamedgroup');
|
||
|
assert.equal(groupData.userTitle, 'cats');
|
||
|
assert.equal(groupData.description, 'cat group');
|
||
|
assert.equal(groupData.hidden, true);
|
||
|
assert.equal(groupData.disableJoinRequests, true);
|
||
|
assert.equal(groupData.private, false);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
describe('groups cover', function () {
|
||
|
var socketGroups = require('../src/socket.io/groups');
|
||
|
var regularUid;
|
||
8 years ago
|
var logoPath = path.join(__dirname, '../test/files/test.png');
|
||
|
var imagePath = path.join(__dirname, '../test/files/groupcover.png');
|
||
8 years ago
|
before(function (done) {
|
||
8 years ago
|
User.create({ username: 'regularuser', password: '123456' }, function (err, uid) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
regularUid = uid;
|
||
|
async.series([
|
||
|
function (next) {
|
||
|
Groups.join('Test', adminUid, next);
|
||
|
},
|
||
|
function (next) {
|
||
|
Groups.join('Test', regularUid, next);
|
||
|
},
|
||
|
function (next) {
|
||
|
helpers.copyFile(logoPath, imagePath, next);
|
||
8 years ago
|
},
|
||
8 years ago
|
], done);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail if user is not logged in or not owner', function (done) {
|
||
8 years ago
|
socketGroups.cover.update({ uid: 0 }, {}, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:no-privileges]]');
|
||
8 years ago
|
socketGroups.cover.update({ uid: regularUid }, {}, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:no-privileges]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should upload group cover image from file', function (done) {
|
||
|
var data = {
|
||
|
groupName: 'Test',
|
||
8 years ago
|
file: imagePath,
|
||
8 years ago
|
};
|
||
8 years ago
|
socketGroups.cover.update({ uid: adminUid }, data, function (err, data) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.getGroupFields('Test', ['cover:url'], function (err, groupData) {
|
||
|
assert.ifError(err);
|
||
|
assert.equal(data.url, groupData['cover:url']);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
|
||
|
it('should upload group cover image from data', function (done) {
|
||
|
var data = {
|
||
|
groupName: 'Test',
|
||
8 years ago
|
imageData: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAgCAYAAAABtRhCAAAACXBIWXMAAC4jAAAuIwF4pT92AAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8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7xf9EDtQdlD3YfVP1v+3Njv3H9qwHeg89HcR/cGhYPP/pH1jw9DBY+Zj8uGDYbrnjg+OTniP3L96fynQ89kzyaeF/6i/suuFxYvfvjV69fO0ZjRoZfyl5O/bXyl/erA6xmv28bCxh6+yXgzMV70VvvtwXfcdx3vo98PT+R8IH8o/2j5sfVT0Kf7kxmTk/8EA5jz/GMzLdsAAAAgY0hSTQAAeiUAAICDAAD5/wAAgOkAAHUwAADqYAAAOpgAABdvkl/FRgAACcJJREFUeNqMl9tvnNV6xn/f+s5z8DCeg88Zj+NYdhJH4KShFoJAIkzVphLVJnsDaiV6gUKaC2qQUFVATbnoValAakuQYKMqBKUUJCgI9XBBSmOROMqGoCStHbA9sWM7nrFn/I3n9B17kcwoabfarj9gvet53+d9nmdJAwMDAAgh8DyPtbU1XNfFMAwkScK2bTzPw/M8dF1/SAhxKAiCxxVF2aeqqqTr+q+Af+7o6Ch0d3f/69TU1KwkSRiGwbFjx3jmmWd47rnn+OGHH1BVFYX/5QRBkPQ87xeSJP22YRi/oapqStM0PM/D931kWSYIgnHf98cXFxepVqtomjZt2/Zf2bb990EQ4Pv+PXfeU1CSpGYhfN9/TgjxQTQaJQgCwuEwQ
|
||
8 years ago
|
};
|
||
8 years ago
|
socketGroups.cover.update({ uid: adminUid }, data, function (err, data) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.getGroupFields('Test', ['cover:url'], function (err, groupData) {
|
||
|
assert.ifError(err);
|
||
|
assert.equal(data.url, groupData['cover:url']);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should update group cover position', function (done) {
|
||
|
var data = {
|
||
|
groupName: 'Test',
|
||
8 years ago
|
position: '50% 50%',
|
||
8 years ago
|
};
|
||
8 years ago
|
socketGroups.cover.update({ uid: adminUid }, data, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.getGroupFields('Test', ['cover:position'], function (err, groupData) {
|
||
|
assert.ifError(err);
|
||
|
assert.equal('50% 50%', groupData['cover:position']);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to update cover position if group name is missing', function (done) {
|
||
|
Groups.updateCoverPosition('', '50% 50%', function (err) {
|
||
|
assert.equal(err.message, '[[error:invalid-data]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should error if user is not owner of group', function (done) {
|
||
|
helpers.loginUser('regularuser', '123456', function (err, jar, io, csrf_token) {
|
||
|
assert.ifError(err);
|
||
8 years ago
|
helpers.uploadFile(nconf.get('url') + '/api/groups/uploadpicture', logoPath, { params: JSON.stringify({ groupName: 'Test' }) }, jar, csrf_token, function (err, res, body) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
assert.equal(res.statusCode, 500);
|
||
8 years ago
|
assert.equal(body.error, '[[error:no-privileges]]');
|
||
8 years ago
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should upload group cover with api route', function (done) {
|
||
|
helpers.loginUser('admin', '123456', function (err, jar, io, csrf_token) {
|
||
|
assert.ifError(err);
|
||
8 years ago
|
helpers.uploadFile(nconf.get('url') + '/api/groups/uploadpicture', logoPath, { params: JSON.stringify({ groupName: 'Test' }) }, jar, csrf_token, function (err, res, body) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
assert.equal(res.statusCode, 200);
|
||
|
Groups.getGroupFields('Test', ['cover:url'], function (err, groupData) {
|
||
|
assert.ifError(err);
|
||
|
assert.equal(body[0].url, groupData['cover:url']);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should fail to remove cover if not owner', function (done) {
|
||
8 years ago
|
socketGroups.cover.remove({ uid: regularUid }, { groupName: 'Test' }, function (err) {
|
||
8 years ago
|
assert.equal(err.message, '[[error:no-privileges]]');
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
it('should remove cover', function (done) {
|
||
8 years ago
|
socketGroups.cover.remove({ uid: adminUid }, { groupName: 'Test' }, function (err) {
|
||
8 years ago
|
assert.ifError(err);
|
||
|
Groups.getGroupFields('Test', ['cover:url'], function (err, groupData) {
|
||
|
assert.ifError(err);
|
||
|
assert(!groupData['cover:url']);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
after(function (done) {
|
||
8 years ago
|
db.emptydb(done);
|
||
10 years ago
|
});
|
||
11 years ago
|
});
|