Improved testing, more linting and added paging to request/all endpoint #95
@@ -28,7 +28,7 @@ class Config {
|
|||||||
const field = new Field(this.fields[section][option]);
|
const field = new Field(this.fields[section][option]);
|
||||||
|
|
||||||
if (field.value === '') {
|
if (field.value === '') {
|
||||||
const envField = process.env[[section.toUpperCase(), option.toUpperCase()].join('_')];
|
const envField = process.env[['SEASONED', section.toUpperCase(), option.toUpperCase()].join('_')];
|
||||||
if (envField !== undefined && envField.length !== 0) { return envField; }
|
if (envField !== undefined && envField.length !== 0) { return envField; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
const configuration = require('src/config/configuration').getInstance();
|
const configuration = require('src/config/configuration').getInstance();
|
||||||
const SqliteDatabase = require('src/database/sqliteDatabase');
|
const SqliteDatabase = require('src/database/sqliteDatabase');
|
||||||
|
|
||||||
const host = process.env.TESTING ? ':memory:' : configuration.get('database', 'host');
|
const database = new SqliteDatabase(configuration.get('database', 'host'));
|
||||||
const database = new SqliteDatabase(host);
|
|
||||||
/**
|
/**
|
||||||
* This module establishes a connection to the database
|
* This module establishes a connection to the database
|
||||||
* specified in the confgiuration file. It tries to setup
|
* specified in the confgiuration file. It tries to setup
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ class TMDB {
|
|||||||
/**
|
/**
|
||||||
* Retrieve a specific movie by id from TMDB.
|
* Retrieve a specific movie by id from TMDB.
|
||||||
* @param {Number} identifier of the movie you want to retrieve
|
* @param {Number} identifier of the movie you want to retrieve
|
||||||
|
* @param {String} type filter results by type (default movie).
|
||||||
* @returns {Promise} succeeds if movie was found
|
* @returns {Promise} succeeds if movie was found
|
||||||
*/
|
*/
|
||||||
lookup(identifier, type = 'movie') {
|
lookup(identifier, type = 'movie') {
|
||||||
@@ -36,7 +37,7 @@ class TMDB {
|
|||||||
const cacheKey = `${this.cacheTags.info}:${type}:${identifier}`;
|
const cacheKey = `${this.cacheTags.info}:${type}:${identifier}`;
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => this.cache.get(cacheKey))
|
.then(() => this.cache.get(cacheKey))
|
||||||
.catch(() => this.tmdb(this.tmdbMethod('info', type), query))
|
.catch(() => this.tmdb(TMDB_METHODS['info'][type]), query))
|
||||||
.catch(() => { throw new Error('Could not find a movie with that id.'); })
|
.catch(() => { throw new Error('Could not find a movie with that id.'); })
|
||||||
.then(response => this.cache.set(cacheKey, response))
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
@@ -50,16 +51,18 @@ class TMDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrive list of of items from TMDB matching the query and/or type given.
|
* Retrive search results from TMDB.
|
||||||
* @param {queryText, page, type} the page number to specify in the request for discover,
|
* @param {String} text query you want to search for
|
||||||
|
* @param {Number} page representing pagination of results
|
||||||
|
* @param {String} type filter results by type (default multi)
|
||||||
* @returns {Promise} dict with query results, current page and total_pages
|
* @returns {Promise} dict with query results, current page and total_pages
|
||||||
*/
|
*/
|
||||||
search(text, page = 1, type = 'multi') {
|
search(text, page = 1, type = 'multi') {
|
||||||
const query = { query: text, page };
|
const query = { query: text, page: page };
|
||||||
const cacheKey = `${this.cacheTags.search}:${page}:${type}:${text}`;
|
const cacheKey = `${this.cacheTags.search}:${page}:${type}:${text}`;
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => this.cache.get(cacheKey))
|
.then(() => this.cache.get(cacheKey))
|
||||||
.catch(() => this.tmdb(this.tmdbMethod('search', type), query))
|
.catch(() => this.tmdb(TMDB_METHODS['search'][type], query))
|
||||||
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
|
.catch(() => { throw new Error('Could not search for movies/shows at tmdb.'); })
|
||||||
.then(response => this.cache.set(cacheKey, response))
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
.then(response => this.mapResults(response))
|
.then(response => this.mapResults(response))
|
||||||
@@ -71,18 +74,17 @@ class TMDB {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches a given list from tmdb.
|
* Fetches a given list from tmdb.
|
||||||
* @param {listName} List we want to fetch.
|
* @param {String} listName Name of list
|
||||||
* @param {type} The to specify in the request for discover (default 'movie').
|
* @param {String} type filter results by type (default movie)
|
||||||
* @param {id} When finding similar a id can be added to query
|
* @param {Number} page representing pagination of results
|
||||||
* @param {page} Page number we want to fetch.
|
|
||||||
* @returns {Promise} dict with query results, current page and total_pages
|
* @returns {Promise} dict with query results, current page and total_pages
|
||||||
*/
|
*/
|
||||||
listSearch(listName, type = 'movie', id, page = '1') {
|
listSearch(listName, type = 'movie', page = '1') {
|
||||||
const params = { id, page };
|
const cacheKey = `${this.cacheTags[listName]}:${type}:${page}`;
|
||||||
const cacheKey = `${this.cacheTags[listName]}:${type}:${id}:${page}`;
|
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
.then(() => this.cache.get(cacheKey))
|
.then(() => this.cache.get(cacheKey))
|
||||||
.catch(() => this.tmdb(this.tmdbMethod(listName, type), params))
|
.catch(() => this.tmdb(TMDB_METHODS[listName][type], page))
|
||||||
|
.catch(() => { throw new Error('Error fetching list from tmdb.')})
|
||||||
.then(response => this.cache.set(cacheKey, response))
|
.then(response => this.cache.set(cacheKey, response))
|
||||||
.then(response => this.mapResults(response, type))
|
.then(response => this.mapResults(response, type))
|
||||||
.catch((error) => { throw new Error(error); })
|
.catch((error) => { throw new Error(error); })
|
||||||
@@ -91,16 +93,10 @@ class TMDB {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
tmdbMethod(apiMethod, type) {
|
|
||||||
const method = TMDB_METHODS[apiMethod][type];
|
|
||||||
if (method !== undefined) return method;
|
|
||||||
throw new Error('Could not find tmdb api method.');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maps our response from tmdb api to a movie/show object.
|
* Maps our response from tmdb api to a movie/show object.
|
||||||
* @param {response} JSON response from tmdb.
|
* @param {String} response from tmdb.
|
||||||
* @param {type} The type declared in listSearch.
|
* @param {String} The type declared in listSearch.
|
||||||
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
|
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
|
||||||
*/
|
*/
|
||||||
mapResults(response, type) {
|
mapResults(response, type) {
|
||||||
@@ -114,6 +110,12 @@ class TMDB {
|
|||||||
.catch((error) => { throw new Error(error); });
|
.catch((error) => { throw new Error(error); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps moviedb library to support Promises.
|
||||||
|
* @param {String} method function name in the library
|
||||||
|
* @param {Object} argument argument to function being called
|
||||||
|
* @returns {Promise} succeeds if callback succeeds
|
||||||
|
*/
|
||||||
tmdb(method, argument) {
|
tmdb(method, argument) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const callback = (error, reponse) => {
|
const callback = (error, reponse) => {
|
||||||
|
|||||||
13
seasoned_api/test/fixtures/arrival-info-success-response.json
vendored
Normal file
13
seasoned_api/test/fixtures/arrival-info-success-response.json
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"background_path": "/yIZ1xendyqKvY3FGeeUYUd5X9Mm.jpg",
|
||||||
|
"id": 329865,
|
||||||
|
"popularity": 26.978601,
|
||||||
|
"poster_path": "/hLudzvGfpi6JlwUnsNhXwKKg4j.jpg",
|
||||||
|
"release_status": "Released",
|
||||||
|
"score": 7.3,
|
||||||
|
"summary": "Taking place after alien crafts land around the world, an expert linguist is recruited by the military to determine whether they come in peace or are a threat.",
|
||||||
|
"tagline": "Why are they here?",
|
||||||
|
"title": "Arrival",
|
||||||
|
"type": "movie",
|
||||||
|
"year": 2016
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ const popularMoviesSuccess = require('test/fixtures/popular-movies-success-respo
|
|||||||
|
|
||||||
describe('As a user I want to get popular movies', () => {
|
describe('As a user I want to get popular movies', () => {
|
||||||
before(() => resetDatabase());
|
before(() => resetDatabase());
|
||||||
before(() => createCacheEntry('p:movie::1', popularMoviesSuccess));
|
before(() => createCacheEntry('p:movie:1', popularMoviesSuccess));
|
||||||
|
|
||||||
it('should return 200 with the information', () =>
|
it('should return 200 with the information', () =>
|
||||||
request(app)
|
request(app)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ const popularShowsSuccess = require('test/fixtures/popular-show-success-response
|
|||||||
|
|
||||||
describe('As a user I want to get popular shows', () => {
|
describe('As a user I want to get popular shows', () => {
|
||||||
before(() => resetDatabase());
|
before(() => resetDatabase());
|
||||||
before(() => createCacheEntry('p:show::1', popularShowsSuccess));
|
before(() => createCacheEntry('p:show:1', popularShowsSuccess));
|
||||||
|
|
||||||
it('should return 200 with the information', () =>
|
it('should return 200 with the information', () =>
|
||||||
request(app)
|
request(app)
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
const resetDatabase = require('test/helpers/resetDatabase');
|
const resetDatabase = require('test/helpers/resetDatabase');
|
||||||
|
const createCacheEntry = require('test/helpers/createCacheEntry');
|
||||||
const app = require('src/webserver/app');
|
const app = require('src/webserver/app');
|
||||||
const request = require('supertest-as-promised');
|
const request = require('supertest-as-promised');
|
||||||
const createUser = require('test/helpers/createUser');
|
const createUser = require('test/helpers/createUser');
|
||||||
const createToken = require('test/helpers/createToken');
|
const createToken = require('test/helpers/createToken');
|
||||||
|
const infoMovieSuccess = require('test/fixtures/arrival-info-success-response.json');
|
||||||
|
|
||||||
describe('As a user I want to request a movie', () => {
|
describe('As a user I want to request a movie', () => {
|
||||||
before(() => {
|
before(() => {
|
||||||
return resetDatabase()
|
return resetDatabase()
|
||||||
.then(() => createUser('test_user', 'test@gmail.com', 'password'));
|
.then(() => createUser('test_user', 'test@gmail.com', 'password'));
|
||||||
})
|
})
|
||||||
|
before(() => createCacheEntry('i:movie:329865', infoMovieSuccess));
|
||||||
|
|
||||||
it('should return 200 when item is requested', () =>
|
it('should return 200 when item is requested', () =>
|
||||||
request(app)
|
request(app)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ const interstellarQuerySuccess = require('test/fixtures/interstellar-query-succe
|
|||||||
|
|
||||||
describe('As an anonymous user I want to search for a movie', () => {
|
describe('As an anonymous user I want to search for a movie', () => {
|
||||||
before(() => resetDatabase());
|
before(() => resetDatabase());
|
||||||
before(() => createCacheEntry('s:1:movie:interstellar', interstellarQuerySuccess));
|
before(() => createCacheEntry('se:1:multi:interstellar', interstellarQuerySuccess));
|
||||||
|
|
||||||
it('should return 200 with the search results even if user is not logged in', () =>
|
it('should return 200 with the search results even if user is not logged in', () =>
|
||||||
request(app)
|
request(app)
|
||||||
|
|||||||
Reference in New Issue
Block a user