Skip to content

Commit

Permalink
Added test for _sendToType
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphenslee committed Oct 23, 2015
1 parent 030b1b9 commit 5be49ed
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ slackClient.on('error',function(error) {
console.log(error);
}
// Login to Slack
slackClient.login();
// Start the Slack RTM session...
slackClient.start();
var message = 'Hello Channel!';
Expand Down
3 changes: 1 addition & 2 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module.exports = {
callback_type: 'callback must be a function',
entity_not_found: 'entity name not found',
invalid_entity_type: 'invalid slack entity type',
invalid_channel_id: 'invalid channel id',
invalid_slack_type: 'not a valid slack type',
invalid_token: 'invalid access token, please provide a valid token.',
item_not_found: 'the channel, group, or user could not be found',
missing_required_arg: 'must supply valid argument(s)',
Expand Down
14 changes: 8 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ SmartSlack.prototype._reconnect = function () {

// Attempt to resolve slack.com and call login when available
this._reconnecting = setInterval(function () { _this._canResolve(); }, 5000);
this.log.debug('Connection lost, waiting to reconnect...');
this.log.info('Connection lost, waiting to reconnect...');
};

/**
Expand All @@ -399,7 +399,9 @@ SmartSlack.prototype._reconnect = function () {
SmartSlack.prototype._send = function (channel, text) {

var message;
var output;
var data;

text = _.escape(text);

message = {
id: this._messageId,
Expand All @@ -409,12 +411,12 @@ SmartSlack.prototype._send = function (channel, text) {
};

try {
output = JSON.stringify(message);
data = JSON.stringify(message);
} catch (error) {
this.log.error(error);
}

this._webSocket.send(output);
this._webSocket.send(data);
this._messageId += 1;

};
Expand Down Expand Up @@ -456,13 +458,13 @@ SmartSlack.prototype._sendToType = function (slackType, typeName, text, callback
break;
default:
entityId = null;
return callback(new TypeError(errors.invalid_slack_type));

}

if (entityId) {
return this._send(entityId, text);
}
callback(new Error(errors.entity_not_found));
callback(new TypeError(errors.invalid_entity_type));
};

module.exports = SmartSlack;
15 changes: 13 additions & 2 deletions lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,19 @@ describe('SmartSlack', function () {

it('exists as method on SmartSlack', function (done) {
var slackClient = new SmartSlack(mockopts);
expect(typeof slackClient._onRtmEvent).to.equal('function');
expect(typeof slackClient._sendToType).to.equal('function');
done();
})
});

it('should return and error if passed invalid type', function (done) {
var slackClient = new SmartSlack(mockopts);
slackClient._sendToType('invalid_type', 'type_name', 'text', function (err, result) {
expect(err).to.be.an('error');
expect(err.message).to.equal('invalid slack entity type');
});
done();

});

});
});
4 changes: 2 additions & 2 deletions lib/slack/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = {
response.on('end', function () {

if (response.statusCode === 200) {
log.debug('API response received succesfully');
log.debug('API response received');
try {
result = JSON.parse(data);
} catch (error) {
Expand All @@ -73,14 +73,14 @@ module.exports = {
}
callback(null, result);


} else {
return callback(new Error('HTTPS response error ' + ' ' + response.statusCode));
}
});
});

request.on('error', function (err) {
log.debug(err);
callback(err);
});

Expand Down
8 changes: 7 additions & 1 deletion lib/slack/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ module.exports = {
channel = result
});

text = _.escape(text);

args = _.extend({
channel: channel,
text: text,
Expand Down Expand Up @@ -94,10 +96,11 @@ module.exports = {
channel = '#' + channel;
}

text = _.escape(text);

args = _.extend({
channel: channel,
text: text,
username: cache.data.user.name,
as_user: true
}, args || {});

Expand All @@ -122,6 +125,9 @@ module.exports = {
if (!_.isString(timestamp) && !_.isString(channel) && _.isString(text)) {
return callback(new Error(errors.missing_required_arg), null);
}

text = _.escape(text);

api.post('chat.update', { ts: timestamp, channel: channel, text: text }, function (err, result) {
if (err) {
return callback(err);
Expand Down

0 comments on commit 5be49ed

Please sign in to comment.