Skip to content

Commit

Permalink
Fixes #3. Create tests for node creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Jun 17, 2013
1 parent 9301ddf commit 6fe028c
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,22 @@ PubSub.prototype.createNode = function(data, callback) {

var self = this
var stanza = this._getStanza(data, 'set', 'create')
if (data.options)
dataForms.addForm(
stanza.root().getChild('pubsub').c('configure'),
data.options,
this.NS_CONFIG
)
if (data.options) {
try {
dataForm.addForm(
stanza.root().getChild('pubsub').c('configure'),
data.options,
this.NS_CONFIG
)
} catch(e) {
return this._clientError(
'Badly formatted data form', data, callback
)
}
}
this.manager.trackId(stanza.root().attr('id'), function(stanza) {
if (stanza.attrs.type == 'error')
callback(self._parseError(stanza), null)
return callback(self._parseError(stanza))
callback(null, true)
})
this.client.send(stanza)
Expand Down
160 changes: 160 additions & 0 deletions test/lib/pubsub.create.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,164 @@ describe('Publish-Subscribe', function() {
pubsub.init(manager)
})

beforeEach(function() {
// Somewhere I'm not clearing a stanza listener
// sadly this addition is required, until located
xmpp.removeAllListeners('stanza')
})

describe('Node creation', function() {

it('Returns error if \'to\' key not provided', function(done) {
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()
}
var request = {}
socket.emit('xmpp.pubsub.create', request, callback)
})

it('Returns error if \'node\' key not provided', 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.create', request, callback)
})

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

it('Successfully handles simple node creation', function(done) {
xmpp.once('stanza', function(stanza) {
stanza.is('iq').should.be.true
stanza.attrs.type.should.equal('set')
stanza.attrs.to.should.equal(request.to)
should.exist(stanza.attrs.id)
stanza.getChild('pubsub', pubsub.NS_PUBSUB).should.exist
var create = stanza.getChild('pubsub').getChild('create')
create.attrs.node.should.equal(request.node)
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'
}
socket.emit(
'xmpp.pubsub.create',
request,
callback
)
})

it('Returns error if invalid data form provided', function(done) {
xmpp.once('stanza', function() {
done('Unexpected outgoing stanza')
})
var callback = function(error, success) {
should.not.exist(success)
error.should.eql({
type: 'modify',
condition: 'client-error',
description: 'Badly formatted data form',
request: request
})
xmpp.removeAllListeners('stanza')
done()
}
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
options: {}
}
socket.emit(
'xmpp.pubsub.create',
request,
callback
)
})

it('Allows advanced node creation', function(done) {
xmpp.once('stanza', function(stanza) {
var create = stanza.getChild('pubsub').getChild('create')
var dataForm =stanza.getChild('pubsub')
.getChild('configure')
.getChild('x', 'jabber:x:data')
dataForm.should.exist
dataForm.attrs.type.should.equal('submit')
var fields = dataForm.children
fields[0].name.should.equal('field')
fields[0].attrs.var.should.equal('FORM_TYPE')
fields[0].attrs.type.should.equal('hidden')
fields[0].getChild('value').getText()
.should.equal(pubsub.NS_CONFIG)
fields[1].attrs.var.should.equal(request.options[0].var)
fields[1].getChild('value').getText()
.should.equal(request.options[0].value)
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',
options: [{
var: 'pubsub#description',
value: 'A new comedy'
}]
}
socket.emit(
'xmpp.pubsub.create',
request,
callback
)
})

})

})

0 comments on commit 6fe028c

Please sign in to comment.