mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-05-19 21:48:23 -05:00
Now that nodejs/node#3072 is mostly fixed, we can finally start using Node 4+ features. This refactor: - uses arrow functions where appropriate Note that arrow functions still aren't used in Mocha, where `this` is sometimes meaningful. This also removes the need for .bind() nearly everywhere, as well as the `self = this` trick. - refactors Validator and Connection into ES6 classes - no longer uses Array#forEach for iterating arrays We strongly prefer for (let i = 0; i < arr.length; i++) because of performance reasons. Most forEaches have been replaced with for..of, though, which is 5x slower than the long-form loop but 2x faster than forEach, which is good enough outside of most inner loops. The only exception is tournaments, which is due for a more invasive refactor soon anyway.
95 lines
2.8 KiB
JavaScript
95 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('assert');
|
|
|
|
let userUtils = require('./../../dev-tools/users-utils.js');
|
|
let Connection = userUtils.Connection;
|
|
let User = userUtils.User;
|
|
|
|
describe('Users features', function () {
|
|
describe('Users', function () {
|
|
describe('get', function () {
|
|
it('should be a function', function () {
|
|
assert.strictEqual(typeof Users.get, 'function');
|
|
});
|
|
|
|
it('should be equal to `Users`', function () {
|
|
assert.strictEqual(Users.get, Users);
|
|
});
|
|
});
|
|
describe('users', function () {
|
|
it('should be a Map', function () {
|
|
assert.ok(Users.users instanceof Map);
|
|
});
|
|
});
|
|
describe('User', function () {
|
|
describe('#disconnectAll', function () {
|
|
for (let totalConnections of [1, 2]) {
|
|
it('should drop all ' + totalConnections + ' connection(s) and mark as inactive', function () {
|
|
let user = new User();
|
|
let iterations = totalConnections;
|
|
while (--iterations) user.mergeConnection(new Connection());
|
|
|
|
user.disconnectAll();
|
|
assert.strictEqual(user.connections.length, 0);
|
|
assert.strictEqual(user.connected, false);
|
|
});
|
|
|
|
it('should unref all ' + totalConnections + ' connection(s)', function () {
|
|
let user = new User();
|
|
let iterations = totalConnections;
|
|
while (--iterations) user.mergeConnection(new Connection());
|
|
|
|
let connections = user.connections.slice();
|
|
|
|
user.disconnectAll();
|
|
for (let i = 0; i < totalConnections; i++) {
|
|
assert.ok(!Users.connections.has(connections[i].id));
|
|
}
|
|
});
|
|
|
|
it('should clear `user` property for all ' + totalConnections + ' connection(s)', function () {
|
|
let user = new User();
|
|
let iterations = totalConnections;
|
|
while (--iterations) user.mergeConnection(new Connection());
|
|
let connections = user.connections.slice();
|
|
|
|
user.disconnectAll();
|
|
for (let i = 0; i < totalConnections; i++) {
|
|
assert.strictEqual(connections[i].user, null);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
describe('#ban', function () {
|
|
afterEach(function () {
|
|
for (let ip in Users.bannedIps) {
|
|
delete Users.bannedIps[ip];
|
|
}
|
|
});
|
|
|
|
it('should disconnect every user at that IP', function () {
|
|
let users = ['127.0.0.1', '127.0.0.1'].map(ip => new User(new Connection(ip)));
|
|
users[0].ban();
|
|
assert.strictEqual(users[0].connected, false);
|
|
assert.strictEqual(users[1].connected, false);
|
|
});
|
|
|
|
it('should not disconnect users at other IPs', function () {
|
|
let users = ['127.0.0.1', '127.0.0.2'].map(ip => new User(new Connection(ip)));
|
|
users[0].ban();
|
|
assert.strictEqual(users[1].connected, true);
|
|
});
|
|
|
|
it('should update IP count properly', function () {
|
|
let user = new User();
|
|
user.ban();
|
|
for (let ip in user.ips) {
|
|
assert.strictEqual(user.ips[ip], 0);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|