Skip to content

Commit

Permalink
Affiliations: Tests added for setting affiliations. Fixes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Jun 22, 2013
1 parent 065fff4 commit 62303fe
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,14 @@ PubSub.prototype.getAffiliations = function(data, callback) {
}

PubSub.prototype.setAffiliation = function(data, callback) {
if (!data.to) return this._clientError("Missing 'to' key", data, callback)
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._client("Missing 'jid' jey", data, callback)

if (!data.jid)
return this._clientError("Missing 'jid' key", data, callback)
if (!data.affiliation)
return this._clientError("Missing 'affiliation' key", data, callback)
var self = this
var stanza = this._getStanza(data, 'set', 'affiliations', this.NS_OWNER)
var detail = { jid: data.jid }
Expand Down
171 changes: 171 additions & 0 deletions test/lib/pubsub.affiliations.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,175 @@ describe('Publish-Subscribe', function() {
})

})

describe('Set affiliation', function() {

it('Errors when 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.affiliation',
request,
callback
)
})

it('Errors when missing \'node\' key', 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.affiliation',
request,
callback
)
})

it('Errors when missing \'jid\' key', 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.affiliation',
request,
callback
)
})

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

it('Sends expected stanza', function(done) {
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
jid: 'romeo@example.com',
affiliation: 'moderator'
}
xmpp.once('stanza', function(stanza) {
stanza.is('iq').should.be.true
stanza.attrs.id.should.exist
stanza.attrs.to.should.equal(request.to)
var element = stanza
.getChild('pubsub', pubsub.NS_OWNER)
.getChild('affiliations')
element.should.exist
element.attrs.node.should.equal(request.node)
element.children.length.should.equal(1)
element.children[0].attrs.jid.should.equal(request.jid)
element.children[0].attrs.affiliation
.should.equal(request.affiliation)
done()
})
socket.emit(
'xmpp.pubsub.affiliation',
request,
function() {}
)
})

it('Handles error stanza', 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: 'romeo@example.com',
affiliation: 'publisher'
}
socket.emit(
'xmpp.pubsub.affiliation',
request,
callback
)
})

it('Sends true with successful request', function(done) {
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',
jid: 'romeo@example.com',
affiliation: 'publisher'
}
socket.emit(
'xmpp.pubsub.affiliation',
request,
callback
)
})
})

})

0 comments on commit 62303fe

Please sign in to comment.