Skip to content

Commit

Permalink
Added test for api module methods
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphenslee committed Oct 19, 2015
1 parent c4ba3d3 commit 70c9c55
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 173 deletions.
17 changes: 8 additions & 9 deletions lib/session.js → lib/cache.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var _ = require('lodash');

var Session = function Session() {
var Cache = function Cache() {

this.startTime = _.now();
this.creator = 'Phillip Henslee <ph2@ph2.us';
this.data = {};

if (Session.caller != Session.getInstance) {
if (Cache.caller != Cache.getInstance) {
throw new Error("This object cannot be instanciated");
}

Expand All @@ -21,17 +20,17 @@ var Session = function Session() {
}
}

Session.instance = null;
Cache.instance = null;
/**
* Session getInstance
* @return Session class
* Cache getInstance
* @return Cache class
*/
Session.getInstance = function () {
Cache.getInstance = function () {
if (this.instance === null) {
this.instance = new Session();
this.instance = new Cache();
}
return this.instance;
}

module.exports = Session.getInstance();
module.exports = Cache.getInstance();

11 changes: 11 additions & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
invalid_token: 'invalid access token, please provide a valid token.',
missing_options_arg: 'missing required argument object options',
callback_type: 'callback must be a function',
missing_required_arg: 'missing or invalid required argument(s)',
entity_not_found: 'entity name not found',
invalid_slack_type: 'not a valid slack type',
slack_api_error: 'Slack API error occurred',
item_not_found: 'the channel, group, or user could not be found'
};

77 changes: 50 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference path="../typings/node/node.d.ts" />
/**
* SmartSlack module
* @description SmartSlack is a node.js module
Expand All @@ -21,7 +20,8 @@ var WebSocket = require('ws');

var Slack = require('./slack/index.js');
var logging = require('./logging')(/** no-opts */);
var errors = require('./slack/errors');
var errors = require('./errors');
var Cache = require('./cache');
var slackTypes = require('./slack/types');

/**
Expand All @@ -44,13 +44,13 @@ function SmartSlack(options) {
throw new Error(errors.invalid_token);
}

// Save the session data
this.session = require('./session');
this.session.defaults(options);
this.session.defaults({ hostname: 'api.slack.com' });
// Save the cache data
this.cache = Cache;
this.cache.defaults(options);
this.cache.defaults({ hostname: 'api.slack.com' });

this.slack = Slack;
this.log = bole('smartslack');
this.log = bole('SmartSlack');

this._connected = false;
this._autoReconnect = options.autoReconnect || true;
Expand Down Expand Up @@ -79,13 +79,13 @@ SmartSlack.prototype.login = function () {
_this.log.debug('Slack client authenticated...');
//_this.log.debug(results);

// Persist the Slack session data
_this.session.data.user = results.self;
_this.session.data.users = results.users;
_this.session.data.channels = results.channels;
_this.session.data.groups = results.groups;
_this.session.data.ims = results.ims;
_this.session.data.team = results.team;
// Cache the Slack session
_this.cache.data.user = results.self;
_this.cache.data.users = results.users;
_this.cache.data.channels = results.channels;
_this.cache.data.groups = results.groups;
_this.cache.data.ims = results.ims;
_this.cache.data.team = results.team;

// Valid for thirty seconds...
_this._socketUrl = results.url;
Expand All @@ -110,8 +110,9 @@ SmartSlack.prototype.sendToChannel = function (channel, text, callback) {
if (channel && typeof channel === 'string' && text && typeof text === 'string') {
this._sendToType(slackTypes.CHANNEL, channel, text, callback);
} else {
callback(new Error(errors.missing_required_arg), null);
this.log.error(errors.missing_required_arg);
callback(new Error(errors.missing_required_arg));
t
}
};

Expand All @@ -128,8 +129,8 @@ SmartSlack.prototype.sendToGroup = function (group, text, callback) {
if (group && typeof group === 'string' && text && typeof text === 'string') {
this._sendToType(slackTypes.GROUP, group, text, callback);
} else {
callback(new Error(errors.missing_required_arg), null);
this.log.error(errors.missing_required_arg);
callback(new Error(errors.missing_required_arg));
}
};

Expand All @@ -146,8 +147,8 @@ SmartSlack.prototype.sendToUser = function (username, text, callback) {
if (username && typeof username === 'string' && text && typeof text === 'string') {
this._sendToType(slackTypes.USER, username, text, callback);
} else {
callback(new Error(errors.missing_required_arg), null);
this.log.error(errors.missing_required_arg);
callback(new Error(errors.missing_required_arg));
}
};

Expand All @@ -164,18 +165,18 @@ SmartSlack.prototype._canResolve = function (callback) {

callback = (typeof callback === 'function') ? callback : function () { };

this.log.debug('Attempting to resolve ' + _this.session.data.hostname);
this.log.debug('Attempting to resolve ' + _this.cache.data.hostname);

dns.resolve4(_this.session.data.hostname, function (err, addresses) {
dns.resolve4(_this.cache.data.hostname, function (err, addresses) {

if (!err) {
_this.log.debug('Resolved ' + _this.session.data.hostname + ' (' + addresses[0] + ')');
_this.log.debug('Resolved ' + _this.cache.data.hostname + ' (' + addresses[0] + ')');

if (_this._reconnecting) {
clearInterval(_this._reconnecting);
_this.login();
}
return callback(true);
return callback(null, true);

} else {

Expand Down Expand Up @@ -241,9 +242,8 @@ SmartSlack.prototype._onRtmEvent = function (message) {
switch (message.type) {

case 'hello':

// Received the hello event message
this.log.debug('Slack hello received, socket connected...');
this.log.info('Slack hello received, socket connected...');
this._connected = true;
this.emit('connected');
this._lastPong = _.now();
Expand All @@ -255,10 +255,33 @@ SmartSlack.prototype._onRtmEvent = function (message) {
case 'message':
this.emit('message', message);
break;

case 'pong':
this._lastPong = _.now();
this.log.debug('Latency: ' + (this._lastPong - message.time) + 'ms');
this.log.debug('Last pong latency: ' + (this._lastPong - message.time) + 'ms');
break;
case 'presence_change':
_.find(this.cache.data.users, { id: message.user }).presence = message.presence;
this.log.info('User presence changed, updating user presence...');
break;
case 'team_join event':
this.log.info('New member joined the team, updating user list...');
this.slack.users.getList(function (err, results) {
if (err) {
console.error(err);
} else {
console.debug(results);
}
});
break;
case 'user_change':
this.log.info('User information has changed, updating user list...');
this.slack.users.getList(function (err, results) {
if (err) {
console.error(err);
} else {
console.debug(results);
}
});
break;
}
};
Expand Down Expand Up @@ -364,13 +387,13 @@ SmartSlack.prototype._sendToType = function (slackType, typeName, text, callback
break;
default:
entityId = null;
return callback(new Error(errors.invalid_slack_type), null);
return callback(new TypeError(errors.invalid_slack_type));
}

if (entityId) {
this._send(entityId, text);
} else {
callback(new Error(errors.entity_not_found), null);
callback(new Error(errors.entity_not_found));
}
};

Expand Down
5 changes: 2 additions & 3 deletions lib/index-spec.js → lib/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var chai = require('chai');
var should = chai.should();
var expect = chai.expect;
var nock = require('nock');
var SmartSlack = require('../lib/index.js');

describe('SmartSlack', function () {
Expand Down Expand Up @@ -29,7 +28,7 @@ describe('SmartSlack', function () {
expect(function () {
// No options
new SmartSlack(null);
}).to.throw('Missing required argument object options');
}).to.throw('missing required argument object options');
done();
});

Expand All @@ -38,7 +37,7 @@ describe('SmartSlack', function () {
expect(function () {
// Bad token
new SmartSlack(mockopts_invalid_token);
}).to.throw('Invalid access token, please provide a valid token.');
}).to.throw('invalid access token, please provide a valid token.');

done();
});
Expand Down
2 changes: 1 addition & 1 deletion lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function configure() {

} else {

outputs = {level: 'info', stream: process.stdout }
outputs = {level: 'error', stream: process.stdout }
}

bole.output(outputs);
Expand Down
Loading

0 comments on commit 70c9c55

Please sign in to comment.