diff --git a/test/nested_paths.test.js b/test/nested_paths.test.js index 52ba7f3..c3aee26 100644 --- a/test/nested_paths.test.js +++ b/test/nested_paths.test.js @@ -1,34 +1,18 @@ 'use strict'; -const fastifyMongooseAPI = require('../fastify-mongoose-api.js'); - const t = require('tap'); const { test } = t; -const Fastify = require('fastify'); const mongoose = require('mongoose'); - -const MONGODB_URL = - process.env.DATABASE_URI || 'mongodb://127.0.0.1/fastifymongooseapitest'; - const BackwardWrapper = require('./BackwardWrapper.js'); -let mongooseConnection = null; -let fastify = null; +const bw = new BackwardWrapper(t); -test('mongoose db initialization', async t => { - t.plan(2); - - mongooseConnection = await BackwardWrapper.createConnection(MONGODB_URL); - t.ok(mongooseConnection); - t.equal(mongooseConnection.readyState, 1, 'Ready state is connected(==1)'); /// connected +test('mongoose db initialization', async () => { + await bw.createConnection(); }); test('schema initialization', async t => { - // t.plan(2); - - // const biographyEpochSchema = mongoose.Schema({ title: String, year: Number }); - const authorSchema = mongoose.Schema({ firstName: String, lastName: String, @@ -39,37 +23,30 @@ test('schema initialization', async t => { } }); - mongooseConnection.model('Author', authorSchema); + bw.conn.model('Author', authorSchema); - t.ok(mongooseConnection.models.Author); + t.ok(bw.conn.models.Author); }); test('clean up test collections', async () => { - await mongooseConnection.models.Author.deleteMany({}).exec(); + await bw.conn.models.Author.deleteMany({}).exec(); }); test('initialization of API server', async t => { - ///// setting up the server - fastify = Fastify(); - - fastify.register(fastifyMongooseAPI, { - models: mongooseConnection.models, + await bw.createServer({ + models: bw.conn.models, prefix: '/api/', setDefaults: true, methods: ['list', 'get', 'post', 'patch', 'put', 'delete', 'options'] }); - await fastify.ready(); - - t.ok(fastify.mongooseAPI, 'mongooseAPI decorator is available'); - t.equal( - fastify.mongooseAPI.apiRouters.Author.collectionName, + bw.fastify.mongooseAPI.apiRouters.Author.collectionName, 'authors', 'Collection name used in API path' ); t.equal( - fastify.mongooseAPI.apiRouters.Author.path, + bw.fastify.mongooseAPI.apiRouters.Author.path, '/api/authors', 'API path is composed with prefix + collectionName' ); @@ -77,7 +54,7 @@ test('initialization of API server', async t => { test('POST item test', async t => { let response = null; - response = await fastify.inject({ + response = await bw.inject(t, { method: 'POST', url: '/api/authors', payload: { @@ -88,13 +65,6 @@ test('POST item test', async t => { } }); - t.equal(response.statusCode, 200, 'POST api ok'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Content-Type is correct' - ); - t.match( JSON.parse(response.payload), { @@ -115,18 +85,11 @@ test('POST item test', async t => { 'POST api ok' ); - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/authors' }); - t.equal(response.statusCode, 200, 'GET api ok'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Content-Type is correct' - ); - t.match( response.json().items[0], { @@ -140,23 +103,17 @@ test('POST item test', async t => { }); test('PUT item test', async t => { - let authorFromDb = await mongooseConnection.models.Author.findOne({ + let authorFromDb = await bw.conn.models.Author.findOne({ firstName: 'Hutin' }); // await BackwardWrapper.populateDoc(bookFromDb.populate('author')); - const response = await fastify.inject({ + const response = await bw.inject(t, { method: 'PUT', url: '/api/authors/' + authorFromDb.id, payload: { lastName: 'Chuvachello', 'biography.born': 1961 } }); - t.equal(response.statusCode, 200); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8' - ); - t.match( response.json(), { @@ -167,8 +124,3 @@ test('PUT item test', async t => { 'PUT api ok' ); }); - -test('teardown', async () => { - await fastify.close(); - await mongooseConnection.close(); -}); diff --git a/test/prefix.test.js b/test/prefix.test.js index 680c5e4..c480570 100644 --- a/test/prefix.test.js +++ b/test/prefix.test.js @@ -1,27 +1,15 @@ 'use strict'; -const fastifyMongooseAPI = require('../fastify-mongoose-api.js'); - const t = require('tap'); const { test } = t; -const Fastify = require('fastify'); const mongoose = require('mongoose'); - -const MONGODB_URL = - process.env.DATABASE_URI || 'mongodb://127.0.0.1/fastifymongooseapitest'; - const BackwardWrapper = require('./BackwardWrapper.js'); -let mongooseConnection = null; -let fastify = null; - -test('mongoose db initialization', async t => { - t.plan(2); +const bw = new BackwardWrapper(t); - mongooseConnection = await BackwardWrapper.createConnection(MONGODB_URL); - t.ok(mongooseConnection); - t.equal(mongooseConnection.readyState, 1, 'Ready state is connected(==1)'); /// connected +test('mongoose db initialization', async () => { + await bw.createConnection(); }); test('schema initialization', async t => { @@ -42,7 +30,7 @@ test('schema initialization', async t => { biography: 'text' }); /// you can use wildcard here too: https://stackoverflow.com/a/28775709/1119169 - mongooseConnection.model('Author', authorSchema); + bw.conn.model('Author', authorSchema); const bookSchema = mongoose.Schema({ title: String, @@ -57,86 +45,79 @@ test('schema initialization', async t => { } }); - mongooseConnection.model('Book', bookSchema); + bw.conn.model('Book', bookSchema); - t.ok(mongooseConnection.models.Author); - t.ok(mongooseConnection.models.Book); + t.ok(bw.conn.models.Author); + t.ok(bw.conn.models.Book); }); test('clean up test collections', async () => { - await mongooseConnection.models.Author.deleteMany({}).exec(); - await mongooseConnection.models.Book.deleteMany({}).exec(); + await bw.conn.models.Author.deleteMany({}).exec(); + await bw.conn.models.Book.deleteMany({}).exec(); }); test('schema ok', async t => { - let author = new mongooseConnection.models.Author(); + let author = new bw.conn.models.Author(); author.firstName = 'Jay'; author.lastName = 'Kay'; author.biography = 'Lived. Died.'; await author.save(); - let book = new mongooseConnection.models.Book(); + let book = new bw.conn.models.Book(); book.title = 'The best book'; book.isbn = 'The best isbn'; book.author = author; await book.save(); - let authorFromDb = await mongooseConnection.models.Author.findOne({ + let authorFromDb = await bw.conn.models.Author.findOne({ firstName: 'Jay' }); - let bookFromDb = await mongooseConnection.models.Book.findOne({ + let bookFromDb = await bw.conn.models.Book.findOne({ title: 'The best book' }); t.ok(authorFromDb); t.ok(bookFromDb); - await BackwardWrapper.populateDoc(bookFromDb.populate('author')); + await bw.populateDoc(bookFromDb.populate('author')); // await bookFromDb.populate('author').execPopulate(); t.equal('' + bookFromDb.author._id, '' + authorFromDb._id); }); test('initialization of API server', async t => { - ///// setting up the server - fastify = Fastify(); - - fastify.register(fastifyMongooseAPI, { - models: mongooseConnection.models, + await bw.createServer({ + models: bw.conn.models, prefix: '/someroute/', setDefaults: true, methods: ['list', 'get', 'post', 'patch', 'put', 'delete', 'options'] }); - - await fastify.ready(); - - t.ok(fastify.mongooseAPI, 'mongooseAPI decorator is available'); t.equal( - Object.keys(fastify.mongooseAPI.apiRouters).length, + Object.keys(bw.fastify.mongooseAPI.apiRouters).length, 2, 'There are 2 APIRoutes, one for each model' ); t.equal( - fastify.mongooseAPI.apiRouters.Author.collectionName, + bw.fastify.mongooseAPI.apiRouters.Author.collectionName, 'authors', 'Collection name used in API path' ); t.equal( - fastify.mongooseAPI.apiRouters.Book.collectionName, + bw.fastify.mongooseAPI.apiRouters.Book.collectionName, 'books', 'Collection name used in API path' ); t.equal( - fastify.mongooseAPI.apiRouters.Author.path, + bw.fastify.mongooseAPI.apiRouters.Author.path, '/someroute/authors', 'API path is composed with prefix + collectionName' ); t.equal( - fastify.mongooseAPI.apiRouters.Book.path, + bw.fastify.mongooseAPI.apiRouters.Book.path, '/someroute/books', 'API path is composed with prefix + collectionName' ); @@ -144,36 +125,17 @@ test('initialization of API server', async t => { test('GET collection endpoints', async t => { let response = null; - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/someroute/books' }); - t.equal(response.statusCode, 200, 'API returns 200 status code'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'API returns correct content type' - ); - t.equal(response.json().total, 1, 'API returns 1 book'); - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/someroute/authors' }); - t.equal(response.statusCode, 200, 'API returns 200 status code'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'API returns correct content type' - ); - t.equal(response.json().total, 1, 'API returns 1 author'); }); - -test('teardown', async () => { - await fastify.close(); - await mongooseConnection.close(); -}); diff --git a/test/refs_in_array.test.js b/test/refs_in_array.test.js index 93a8c0d..a3c7a7f 100644 --- a/test/refs_in_array.test.js +++ b/test/refs_in_array.test.js @@ -1,27 +1,15 @@ 'use strict'; -const fastifyMongooseAPI = require('../fastify-mongoose-api.js'); - const t = require('tap'); const { test } = t; -const Fastify = require('fastify'); const mongoose = require('mongoose'); - -const MONGODB_URL = - process.env.DATABASE_URI || 'mongodb://127.0.0.1/fastifymongooseapitest'; - const BackwardWrapper = require('./BackwardWrapper.js'); -let mongooseConnection = null; -let fastify = null; +const bw = new BackwardWrapper(t); -test('mongoose db initialization', async t => { - t.plan(2); - - mongooseConnection = await BackwardWrapper.createConnection(MONGODB_URL); - t.ok(mongooseConnection); - t.equal(mongooseConnection.readyState, 1, 'Ready state is connected(==1)'); /// connected +test('mongoose db initialization', async () => { + await bw.createConnection(); }); test('schema initialization', async t => { @@ -43,7 +31,7 @@ test('schema initialization', async t => { biography: 'text' }); /// you can use wildcard here too: https://stackoverflow.com/a/28775709/1119169 - mongooseConnection.model('Author', authorSchema); + bw.conn.model('Author', authorSchema); const bookSchema = mongoose.Schema({ title: String, @@ -61,31 +49,31 @@ test('schema initialization', async t => { return object; }; - mongooseConnection.model('Book', bookSchema); + bw.conn.model('Book', bookSchema); - t.ok(mongooseConnection.models.Author); - t.ok(mongooseConnection.models.Book); + t.ok(bw.conn.models.Author); + t.ok(bw.conn.models.Book); }); test('clean up test collections', async () => { - await mongooseConnection.models.Author.deleteMany({}).exec(); - await mongooseConnection.models.Book.deleteMany({}).exec(); + await bw.conn.models.Author.deleteMany({}).exec(); + await bw.conn.models.Book.deleteMany({}).exec(); }); test('schema ok', async t => { - let book = new mongooseConnection.models.Book(); + let book = new bw.conn.models.Book(); book.title = 'The best book'; book.isbn = 'The best isbn'; await book.save(); - let book2 = new mongooseConnection.models.Book(); + let book2 = new bw.conn.models.Book(); book2.title = 'The best book2'; book2.isbn = 'The best isbn2'; await book2.save(); - let author = new mongooseConnection.models.Author(); + let author = new bw.conn.models.Author(); author.firstName = 'Jay'; author.lastName = 'Kay'; author.biography = 'Lived. Died.'; @@ -93,13 +81,13 @@ test('schema ok', async t => { await author.save(); - let authorFromDb = await mongooseConnection.models.Author.findOne({ + let authorFromDb = await bw.conn.models.Author.findOne({ firstName: 'Jay' }).exec(); - let bookFromDb = await mongooseConnection.models.Book.findOne({ + let bookFromDb = await bw.conn.models.Book.findOne({ title: 'The best book' }).exec(); - let book2FromDb = await mongooseConnection.models.Book.findOne({ + let book2FromDb = await bw.conn.models.Book.findOne({ title: 'The best book2' }).exec(); @@ -107,50 +95,43 @@ test('schema ok', async t => { t.ok(bookFromDb); t.ok(book2FromDb); - await BackwardWrapper.populateDoc(authorFromDb.populate('inspired')); + await bw.populateDoc(authorFromDb.populate('inspired')); t.equal('' + authorFromDb.inspired[0]._id, '' + book._id); t.equal('' + authorFromDb.inspired[1]._id, '' + book2._id); }); test('initialization of API server', async t => { - ///// setting up the server - fastify = Fastify(); - - fastify.register(fastifyMongooseAPI, { - models: mongooseConnection.models, + await bw.createServer({ + models: bw.conn.models, prefix: '/api/', setDefaults: true, methods: ['list', 'get', 'post', 'patch', 'put', 'delete', 'options'] }); - - await fastify.ready(); - - t.ok(fastify.mongooseAPI, 'mongooseAPI decorator is available'); t.equal( - Object.keys(fastify.mongooseAPI.apiRouters).length, + Object.keys(bw.fastify.mongooseAPI.apiRouters).length, 2, 'There are 2 APIRoutes, one for each model' ); t.equal( - fastify.mongooseAPI.apiRouters.Author.collectionName, + bw.fastify.mongooseAPI.apiRouters.Author.collectionName, 'authors', 'Collection name used in API path' ); t.equal( - fastify.mongooseAPI.apiRouters.Book.collectionName, + bw.fastify.mongooseAPI.apiRouters.Book.collectionName, 'books', 'Collection name used in API path' ); t.equal( - fastify.mongooseAPI.apiRouters.Author.path, + bw.fastify.mongooseAPI.apiRouters.Author.path, '/api/authors', 'API path is composed with prefix + collectionName' ); t.equal( - fastify.mongooseAPI.apiRouters.Book.path, + bw.fastify.mongooseAPI.apiRouters.Book.path, '/api/books', 'API path is composed with prefix + collectionName' ); @@ -158,18 +139,11 @@ test('initialization of API server', async t => { test('GET collection endpoints', async t => { let response = null; - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/books' }); - t.equal(response.statusCode, 200, 'API returns 200 status code'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'API returns correct content type' - ); - t.equal(response.json().total, 2, 'API returns 2 books'); t.equal( response.json().items[0].isbn, @@ -182,48 +156,34 @@ test('GET collection endpoints', async t => { 'apiValues model method works' ); - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/authors' }); - t.equal(response.statusCode, 200, 'API returns 200 status code'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'API returns correct content type' - ); - t.equal(response.json().total, 1, 'API returns 1 author'); t.equal(response.json().items.length, 1, 'API returns 1 author'); }); test('GET single item array Refs', async t => { - let authorFromDb = await mongooseConnection.models.Author.findOne({ + let authorFromDb = await bw.conn.models.Author.findOne({ firstName: 'Jay' }).exec(); - let bookFromDb = await mongooseConnection.models.Book.findOne({ + let bookFromDb = await bw.conn.models.Book.findOne({ title: 'The best book' }).exec(); - let book2FromDb = await mongooseConnection.models.Book.findOne({ + let book2FromDb = await bw.conn.models.Book.findOne({ title: 'The best book2' }).exec(); - await BackwardWrapper.populateDoc(authorFromDb.populate('inspired')); + await bw.populateDoc(authorFromDb.populate('inspired')); let response = null; - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/books/' + bookFromDb._id + '/authors' }); - t.equal(response.statusCode, 200, 'API returns 200 status code'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'API returns correct content type' - ); - t.equal( response.json().total, 1, @@ -232,18 +192,11 @@ test('GET single item array Refs', async t => { t.equal(response.json().items.length, 1, 'API returns 1 refed author'); t.match(response.json().items[0], { firstName: 'Jay' }, 'Refed author'); - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/books/' + book2FromDb._id + '/authors' }); - t.equal(response.statusCode, 200, 'API returns 200 status code'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'API returns correct content type' - ); - t.equal( response.json().total, 1, @@ -254,23 +207,16 @@ test('GET single item array Refs', async t => { }); test('GET single item with populated array field', async t => { - let authorFromDb = await mongooseConnection.models.Author.findOne({ + let authorFromDb = await bw.conn.models.Author.findOne({ firstName: 'Jay' }).exec(); - await BackwardWrapper.populateDoc(authorFromDb.populate('inspired')); + await bw.populateDoc(authorFromDb.populate('inspired')); - const response = await fastify.inject({ + const response = await bw.inject(t, { method: 'GET', url: '/api/authors/' + authorFromDb.id + '?populate=inspired' }); - t.equal(response.statusCode, 200, 'Status code is 200'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Content-Type is application/json; charset=utf-8' - ); - t.match(response.json(), { firstName: 'Jay' }, 'Single item data ok'); t.equal(response.json().inspired.length, 2, '2 items in ref array'); @@ -285,10 +231,3 @@ test('GET single item with populated array field', async t => { 'apiValues model method works' ); }); - -// }); - -test('teardown', async () => { - await fastify.close(); - await mongooseConnection.close(); -}); diff --git a/test/schemas_from_path.js b/test/schemas_from_path.js index dcb84ef..39eaece 100644 --- a/test/schemas_from_path.js +++ b/test/schemas_from_path.js @@ -1,7 +1,5 @@ 'use strict'; -const fastifyMongooseAPI = require('../fastify-mongoose-api.js'); - const t = require('tap'); const { test } = t; @@ -9,16 +7,10 @@ const fs = require('fs'); const os = require('os'); const path = require('path'); -const Fastify = require('fastify'); const mongoose = require('mongoose'); - -const MONGODB_URL = - process.env.DATABASE_URI || 'mongodb://127.0.0.1/fastifymongooseapitest'; - const BackwardWrapper = require('./BackwardWrapper.js'); -let mongooseConnection = null; -let fastify = null; +const bw = new BackwardWrapper(t); const schema_authors = { name: 'authors', @@ -127,74 +119,53 @@ test('valid schemaDir', async t => { t.not(tmpPath, undefined, 'schemaDir is valid'); }); -test('mongoose db initialization', async t => { - mongooseConnection = await BackwardWrapper.createConnection(MONGODB_URL); - t.ok(mongooseConnection); - t.equal(mongooseConnection.readyState, 1, 'Ready state is connected(==1)'); /// connected +test('mongoose db initialization', async () => { + await bw.createConnection(); }); test('schema initialization', async t => { const authorSchema = mongoose.Schema(schema_authors.schema); - mongooseConnection.model('Author', authorSchema); + bw.conn.model('Author', authorSchema); const bookSchema = mongoose.Schema(schema_books.schema); - mongooseConnection.model('Book', bookSchema); + bw.conn.model('Book', bookSchema); - t.ok(mongooseConnection.models.Author); - t.ok(mongooseConnection.models.Book); + t.ok(bw.conn.models.Author); + t.ok(bw.conn.models.Book); }); test('clean up test collections', async () => { - await mongooseConnection.models.Author.deleteMany({}).exec(); - await mongooseConnection.models.Book.deleteMany({}).exec(); + await bw.conn.models.Author.deleteMany({}).exec(); + await bw.conn.models.Book.deleteMany({}).exec(); }); test('initialization of API server', async () => { - // setting up the server - fastify = Fastify(); - - fastify.register(fastifyMongooseAPI, { - models: mongooseConnection.models, + await bw.createServer({ + models: bw.conn.models, setDefaults: true, schemaDirPath: tmpPath }); - - await fastify.ready(); }); test('POST author item test', async t => { let response = null; - response = await fastify.inject({ + response = await bw.inject(t, { method: 'POST', url: '/api/authors', payload: { firstName: 'Hutin', lastName: 'Puylo' } }); - t.equal(response.statusCode, 200, 'POST request successful'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' - ); - t.match( response.json(), { firstName: 'Hutin', lastName: 'Puylo' }, 'POST api ok' ); - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/authors' }); - t.equal(response.statusCode, 200, 'GET request successful'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' - ); - t.match( response.json().items[0], { firstName: 'Hutin', lastName: 'Puylo' }, @@ -204,18 +175,14 @@ test('POST author item test', async t => { }); test('POST only firstName', async t => { - let response = null; - response = await fastify.inject({ - method: 'POST', - url: '/api/authors', - payload: { firstName: 'Hutin' } - }); - - t.equal(response.statusCode, 400, 'POST request failed'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' + const response = await bw.inject( + t, + { + method: 'POST', + url: '/api/authors', + payload: { firstName: 'Hutin' } + }, + 400 ); t.match( @@ -227,7 +194,7 @@ test('POST only firstName', async t => { test('POST valid birthday', async t => { let response = null; - response = await fastify.inject({ + response = await bw.inject(t, { method: 'POST', url: '/api/authors', payload: { @@ -237,30 +204,17 @@ test('POST valid birthday', async t => { } }); - t.equal(response.statusCode, 200, 'POST request successful'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' - ); t.match( response.json(), { firstName: 'Hutin', lastName: 'Puylo', birthday: '1969-07-06' }, 'POST api ok' ); - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/authors' }); - t.equal(response.statusCode, 200, 'GET request successful'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' - ); - t.match( response.json().items[1], { firstName: 'Hutin', lastName: 'Puylo', birthday: '1969-07-06' }, @@ -270,23 +224,20 @@ test('POST valid birthday', async t => { }); test('POST invalid birthday', async t => { - let response = null; - response = await fastify.inject({ - method: 'POST', - url: '/api/authors', - payload: { - firstName: 'Hutin', - lastName: 'Puylo', - birthday: '1969-30-06' - } - }); - - t.equal(response.statusCode, 400, 'POST request unsuccessful'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' + const response = await bw.inject( + t, + { + method: 'POST', + url: '/api/authors', + payload: { + firstName: 'Hutin', + lastName: 'Puylo', + birthday: '1969-30-06' + } + }, + 400 ); + t.match( response.json().message, 'body/birthday must match format "date"', @@ -295,20 +246,12 @@ test('POST invalid birthday', async t => { }); test('POST book item test', async t => { - let response = null; - response = await fastify.inject({ + const response = await bw.inject(t, { method: 'POST', url: '/api/books', payload: { title: 'Critique of Practical Reason', isbn: '1519394632' } }); - t.equal(response.statusCode, 200, 'POST request successful'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' - ); - t.match( response.json(), { title: 'Critique of Practical Reason', isbn: '1519394632' }, @@ -317,18 +260,14 @@ test('POST book item test', async t => { }); test('POST book without isbn', async t => { - let response = null; - response = await fastify.inject({ - method: 'POST', - url: '/api/books', - payload: { title: 'Critique of Practical Reason' } - }); - - t.equal(response.statusCode, 400, 'POST request unsuccessful'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' + const response = await bw.inject( + t, + { + method: 'POST', + url: '/api/books', + payload: { title: 'Critique of Practical Reason' } + }, + 400 ); t.match( @@ -339,39 +278,30 @@ test('POST book without isbn', async t => { }); test('Shutdown API server', async () => { - await fastify.close(); + await bw.fastify.close(); }); // now birthday is required too schema_authors.ref[0].required = ['firstName', 'lastName', 'birthday']; test('reload API server with schemas', async () => { - // setting up the server - fastify = Fastify(); - - fastify.register(fastifyMongooseAPI, { - models: mongooseConnection.models, + await bw.createServer({ + models: bw.conn.models, setDefaults: true, schemas: [schema_authors], // this replaces one loaded in dirPath schemaDirPath: tmpPath }); - - await fastify.ready(); }); test('POST without birthday', async t => { - let response = null; - response = await fastify.inject({ - method: 'POST', - url: '/api/authors', - payload: { firstName: 'Hutin', lastName: 'Puylo' } - }); - - t.equal(response.statusCode, 400, 'POST request unsuccessful'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' + const response = await bw.inject( + t, + { + method: 'POST', + url: '/api/authors', + payload: { firstName: 'Hutin', lastName: 'Puylo' } + }, + 400 ); t.match( @@ -382,8 +312,7 @@ test('POST without birthday', async t => { }); test('POST with birthday', async t => { - let response = null; - response = await fastify.inject({ + const response = await bw.inject(t, { method: 'POST', url: '/api/authors', payload: { @@ -393,12 +322,6 @@ test('POST with birthday', async t => { } }); - t.equal(response.statusCode, 200, 'POST request successful'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Response has correct content type' - ); t.match( response.json(), { firstName: 'Hutin', lastName: 'Puylo', birthday: '1969-07-06' }, @@ -407,8 +330,6 @@ test('POST with birthday', async t => { }); test('teardown', async () => { - await fastify.close(); - await mongooseConnection.close(); fs.unlinkSync(path.join(tmpPath, 'authors.schema.js')); fs.unlinkSync(path.join(tmpPath, 'subdir', 'books.schema.js')); fs.rmdirSync(path.join(tmpPath, 'subdir')); diff --git a/test/validation_schemas.test.js b/test/validation_schemas.test.js index 89e6c69..53a3acf 100644 --- a/test/validation_schemas.test.js +++ b/test/validation_schemas.test.js @@ -1,20 +1,12 @@ 'use strict'; -const fastifyMongooseAPI = require('../fastify-mongoose-api.js'); - const t = require('tap'); const { test } = t; -const Fastify = require('fastify'); const mongoose = require('mongoose'); - -const MONGODB_URL = - process.env.DATABASE_URI || 'mongodb://127.0.0.1/fastifymongooseapitest'; - const BackwardWrapper = require('./BackwardWrapper.js'); -let mongooseConnection = null; -let fastify = null; +const bw = new BackwardWrapper(t); let collection = 'authors'; @@ -113,72 +105,51 @@ let schema_full = { routeDelete: {} }; -test('mongoose db initialization', async t => { - mongooseConnection = await BackwardWrapper.createConnection(MONGODB_URL); - t.ok(mongooseConnection); - t.equal(mongooseConnection.readyState, 1, 'Ready state is connected(==1)'); /// connected +test('mongoose db initialization', async () => { + await bw.createConnection(); }); test('schema initialization', async t => { const authorSchema = mongoose.Schema(schema_base.schema); - mongooseConnection.model('Author', authorSchema); - t.ok(mongooseConnection.models.Author); + bw.conn.model('Author', authorSchema); + t.ok(bw.conn.models.Author); }); // base, empty, with_ref should have old working mode //[schema_base, schema_empty, schema_with_ref].forEach(schema => { [schema_base, schema_empty, schema_with_ref].forEach(schema => { - let fastify; - test('clean up test collections', async () => { - await mongooseConnection.models.Author.deleteMany({}).exec(); + await bw.conn.models.Author.deleteMany({}).exec(); }); test('initialization of API server', async () => { - ///// setting up the server - fastify = Fastify(); - - fastify.register(fastifyMongooseAPI, { - models: mongooseConnection.models, + await bw.createServer({ + models: bw.conn.models, setDefaults: true, schemas: [schema] }); - - await fastify.ready(); }); test('POST item test', async t => { let response = null; - response = await fastify.inject({ + response = await bw.inject(t, { method: 'POST', url: '/api/authors', payload: { firstName: 'Hutin', lastName: 'Puylo' } }); - t.equal(response.statusCode, 200, 'POST api ok'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8' - ); - t.match( response.json(), { firstName: 'Hutin', lastName: 'Puylo' }, 'POST api ok' ); - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/authors' }); - t.equal(response.statusCode, 200, 'GET api ok'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8' - ); - t.match( response.json().items[0], { firstName: 'Hutin', lastName: 'Puylo' }, @@ -188,8 +159,7 @@ test('schema initialization', async t => { }); test('NO validation so, also a field is valid', async t => { - let response = null; - response = await fastify.inject({ + const response = await bw.inject(t, { method: 'POST', url: '/api/authors', payload: { @@ -197,18 +167,12 @@ test('schema initialization', async t => { } }); - t.equal(response.statusCode, 200, 'POST api ok'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8' - ); - t.match(response.json(), { firstName: 'Hutin' }, 'POST api ok'); }); test('POST valid birthday', async t => { let response = null; - response = await fastify.inject({ + response = await bw.inject(t, { method: 'POST', url: '/api/authors', payload: { @@ -218,11 +182,6 @@ test('schema initialization', async t => { } }); - t.equal(response.statusCode, 200, 'POST api ok'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8' - ); t.match( response.json(), { @@ -233,17 +192,11 @@ test('schema initialization', async t => { 'POST api ok' ); - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/authors' }); - t.equal(response.statusCode, 200, 'GET api ok'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8' - ); - t.match( response.json().items[2], { @@ -257,18 +210,19 @@ test('schema initialization', async t => { }); test('POST invalid birthday', async t => { - let response = null; - response = await fastify.inject({ - method: 'POST', - url: '/api/authors', - payload: { - firstName: 'Hutin', - lastName: 'Puylo', - birthday: '1969-30-06' - } - }); - - t.equal(response.statusCode, 500, 'Internal server error'); + const response = await bw.inject( + t, + { + method: 'POST', + url: '/api/authors', + payload: { + firstName: 'Hutin', + lastName: 'Puylo', + birthday: '1969-30-06' + } + }, + 500 + ); t.type(response.json().message, 'string'); // it may be a simple: @@ -284,39 +238,31 @@ test('schema initialization', async t => { }); test('Shutdown API server', async () => { - await fastify.close(); + await bw.fastify.close(); }); }); test('clean up test collections', async () => { - await mongooseConnection.models.Author.deleteMany({}).exec(); + await bw.conn.models.Author.deleteMany({}).exec(); }); test('initialization of API server with validation', async () => { - ///// setting up the server - fastify = Fastify(); - - fastify.register(fastifyMongooseAPI, { - models: mongooseConnection.models, + await bw.createServer({ + models: bw.conn.models, setDefaults: true, schemas: [schema_full] }); - - await fastify.ready(); }); test('Check required validation', async t => { - let response = null; - response = await fastify.inject({ - method: 'POST', - url: '/api/authors', - payload: { firstName: 'Hutin' } - }); - - t.equal( - response.statusCode, - 400, - 'POST api with missing required property should return 400' + const response = await bw.inject( + t, + { + method: 'POST', + url: '/api/authors', + payload: { firstName: 'Hutin' } + }, + 400 ); t.match( @@ -328,7 +274,7 @@ test('Check required validation', async t => { test('POST valid birthday', async t => { let response = null; - response = await fastify.inject({ + response = await bw.inject(t, { method: 'POST', url: '/api/authors', payload: { @@ -338,30 +284,17 @@ test('POST valid birthday', async t => { } }); - t.equal(response.statusCode, 200, 'POST api should return 200'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Content-Type should be application/json; charset=utf-8' - ); t.match( response.json(), { firstName: 'Hutin', lastName: 'Puylo', birthday: '1969-07-06' }, 'POST api ok' ); - response = await fastify.inject({ + response = await bw.inject(t, { method: 'GET', url: '/api/authors' }); - t.equal(response.statusCode, 200, 'GET api should return 200'); - t.equal( - response.headers['content-type'], - 'application/json; charset=utf-8', - 'Content-Type should be application/json; charset=utf-8' - ); - t.match( response.json().items[0], { firstName: 'Hutin', lastName: 'Puylo', birthday: '1969-07-06' }, @@ -372,21 +305,20 @@ test('POST valid birthday', async t => { test('POST invalid birthday', async t => { let response = null; - response = await fastify.inject({ - method: 'POST', - url: '/api/authors', - payload: { - firstName: 'Hutin', - lastName: 'Puylo', - birthday: '1969-30-06' - } - }); - - t.equal( - response.statusCode, - 400, - 'POST api with invalid birthday should return 400' + response = await bw.inject( + t, + { + method: 'POST', + url: '/api/authors', + payload: { + firstName: 'Hutin', + lastName: 'Puylo', + birthday: '1969-30-06' + } + }, + 400 ); + t.equal( response.headers['content-type'], 'application/json; charset=utf-8', @@ -398,8 +330,3 @@ test('POST invalid birthday', async t => { 'POST api ok' ); }); - -test('teardown', async () => { - await fastify.close(); - await mongooseConnection.close(); -});