|
| 1 | +var buster = require('buster'); |
| 2 | +buster.spec.expose(); // Make spec functions global |
| 3 | +var assert = buster.assert; |
| 4 | + |
| 5 | +var restify = require('restify'); |
| 6 | +var support = require('../../../support'); |
| 7 | +var http = support.http; |
| 8 | +var User = main.models.User; |
| 9 | + |
| 10 | +var spec = describe("users/controller.js", function () { |
| 11 | + var app; |
| 12 | + var http_client; |
| 13 | + |
| 14 | + beforeAll(function () { |
| 15 | + http_client = http.client(); |
| 16 | + app = main.app(); |
| 17 | + app.register('users', {port: http.port}); |
| 18 | + }); |
| 19 | + |
| 20 | + afterAll(function () { |
| 21 | + app.close_server(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe("POST /users", function () { |
| 25 | + var params; |
| 26 | + |
| 27 | + beforeEach(function () { |
| 28 | + params = {username: support.random.string(), password: support.random.string()}; |
| 29 | + }); |
| 30 | + |
| 31 | + it("passes params to User model", function (done) { |
| 32 | + this.stub(User, 'create', function (args, cb) { |
| 33 | + cb(null, {}); |
| 34 | + }); |
| 35 | + |
| 36 | + http_client.post('/users', params, function (err, result) { |
| 37 | + buster.refute(err); |
| 38 | + assert(User.create.calledWith(params)); |
| 39 | + assert(true); |
| 40 | + User.create.restore(); |
| 41 | + done(); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + describe("when model returns an error", function () { |
| 46 | + it("responds with the error", function (done) { |
| 47 | + var fake_err = new restify.InvalidArgumentError('foo'); |
| 48 | + this.stub(User, 'create', function (args, cb) { |
| 49 | + cb(fake_err, {}); |
| 50 | + }); |
| 51 | + |
| 52 | + http_client.post('/users', params, function (err, result) { |
| 53 | + assert(User.create.calledWith(params)); |
| 54 | + assert.equals(result.code, 'InvalidArgument'); |
| 55 | + assert.equals(result.message, 'foo'); |
| 56 | + User.create.restore(); |
| 57 | + done(); |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments