Skip to content

Commit

Permalink
Support setting of subscription by owner. Fixes #20.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lloyd Watkin committed Aug 9, 2013
1 parent be74213 commit 6f29649
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 1 deletion.
31 changes: 31 additions & 0 deletions lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ PubSub.prototype.registerEvents = function() {
this.socket.on('xmpp.pubsub.unsubscribe', function(data, callback) {
self.unsubscribe(data, callback)
})
this.socket.on('xmpp.pubsub.subscription', function(data, callback) {
self.setSubscription(data, callback)
})
this.socket.on('xmpp.pubsub.subscription.config.get', function(data, callback) {
self.subscriptionConfigurationGet(data, callback)
})
Expand Down Expand Up @@ -184,6 +187,34 @@ PubSub.prototype.unsubscribe = function(data, callback) {
this.client.send(stanza)
}

PubSub.prototype.setSubscription = function(data, callback) {
if (typeof callback !== 'function')
return this._clientError('Missing callback', data)
if (!data.to) return this._clientError("Missing 'to' key", data, callback)
if (!data.node)
return this._clientError("Missing 'node' key", data, callback)
if (!data.jid)
return this._clientError("Missing 'jid' key", data, callback)
if (!data.subscription)
return this._clientError("Missing 'subscription' key", data, callback)

var self = this
var stanza = this._getStanza(
{ to: data.to, node: data.node }, 'set', 'subscriptions', this.NS_OWNER
)
var subscription = stanza.c(
'subscription',
{ jid: data.jid, subscription: data.subscription }
)

this.manager.trackId(stanza.root().attr('id'), function(stanza) {
if ('error' == stanza.attrs.type)
return callback(self._parseError(stanza), null)
callback(null, true)
})
this.client.send(stanza)
}

PubSub.prototype.subscriptionConfigurationGet = function(data, callback) {
if (typeof callback !== 'function')
return this._clientError('Missing callback', data)
Expand Down
2 changes: 1 addition & 1 deletion test/lib/pubsub.create.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('Publish-Subscribe', function() {
it('Allows advanced node creation', function(done) {
xmpp.once('stanza', function(stanza) {
var create = stanza.getChild('pubsub').getChild('create')
var dataForm =stanza.getChild('pubsub')
var dataForm = stanza.getChild('pubsub')
.getChild('configure')
.getChild('x', 'jabber:x:data')
dataForm.should.exist
Expand Down
214 changes: 214 additions & 0 deletions test/lib/pubsub.subscription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
var should = require('should')
, PubSub = require('../../lib/pubsub')
, ltx = require('ltx')
, helper = require('../helper')

var RSM_NS = require('xmpp-ftw/lib/utils/xep-0059').NS

describe('Publish-Subscribe', function() {

var pubsub, socket, xmpp, manager

before(function() {
socket = new helper.Eventer()
xmpp = new helper.Eventer()
manager = {
socket: socket,
client: xmpp,
trackId: function(id, callback) {
this.callback = callback
},
makeCallback: function(error, data) {
this.callback(error, data)
},
jid: 'juliet@example.net'
}
pubsub = new PubSub()
pubsub.init(manager)
})

describe('Subscription', function() {

it('Errors when no callback provided', function(done) {
xmpp.once('stanza', function() {
done('Unexpected outgoing stanza')
})
socket.once('xmpp.error.client', function(error) {
error.type.should.equal('modify')
error.condition.should.equal('client-error')
error.description.should.equal("Missing callback")
error.request.should.eql({})
xmpp.removeAllListeners('stanza')
done()
})
socket.emit('xmpp.pubsub.subscription', {})
})

it('Errors when non-function callback provided', function(done) {
xmpp.once('stanza', function() {
done('Unexpected outgoing stanza')
})
socket.once('xmpp.error.client', function(error) {
error.type.should.equal('modify')
error.condition.should.equal('client-error')
error.description.should.equal("Missing callback")
error.request.should.eql({})
xmpp.removeAllListeners('stanza')
done()
})
socket.emit('xmpp.pubsub.subscription', {}, true)
})

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.subscription', 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.subscription', request, callback)
})

it('Errors if no \'jid\' key provided', function(done) {
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night'
}
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 'jid' key")
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
done()
}
socket.emit('xmpp.pubsub.subscription', request, callback)
})

it('Errors if no \'subscription\' key provided', function(done) {
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
jid: 'juliet@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 'subscription' key")
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
done()
}
socket.emit('xmpp.pubsub.subscription', request, callback)
})

it('Sends expected stanza', function(done) {
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
jid: 'juliet@shakespeare.lit',
subscription: 'subscribed'
}
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_OWNER).should.exist
var subscriptions = stanza.getChild('pubsub')
.getChild('subscriptions')
subscriptions.should.exist
subscriptions.attrs.node.should.equal(request.node)

var subscription = subscriptions.getChild('subscription')
subscription.attrs.jid.should.equal(request.jid)
subscription.attrs.subscription
.should.equal(request.subscription)

done()
})
socket.emit('xmpp.pubsub.subscription', request, function() {})
})

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',
jid: 'juliet@shakespeare.lit',
subscription: 'subscribed'
}
socket.emit(
'xmpp.pubsub.subscription',
request,
callback
)
})

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

})

})

0 comments on commit 6f29649

Please sign in to comment.