Skip to content

Commit

Permalink
Fixed error in setPresence method and created test
Browse files Browse the repository at this point in the history
  • Loading branch information
philliphenslee committed Oct 23, 2015
1 parent 04f7fd3 commit 06a62f9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 17 deletions.
7 changes: 3 additions & 4 deletions lib/slack/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,17 @@ module.exports = {
callback(null, result);
});
},
setPresence:
/**
* Sets the presence of the authenticated user
* @param {string} presence auto || away
* @param {function} callback(err,result)
*/
function (presence, callback) {
setPresence: function (presence, callback) {

callback = (_.isFunction(callback)) ? callback : _.noop;

if (presence !== 'away' || presence !== 'auto') {
return callback(new Error(errors.missing_required_arg), null);
if (!_.isString(presence) && presence.match(/(away|auto)/)) {
return callback(new Error(errors.missing_required_arg));
}

api.post('users.setPresence', { presence: presence }, function (err, result) {
Expand Down
84 changes: 71 additions & 13 deletions lib/slack/users.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ describe('users', function () {
});

});
it('should return an api error to caller', function (done) {

var scope = nock('https://slack.com')
.post('/api/im.open')
.reply(200, { ok: false, error: 'user_not_found' });

users.getImChannel('U0E1F2G3H4', function (err, result) {
expect(err).to.be.an('error');
expect(err.message).to.equal('user_not_found');
done();
})
});

});
// function get(name, callback)
Expand All @@ -133,7 +145,7 @@ describe('users', function () {
done();
});

it('should open channel by id if not found in cache', function (done) {
it('should open channel by id if not found in cache', function (done) {

var scope = nock('https://slack.com')
.post('/api/users.list')
Expand All @@ -144,7 +156,18 @@ describe('users', function () {
expect(result.ok).to.equal(true);
done();
});
});
it('should return an api error to caller', function (done) {

var scope = nock('https://slack.com')
.post('/api/users.list')
.reply(200, { ok: false, error: 'user_not_found' });

users.getList('U0E1F2G3H4', function (err, result) {
expect(err).to.be.an('error');
expect(err.message).to.equal('user_not_found');
done();
})
});

});
Expand Down Expand Up @@ -180,6 +203,18 @@ describe('users', function () {
});

});
it('should return an api error to caller', function (done) {

var scope = nock('https://slack.com')
.post('/api/users.getPresence')
.reply(200, { ok: false, error: 'user_not_found' });

users.getPresence('U0E1F2G3H4', function (err, result) {
expect(err).to.be.an('error');
expect(err.message).to.equal('user_not_found');
done();
})
});

});
// function get(name, callback)
Expand Down Expand Up @@ -214,34 +249,57 @@ describe('users', function () {
});

});
it('should return an api error to caller', function (done) {

var scope = nock('https://slack.com')
.post('/api/users.info')
.reply(200, { ok: false, error: 'user_not_found' });

users.getInfo('U0E1F2G3H4', function (err, result) {
expect(err).to.be.an('error');
expect(err.message).to.equal('user_not_found');
done();
})
});

});
// function get(name, callback)
describe('#setPresence', function () {

it('should return an error to callback if missing required string argument', function (done) {

users.setPresence(null, function (err, result) {
users.setPresence('away', function (err, result) {
expect(err).to.not.equal(null);
expect(err.message).to.equal('must supply valid argument(s)');
});
done();
});

// it('should return api response', function (done) {
it('should return api response', function (done) {

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

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

// var scope = nock('https://slack.com')
// .post('/api/users.setPresence')
// .reply(200, { ok: true });
});
it('should return an api error to caller', function (done) {

// users.setPresence('away', function (err, result) {
// console.log(err);
// expect(result).to.be.an('object');
// expect(result.ok).to.equal(true);
// done();
// });
var scope = nock('https://slack.com')
.post('/api/users.setPresence')
.reply(200, { ok: false, error: 'account_inactive' });

// });
users.setPresence('auto', function (err, result) {
expect(err).to.be.an('error');
expect(err.message).to.equal('account_inactive');
done();
})
});

});

Expand Down

0 comments on commit 06a62f9

Please sign in to comment.