Skip to content

Commit

Permalink
Publishing tests completed. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Jun 22, 2013
1 parent 78bc903 commit 5054477
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 17 deletions.
10 changes: 6 additions & 4 deletions lib/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,14 @@ PubSub.prototype.publish = function(data, callback) {
try {
stanza.c('item', details).children = [ this._getItemParser().build(data.content) ]
} catch (e) {
return callback(e, null)
return this._clientError(
'Could not parse content to stanza', data, callback
)
}
if (data.options) {
stanza.root().getChild('pubsub').c('publish-options')
var options = stanza.root().getChild('pubsub').c('publish-options')
try {
dataForm.addForm(stanza, data.options, this.NS_PUBLISH_OPTIONS)
dataForm.addForm(options, data.options, this.NS_PUBLISH_OPTIONS)
} catch(e) {
return this._clientError(
'Badly formatted data form', data, callback
Expand Down Expand Up @@ -472,7 +474,7 @@ PubSub.prototype._getItemParser = function() {
}

PubSub.prototype.setItemParser = function(parser) {
this.itemParser = itemParser
this.itemParser = parser
return this
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"url": "https://github.com/lloydwatkin/xmpp-ftw-pubsub/issues"
},
"dependencies": {
"ltx": "*"
"ltx": "*",
"xmpp-ftw-item-parser": "*"
},
"devDependencies": {
"mocha": "~1.8.1",
Expand Down
4 changes: 4 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ Eventer.prototype.send = function(stanza) {
this.emit('stanza', stanza.root())
}
exports.Eventer = Eventer

exports.failingItemParser = function() {
throw new Error('FAIL!')
}
256 changes: 244 additions & 12 deletions test/lib/pubsub.items.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,279 @@ describe('Publish-Subscribe', function() {
describe('Publishing items', function() {

it('Errors if \'to\' key missing', function(done) {
done('Not implemented yet')
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.publish',
request,
callback
)
})

it('Errors if \'node\' key missing', function(done) {
done('Not implemented yet')
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.publish',
request,
callback
)

})

it('Errors if no message content', function(done) {
done('Not implemented yet')
it('Errors if no message 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 message content")
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
done()
}
socket.emit(
'xmpp.pubsub.publish',
request,
callback
)
})

it('Errors if empty message content', function(done) {

var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
content: ''
}
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 message content")
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
done()
}
socket.emit(
'xmpp.pubsub.publish',
request,
callback
)
})

it('Returns error if item content can be built', function(done) {
done('Not implemented yet')
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
content: 'hello world'
}
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("Could not parse content to stanza")
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
pubsub.setItemParser()
done()
}

pubsub.setItemParser(helper.failingItemParser)
socket.emit(
'xmpp.pubsub.publish',
request,
callback
)
})

it('Errors if publish options can\'t be parsed', function(done) {
done('Not implemented yet')
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
content: 'hello world',
options: 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("Badly formatted data form")
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
done()
}

socket.emit(
'xmpp.pubsub.publish',
request,
callback
)
})

it('Sends expected stanza', function(done) {
done('Not implemented yet')
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
content: 'hello world'
}
xmpp.once('stanza', function(stanza) {
stanza.is('iq').should.be.true
stanza.attrs.to.should.equal(request.to)
stanza.attrs.id.should.exist
stanza.attrs.type.should.equal('set')
var publish = stanza.getChild('pubsub', pubsub.NS_PUBSUB)
.getChild('publish')
publish.should.exist
publish.attrs.node.should.equal(request.node)
publish.getChild('item').children.length.should.equal(1)
publish.getChild('item').getChildText('body')
.should.equal(request.content)
done()
})
socket.emit(
'xmpp.pubsub.publish',
request,
function() {}
)
})

it('Sends expected stanza with set ID', function(done) {
done('Not implemented yet')
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
content: 'hello world',
id: '111'
}
xmpp.once('stanza', function(stanza) {
stanza.getChild('pubsub', pubsub.NS_PUBSUB)
.getChild('publish').getChild('item').attrs.id
.should.equal(request.id)
done()
})
socket.emit(
'xmpp.pubsub.publish',
request,
function() {}
)
})

it('Sends expected stanza with publish options', function(done) {
done('Not implemented yet')
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
content: 'hello world',
options: [
{ var: 'pubsub#access_model', value: 'presence' }
]
}
xmpp.once('stanza', function(stanza) {
var dataForm = stanza.getChild('pubsub', pubsub.NS_PUBSUB)
.getChild('publish-options')
.getChild('x', 'jabber:x:data')
dataForm.should.exist
dataForm.attrs.type.should.equal('submit')
dataForm.children.length.should.equal(2)
dataForm.children[0].attrs.var.should.equal('FORM_TYPE')
dataForm.children[0].attrs.type.should.equal('hidden')
dataForm.children[0].name.should.equal('field')
dataForm.children[0].getChildText('value')
.should.equal(pubsub.NS_PUBLISH_OPTIONS)
dataForm.children[1].name.should.equal('field')
dataForm.children[1].attrs.var
.should.equal('pubsub#access_model')
dataForm.children[1].getChildText('value')
.should.equal('presence')
done()
})
socket.emit(
'xmpp.pubsub.publish',
request,
function() {}
)
})

it('Handles error stanza response', function(done) {
done('Not implemented yet')
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',
content: 'hello world'
}
socket.emit(
'xmpp.pubsub.publish',
request,
callback
)
})

it('Returns post ID on success', function(done) {

xmpp.once('stanza', function(stanza) {
manager.makeCallback(helper.getStanza('publish'))
})
var callback = function(error, success) {
should.not.exist(error)
success.id.should.equal('123456')
done()
}
var request = {
to: 'pubsub.shakespeare.lit',
node: 'twelfth night',
content: 'hello world'
}
socket.emit(
'xmpp.pubsub.publish',
request,
callback
)
})

})
Expand Down
10 changes: 10 additions & 0 deletions test/resources/publish
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<iq type='result'
from='pubsub.shakespeare.lit'
to='juliet@example.net/balcony'
id='1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<publish node='twelfth night'>
<item id='123456'/>
</publish>
</pubsub>
</iq>

0 comments on commit 5054477

Please sign in to comment.