more tests, added ability to remove clients from a channel

This commit is contained in:
Josh Burman
2019-03-14 14:17:08 -04:00
parent 21f77fa4c3
commit 40691d9815
17 changed files with 316 additions and 5 deletions

View File

@ -64,6 +64,27 @@ class ChannelManager {
return new ChannelBase(data.channel);
}
}
removeClientFromChannel(client_id: number, channel_id: string) {
var index: number = 0;
for (let channel of this.channels) {
if (channel.id == channel_id) {
this.channels.splice(index, 1);
if (channel.removeClient(client_id)) {
logger.accessLog.info(`client removed from channel - channel: ${channel_id}, client: ${client_id}`);
return true;
} else {
logger.errorLog.info(`client not removed from channel, or doesn't exist in channel - channel: ${channel_id}, client: ${client_id}`);
return false
}
}
index++;
}
return false;
}
};
export default ChannelManager;

View File

@ -33,6 +33,21 @@ class ChannelBase {
return null;
}
removeClient(id: number) {
var index: number = 0;
for (let client of this.clients) {
if (client.id == id) {
this.clients.splice(index, 1);
return true;
}
index++;
}
return false;
}
broadcastMessage(from: ClientBase|MHSClient|null, message: string) {
for (let client of this.clients) {
if (client != from) {

View File

@ -39,4 +39,14 @@ describe('ChannelBase', function () {
var result = channel.broadcastMessage(client, 'test message');
expect(result.status).to.be.equal('success');
});
it('should be able to remove client from channel', function () {
var result = channel.removeClient(client.id);
expect(result).to.be.equal(true);
});
it('should not be able to remove client that doesnn\'t exist from channel', function () {
var result = channel.removeClient(126);
expect(result).to.be.equal(false);
});
});

View File

@ -55,4 +55,19 @@ describe('ChannelManager', function () {
var result = channelManager.createByChannelType(data2);
expect(result.id).to.be.equal('test channel 2');
});
it('should remove client from channel', function () {
var result = channelManager.removeClientFromChannel(client.id , 'test channel');
expect(result).to.be.equal(true);
});
it('should be unable to remove a client that doesn\'t exit from channel', function () {
var result = channelManager.removeClientFromChannel(126 , 'test channel');
expect(result).to.be.equal(false);
});
it('should be unable to remove a client from a channel that doesn\'t exist', function () {
var result = channelManager.removeClientFromChannel(client.id , 'test channel 3');
expect(result).to.be.equal(false);
});
});

View File

@ -0,0 +1,29 @@
import * as WebSocket from 'ws';
import ClientBase from '../clients/clientBase';
var expect = require('chai').expect;
var assert = require('chai').assert;
var sinon = require('sinon');
var name: string = 'test channel';
var data: any = { 'client': 'test', 'client_type':'site', 'user_id': 125, 'user_type': 'user', 'channel': name }
var WebSocketClient = require('websocket').client;
var wsClient = new WebSocketClient();
var client: ClientBase = new ClientBase(data, wsClient);
describe('ClientBase', function () {
it('should get client data', function () {
var result = client.getData();
expect(result.user_id).to.be.equal(125);
});
it('should get client type', function () {
var result = client.clientType();
expect(result).to.be.equal('test');
});
it('should get client object type', function () {
var result = client.type();
expect(result).to.be.equal('site');
});
});