Skip to content

Commit

Permalink
Added methods and test for groups api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphenslee committed Oct 14, 2015
1 parent 9d599c3 commit b3338e6
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 15 deletions.
57 changes: 55 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,53 @@ SmartSlack.prototype.getChannelById = function (id) {
}
}

/**
* Gets a group by id
* @return {object} group object
*/
SmartSlack.prototype.getGroupById = function (id) {
if (id && this.groups) {
return _.find(this.groups, { id: id });
} else {
throw new Error(errors.missing_required_arg);
}
}

/**
* Gets a group by name
* @return {object} group object
*/
SmartSlack.prototype.getGroupByName = function (name) {
if (name && this.groups) {
return _.find(this.groups, { name: name });
} else {
throw new Error(errors.missing_required_arg);
}
}

/**
* Gets group list
* @param {function} callback
* @return {array} the group list
*/
SmartSlack.prototype.getActiveGroups = function (callback) {
var _this = this;
this._apiCall('groups.list', { "exclude_archived": "1" }, function (data) {

if (data.ok) {
// Save the group list
_this.groups = data.groups;
if (callback) {
callback(data.groups);
}
} else {
if (callback) {
callback(data);
}
}
});
}

/**
* Gets a user by name
* @return {object} user object
Expand Down Expand Up @@ -340,8 +387,14 @@ SmartSlack.prototype.sendSocket = function (channel, text) {
* Sets the users presence
* @param {string} auto || away
*/
SmartSlack.prototype.setPresence = function (presence) {
this._apiCall('users.setPresence', { "presence": presence })
SmartSlack.prototype.setPresence = function (presence,callback) {
if (presence && presence === 'away' || presence === 'auto') {
this._apiCall('users.setPresence', { "presence": presence },function(data) {
callback(data);
})
} else {
throw new Error(errors.missing_required_arg);
}
}

/**
Expand Down
130 changes: 117 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,7 @@ describe('SmartSlack', function () {
"name": "fun",
"created": 1360782804,
"creator": "U024BE7LH",
"is_archived": false,
"is_member": false,
"num_members": 6,
"topic": {
"value": "Fun times",
"creator": "U024BE7LV",
"last_set": 1369677212
},
"purpose": {
"value": "This channel is for fun",
"creator": "U024BE7LH",
"last_set": 1360782804
}
// ...
}

]
Expand All @@ -158,6 +146,44 @@ describe('SmartSlack', function () {
});

});

describe('#getActiveGroups', function () {

var response = {
"ok": true,
"groups": [
{
"id": "C024BE91L",
"name": "special group",
"created": 1360782804,
"creator": "U024BE7LH"
}
// ...
]
}

var slackClient = new SmartSlack(mockopts);

it('exists as a public method on SmartSlack', function (done) {
expect(typeof slackClient.getActiveGroups).to.equal('function');
done();
})

it('should return an array of active Slack groups', function (done) {

var scope = nock('https://slack.com')
.post('/api/groups.list')
.reply(200, response);

slackClient.getActiveGroups(function (data) {
expect(data).to.be.an('array');
expect(data[0].name).to.equal('special group');
done();
});
});

});


describe('#getChannelByName', function () {

Expand Down Expand Up @@ -252,6 +278,44 @@ describe('SmartSlack', function () {
})
});

describe('#getGroupById', function () {

var slackClient = new SmartSlack(mockopts);

slackClient.groups = [ { id: 'G0BC7NYJ0',
name: 'groupname',
is_group: true,
created: 1443296143,
creator: 'U0BC6D9V1',
is_archived: false,
is_mpim: false,
members: [ 'U0BC6D9V1', 'U0BN3JFH7' ],
topic: { value: '', creator: '', last_set: 0 },
purpose:
{ value: 'A private group',
creator: 'U0BC6D9V1',
last_set: 1443296143 } } ]

it('exists as public method on SmartSlack', function (done) {
expect(typeof slackClient.getGroupById).to.equal('function');
done();
});

it('should check for a valid group id argument', function(done){
expect(function () {
slackClient.getGroupById();
}).to.throw('Error missing or invalid required argument');
done();
})

it('should return a group object', function (done) {
var group = slackClient.getGroupById('G0BC7NYJ0');
expect(group).to.be.an('object');
expect(group.name).to.equal('groupname');
done();
})
});

describe('#getUserByName', function () {

var slackClient = new SmartSlack(mockopts);
Expand Down Expand Up @@ -348,6 +412,17 @@ describe('SmartSlack', function () {

});

describe('#onRtmEvent', function () {

var slackClient = new SmartSlack(mockopts);

it('exists as a public method on SmartSlack', function (done) {
expect(typeof slackClient.onRtmEvent).to.equal('function');
done();
})

});

describe('#postMessage', function () {

var slackClient = new SmartSlack(mockopts);
Expand Down Expand Up @@ -413,6 +488,35 @@ describe('SmartSlack', function () {

})

describe('#setPresence', function () {

var slackClient = new SmartSlack(mockopts);

it('should require valid presence {string} argument', function(done){

expect(function () {
slackClient.setPresence();
}).to.throw('Error missing or invalid required argument');
done();
})

it('should return message response', function (done) {

var scope = nock('https://slack.com')
.post('/api/users.setPresence')
.reply(200, {
"ok": true,
});

slackClient.setPresence('away', function (data) {
expect(data).to.be.an('object');
expect(data.ok).to.equal(true);
done();
});
});

});

describe('#_apiCall', function () {

var slackClient = new SmartSlack(mockopts);
Expand Down

0 comments on commit b3338e6

Please sign in to comment.