Skip to content

Commit

Permalink
Node affiliations: Added tests. Fixes #14.
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Jun 22, 2013
1 parent 1479174 commit 2e9f906
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,11 @@ PubSub.prototype.getAffiliations = function(data, callback) {
if (stanza.attrs.type == 'error')
return callback(self._parseError(stanza), null)
var affiliations = []
stanza.getChild('pubsub').getChild('affiliation').forEach(function(affiliation) {
stanza.getChild('pubsub')
.getChild('affiliations')
.children.forEach(function(affiliation) {
affiliations.push({
jid: self._getJid(affiliation.attrs.jid),
node: affiliation.attrs.node,
affiliation: affiliation.attrs.affiliation
})
})
Expand Down
173 changes: 173 additions & 0 deletions test/lib/pubsub.affiliations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
var should = require('should')
, PubSub = require('../../lib/pubsub')
, ltx = require('ltx')
, helper = require('../helper')

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)
}
}
pubsub = new PubSub()
pubsub.init(manager)
})

describe('Get affiliations', function() {

it('Errors if missing \'to\' key', 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.affiliations', request, callback)
})

it('Errors if node subs requested and no owner', function(done) {
var request = { to: 'pubsub.shakespeare.lit', owner: true }
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("Can only do 'owner' for a node")
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
done()
}
socket.emit('xmpp.pubsub.affiliations', request, callback)
})

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

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

it('Correct stanza for user affiliations to node', 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('get')
stanza.attrs.id.should.exist
stanza.getChild('pubsub', pubsub.NS_PUBSUB).should.exist
var pubsubElement = stanza.getChild('pubsub')
pubsubElement.getChild('affiliations').should.exist
pubsubElement.getChild('affiliations').attrs.node
.should.equal(request.node)
done()
})
socket.emit('xmpp.pubsub.affiliations', request)
})

it('Handles 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.affiliations',
request,
callback
)

})

it('Sends a list of affiliations', function(done) {
xmpp.once('stanza', function(stanza) {
manager.makeCallback(helper.getStanza('affiliations'))
})
var callback = function(error, data) {
should.not.exist(error)
data.length.should.equal(2)
data[0].node.should.equal('twelfth night')
data[0].affiliation.should.equal('owner')
data[1].node.should.equal('a comedy of errors')
data[1].affiliation.should.equal('publisher')
done()
}
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night'
}
socket.emit(
'xmpp.pubsub.affiliations',
request,
callback
)
})

})
})
11 changes: 11 additions & 0 deletions test/resources/affiliations
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<iq type='result'
from='pubsub.shakespeare.lit'
to='juliet@example.net/balcony'
id='1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<affiliations>
<affiliation node='twelfth night' affiliation='owner' />
<affiliation node='a comedy of errors' affiliation='publisher' />
</affiliations>
</pubsub>
</iq>

0 comments on commit 2e9f906

Please sign in to comment.