Skip to content

Commit

Permalink
Merge pull request #95 from KevinMidboe/docs_testing
Browse files Browse the repository at this point in the history
Improved testing, more linting and added paging to request/all endpoint
  • Loading branch information
KevinMidboe authored Mar 21, 2018
2 parents c1461e1 + 096bbdf commit 451b676
Show file tree
Hide file tree
Showing 18 changed files with 714 additions and 61 deletions.
17 changes: 17 additions & 0 deletions seasoned_api/conf/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"database": {
"host": ":memory:"
},
"webserver": {
"port": 31400
},
"tmdb": {
"apiKey": "bogus-api-key"
},
"raven": {
"DSN": ""
},
"authentication": {
"secret": "secret"
}
}
7 changes: 5 additions & 2 deletions seasoned_api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
"main": "webserver/server.js",
"scripts": {
"start": "cross-env SEASONED_CONFIG=conf/development.json NODE_PATH=. node src/webserver/server.js",
"test": "cross-env SEASONED_CONFIG=conf/development.json TESTING=true NODE_PATH=. mocha --recursive test/system",
"test": "cross-env SEASONED_CONFIG=conf/test.json NODE_PATH=. mocha --recursive test",
"coverage": "cross-env SEASONED_CONFIG=conf/test.json NODE_PATH=. istanbul cover -x script/autogenerate-documentation.js --include-all-sources --dir test/.coverage node_modules/mocha/bin/_mocha --recursive test/**/* -- --report lcovonly && cat test/.coverage/lcov.info | coveralls && rm -rf test/.coverage",
"lint": "./node_modules/.bin/eslint src/"
},
"dependencies": {
"bcrypt-nodejs": "^0.0.3",
"blanket": "^1.2.3",
"body-parser": "~1.0.1",
"codecov": "^3.0.0",
"cross-env": "^3.1.3",
"express": "~4.11.0",
"jsonwebtoken": "^8.0.1",
"mocha-lcov-reporter": "^1.3.0",
"mongoose": "^3.6.13",
"moviedb": "^0.2.10",
"node-cache": "^4.1.1",
Expand All @@ -28,7 +31,7 @@
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.8.0",
"istanbul": "^0.4.5",
"mocha": "^3.1.0",
"mocha": "^5.0.4",
"supertest": "^2.0.1",
"supertest-as-promised": "^4.0.1"
}
Expand Down
2 changes: 1 addition & 1 deletion seasoned_api/src/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Config {
const field = new Field(this.fields[section][option]);

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; }
}

Expand Down
3 changes: 1 addition & 2 deletions seasoned_api/src/database/database.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const configuration = require('src/config/configuration').getInstance();
const SqliteDatabase = require('src/database/sqliteDatabase');

const host = process.env.TESTING ? ':memory:' : configuration.get('database', 'host');
const database = new SqliteDatabase(host);
const database = new SqliteDatabase(configuration.get('database', 'host'));
/**
* This module establishes a connection to the database
* specified in the confgiuration file. It tries to setup
Expand Down
34 changes: 17 additions & 17 deletions seasoned_api/src/plex/requestRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class RequestRepository {
this.queries = {
insertRequest: `INSERT INTO requests(id,title,year,poster_path,background_path,requested_by,ip,user_agent,type)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
fetchRequestedItems: 'SELECT * FROM requests ORDER BY date DESC',
fetchRequestedItemsByStatus: 'SELECT * FROM requests WHERE status IS ? AND type LIKE ? ORDER BY date DESC',
fetchRequestedItems: 'SELECT * FROM requests ORDER BY date DESC LIMIT 25 OFFSET ?*25-25',
fetchRequestedItemsByStatus: 'SELECT * FROM requests WHERE status IS ? AND type LIKE ? ORDER BY date DESC LIMIT 25 OFFSET ?*25-25',
updateRequestedById: 'UPDATE requests SET status = ? WHERE id is ? AND type is ?',
checkIfIdRequested: 'SELECT * FROM requests WHERE id IS ? AND type IS ?',
userRequests: 'SELECT * FROM requests WHERE requested_by IS ?',
Expand Down Expand Up @@ -58,23 +58,23 @@ class RequestRepository {
* @returns {Promise} If nothing has gone wrong.
*/
sendRequest(identifier, type, ip, user_agent, user) {
return Promise.resolve()
.then(() => tmdb.lookup(identifier, type))
.then((movie) => {
const username = user === undefined ? undefined : user.username;
// Add request to database
return this.database.run(this.queries.insertRequest, [movie.id, movie.title, movie.year, movie.poster_path, movie.background_path, username, ip, user_agent, movie.type]);
});
return Promise.resolve()
.then(() => tmdb.lookup(identifier, type))
.then((movie) => {
const username = user === undefined ? undefined : user.username;
// Add request to database
return this.database.run(this.queries.insertRequest, [movie.id, movie.title, movie.year, movie.poster_path, movie.background_path, username, ip, user_agent, movie.type]);
});
}

fetchRequested(status, type = '%') {
return Promise.resolve()
.then(() => {
if (status === 'requested' || status === 'downloading' || status === 'downloaded') {
return this.database.all(this.queries.fetchRequestedItemsByStatus, [status, type]);
}
return this.database.all(this.queries.fetchRequestedItems);
});
fetchRequested(status, page = '1', type = '%') {
return Promise.resolve()
.then(() => {
if (status === 'requested' || status === 'downloading' || status === 'downloaded')
return this.database.all(this.queries.fetchRequestedItemsByStatus, [status, type, page]);
else
return this.database.all(this.queries.fetchRequestedItems, page);
})
}

userRequests(user) {
Expand Down
57 changes: 27 additions & 30 deletions seasoned_api/src/tmdb/tmdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ class TMDB {
/**
* Retrieve a specific movie by id from TMDB.
* @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
*/
lookup(identifier, type = 'movie') {
const query = { id: identifier };
const cacheKey = `${this.cacheTags.info}:${type}:${identifier}`;
return Promise.resolve()
.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.'); })
.then(response => this.cache.set(cacheKey, response))
.then((response) => {
Expand All @@ -50,70 +51,66 @@ class TMDB {
}

/**
* Retrive list of of items from TMDB matching the query and/or type given.
* @param {queryText, page, type} the page number to specify in the request for discover,
* Retrive search results from TMDB.
* @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
*/
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}`;
return Promise.resolve()
.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.'); })
.then(response => this.cache.set(cacheKey, response))
.then(response => this.mapResults(response))
.catch((error) => { throw new Error(error); })
.then(([mappedResults, pagenumber, totalpages, total_results]) => ({
results: mappedResults, page: pagenumber, total_results, total_pages: totalpages,
}));
}

/**
* Fetches a given list from tmdb.
* @param {listName} List we want to fetch.
* @param {type} The to specify in the request for discover (default 'movie').
* @param {id} When finding similar a id can be added to query
* @param {page} Page number we want to fetch.
* @param {String} listName Name of list
* @param {String} type filter results by type (default movie)
* @param {Number} page representing pagination of results
* @returns {Promise} dict with query results, current page and total_pages
*/
listSearch(listName, type = 'movie', id, page = '1') {
const params = { id, page };
const cacheKey = `${this.cacheTags[listName]}:${type}:${id}:${page}`;
listSearch(listName, type = 'movie', page = '1') {
const query = { page: page }
console.log(query)
const cacheKey = `${this.cacheTags[listName]}:${type}:${page}`;
return Promise.resolve()
.then(() => this.cache.get(cacheKey))
.catch(() => this.tmdb(this.tmdbMethod(listName, type), params))
.catch(() => this.tmdb(TMDB_METHODS[listName][type], query))
.catch(() => { throw new Error('Error fetching list from tmdb.')})
.then(response => this.cache.set(cacheKey, response))
.then(response => this.mapResults(response, type))
.catch((error) => { throw new Error(error); })
.then(([mappedResults, pagenumber, totalpages, total_results]) => ({
results: mappedResults, page: pagenumber, total_pages: totalpages, total_results,
}));
}

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.
* @param {response} JSON response from tmdb.
* @param {type} The type declared in listSearch.
* @param {String} response from tmdb.
* @param {String} The type declared in listSearch.
* @returns {Promise} dict with tmdb results, mapped as movie/show objects.
*/
mapResults(response, type) {
console.log(response.page)
return Promise.resolve()
.then(() => {
const mappedResults = response.results.filter((element) => {
return (element.media_type === 'movie' || element.media_type === 'tv' || element.media_type === undefined);
}).map((element) => convertTmdbToSeasoned(element, type));
return [mappedResults, response.page, response.total_pages, response.total_results];
return {results: mappedResults, page: response.page, total_pages: response.total_pages, total_results: response.total_results}
})
.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) {
return new Promise((resolve, reject) => {
const callback = (error, reponse) => {
Expand Down
4 changes: 2 additions & 2 deletions seasoned_api/src/webserver/controllers/plex/fetchRequested.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const requestRepository = new RequestRepository();
*/
function fetchRequestedController(req, res) {
// const user = req.loggedInUser;
const { status } = req.query;
const { status, page } = req.query;

requestRepository.fetchRequested(status)
requestRepository.fetchRequested(status, page)
.then((requestedItems) => {
res.send({ success: true, results: requestedItems, total_results: requestedItems.length });
})
Expand Down
4 changes: 2 additions & 2 deletions seasoned_api/src/webserver/controllers/tmdb/listSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
*/
function listSearchController(req, res) {
const listname = req.params.listname;
const { type, id, page } = req.query;
tmdb.listSearch(listname, type, id, page)
const { type, page } = req.query;
tmdb.listSearch(listname, type, page)
.then((results) => {
res.send(results);
}).catch((error) => {
Expand Down
13 changes: 13 additions & 0 deletions seasoned_api/test/fixtures/arrival-info-success-response.json
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 451b676

Please sign in to comment.