diff --git a/tests/unit/test-module/controllers/TestController.js b/tests/unit/test-module/controllers/TestController.js index 41f27e3..3b3de88 100644 --- a/tests/unit/test-module/controllers/TestController.js +++ b/tests/unit/test-module/controllers/TestController.js @@ -5,9 +5,12 @@ module.exports = function ( Controller, TestService ) { service: TestService }, { - putAction: function() { - this.putCalled = true; - return this._super.apply( this, arguments ); - } + customAction: function() { + this.send( { message: 'Hello from customAction' }, 200 ); + }, + + hidden: function() { + this.send( { message: 'You will never see the contents of this function via the browser!' }, 200 ); + } }); }; \ No newline at end of file diff --git a/tests/unit/test-module/controllers/TestMiddlewareAndRouteController.js b/tests/unit/test-module/controllers/TestMiddlewareAndRouteController.js new file mode 100644 index 0000000..217917b --- /dev/null +++ b/tests/unit/test-module/controllers/TestMiddlewareAndRouteController.js @@ -0,0 +1,19 @@ +module.exports = function ( Controller ) { + var middlewareWasCalled = false; + return Controller.extend( + { + route: '/testcustomroute|/testcustomroutes', + + autoRouting: [ 'middlewareTester' ], + + middlewareTester: function( req, res, next ) { + middlewareWasCalled = true; + next(); + } + }, + { + listAction: function() { + this.send( { message: 'Hello from TestMiddlewareAndRouteController' }, 200 ); + } + }); +}; \ No newline at end of file diff --git a/tests/unit/test.classes.Controller.js b/tests/unit/test.classes.Controller.js index 3e727b0..7d1ff4a 100644 --- a/tests/unit/test.classes.Controller.js +++ b/tests/unit/test.classes.Controller.js @@ -5,25 +5,25 @@ var expect = require( 'chai' ).expect , path = require( 'path' ) , util = require( 'util' ) , injector = require( 'injector' ) - , debug = require( 'debug' )( 'ControllerTests' ) , models = [ { name: 'Testing' }, { name: 'Testing' } ]; describe ( 'Controller', function () { before( function( done ) { TestController = injector.getInstance( 'testModule' ).controllers.TestController; + TestMiddlewareAndRouteController = injector.getInstance( 'testModule' ).controllers.TestMiddlewareAndRouteController; done(); }); function fakeRequest( req ) { req.method = req.method || 'GET'; - req.uri = req.uri || '/test'; + req.url = req.url || '/test'; req.query = req.query || {}; req.body = req.body || {}; req.params = req.params || {}; return req; - }; + } function fakeResponse( cb ) { return { @@ -39,18 +39,101 @@ describe ( 'Controller', function () { }, 10 ); } }; - }; - - describe( 'Configuration and features', function() { - it( 'Does not route when Class.autoRouting = false' ); - it( 'Calls middleware functions when Class.autoRouting = [ middlewareFunc, ... ]' ) - it( 'Can figure out Controller name by filename of extending file' ); - it( 'Allows for strict overriding of route' ); - it( 'Allows your to use | to define multiple strict overriding routes, like "route|route|route|route" .... ' ) - it( 'Can correctly calculate both plural and non plural routes for controller based on name' ); - it( 'Does not autoBind routes more than once' ); - it( 'Correctly assigns this.action before calling restful or action based route functions' ); - it( 'Attach function correctly attachs and routes' ); + } + + it( 'Allows route to be strictly defined', function( done ) { + expect( TestMiddlewareAndRouteController.route ).to.eql( '/testcustomroute|/testcustomroutes' ); + done(); + }); + + it( 'Allows route (non plural and plural) to be guessed based on filename', function( done ) { + expect( TestController.route ).to.eql( '/test|/tests' ); + done(); + }); + + it( 'Routes to functions that have "Action" on the end of their name', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/custom' + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( { message: 'Hello from customAction' } ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'customAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Cannot route to functions that have "Action" on the end of their name', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/hidden' + }); + + // Should fall through to the listAction (based on restfulRouting) + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( [] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'listAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Allows for multiple strict override routes separated by "|", like "/testcustomroute|/testcustomroutes"', function( done ) { + var ctrlOne = null + , ctrlTwo = null + , called = 0; + + var reqOne = fakeRequest({ + method: 'GET', + uri: '/customroute' + }); + + var resOne = fakeResponse( function( code, result ) { + expect( result ).to.eql( { message: 'Hello from TestMiddlewareAndRouteController' } ); + expect( code ).to.equal( 200 ); + expect( ctrlOne.action ).to.equal( 'listAction' ); + + called++; + + if ( called === 2 ) { + done(); + } + }); + + var reqTwo = fakeRequest({ + method: 'GET', + url: '/customroutes' + }); + + var resTwo = fakeResponse( function( code, result ) { + expect( result ).to.eql( { message: 'Hello from TestMiddlewareAndRouteController' } ); + expect( code ).to.equal( 200 ); + expect( ctrlTwo.action ).to.equal( 'listAction' ); + + called++; + + if ( called === 2 ) { + done(); + } + }); + + this.timeout( 10000 ); + ctrlOne = TestMiddlewareAndRouteController.callback( 'newInstance' )( reqOne, resOne ); + ctrlTwo = TestMiddlewareAndRouteController.callback( 'newInstance' )( reqTwo, resTwo ); }); describe( 'Restful Routing', function() { @@ -67,9 +150,8 @@ describe ( 'Controller', function () { }); var res = fakeResponse( function( code, result ) { - console.dir( arguments ); - expect( code ).to.equal( 200 ); expect( result ).to.be.an( 'object' ); + expect( code ).to.equal( 200 ); expect( ctrl.action ).to.equal( 'postAction' ); expect( result ).to.have.property( 'id' ); @@ -85,7 +167,7 @@ describe ( 'Controller', function () { }); this.timeout( 10000 ); - var ctrl = TestController.callback( 'newInstance' )( req, res ); + ctrl = TestController.callback( 'newInstance' )( req, res ); }); it( 'Should not be able to create a new model without posting data', function( done ) { @@ -96,8 +178,8 @@ describe ( 'Controller', function () { }); var res = fakeResponse( function( code, result ) { - expect( code ).to.equal( 500 ); expect( result ).to.be.an( 'object' ); + expect( code ).to.equal( 500 ); expect( ctrl.action ).to.equal( 'postAction' ); expect( result ).to.have.property( 'error' ); @@ -122,8 +204,8 @@ describe ( 'Controller', function () { }); var res = fakeResponse( function( code, result ) { - expect( code ).to.equal( 200 ); expect( result ).to.be.an( 'object' ); + expect( code ).to.equal( 200 ); expect( ctrl.action ).to.equal( 'putAction' ); expect( result ).to.have.property( 'id' ); @@ -162,8 +244,8 @@ describe ( 'Controller', function () { delete req.body.id; var res = fakeResponse( function( code, result ) { - expect( code ).to.equal( 200 ); expect( result ).to.be.an( 'object' ); + expect( code ).to.equal( 200 ); expect( ctrl.action ).to.equal( 'putAction' ); expect( result ).to.have.property( 'id' ); @@ -190,38 +272,335 @@ describe ( 'Controller', function () { describe( '.listAction(): Using route "GET /test" or "GET /tests"', function() { - it( 'Should send all existing model instances as an array') - it( 'Should send an array of matching model instances, using QueryString (/test?field=value&)') + it( 'Should send all existing model instances as an array', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET' + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( [ models[ 0 ] ] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'listAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should send an array of matching model instances, using QueryString (/test?field=value&)', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test', + query: { + name: models[ 0 ].name + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( [ models[ 0 ] ] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'listAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); }); describe( '.getAction(): Using route "GET /test" or "GET /tests"', function() { - it( 'Should send an existing model instance by id when its specified in the uri/url like /test/:id'); - it( 'Should send an existing model instance by id when using the QueryString like /test?id=1'); - it( 'Should not return non existant models (or crash) for either QueryString or URI'); - it( 'Should call listAction() when there is no id specified in either the QueryString or URI') + it( 'Should send an existing model instance by id when its specified in the uri/url like /test/:id', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/' + models[ 0 ].id, + params: { + id: models[ 0 ].id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( models[ 0 ] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'getAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should send an existing model instance by id when using the QueryString like /test?id=1', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/' + models[ 0 ].id, + query: { + id: models[ 0 ].id + }, + params: { + id: models[ 0 ].id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( models[ 0 ] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'getAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should not return non existant models (or crash) for either QueryString or URI', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/99999999', + query: { + id: '99999999' + }, + params: { + id: '99999999' + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( 'Test doesn\'t exist.' ); + expect( code ).to.equal( 403 ); + expect( ctrl.action ).to.equal( 'getAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should call listAction() when there is no id specified in either the QueryString or URI', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test' + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( [ models[ 0 ] ] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'listAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); }); describe( '.putAction(): Using route "PUT /test/:id" or "PUT /tests?id=1"', function() { - it( 'Can find and update model instances' ); + it( 'Update a model when the id is in QueryString like /test?id=1', function( done ) { + var ctrl = null + + models[ 0 ].name = 'putAction updated with querystring id'; + + var model = underscore.extend( {}, models[ 0 ] ) + , id = model.id; + + delete model.id; + + var req = fakeRequest({ + method: 'PUT', + body: model, + url: '/test/' + id, + query: { + id: id + }, + params: { + id: id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.be.an( 'object' ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'putAction' ); + + expect( result ).to.have.property( 'id' ); + expect( result.id ).to.equal( id ); + model.id = id; + + expect( result ).to.have.property( 'name' ); + expect( result.name ).to.equal( 'putAction updated with querystring id' ); + models[ 0 ].name = result.name; + + expect( result ).to.have.property( 'updatedAt' ); + model.updatedAt = result.updatedAt; + + expect( result ).to.have.property( 'deletedAt' ); + expect( result.deletedAt ).to.equal( null ); + + model.deletedAt = result.deletedAt; + + expect( result ).to.eql( model ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Update a model when the id is in URL like /test/1', function( done ) { + var ctrl = null; + + models[ 0 ].name = 'putAction updated with id in url'; + + var model = underscore.extend( {}, models[ 0 ] ); + + var req = fakeRequest({ + method: 'PUT', + body: model, + url: '/test/' + model.id, + params: { + id: model.id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.be.an( 'object' ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'putAction' ); + + expect( result ).to.have.property( 'id' ); + expect( result.id ).to.equal( model.id ); + + expect( result ).to.have.property( 'name' ); + expect( result.name ).to.equal( 'putAction updated with id in url' ); + models[ 0 ].name = result.name; + + expect( result ).to.have.property( 'updatedAt' ); + model.updatedAt = result.updatedAt; + + expect( result ).to.have.property( 'deletedAt' ); + expect( result.deletedAt ).to.equal( null ); + + model.deletedAt = result.deletedAt; + + expect( result ).to.eql( model ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should not update non existant models (or crash) for either QueryString or URI', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'PUT', + body: { id: '9999999' }, + url: '/test/9999999', + params: { + id: '9999999' + }, + query: { + id: '9999999' + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( 'Test doesn\'t exist.' ); + expect( code ).to.equal( 403 ); + expect( ctrl.action ).to.equal( 'putAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); }); describe( '.deleteAction(): Using route "DELETE /test/:id" or "DELETE /tests?id=1"', function() { - it( 'Can delete model instances' ); - it( 'Handles errors nicely' ); + it( 'Delete a model instances with id in the QueryString or URL like /test/1', function( done ) { + var ctrl = null; + var req = fakeRequest({ + method: 'DELETE', + url: '/test/' + models[ 0 ].id, + params: { + id: models[ 0 ].id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( {} ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'deleteAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should not delete non existant models (or crash) for either QueryString or URI', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'DELETE', + body: { id: '9999999' }, + url: '/test/9999999', + params: { + id: '9999999' + }, + query: { + id: '9999999' + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( 'Test doesn\'t exist.' ); + expect( code ).to.equal( 403 ); + expect( ctrl.action ).to.equal( 'deleteAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); }); }); describe( 'Action Routing', function() { - describe( '.postAction(): Using route "POST /test" or "POST /tests"', function() { + describe( '.postAction(): Using route "POST /test/post" or "POST /tests/post"', function() { it( 'Should create a new model instance and save it in the database', function( done ) { var ctrl = null @@ -229,7 +608,11 @@ describe ( 'Controller', function () { var req = fakeRequest({ method: 'POST', - body: underscore.extend( {}, model ) + body: underscore.extend( {}, model ), + url: '/test/post', + params: { + action: 'post' + } }); var res = fakeResponse( function( code, result ) { @@ -246,18 +629,24 @@ describe ( 'Controller', function () { model.createdAt = result.createdAt; model.updatedAt = result.updatedAt; + model.deletedAt = null; + done(); }); this.timeout( 10000 ); - var ctrl = TestController.callback( 'newInstance' )( req, res ); + ctrl = TestController.callback( 'newInstance' )( req, res ); }); it( 'Should not be able to create a new model without posting data', function( done ) { var ctrl = null; var req = fakeRequest({ - method: 'POST' + method: 'POST', + url: '/test/post', + params: { + action: 'post' + } }); var res = fakeResponse( function( code, result ) { @@ -277,33 +666,348 @@ describe ( 'Controller', function () { }); - describe( '.listAction(): Using route "GET /test" or "GET /tests"', function() { + describe( '.listAction(): Using route "GET /test/list" or "GET /tests/list"', function() { - it( 'Should send all existing model instances as an array') - it( 'Should send an array of matching model instances, using QueryString (/test?field=value&)') + it( 'Should send all existing model instances as an array', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/list', + params: { + action: 'list' + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( [ models[ 1 ] ] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'listAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should send an array of matching model instances, using QueryString (/test/list?field=value&)', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/list', + params: { + action: 'list' + }, + query: { + name: models[ 1 ].name + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( [ models[ 1 ] ] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'listAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); }); describe( '.getAction(): Using route "GET /test" or "GET /tests"', function() { - it( 'Should send an existing model instance by id when its specified in the uri/url like /test/:id'); - it( 'Should send an existing model instance by id when using the QueryString like /test?id=1'); - it( 'Should not return non existant models (or crash) for either QueryString or URI'); - it( 'Should call listAction() when there is no id specified in either the QueryString or URI') + it( 'Should send an existing model instance by id when its specified in the uri/url like /test/:id', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/get/' + models[ 1 ].id, + params: { + action: 'get', + id: models[ 1 ].id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( models[ 1 ] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'getAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should send an existing model instance by id when using the QueryString like /test?id=1', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/get/' + models[ 1 ].id, + query: { + id: models[ 1 ].id + }, + params: { + action: 'get', + id: models[ 1 ].id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( models[ 1 ] ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'getAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should not return non existant models (or crash) for either QueryString or URI', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/get/99999999', + query: { + id: '99999999' + }, + params: { + action: 'get', + id: '99999999' + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( 'Test doesn\'t exist.' ); + expect( code ).to.equal( 403 ); + expect( ctrl.action ).to.equal( 'getAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should not list non existant models when there is no id specified in either the QueryString or URI', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/get', + params: { + action: 'get' + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( { error: 'Unhandled exception: You must specify either an id or an object containing fields to find a Test' } ); + expect( code ).to.equal( 500 ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); }); describe( '.putAction(): Using route "PUT /test/:id" or "PUT /tests?id=1"', function() { - it( 'Can find and update model instances' ); + it( 'Update a model when the id is in QueryString like /test?id=1', function( done ) { + var ctrl = null + + models[ 1 ].name = 'putAction updated with querystring id'; + + var model = underscore.extend( {}, models[ 1 ] ) + , id = model.id; + + delete model.id; + + var req = fakeRequest({ + method: 'POST', + body: model, + url: '/test/put/' + id, + query: { + id: id + }, + params: { + action: 'put', + id: id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.be.an( 'object' ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'putAction' ); + + expect( result ).to.have.property( 'id' ); + expect( result.id ).to.equal( id ); + model.id = id; + + expect( result ).to.have.property( 'name' ); + expect( result.name ).to.equal( 'putAction updated with querystring id' ); + models[ 1 ].name = result.name; + + expect( result ).to.have.property( 'updatedAt' ); + model.updatedAt = result.updatedAt; + + expect( result ).to.have.property( 'deletedAt' ); + expect( result.deletedAt ).to.equal( null ); + + model.deletedAt = result.deletedAt; + + expect( result ).to.eql( model ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Update a model when the id is in URL like /test/1', function( done ) { + var ctrl = null; + + models[ 1 ].name = 'putAction updated with id in url'; + + var model = underscore.extend( {}, models[ 1 ] ); + + var req = fakeRequest({ + method: 'POST', + body: model, + url: '/test/put/' + model.id, + params: { + action: 'put', + id: model.id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.be.an( 'object' ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'putAction' ); + + expect( result ).to.have.property( 'id' ); + expect( result.id ).to.equal( model.id ); + + expect( result ).to.have.property( 'name' ); + expect( result.name ).to.equal( 'putAction updated with id in url' ); + models[ 1 ].name = result.name; + + expect( result ).to.have.property( 'updatedAt' ); + model.updatedAt = result.updatedAt; + + expect( result ).to.have.property( 'deletedAt' ); + expect( result.deletedAt ).to.equal( null ); + + model.deletedAt = result.deletedAt; + + expect( result ).to.eql( model ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should not update non existant models (or crash) for either QueryString or URI', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'PUT', + body: { id: '9999999' }, + url: '/test/put/9999999', + params: { + action: 'put', + id: '9999999' + }, + query: { + id: '9999999' + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( 'Test doesn\'t exist.' ); + expect( code ).to.equal( 403 ); + expect( ctrl.action ).to.equal( 'putAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); }); describe( '.deleteAction(): Using route "DELETE /test/:id" or "DELETE /tests?id=1"', function() { - it( 'Can delete model instances' ); - it( 'Handles errors nicely' ); + it( 'Delete a model instances with id in the QueryString or URL like /test/1', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + url: '/test/delete/' + models[ 1 ].id, + params: { + action: 'delete', + id: models[ 1 ].id + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( {} ); + expect( code ).to.equal( 200 ); + expect( ctrl.action ).to.equal( 'deleteAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); + + it( 'Should not delete non existant models (or crash) for either QueryString or URI', function( done ) { + var ctrl = null; + + var req = fakeRequest({ + method: 'GET', + body: { id: '9999999' }, + url: '/test/delete/9999999', + params: { + action: 'delete', + id: '9999999' + }, + query: { + id: '9999999' + } + }); + + var res = fakeResponse( function( code, result ) { + expect( result ).to.eql( 'Test doesn\'t exist.' ); + expect( code ).to.equal( 403 ); + expect( ctrl.action ).to.equal( 'deleteAction' ); + + done(); + }); + + this.timeout( 10000 ); + ctrl = TestController.callback( 'newInstance' )( req, res ); + }); }); diff --git a/tests/unit/test.utils.moduleLoader.js b/tests/unit/test.utils.moduleLoader.js index 4314d07..9a3fad5 100644 --- a/tests/unit/test.utils.moduleLoader.js +++ b/tests/unit/test.utils.moduleLoader.js @@ -39,6 +39,7 @@ describe( 'test.utils.moduleLoader', function() { this.timeout( 10000 ); moduleLdr.on( 'modulesLoaded', function() { + // @TODO this only supports the orm module!!! injector .getInstance( 'sequelize' ) .sync( { force: true } )