Skip to content

Commit

Permalink
Added tests for subscription authorisation requests. Fixes #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Jun 23, 2013
1 parent e4df8de commit 9b6f0ef
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ PubSub.prototype.registerEvents = function() {
PubSub.prototype.handles = function(stanza) {
if (stanza.is('message')
&& (null != stanza.getChild('event', this.NS_EVENT))) return true
var field, value, x
return (stanza.is('message')
&& (null != stanza.getChild('x'))
&& (null != (field = stanza.getChildByAttr('type', 'hidden')))
&& (null != (x = stanza.getChild('x')))
&& (null != (field = x.getChildByAttr('type', 'hidden')))
&& (null != (value = field.getChild('value')))
&& (this.NS_SUBSCRIBE_AUTHORISATION == value.getText()))
}
Expand Down Expand Up @@ -613,14 +614,19 @@ PubSub.prototype._handleSubscriptionAuthorisation = function(stanza) {
var data = { from: stanza.attrs.from, id: stanza.attrs.id }
var to = stanza.attrs.from
var id = stanza.attrs.id
var self = this
data.form = dataForm.parseFields(stanza.getChild('x'))
this.socket.emit('xmpp.pubsub.push.authorisation', data, function(data) {
var stanza = new builder.Element(
'message',
{ to: to, id: id }
)
dataForm.addForm(stanza, data, this.NS_SUBSCRIPTION_AUTHORISATION)
this.client.send(stanza)
try {
dataForm.addForm(stanza, data, self.NS_SUBSCRIBE_AUTHORISATION)
} catch(e) {
return self._clientError('Badly formatted data form', data)
}
self.client.send(stanza)
})
}

Expand Down
112 changes: 112 additions & 0 deletions test/lib/pubsub.events.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,116 @@ describe('Publish-Subscribe', function() {
pubsub.init(manager)
})

describe('Handles certain packets', function() {

it('Doesn\'t handle <iq> packets', function() {
pubsub.handles(new ltx.parse('<iq/>')).should.be.false
})

it('Doesn\'t handle <presence> packets', function() {
pubsub.handles(new ltx.parse('<presence/>')).should.be.false
})

it('Handles messages with \'event\' namespace', function() {
var stanza = new ltx.parse('<message><event xmlns="'
+ pubsub.NS_EVENT + '" /></message>')
pubsub.handles(stanza).should.be.true
})

it('Handles authorisation requests', function() {
var stanza = new ltx.parse(
'<message><x><field type="hidden"><value>'
+ pubsub.NS_SUBSCRIBE_AUTHORISATION
+ '</value></field></x></message>'
)
pubsub.handles(stanza).should.be.true
})

it('Doesn\'t handle other messages', function() {
var stanza = new ltx.parse('<message><event xmlns="'
+ pubsub.NS_PUBSUB + '" /></message>')
pubsub.handles(stanza).should.be.false
})

})

describe('Handles authorisation requests', function() {

it('Sends expected data', function(done) {
var stanza = helper.getStanza('subscription-authorisation')
var callback = function(data, callback) {
data.id.should.equal('1')
data.from.should.equal('pubsub.shakespeare.lit')
data.form.title.should.equal('Subscription request')
data.form.instructions.should.equal('Ok or cancel')
data.form.fields.length.should.equal(3)

data.form.fields[0].var.should.equal('pubsub#node')
data.form.fields[0].type.should.equal('text-single')
data.form.fields[0].label.should.equal('Node')
data.form.fields[0].value.should.equal('twelfth night')

data.form.fields[1].var.should.equal('pubsub#subscriber_jid')
data.form.fields[1].type.should.equal('jid-single')
data.form.fields[1].label.should.equal('Subscriber Address')
data.form.fields[1].value.should.equal('romeo@example.com')

data.form.fields[2].var.should.equal('pubsub#allow')
data.form.fields[2].type.should.equal('boolean')
data.form.fields[2].label.should.equal('Allow?')
data.form.fields[2].value.should.be.false

done()
}
socket.once('xmpp.pubsub.push.authorisation', callback)
pubsub.handle(stanza)
})

it('Errors if data form can not be parsed', function(done) {
var stanza = helper.getStanza('subscription-authorisation')
var callback = function(data, callback) {
callback({})
}
// No client callback so we arrive at a standard event
socket.once('xmpp.error.client', function(data) {
data.should.eql({ type: 'modify',
condition: 'client-error',
description: 'Badly formatted data form',
request: {}
})
done()
})

socket.once('xmpp.pubsub.push.authorisation', callback)
pubsub.handle(stanza)
})

it('Sends expected response stanza', function(done) {
var stanza = helper.getStanza('subscription-authorisation')
xmpp.once('stanza', function(stanza) {
stanza.is('message').should.be.true
stanza.attrs.to.should.equal('pubsub.shakespeare.lit')
stanza.attrs.id.should.equal('1')
var dataForm = stanza.getChild('x', 'jabber:x:data')
dataForm.should.exist
dataForm.attrs.type.should.equal('submit')
dataForm.children.length.should.equal(2)
dataForm.children[0].attrs.var.should.equal('FORM_TYPE')
dataForm.children[0].getChildText('value')
.should.equal(pubsub.NS_SUBSCRIBE_AUTHORISATION)
dataForm.children[1].attrs.var.should.equal('pubsub#allow')
dataForm.children[1].getChildText('value')
.should.equal('true')
done()
})
var callback = function(data, callback) {
callback([
{ var: 'pubsub#allow', value: true }
])
}
socket.once('xmpp.pubsub.push.authorisation', callback)
pubsub.handle(stanza)
})
})

})
21 changes: 21 additions & 0 deletions test/resources/subscription-authorisation
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<message to='juliet@example.net' from='pubsub.shakespeare.lit' id='1'>
<x xmlns='jabber:x:data' type='form'>
<title>Subscription request</title>
<instructions>Ok or cancel</instructions>
<field var='FORM_TYPE' type='hidden'>
<value>http://jabber.org/protocol/pubsub#subscribe_authorization</value>
</field>
<field var='pubsub#node' type='text-single' label='Node'>
<value>twelfth night</value>
</field>
<field var='pubsub#subscriber_jid'
type='jid-single'
label='Subscriber Address'>
<value>romeo@example.com</value>
</field>
<field var='pubsub#allow' type='boolean'
label='Allow?'>
<value>false</value>
</field>
</x>
</message>

0 comments on commit 9b6f0ef

Please sign in to comment.