Skip to content

Commit

Permalink
Unsubscribe: All tests added. Fixes #6.
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Jun 22, 2013
1 parent 913da96 commit 2872813
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ PubSub.prototype.unsubscribe = function(data, callback) {
var self = this
var stanza = this._getStanza(data, 'set', 'unsubscribe')
this.manager.trackId(stanza.root().attr('id'), function(stanza) {
if (stanza.attrs.type == 'error') callback(self._parseError(stanza), null)
if (stanza.attrs.type == 'error')
return callback(self._parseError(stanza), null)
callback(null, true)
})
this.client.send(stanza)
Expand Down
116 changes: 116 additions & 0 deletions test/lib/pubsub.subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,120 @@ describe('Publish-Subscribe', function() {

})

describe('Unsubscribe', function() {

it('Errors if no \'to\' key provided', function(done) {
var request = {}
xmpp.once('stanza', function() {
done('Unexpected outgoing stanza')
})
var callback = function(error, success) {
should.not.exist(success)
error.type.should.equal('modify')
error.condition.should.equal('client-error')
error.description.should.equal("Missing 'to' key")
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
done()
}
socket.emit('xmpp.pubsub.unsubscribe', request, callback)
})

it('Errors if no \'node\' key provided', function(done) {
var request = { to: 'pubsub.shakespeare.lit' }
xmpp.once('stanza', function() {
done('Unexpected outgoing stanza')
})
var callback = function(error, success) {
should.not.exist(success)
error.type.should.equal('modify')
error.condition.should.equal('client-error')
error.description.should.equal("Missing 'node' key")
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
done()
}
socket.emit('xmpp.pubsub.unsubscribe', request, callback)
})

it('Sends expected stanza', function(done) {
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night'
}
xmpp.once('stanza', function(stanza) {
stanza.is('iq').should.be.true
stanza.attrs.to.should.equal(request.to)
stanza.attrs.type.should.equal('set')
stanza.attrs.id.should.exist
stanza.getChild('pubsub', pubsub.NS_PUBSUB).should.exist
var pubsubElement = stanza.getChild('pubsub')
pubsubElement.getChild('unsubscribe').should.exist
pubsubElement.getChild('unsubscribe').attrs.node
.should.equal(request.node)
done()
})
socket.emit('xmpp.pubsub.unsubscribe', request)
})

it('Sends expected stanza when \'subscription id\'', function(done) {
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
id: '123456'
}
xmpp.once('stanza', function(stanza) {
stanza.getChild('pubsub')
.getChild('unsubscribe')
.attrs.subid.should.equal('123456')
done()
})
socket.emit('xmpp.pubsub.unsubscribe', request)
})

it('Handles an error stanza response', function(done) {
xmpp.once('stanza', function(stanza) {
manager.makeCallback(helper.getStanza('iq-error'))
})
var callback = function(error, success) {
should.not.exist(success)
error.should.eql({
type: 'cancel',
condition: 'error-condition'
})
done()
}
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night'
}
socket.emit(
'xmpp.pubsub.unsubscribe',
request,
callback
)
})

it('Handles success response', function(done) {
xmpp.once('stanza', function(stanza) {
manager.makeCallback(helper.getStanza('iq-result'))
})
var callback = function(error, success) {
should.not.exist(error)
success.should.be.true
done()
}
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night'
}
socket.emit(
'xmpp.pubsub.unsubscribe',
request,
callback
)
})

})

})
12 changes: 12 additions & 0 deletions test/resources/subscribe-subid
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<iq type='result'
from='pubsub.shakespeare.lit'
to='juliet@example.net/balcony'
id='1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<subscription
node='twelfth night'
jid='juliet@example.net'
subid='123456'
subscription='subscribed' />
</pubsub>
</iq>

0 comments on commit 2872813

Please sign in to comment.