Skip to content

Commit

Permalink
Subscription configuration: Fixes #10. Fixes #8.
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Jun 22, 2013
1 parent ee8d190 commit 1479174
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 30 deletions.
29 changes: 8 additions & 21 deletions lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ PubSub.prototype.createNode = function(data, callback) {
this.NS_CONFIG
)
} catch(e) {
console.error(e)
return this._clientError(
'Badly formatted data form', data, callback
)
Expand Down Expand Up @@ -187,13 +186,9 @@ PubSub.prototype.subscriptionConfigurationGet = function(data, callback) {
this.manager.trackId(stanza.root().attr('id'), function(stanza) {
if (stanza.attrs.type == 'error')
return callback(self._parseError(stanza), null)
var title, description
var data = {}
if (null != (title = stanza.getChild('title')))
data.title = title.getText()
if (null != (description = stanza.getChild('description')))
data.description = description.getText
data.form = dataForm.parseFields(stanza.getChild('query').getChild('x'))
var data = dataForm.parseFields(
stanza.getChild('pubsub').getChild('options').getChild('x')
)
callback(null, data)
})
this.client.send(stanza)
Expand All @@ -209,13 +204,9 @@ PubSub.prototype.subscriptionDefaultConfigurationGet = function(data, callback)
this.manager.trackId(stanza.root().attr('id'), function(stanza) {
if (stanza.attrs.type == 'error')
return callback(self._parseError(stanza), null)
var title, description
var data = {}
if (null != (title = stanza.getChild('title')))
data.title = title.getText()
if (null != (description = stanza.getChild('description')))
data.description = description.getText
data.form = dataForm.parseFields(stanza.getChild('query').getChild('x'))
var data = dataForm.parseFields(
stanza.getChild('pubsub').getChild('default').getChild('x')
)
callback(null, data)
})
this.client.send(stanza)
Expand All @@ -230,11 +221,10 @@ PubSub.prototype.subscriptionConfigurationSet = function(data, callback) {
return this._clientError("Missing 'form' key", data, callback)

var self = this
var stanza = this._getStanza(data, 'get', 'options')
var stanza = this._getStanza(data, 'set', 'options')
try {
dataForm.addForm(stanza, data.form, this.NS_SUB_OPTIONS)
dataForm.addForm(stanza, data.form, this.NS_SUB_OPTIONS, 'form')
} catch(e) {
console.error(e)
return this._clientError('Badly formatted data form', data, callback)
}
this.manager.trackId(stanza.root().attr('id'), function(stanza) {
Expand All @@ -259,15 +249,13 @@ PubSub.prototype.publish = function(data, callback) {
try {
stanza.c('item', details).children = [ this._getItemParser().build(data.content) ]
} catch (e) {
console.error('Could not build pubsub payload', e)
return callback(e, null)
}
if (data.options) {
stanza.root().getChild('pubsub').c('publish-options')
try {
dataForm.addForm(stanza, data.options, this.NS_PUBLISH_OPTIONS)
} catch(e) {
console.error(e)
return this._clientError(
'Badly formatted data form', data, callback
)
Expand Down Expand Up @@ -347,7 +335,6 @@ PubSub.prototype.setNodeConfiguration = function(data, callback) {
try {
dataForm.addForm(stanza, data.form, this.NS_CONFIG)
} catch(e) {
console.error(e)
return this._clientError('Badly formatted data form', data, callback)
}
this.manager.trackId(stanza.root().attr('id'), function(stanza) {
Expand Down
155 changes: 146 additions & 9 deletions test/lib/pubsub.subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ describe('Publish-Subscribe', function() {
},
makeCallback: function(error, data) {
this.callback(error, data)
}
},
jid: 'juliet@example.net'
}
pubsub = new PubSub()
pubsub.init(manager)
Expand Down Expand Up @@ -530,7 +531,29 @@ describe('Publish-Subscribe', function() {
})

it('Returns data from successful request', function(done) {
done('Not implemented yet')
xmpp.once('stanza', function(stanza) {
manager.makeCallback(
helper.getStanza('subscription-options-default')
)
})
var callback = function(error, data) {
should.not.exist(error)
data.fields.length.should.equal(2)
data.fields[0].var.should.equal('pubsub#notifications')
data.fields[0].value.should.equal('1')
data.fields[1].var.should.equal('pubsub#include_body')
data.fields[1].value.should.equal('0')
done()
}
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night'
}
socket.emit(
'xmpp.pubsub.subscription.config.default',
request,
callback
)
})

})
Expand Down Expand Up @@ -559,7 +582,7 @@ describe('Publish-Subscribe', function() {
})

it('Errors when no \'node\' key provided', function(done) {
var request = { to: 'pubsub.shakespeare.lit' }
var request = { to: 'pubsub.shakespeare.lit' }
xmpp.once('stanza', function() {
done('Unexpected outgoing stanza')
})
Expand All @@ -580,7 +603,26 @@ describe('Publish-Subscribe', function() {
})

it('Sends expected stanza', function(done) {
done('Not implemented yet')
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night'
}
xmpp.once('stanza', function(stanza) {
stanza.is('iq').should.equal.true
stanza.attrs.type.should.equal('get')
stanza.attrs.to.should.equal(request.to)
stanza.attrs.id.should.exist
stanza.getChild('pubsub', pubsub.NS_PUBSUB).should.exist
var element = stanza.getChild('pubsub').getChild('options')
element.should.exist
element.attrs.node.should.equal(request.node)
done()
})
socket.emit(
'xmpp.pubsub.subscription.config.get',
request,
function(){}
)
})

it('Handles error response', function(done) {
Expand All @@ -607,7 +649,30 @@ describe('Publish-Subscribe', function() {
})

it('Returns data from successful request', function(done) {
done('Not implemented yet')
xmpp.once('stanza', function(stanza) {
manager.makeCallback(
helper.getStanza('subscription-options')
)
})
var callback = function(error, data) {
should.not.exist(error)
data.fields.length.should.equal(2)
data.fields[0].var.should.equal('pubsub#notifications')
data.fields[0].value.should.equal('1')
data.fields[1].var.should.equal('pubsub#include_body')
data.fields[1].value.should.equal('0')
done()
}
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night'
}
socket.emit(
'xmpp.pubsub.subscription.config.get',
request,
callback
)

})

})
Expand Down Expand Up @@ -706,11 +771,66 @@ describe('Publish-Subscribe', function() {
})

it('Sends expected stanza', function(done) {
done('Not implemented yet')
xmpp.once('stanza', function(stanza) {
stanza.is('iq').should.be.true
stanza.attrs.type.should.equal('set')
stanza.attrs.to.should.equal(request.to)
stanza.attrs.id.should.exist
var options = stanza.getChild('pubsub', pubsub.NS_PUBSUB)
.getChild('options')
options.attrs.node.should.equal(request.node)
options.attrs.jid.should.equal(request.jid)
var dataForm = options.getChild('x', 'jabber:x:data')
dataForm.should.exist
dataForm.attrs.type.should.equal('form')
dataForm.children.length.should.equal(3)
var fields = dataForm.children
fields[0].attrs.var.should.equal('FORM_TYPE')
fields[0].attrs.type.should.equal('hidden')
fields[0].getChildText('value')
.should.equal(pubsub.NS_SUB_OPTIONS)
fields[1].attrs.var.should.equal('pubsub#notifications')
fields[1].getChildText('value').should.equal('true')
fields[2].attrs.var.should.equal('pubsub#include_body')
fields[2].getChildText('value').should.equal('false')
done()
})
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
form: [
{ var: 'pubsub#notifications', value: true },
{ var: 'pubsub#include_body', value: false }
],
jid: 'romeo@example.com'
}
socket.emit(
'xmpp.pubsub.subscription.config.set',
request,
function() {}
)
})

it('Fills JID if not provided', function(done) {
done('Not implemented yet')
xmpp.once('stanza', function(stanza) {
stanza.getChild('pubsub', pubsub.NS_PUBSUB)
.getChild('options')
.attrs.jid.should.equal(manager.jid)
done()
})
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
form: [
{ var: 'pubsub#notifications', value: true },
{ var: 'pubsub#include_body', value: false }
]
}
socket.emit(
'xmpp.pubsub.subscription.config.set',
request,
function() {}
)
})

it('Handles error response stanza', function(done) {
Expand Down Expand Up @@ -738,7 +858,24 @@ describe('Publish-Subscribe', function() {
})

it('Returns true for succesful set', function(done) {
done('Not implemented yet')
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',
form: []
}
socket.emit(
'xmpp.pubsub.subscription.config.set',
request,
callback
)
})

})
Expand Down
16 changes: 16 additions & 0 deletions test/resources/subscription-options
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<iq type='result'
from='pubsub.shakespeare.lit'
to='juliet@example.net/balcony'
id='1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<options>
<x xmlns='jabber:x:data' type='result'>
<field var='FORM_TYPE' type='hidden'>
<value>http://jabber.org/protocol/pubsub#subscribe_options</value>
</field>
<field var='pubsub#notifications'><value>1</value></field>
<field var='pubsub#include_body'><value>0</value></field>
</x>
</options>
</pubsub>
</iq>
16 changes: 16 additions & 0 deletions test/resources/subscription-options-default
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<iq type='result'
from='pubsub.shakespeare.lit'
to='juliet@example.net/balcony'
id='1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<default>
<x xmlns='jabber:x:data' type='result'>
<field var='FORM_TYPE' type='hidden'>
<value>http://jabber.org/protocol/pubsub#subscribe_options</value>
</field>
<field var='pubsub#notifications'><value>1</value></field>
<field var='pubsub#include_body'><value>0</value></field>
</x>
</default>
</pubsub>
</iq>

0 comments on commit 1479174

Please sign in to comment.