REST avec NodeJS & Express - Tests Unitaires
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert') | |
, db = require('../db')({namespace:'bookmarks-test-db'}) | |
, the_bookmark = {} | |
require('vows') | |
.describe('bookmarks-db') | |
.addBatch({ | |
"Initialize": { | |
topic: function () { | |
db.deleteAll(this.callback); | |
}, | |
"deleteAll": function (err, placeholder /* required for Vows to detect I want the error to be passed */) { | |
assert.isNull(err); | |
} | |
} | |
}).addBatch({ | |
"Creation": { | |
topic: function () { | |
db.save({ "title": "Google", "url": "http://www.google.com", "tags": [ "google", "search" ] }, this.callback); | |
}, | |
"save (new)": function (err, bookmark, created) { | |
assert.isNull(err); | |
assert.isTrue(created); | |
assert.include(bookmark, 'id'); | |
assert.equal(bookmark.title, 'Google'); | |
assert.equal(bookmark.url, 'http://www.google.com'); | |
assert.deepEqual(bookmark.tags, ['google', 'search']); | |
the_bookmark = bookmark; | |
} | |
} | |
}).addBatch({ | |
"Fetch": { | |
topic: function () { | |
db.fetchOne(the_bookmark.id, this.callback) | |
}, | |
"check existing": function (err, bookmark) { | |
assert.isNull(err); | |
assert.isObject(bookmark); | |
assert.deepEqual(bookmark, the_bookmark); | |
} | |
} | |
}).addBatch({ | |
"Update": { | |
topic: function () { | |
the_bookmark.title = 'Google.com'; | |
db.save(the_bookmark, this.callback); | |
}, | |
"save (update)": function (err, bookmark, created) { | |
assert.isNull(err); | |
assert.isFalse(created); | |
assert.equal(bookmark.title, the_bookmark.title); | |
assert.equal(bookmark.url, the_bookmark.url); | |
assert.deepEqual(bookmark.tags, the_bookmark.tags); | |
} | |
} | |
}).addBatch({ | |
"Delete": { | |
topic: function () { | |
db.deleteOne(the_bookmark.id, this.callback); | |
}, | |
"Deleted": function (err, deleted) { | |
assert.isNull(err); | |
assert.isTrue(deleted); | |
} | |
} | |
}).addBatch({ | |
"Finalize": { | |
topic: db, | |
"Clean": function (db) { | |
db.deleteAll(); | |
}, | |
"Close connection": function (db) { | |
db.close(); | |
} | |
} | |
}).export(module) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require('../bookmarks-rest'); | |
require('./rest-test-suite')(app, '').export(module); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require('../app'); | |
// Expose app.db | |
app.db = app.bookmarks_app.db; | |
require('./rest-test-suite')(app, '/bookmarks').export(module); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (app, root) { | |
const PORT = 3000; | |
var assert = require('assert') | |
, bookmark = { | |
"title": "Google", | |
"url": "http://www.google.com", | |
"tags": [ "google", "search" ] | |
} | |
, expected_id = 1; | |
// Configure REST API host & URL | |
var suite = require('api-easy') | |
.describe('bookmarks-rest') | |
.use('localhost', PORT); | |
if (root) suite.root(root) | |
// Initially: start server | |
suite.expect('Start server', function () { | |
app.db.configure({namespace: 'bookmarks-test-rest'}); | |
app.listen(PORT); | |
}).next() | |
// 0. Test errors | |
.get().expect(406) // Invalid headers | |
.setHeader('Accept', 'application/json') | |
.get().expect(200).next() // OK | |
.put().expect(406) // Invalid headers | |
.setHeader('Content-Type', 'application/json') | |
.put().expect(405).next() // Method not allowed | |
// Invalid data ↓ | |
.post({"toto":"tata"}).expect(400).next() | |
.post({}).expect(400).next() | |
.post({"url":"url","title":"title","tags":"some tag"}).expect(400).next() | |
.post({"url":"url","title":"title","extra":"attribute"}).expect(400).next() | |
// Valid data ↓ | |
.post({"url":"http://url1.com","title":"title1"}).expect(200).next() | |
.post({"url":"http://url2.com","title":"title2","tags":["some tag"]}).expect(200).next() | |
// 1. Empty database | |
.del() | |
.expect(200) | |
.next() | |
// 2. Add a new bookmark | |
.post(bookmark) | |
.expect('Has ID', function (err, res, body) { | |
var obj; | |
assert.doesNotThrow(function() { obj = JSON.parse(body) }, SyntaxError); | |
assert.isObject(obj); | |
assert.include(obj, 'id'); | |
assert.equal(expected_id, obj.id); | |
bookmark.id = obj.id; | |
}) | |
.undiscuss().next() | |
// 3.1. Check that the freshly created bookmark appears | |
.get() | |
.expect('Collection', function (err, res, body) { | |
var obj; | |
assert.doesNotThrow(function() { obj = JSON.parse(body) }, SyntaxError); | |
assert.isArray(obj); | |
assert.include(obj, root + '/bookmark/' + expected_id); | |
}) | |
// 3.2. Get the freshly created bookmark | |
.get('/bookmark/' + expected_id) | |
.expect('Found bookmark', function (err, res, body) { | |
var obj; | |
assert.doesNotThrow(function() { obj = JSON.parse(body) }, SyntaxError); | |
assert.deepEqual(obj, bookmark); | |
}) | |
.next() | |
// 4. Update bookmark | |
.put('/bookmark/' + expected_id, {"title": "Google.com"}) | |
.expect('Updated bookmark', function (err, res, body) { | |
var obj; | |
assert.doesNotThrow(function() { obj = JSON.parse(body) }, SyntaxError); | |
bookmark.title = "Google.com"; | |
assert.deepEqual(obj, bookmark); | |
}) | |
.next() | |
// 5. Delete bookmark | |
.del('/bookmark/' + expected_id) | |
.expect(200) | |
.next() | |
// 6. Check deletion | |
.get('/bookmark/' + expected_id) | |
.expect(404) | |
.next() | |
// 7. Check all bookmarks are gone | |
.get() | |
.expect('Empty database', function (err, res, body) { | |
var obj; | |
assert.doesNotThrow(function() { obj = JSON.parse(body) }, SyntaxError); | |
assert.isArray(obj); | |
assert.equal(obj.length, 0); | |
}) | |
// 8. Test unallowed methods | |
.post('/bookmark/' + expected_id, bookmark).expect(405) | |
.put(bookmark).expect(405) | |
// Finally: clean, and stop server | |
.expect('Clean & exit', function () { | |
app.db.deleteAll(function () { app.close() }); | |
}); | |
return suite; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment