UNPKG

gear-crud-api

Version:
337 lines (275 loc) 10.1 kB
var Gearbox = require('gearbox'); var chai = require('chai'); var chaiSubset = require('chai-subset'); var expect = require('chai').expect; chai.use(chaiSubset); var rest = require('restler'); var path = require('path'); var box; var crud; var dao; var web; var modelPrefix; var repo; describe ('Setup for no-auth tests', function() { this.timeout(50000); it('should get a new Gearbox instance for no-auth tests', function () { box = new Gearbox( { gearConfig: { logger: { level: 'debug' }, repo: { baseDir: path.resolve(__dirname, './repo') }, dao: { dataSource: { connector: 'memory' //connector: 'postgresql', //host: 'localhost', //port: 5432, //database: 'gearbox', //username: 'gearbox', //password: 'gearbox' } } } } ); }); it('should explicitly add dir containing crudApi gear (no-auth tests)', function () { box.registerGearPath('crudApi', __dirname, './..'); }); it ('should get some gears for auth tests', function(done) { box.get(['crudApi', 'repo'], {}, function(err, gears) { expect(err).to.equal(null); crud = gears.crudApi; dao = crud.dao; web = crud.web; repo = gears.repo; modelPrefix = web.getBaseUrl() + 'model/gboxTest.customers.1/'; done(); }); }); it ("should ensure customers blueprint", function (done) { repo.ensure ('./blueprints/customers', {baseDir: __dirname}, function (err) { expect(err).to.equal(null); done(); }); }); //it ('should register a simple customer dao (for no-auth tests)', function(done) { // dao.registerDao ( // 'customer', // { // customerNo: {type: 'number', id:true, required: true}, // email: {type: 'string', required: true}, // password: {type: 'string'}, // birthDate: {type: 'date'}, // activated: {type: 'boolean'} // }, // {}, // function (err) { // done(); // } // ); //}); //it ('should finalize customer dao (for no-auth tests)', function (done) { // dao.finalize( // 'customer', // {}, // function (err) { // expect(err).to.equal(null); // done(); // } // ); //}); it ('should start web server for basic tests (for no-auth tests)', function(done) { web.start(function() { done(); }); }); it ("should post a new customer (for no-auth tests)", function(done) { rest.postJson(modelPrefix + 'customer', { customerNo: 123, email: 'someone@gearboxjs.org', password: 'secret', activated: false }).on('complete', function(data, res) { expect(res.statusCode).to.equal (201); done(); }); }); it ("should get new customer (for no-auth tests)", function(done) { rest.get(modelPrefix + 'customer/123').on('complete', function(data, res) { expect(res.statusCode).to.equal (200); expect(data).to.containSubset ({ customerNo: 123, email: 'someone@gearboxjs.org', password: 'secret', activated: false }); done(); }); }); it ("should update customer (for no-auth tests)", function(done) { rest.putJson( modelPrefix + 'customer/123', { customerNo: 123, email: 'someone.else@gearboxjs.org', password: 'secret', activated: true } ).on('complete', function(err, res) { expect(res.statusCode).to.equal (200); done(); }); }); it ("should fail posting duplicate customer (for no-auth tests)", function(done) { rest.postJson(modelPrefix + 'customer', { customerNo: 123, email: 'someone@gearboxjs.org', password: 'secret', activated: false }).on('complete', function(err, res) { expect(res.statusCode).to.equal (500); expect(err.name).to.equal('apiCreateFail'); expect(err.message).to.equal('Failed to create customer - Duplicate entry for customer.customerNo'); done(); }); }); it ("should delete customer (for no-auth tests)", function(done) { rest.del (modelPrefix + 'customer/123').on('complete', function(data, res) { expect(res.statusCode).to.equal (200); done(); }); }); it ("should fail getting deleted customer (for no-auth tests)", function(done) { rest.get(modelPrefix + 'customer/123').on('complete', function(err, res) { expect(res.statusCode).to.equal (404); expect(err.name).to.equal('unknownId'); expect(err.message).to.equal('Unable to find customer with id 123'); done(); }); }); it ("should fail updating deleted customer (for no-auth tests)", function(done) { rest.putJson(modelPrefix + 'customer/123').on('complete', function(err, res) { expect(res.statusCode).to.equal (404); expect(err.name).to.equal('unknownId'); expect(err.message).to.equal('Unable to find customer with id 123'); done(); }); }); it ("should fail deleting deleted customer (for no-auth tests)", function(done) { rest.del(modelPrefix + 'customer/123').on('complete', function(err, res) { expect(res.statusCode).to.equal (404); expect(err.name).to.equal('unknownId'); expect(err.message).to.equal('Unable to find customer with id 123'); done(); }); }); it ("should post three new customers (for no-auth tests)", function(done) { rest.postJson(modelPrefix + 'customer', [ { customerNo: 124, email: 'moe.howard@gearboxjs.org', password: 'moe123', activated: true }, { customerNo: 125, email: 'larry.fine@gearboxjs.org', password: 'larry123', activated: false }, { customerNo: 126, email: 'curly.howard@gearboxjs.org', password: 'curly123', activated: true } ] ).on('complete', function(data, res) { expect(res.statusCode).to.equal (201); done(); }); }); it ("should get all three customers (for no-auth tests)", function(done) { rest.get(modelPrefix + 'customer').on('complete', function(data, res) { expect(res.statusCode).to.equal (200); expect (data.length).to.equal(3); done(); }); }); it ("should get the two activated customers (for no-auth tests)", function(done) { rest.get(modelPrefix + 'customer?filter[activated]=true&order=email DESC').on('complete', function(data, res) { expect(res.statusCode).to.equal (200); expect (data).to.containSubset ( [ { customerNo: 124, email: 'moe.howard@gearboxjs.org', password: 'moe123', activated: true }, { customerNo: 126, email: 'curly.howard@gearboxjs.org', password: 'curly123', activated: true } ] ); done(); }); }); it ("should order customers by e-mail ASC (for no-auth tests)", function(done) { rest.get(modelPrefix + 'customer?order=email ASC&skip=1&fields=customerNo,password').on('complete', function(data, res) { expect(res.statusCode).to.equal (200); expect (data).to.eql( [ { customerNo: 125, password: 'larry123' }, { customerNo: 124, password: 'moe123' } ] ); done(); }); }); it ("should delete customer 124 (for auth tests)", function(done) { rest.del ( modelPrefix + 'customer/124' ).on('complete', function(data, res) { expect(res.statusCode).to.equal (200); done(); }); }); it ("should delete customer 125 (for auth tests)", function(done) { rest.del ( modelPrefix + 'customer/125' ).on('complete', function(data, res) { expect(res.statusCode).to.equal (200); done(); }); }); it ("should delete customer 126 (for auth tests)", function(done) { rest.del ( modelPrefix + 'customer/126' ).on('complete', function(data, res) { expect(res.statusCode).to.equal (200); done(); }); }); it ('should stop web server for basic tests (for no-auth tests)', function(done) { web.stop(function() { done(); }); }); });