Spaces:
Running
Running
File size: 5,813 Bytes
5c2ed06 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
'use strict';
const assert = require('assert').strict;
const { makeUser, makeConnection } = require('../users-utils');
describe('Users features', () => {
describe('Users', () => {
describe('get', () => {
it('should be a function', () => {
assert.equal(typeof Users.get, 'function');
});
});
describe('connections', () => {
it('should be a Map', () => {
assert(Users.connections instanceof Map);
});
});
describe('users', () => {
it('should be a Map', () => {
assert(Users.users instanceof Map);
});
});
describe('Connection', () => {
describe('#onDisconnect', () => {
beforeEach(function () {
this.connection = makeConnection('127.0.0.1');
});
it('should remove the connection from Users.connections', function () {
const connectionid = this.connection.id;
this.connection.destroy();
assert.equal(Users.connections.has(connectionid), false);
});
it('should destroy any user on the connection as well', function () {
const user = makeUser('', this.connection);
const userid = user.id;
assert.equal(Users.users.has(userid), true, 'before disconnecting');
user.disconnectAll();
user.destroy();
assert.equal(Users.users.has(userid), false, 'after disconnecting');
});
});
describe('#joinRoom', () => {
let room, connection;
beforeEach(() => {
connection = makeConnection('127.0.0.1');
});
afterEach(() => {
connection.destroy();
if (room) room.destroy();
});
it('should join a room if not already present', () => {
room = Rooms.createChatRoom('test');
connection.joinRoom(Rooms.get('test'));
assert(connection.inRooms.has('test'));
});
});
describe('#leaveRoom', () => {
let room;
afterEach(() => {
if (room) room.destroy();
});
it('should leave a room that is present', function () {
this.connection = makeConnection('127.0.0.1');
room = Rooms.createChatRoom('test');
this.connection.joinRoom(room);
this.connection.leaveRoom(room);
assert(!this.connection.inRooms.has('test'));
});
});
});
describe('User', () => {
it('should store IP addresses after disconnect', () => {
const conn = makeConnection('127.0.0.1');
const user = makeUser('', conn);
assert.deepEqual(['127.0.0.1'], user.ips);
user.onDisconnect(conn);
assert.deepEqual(['127.0.0.1'], user.ips);
});
describe('#disconnectAll', () => {
for (const totalConnections of [1, 2]) {
it('should drop all ' + totalConnections + ' connection(s) and mark as inactive', () => {
const user = makeUser();
let iterations = totalConnections;
while (--iterations) user.mergeConnection(makeConnection());
user.disconnectAll();
assert.equal(user.connections.length, 0);
assert.equal(user.connected, false);
});
it('should unref all ' + totalConnections + ' connection(s)', () => {
const user = makeUser();
let iterations = totalConnections;
while (--iterations) user.mergeConnection(makeConnection());
const connections = user.connections.slice();
user.disconnectAll();
for (let i = 0; i < totalConnections; i++) {
assert(!Users.connections.has(connections[i].id));
}
});
it('should clear `user` property for all ' + totalConnections + ' connection(s)', () => {
const user = makeUser();
let iterations = totalConnections;
while (--iterations) user.mergeConnection(makeConnection());
const connections = user.connections.slice();
user.disconnectAll();
for (let i = 0; i < totalConnections; i++) {
assert.equal(connections[i].user, null);
}
});
}
});
describe('#ban (network) (slow)', () => {
afterEach(() => {
for (const ip in Users.bannedIps) {
delete Users.bannedIps[ip];
}
});
it('should disconnect every user at that IP', async () => {
Punishments.sharedIps = new Map();
const user1 = makeUser('', '192.168.1.1');
const user2 = makeUser('', '192.168.1.1');
await Punishments.ban(user1);
assert.equal(user1.connected, false);
assert.equal(user2.connected, false);
});
it('should not disconnect users at other IPs', async () => {
const user1 = makeUser('', '192.168.1.1');
const user2 = makeUser('', '192.168.1.2');
await Punishments.ban(user1);
assert.equal(user2.connected, true);
});
});
describe('#can', () => {
let room;
afterEach(() => {
for (const user of Users.users.values()) {
user.disconnectAll();
user.destroy();
}
if (room) {
if (room) room.destroy();
}
});
it(`should allow 'u' permissions on lower ranked users`, () => {
const user = makeUser();
user.tempGroup = '@';
assert.equal(user.can('globalban', user), false, 'targeting self');
const target = makeUser();
target.tempGroup = ' ';
assert.equal(user.can('globalban', target), true, 'targeting lower rank');
target.tempGroup = '@';
assert.equal(user.can('globalban', target), false, 'targeting same rank');
target.tempGroup = '~';
assert.equal(user.can('globalban', target), false, 'targeting higher rank');
});
it(`should not allow users to demote themselves`, () => {
room = Rooms.createChatRoom("test");
const user = makeUser("User");
user.joinRoom(room);
for (const group of [' ', '+', '@']) {
room.auth.set(user.id, group);
assert.equal(room.auth.get(user.id), group, 'before demotion attempt');
Chat.parse("/roomdeauth User", room, user, user.connections[0]);
assert.equal(room.auth.get(user.id), group, 'after demotion attempt');
}
});
});
});
});
});
|