Moved contents of seasoned_api up to root folder

This commit is contained in:
2022-08-19 01:03:27 +02:00
parent 0efc109992
commit 56262a45c8
134 changed files with 885 additions and 32 deletions

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

View File

@@ -0,0 +1 @@
[{"adult":false,"backdrop_path":"/mVr0UiqyltcfqxbAUcLl9zWL8ah.jpg","belongs_to_collection":{"id":422837,"name":"Blade Runner Collection","poster_path":"/cWESb1o9lW2i2Z3Xllv9u40aNIk.jpg","backdrop_path":"/bSHZIvLoPBWyGLeiAudN1mXdvQX.jpg"},"budget":150000000,"genres":[{"id":9648,"name":"Mystery"},{"id":878,"name":"Science Fiction"},{"id":53,"name":"Thriller"}],"homepage":"http://bladerunnermovie.com/","id":335984,"imdb_id":"tt1856101","original_language":"en","original_title":"Blade Runner 2049","overview":"Thirty years after the events of the first film, a new blade runner, LAPD Officer K, unearths a long-buried secret that has the potential to plunge what's left of society into chaos. K's discovery leads him on a quest to find Rick Deckard, a former LAPD blade runner who has been missing for 30 years.","popularity":30.03,"poster_path":"/gajva2L0rPYkEWjzgFlBXCAVBE5.jpg","production_companies":[{"id":79529,"logo_path":"/gVN3k8emmKy4iV4KREWcCtxusZK.png","name":"Torridon Films","origin_country":"US"},{"id":101829,"logo_path":"/8IOjCvgjq0zTrtP91cWD3kL2jMK.png","name":"16:14 Entertainment","origin_country":"US"},{"id":1645,"logo_path":"/6Ry6uNBaa0IbbSs1XYIgX5DkA9r.png","name":"Scott Free Productions","origin_country":""},{"id":5,"logo_path":"/71BqEFAF4V3qjjMPCpLuyJFB9A.png","name":"Columbia Pictures","origin_country":"US"},{"id":1088,"logo_path":"/9WOE5AQUXbOtLU6GTwfjS8OMF0v.png","name":"Alcon Entertainment","origin_country":"US"},{"id":78028,"logo_path":"/sTFcDFfJaSVT3sv3DoaZDE4SlGB.png","name":"Thunderbird Entertainment","origin_country":"CA"},{"id":174,"logo_path":"/ky0xOc5OrhzkZ1N6KyUxacfQsCk.png","name":"Warner Bros. Pictures","origin_country":"US"}],"production_countries":[{"iso_3166_1":"CA","name":"Canada"},{"iso_3166_1":"US","name":"United States of America"},{"iso_3166_1":"HU","name":"Hungary"},{"iso_3166_1":"GB","name":"United Kingdom"}],"release_date":"2017-10-04","revenue":259239658,"runtime":163,"spoken_languages":[{"iso_639_1":"en","name":"English"},{"iso_639_1":"fi","name":"suomi"}],"status":"Released","tagline":"There's still a page left.","title":"Blade Runner 2049","video":false,"vote_average":7.3,"vote_count":5478}]

View File

@@ -0,0 +1,6 @@
{
"page":1,
"results":[],
"total_results":0,
"total_pages":1
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,89 @@
{
"number_of_items_on_page": 5,
"page": 1,
"results": [
{
"background": "/xu9zaAevzQ5nnrsXN6JcahLnG4i.jpg",
"genre": [
12,
18,
878
],
"id": 157336,
"matchedInPlex": false,
"popularity": 50.262329,
"poster": "/nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg",
"rating": 8.1,
"summary": "Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.",
"title": "Interstellar",
"type": "movie",
"vote_count": 12095,
"year": 2014
},
{
"background": "/bT5jpIZE50MI0COE8pOeq0kMpQo.jpg",
"genre": [
99
],
"id": 301959,
"matchedInPlex": false,
"popularity": 6.174326,
"poster": "/xZwUIPqBHyJ2QIfMPANOZ1mAld6.jpg",
"rating": 7.9,
"summary": "Behind the scenes of Christopher Nolan's sci-fi drama, which stars Matthew McConaughey and Anne Hathaway",
"title": "Interstellar: Nolan's Odyssey",
"type": "movie",
"vote_count": 102,
"year": 2014
},
{
"background": "/yTnHa6lgIv8rNneSNBDkBe8MnZe.jpg",
"genre": [
878
],
"id": 398188,
"matchedInPlex": false,
"popularity": 3.847981,
"poster": "/cjvTebuqD8wmhchHE286ltVcbX6.jpg",
"rating": 4.7,
"summary": "For Millennia the Aliien force has watched and waited, a brooding menace that has now at last decided to take over the Earth. Communications systems worldwide are sent into chaos by a strange atmospheric interference and this has turned into a global phenomenon. A massive spaceship headed towards Earth and smaller spaceships began to cover entire cities around the world. Suddenly, the wonder turns into horror as the spaceships destroy the cities with energy weapons. When the world counterattacks, the alien ships are invincible to military weapons. The survivors have to use their wits to kill the aliens, or die.",
"title": "Interstellar Wars",
"type": "movie",
"vote_count": 5,
"year": 2016
},
{
"background": "/mgb6tVEieDYLpQt666ACzGz5cyE.jpg",
"genre": [
35
],
"id": 287954,
"matchedInPlex": false,
"popularity": 2.778622,
"poster": "/buoq7zYO4J3ttkEAqEMWelPDC0G.jpg",
"rating": 7,
"summary": "An undeniably beautiful alien is sent to Earth to study the complex mating rituals of human beings, which leads to the young interstellar traveler experiencing the passion that surrounds the centuries-old ritual of the species.",
"title": "Lolita from Interstellar Space",
"type": "movie",
"vote_count": 1,
"year": 2014
},
{
"background": null,
"genre": [
99
],
"id": 336592,
"matchedInPlex": false,
"popularity": 2.147155,
"poster": "/6KBD7YSBjCfgBgHwpsQo3G3GGdx.jpg",
"rating": 7.8,
"summary": "The science of Christopher Nolan's sci-fi, Interstellar.",
"title": "The Science of Interstellar",
"type": "movie",
"vote_count": 6,
"year": 2014
}
],
"total_pages": 1
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,9 @@
const Cache = require('src/tmdb/cache');
const SqliteDatabase = require('src/database/sqliteDatabase');
function createCacheEntry(key, value) {
const cache = new Cache();
return cache.set(key, value);
}
module.exports = createCacheEntry;

View File

@@ -0,0 +1,10 @@
const User = require('src/user/user');
const Token = require('src/user/token');
function createToken(username, secret) {
const user = new User(username);
const token = new Token(user);
return token.toString(secret);
}
module.exports = createToken;

View File

@@ -0,0 +1,11 @@
const User = require('src/user/user');
const UserSecurity = require('src/user/userSecurity');
function createUser(username, password) {
const userSecurity = new UserSecurity();
const user = new User(username)
return Promise.resolve(userSecurity.createNewUser(user, password))
}
module.exports = createUser;

View File

@@ -0,0 +1,9 @@
const establishedDatabase = require('src/database/database');
function resetDatabase() {
return Promise.resolve()
.then(() => establishedDatabase.tearDown())
.then(() => establishedDatabase.setUp())
}
module.exports = resetDatabase;

View File

@@ -0,0 +1,16 @@
const tmdbMock = () => ({
error: null,
response: null,
searchMovie(query, callback) {
callback(this.error, this.response);
},
movieInfo(query, callback) {
callback(this.error, this.response);
},
miscPopularMovies(callback) {
console.log('miscPopMovies callback', callback)
callback(this.error, this.response);
},
});
module.exports = tmdbMock;

View File

@@ -0,0 +1,16 @@
const assert = require('assert');
const request = require('supertest-as-promised');
const app = require('src/webserver/app');
const resetDatabase = require('test/helpers/resetDatabase');
describe('As a user I want to register', () => {
before(() => resetDatabase());
it('should return 200 and a message indicating success', () =>
request(app)
.post('/api/v1/user')
.send({ username: 'test', email: 'test@gmail.com', password: 'password' })
.expect(200)
.then(response => assert.equal(response.body.message, 'Welcome to Seasoned!'))
);
});

View File

@@ -0,0 +1,14 @@
/* eslint-disable no-return-assign */
const net = require('net');
xdescribe('As a developer I want the server to start', () => {
beforeEach(() =>
this.server = require('src/webserver/server'));
it('should listen on port 31400', (done) => {
net.createConnection(31400, done);
});
afterEach(() =>
this.server.close());
});

View File

@@ -0,0 +1,27 @@
const assert = require('assert');
const request = require('supertest-as-promised');
const app = require('src/webserver/app');
const createUser = require('test/helpers/createUser');
const resetDatabase = require('test/helpers/resetDatabase');
describe('As a user I want to log in', () => {
before(() => {
return resetDatabase()
.then(() => createUser('test_user', 'password'))
})
it('should return 200 with a token if correct credentials are given', () =>
request(app)
.post('/api/v1/user/login')
.send({ username: 'test_user', password: 'password' })
.expect(200)
.then(response => assert.equal(typeof response.body.token, 'string'))
);
it('should return 401 if incorrect credentials are given', () =>
request(app)
.post('/api/v1/user/login')
.send({ username: 'test_user', password: 'anti-password' })
.expect(401)
);
});

View File

@@ -0,0 +1,16 @@
const assert = require('assert');
const resetDatabase = require('test/helpers/resetDatabase');
const app = require('src/webserver/app');
const request = require('supertest-as-promised');
describe('As a user I want a forbidden error if the token is malformed', () => {
before(() => resetDatabase());
it('should return 401', () =>
request(app)
.get('/api/v1/pirate/search?query=test')
.set('Authorization', 'maLfOrMed TOKEN')
.expect(401)
.then(response => assert.equal(response.body.error, 'You must be logged in.'))
);
});

View File

@@ -0,0 +1,20 @@
const assert = require('assert');
const request = require('supertest-as-promised');
const app = require('src/webserver/app');
const createUser = require('test/helpers/createUser');
const resetDatabase = require('test/helpers/resetDatabase');
describe('As a user I want error when registering existing username', () => {
before(() => {
return resetDatabase()
.then(() => createUser('test_user', 'password'))
})
it('should return 401 with error message when same username is given', () =>
request(app)
.post('/api/v1/user')
.send({ username: 'test_user', password: 'password' })
.expect(401)
.then(response => assert.equal(response.text, '{"success":false,"message":"That username is already registered"}'))
);
});

View File

@@ -0,0 +1,18 @@
const assert = require('assert');
const createCacheEntry = require('test/helpers/createCacheEntry');
const resetDatabase = require('test/helpers/resetDatabase');
const request = require('supertest-as-promised');
const app = require('src/webserver/app');
const popularMoviesSuccess = require('test/fixtures/popular-movies-success-response.json');
describe('As a user I want to get popular movies', () => {
before(() => resetDatabase());
before(() => createCacheEntry('pm:1', popularMoviesSuccess));
it('should return 200 with the information', () =>
request(app)
.get('/api/v2/movie/popular')
.expect(200)
.then(response => assert.equal(response.body.results.length, 20))
);
});

View File

@@ -0,0 +1,18 @@
const assert = require('assert');
const createCacheEntry = require('test/helpers/createCacheEntry');
const resetDatabase = require('test/helpers/resetDatabase');
const request = require('supertest-as-promised');
const app = require('src/webserver/app');
const popularShowsSuccess = require('test/fixtures/popular-show-success-response.json');
describe('As a user I want to get popular shows', () => {
before(() => resetDatabase());
before(() => createCacheEntry('pt:1', popularShowsSuccess));
it('should return 200 with the information', () =>
request(app)
.get('/api/v2/show/popular')
.expect(200)
.then(response => assert.equal(response.body.results.length, 20))
);
});

View File

@@ -0,0 +1,23 @@
const resetDatabase = require('test/helpers/resetDatabase');
const createCacheEntry = require('test/helpers/createCacheEntry');
const app = require('src/webserver/app');
const request = require('supertest-as-promised');
const createUser = require('test/helpers/createUser');
const createToken = require('test/helpers/createToken');
const infoMovieSuccess = require('test/fixtures/blade_runner_2049-info-success-response.json');
describe('As a user I want to request a movie', () => {
before(async () => {
await resetDatabase()
await createUser('test_user', 'test@gmail.com', 'password')
})
before(() => createCacheEntry('mi:335984:false', infoMovieSuccess));
it('should return 200 when item is requested', () =>
request(app)
.post('/api/v2/request')
.set('authorization', createToken('test_user', 'secret'))
.send({ id: 335984, type: 'movie' })
.expect(200)
);
});

View File

@@ -0,0 +1,16 @@
const createCacheEntry = require('test/helpers/createCacheEntry');
const resetDatabase = require('test/helpers/resetDatabase');
const request = require('supertest-as-promised');
const app = require('src/webserver/app');
const interstellarQuerySuccess = require('test/fixtures/interstellar-query-movie-success-response.json');
describe('As an anonymous user I want to search for a movie', () => {
before(() => resetDatabase());
before(() => createCacheEntry('mos:1:interstellar', interstellarQuerySuccess));
it('should return 200 with the search results even if user is not logged in', () =>
request(app)
.get('/api/v2/search/movie?query=interstellar&page=1')
.expect(200)
);
});

View File

@@ -0,0 +1,63 @@
const assert = require('assert');
const Config = require('src/config/configuration.js');
describe('Config', () => {
before(() => {
this.backedUpEnvironmentVariables = Object.assign({}, process.env);
this.backedUpConfigFields = Object.assign({}, Config.getInstance().fields);
});
after(() => {
process.env = this.backedUpEnvironmentVariables;
Config.getInstance().fields = this.backedUpConfigFields;
});
it('should retrieve section and option from config file', () => {
Config.getInstance().fields = { 'webserver': { 'port': 1337 } };
assert.equal(Config.getInstance().get('webserver', 'port'), 1337);
});
it('should resolve to environment variables if option is filtered with env', () => {
Config.getInstance().fields = { 'webserver': { 'port': 'env|SEASONED_WEBSERVER_PORT' } };
process.env.SEASONED_WEBSERVER_PORT = '1338';
assert.equal(Config.getInstance().get('webserver', 'port'), 1338);
});
it('raises an exception if the environment variable does not exist', () => {
Config.getInstance().fields = { 'webserver': { 'port': 'env|DOES_NOT_EXIST' } };
process.env.SEASONED_WEBSERVER_PORT = '1338';
assert.throws(() => Config.getInstance().get('webserver', 'port'), /empty/);
});
it('raises an exception if the environment variable is empty', () => {
Config.getInstance().fields = { 'webserver': { 'port': 'env|SEASONED_WEBSERVER_PORT' } };
process.env.SEASONED_WEBSERVER_PORT = '';
assert.throws(() => Config.getInstance().get('webserver', 'port'), /empty/);
});
it('raises an exception if the section does not exist in the file', () => {
Config.getInstance().fields = { 'webserver': { 'port': '1338' } };
assert.throws(() => Config.getInstance().get('woops', 'port'), /does not exist/);
});
it('raises an exception if the option does not exist in the file', () => {
Config.getInstance().fields = { 'webserver': { 'port': '1338' } };
assert.throws(() => Config.getInstance().get('webserver', 'woops'), /does not exist/);
});
it('returns an array if field is an array', () => {
Config.getInstance().fields = { 'bouncer': { 'whitelist': [1, 2, 3] } };
assert.deepEqual(Config.getInstance().get('bouncer', 'whitelist'), [1, 2, 3]);
});
it('decodes field as base64 if base64| is before the variable', () => {
Config.getInstance().fields = { 'webserver': { 'port': 'base64|MTMzOA==' } };
assert.equal(Config.getInstance().get('webserver', 'port'), 1338);
});
it('decodes environment variable as base64 if BASE64= is before the variable', () => {
Config.getInstance().fields = { 'webserver': { 'port': 'env|base64|SEASONED_WEBSERVER_PORT' } };
process.env.SEASONED_WEBSERVER_PORT = 'MTMzOA==';
assert.equal(Config.getInstance().get('webserver', 'port'), 1338);
});
});

View File

@@ -0,0 +1,72 @@
const assert = require('assert');
const Field = require('src/config/field.js');
describe('Field', () => {
it('should return an array if it is an array', () => {
const field = new Field([1, 2, 3]);
assert.deepEqual(field.value, [1, 2, 3]);
});
it('should return the plain value if it is an ordinary field', () => {
const field = new Field('plain value');
assert.equal(field.value, 'plain value');
});
it('should return false if boolean false is field', () => {
const field = new Field(false);
assert.equal(field.value, false);
});
it('should not include any invalid filters', () => {
const field = new Field('invalid-filter|plain value');
assert.equal(field.value, 'plain value');
});
it('should return the decoded value if it is filtered through base64', () => {
const field = new Field('base64|ZW5jb2RlZCB2YWx1ZQ==');
assert.equal(field.value, 'encoded value');
});
it('should not decode the value if it missing the filter', () => {
const field = new Field('ZW5jb2RlZCB2YWx1ZQ==');
assert.equal(field.value, 'ZW5jb2RlZCB2YWx1ZQ==');
});
it('should retrieve the environment variable if env filter is used', () => {
const environmentVariables = { REDIS_URL: 'redis://127.0.0.1:1234' };
const field = new Field('env|REDIS_URL', environmentVariables);
assert.equal(field.value, 'redis://127.0.0.1:1234');
});
it('should return undefined if the environment variable does not exist', () => {
const environmentVariables = { HTTP_PORT: 8080 };
const field = new Field('env|REDIS_URL', environmentVariables);
assert.equal(field.value, undefined);
});
it('should return undefined if the environment variable is an empty string', () => {
const environmentVariables = { REDIS_URL: '' };
const field = new Field('env|REDIS_URL', environmentVariables);
assert.deepEqual(field.value, undefined);
});
describe('Multiple filters', () => {
it('should decode the environment variable if base64 and env filter are used', () => {
const environmentVariables = { REDIS_URL: 'cmVkaXM6Ly9kYWdibGFkZXQubm8vMTIzNA==' };
const field = new Field('env|base64|REDIS_URL', environmentVariables);
assert.equal(field.value, 'redis://dagbladet.no/1234');
});
it('should disregard the order of filters when env and base64 are used', () => {
const environmentVariables = { REDIS_URL: 'cmVkaXM6Ly9kYWdibGFkZXQubm8vMTIzNA==' };
const field = new Field('base64|env|REDIS_URL', environmentVariables);
assert.equal(field.value, 'redis://dagbladet.no/1234');
});
it('should return undefined if both filters are used and env var does not exist', () => {
const environmentVariables = { REDIS_URL: 'cmVkaXM6Ly9kYWdibGFkZXQubm8vMTIzNA==' };
const field = new Field('base64|env|REDIS_LOL', environmentVariables);
assert.equal(field.value, undefined);
});
});
});

View File

@@ -0,0 +1,34 @@
const assert = require('assert');
const Filters = require('src/config/filters.js');
describe('Filters', () => {
it('should extract base64 as filter if it is at start of string followed by pipe', () => {
const filters = new Filters('base64|');
assert.deepEqual(filters.filters, ['base64']);
});
it('should extract base64 and env as filters if both are separated by pipe', () => {
const filters = new Filters('base64|env|');
assert.deepEqual(filters.filters, ['base64', 'env']);
});
it('should not extract any filters if none are present', () => {
const filters = new Filters('base64');
assert.deepEqual(filters.filters, []);
});
it('should strip env filter from the value', () => {
const filters = new Filters('env|HELLO');
assert.deepEqual(filters.removeFiltersFromValue(), 'HELLO');
});
it('should strip env and base64 filter from the value', () => {
const filters = new Filters('env|base64|HELLO');
assert.deepEqual(filters.removeFiltersFromValue(), 'HELLO');
});
it('should strip no filters from the value if there are no filters', () => {
const filters = new Filters('HELLO');
assert.deepEqual(filters.removeFiltersFromValue(), 'HELLO');
});
});

View File

@@ -0,0 +1,31 @@
const assert = require('assert');
// const convertTmdbToMovie = require('src/tmdb/convertTmdbToMovie');
const { Movie } = require('src/tmdb/types');
const bladeRunnerQuerySuccess = require('test/fixtures/blade_runner_2049-info-success-response.json')
describe('Convert tmdb movieInfo to movie', () => {
beforeEach(() => [this.bladeRunnerTmdbMovie] = bladeRunnerQuerySuccess);
it('should translate the tmdb release date to movie year', () => {
const bladeRunner = Movie.convertFromTmdbResponse(this.bladeRunnerTmdbMovie);
assert.strictEqual(bladeRunner.year, 2017);
});
it('should translate the tmdb release date to instance of Date', () => {
const bladeRunner = Movie.convertFromTmdbResponse(this.bladeRunnerTmdbMovie);
assert(bladeRunner.releaseDate instanceof Date);
});
it('should translate the tmdb title to title', () => {
const bladeRunner = Movie.convertFromTmdbResponse(this.bladeRunnerTmdbMovie);
assert.equal(bladeRunner.title, 'Blade Runner 2049');
});
it('should translate the tmdb vote_average to rating', () => {
const bladeRunner = Movie.convertFromTmdbResponse(this.bladeRunnerTmdbMovie);
assert.equal(bladeRunner.rating, 7.3);
});
});

View File

@@ -0,0 +1,31 @@
const assert = require('assert');
// const Movie = require('src/movie/movie');
const TMDB = require('src/tmdb/tmdb');
const Cache = require('src/tmdb/cache');
const SqliteDatabase = require('src/database/sqliteDatabase');
const tmdbMock = require('test/helpers/tmdbMock');
const emptyQuerySuccess = require('test/fixtures/empty-query-success-response.json');
const interstellarQuerySuccess = require('test/fixtures/arrival-info-success-response.json');
const popularMovieSuccessResponse = require('test/fixtures/popular-movies-success-response.json');
describe('TMDB', function test() {
beforeEach(() => {
this.mockMoviedb = tmdbMock();
this.database = new SqliteDatabase(':memory:');
return Promise.resolve()
.then(() => this.database.setUp());
});
describe('popular', () => {
it('should return the "Blade Runner 2049" year in the collection of popular movies', () => {
this.mockMoviedb.response = popularMovieSuccessResponse;
const cache = new Cache(this.database);
const tmdb = new TMDB(cache, 'bogus-pi-key', this.mockMoviedb);
return tmdb.popular()
.then(movies =>
assert.equal(movies[0].title, "Blade Runner 2049")
);
});
})
});