Skip to content

Commit

Permalink
Tests for incoming events. Fixes #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Jun 23, 2013
1 parent 9b6f0ef commit 92552cd
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ PubSub.prototype.setItemParser = function(parser) {
}

PubSub.prototype._eventNotification = function(stanza) {
var items, subscription, affiliations, configuration, del, purge
var event = stanza.getChild('event', this.NS_EVENT)
if (items = event.getChild('items')) {
if (items.getChild('item'))
Expand Down Expand Up @@ -523,7 +524,8 @@ PubSub.prototype._configurationUpdate = function(stanza, configuration) {

PubSub.prototype._getConfigurationChanges = function(configuration, data) {
data.node = configuration.attrs.node
if (x = configuration.getChild('x', dataForm.NS))
var x
if (null != (x = configuration.getChild('x', dataForm.NS)))
data.configuration = dataForm.parseFields(x)
}

Expand All @@ -538,7 +540,7 @@ PubSub.prototype._getSubscriptionUpdate = function(subscription, data) {
data.node = subscription.attrs.node
data.subscription = subscription.attrs.subscription
if (subscription.attrs.jid)
data.jid = subscriptions.attrs.jid
data.jid = this._getJid(subscription.attrs.jid)
}

PubSub.prototype._affiliationUpdate = function(stanza, affiliations) {
Expand All @@ -548,12 +550,12 @@ PubSub.prototype._affiliationUpdate = function(stanza, affiliations) {
return true
}

PubSub.prototype._getAffiliationUpdate = function(affilations, data) {
PubSub.prototype._getAffiliationUpdate = function(affiliations, data) {
var affiliation = affiliations.getChild('affiliation')
data.node = affiliations.attrs.node
data.affiliation = affiliation.attrs.affiliation
if (affiliation.attrs.jid)
data.jid = affiliation.attrs.jid
data.jid = this._getJid(affiliation.attrs.jid)
}

PubSub.prototype._deleteNodeNotification = function(stanza, del) {
Expand All @@ -565,13 +567,14 @@ PubSub.prototype._deleteNodeNotification = function(stanza, del) {

PubSub.prototype._getDeleteNodeNotification = function(del, data) {
data.node = del.attrs.node
var redirect
if (redirect = del.getChild('redirect'))
data.redirect = redirect.attrs.uri
}

PubSub.prototype._itemDeleteNotification = function(stanza, items) {
var data = { from: stanza.attrs.from }
this._getItemData(items, data)
this._getItemData(items, data, 'retract')
this._getHeaderData(stanza, data)
this.socket.emit('xmpp.pubsub.push.retract', data)
return true
Expand All @@ -581,8 +584,9 @@ PubSub.prototype._itemNotification = function(stanza, items) {
var data = { from: stanza.attrs.from }
this._getItemData(items, data)
if (stanza.getChild('headers'))
this._getHeaderData(stanza, data)
if (stanza.getChild('delay')) data.delay = stanza.getChild('delay').attrs.stamp
this._getHeaderData(stanza, data)
if (stanza.getChild('delay'))
data.delay = stanza.getChild('delay').attrs.stamp
this.socket.emit('xmpp.pubsub.push.item', data)
return true
}
Expand All @@ -594,18 +598,21 @@ PubSub.prototype._purgeNodeNotification = function(stanza, purge) {
}

PubSub.prototype._getHeaderData = function(stanza, data) {
var headers
if (!(headers = stanza.getChild('headers', this.NS_HEADERS))) return
data.headers = []
headers.getChildren().forEach(function(header) {
headers.children.forEach(function(header) {
data.headers.push({name: header.attrs.name, value: header.getText() })
})
}

PubSub.prototype._getItemData = function(items, data) {
PubSub.prototype._getItemData = function(items, data, type) {
data.node = items.attrs.node
if (!(item = items.getChild('item'))) return false
var item
if (!(item = items.getChild(type || 'item'))) return false
data.id = item.attrs.id
if (item.attrs.publisher) item.publisher = ite.attrs.publisher
if (item.attrs.publisher)
data.publisher = this._getJid(item.attrs.publisher)
if (item.children.length > 0)
data.entry = this._getItemParser().parse(item)
}
Expand Down
231 changes: 231 additions & 0 deletions test/lib/pubsub.events.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,237 @@ describe('Publish-Subscribe', function() {
socket.once('xmpp.pubsub.push.authorisation', callback)
pubsub.handle(stanza)
})

})

describe('Handles incoming event notifications', function() {

describe('New items', function() {

it('Handles basic item notification', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<items node="twelfth night">'
+ '<item id="item-5"></item>'
+ '</items></event></message>'
)
socket.once('xmpp.pubsub.push.item', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
data.id.should.equal('item-5')
done()
})
pubsub.handle(stanza)
})

it('Handles full item notification', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<items node="twelfth night">'
+ '<item id="item-5" publisher="romeo@example.com">'
+ '<body>item-5-content</body>'
+ '</item>'
+ '</items></event>'
+ '<delay stamp="2013-06-23 20:00:00+0100" />'
+ '<headers xmlns="' + pubsub.NS_HEADERS + '">'
+ '<header name="key">value</header>'
+ '</headers>'
+ '</message>'
)
socket.once('xmpp.pubsub.push.item', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
data.id.should.equal('item-5')
data.entry.should.eql({ body: 'item-5-content' })
data.delay.should.equal("2013-06-23 20:00:00+0100")
data.headers.should.eql([
{ name: 'key', value: 'value' }
])
data.publisher.should.eql({
domain: 'example.com',
user: 'romeo'
})
done()
})
pubsub.handle(stanza)
})

})

describe('Item retract', function() {

it('Handles a delete', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<items node="twelfth night">'
+ '<retract id="item-5" />'
+ '</items></event>'
+ '</message>'
)
socket.once('xmpp.pubsub.push.retract', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
data.id.should.equal('item-5')
done()
})
pubsub.handle(stanza)

})

it('Handles delete with headers', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<items node="twelfth night">'
+ '<retract id="item-5" />'
+ '</items></event>'
+ '<headers xmlns="' + pubsub.NS_HEADERS + '">'
+ '<header name="key">value</header>'
+ '</headers>'
+ '</message>'
)
socket.once('xmpp.pubsub.push.retract', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
data.id.should.equal('item-5')
data.headers.should.eql([
{ name: 'key', value: 'value' }
])
done()
})
pubsub.handle(stanza)
})

})

it('Passes on subscription updates', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<subscription subscription="subscribed" '
+ 'node="twelfth night" jid="romeo@example.com" />'
+ '</event></message>'
)
socket.once('xmpp.pubsub.push.subscription', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
data.subscription.should.equal('subscribed')
data.jid.should.eql({
domain: 'example.com',
user: 'romeo'
})
done()
})
pubsub.handle(stanza)
})

it('Passes on affiliation change', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<affiliations node="twelfth night">'
+ '<affiliation affiliation="publisher" '
+ 'jid="romeo@example.com" />'
+ '</affiliations>'
+ '</event></message>'
)
socket.once('xmpp.pubsub.push.affiliation', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
data.affiliation.should.equal('publisher')
data.jid.should.eql({
domain: 'example.com',
user: 'romeo'
})
done()
})
pubsub.handle(stanza)
})

it('Handles configuration changes', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<configuration node="twelfth night">'
+ '<x xmlns="jabber:x:data" type="result">'
+ '<field var="FORM_TYPE" type="hidden">'
+ '<value>'
+ 'http://jabber.org/protocol/pubsub#node_config'
+ '</value>'
+ '</field>'
+ '<field var="pubsub#title">'
+ '<value>A great comedy</value>'
+ '</field>'
+ '</x>'
+ '</configuration></event></message>'
)
socket.once('xmpp.pubsub.push.configuration', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
data.configuration.should.exist
data.configuration.fields.length.should.equal(1)
data.configuration.fields[0].var.should.equal('pubsub#title')
data.configuration.fields[0].value
.should.equal('A great comedy')
done()
})
pubsub.handle(stanza)
})

describe('Node delete', function() {

it('Can handle basic node delete', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<delete node="twelfth night"/>'
+ '</event></message>'
)
socket.once('xmpp.pubsub.push.delete', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
done()
})
pubsub.handle(stanza)
})

it('Can handle node delete with redirect', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<delete node="twelfth night">'
+ '<redirect uri="pubsub.marlowe.lit?node=dido" />'
+ '</delete></event></message>'
)
socket.once('xmpp.pubsub.push.delete', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
data.redirect.should.equal('pubsub.marlowe.lit?node=dido')
done()
})
pubsub.handle(stanza)
})

})

it('Node purge notification', function(done) {
var stanza = new ltx.parse(
'<message from="pubsub.shakespeare.lit">'
+ '<event xmlns="' + pubsub.NS_EVENT + '">'
+ '<purge node="twelfth night"/>'
+ '</event></message>'
)
socket.once('xmpp.pubsub.push.purge', function(data) {
data.from.should.equal('pubsub.shakespeare.lit')
data.node.should.equal('twelfth night')
done()
})
pubsub.handle(stanza)
})

})

})

0 comments on commit 92552cd

Please sign in to comment.