Fix: Tests lint and src folder (#138)

* Automaticly fixable eslint issues, mostly 3 -> 2 space indentation

* fix: updated plex_userid to camelcase

* Linted and some consistency refactor on middleware

* eslint uses ecmaversion 2020 & allow empty catch rule

* Started linting source files

* Fixed eslint errors & improved a lot of error handling

* Set 2 eslint rules as warning temporarly

* Updated all import statements to be relative

* Updated mocha & nyc, resolved all lint issues in tests/

* Updated mocha & nyc. Removed production config. Updated gitignore

* Updated test commands to omit system tests, no exit code

* Updated test configuration w/ missing keys

* Chai modules defined in package.json & resolved linting errors

* Dockerfile copies development.example -> production.json. Simplified commands

* All api calls from tests use same chaiHttp implementation

Removes a list of fetch alternatives after being replaced by chaiHttp:
 - request
 - request-promise
 - supertest
 - supertest-as-promised

* Tests should use redis (mock) cache, not tmdb sqlite cache

* Disabled test asADeveloperIWantTheServerToStart

* Re-enable tests/system

* Use chaiHttp in asAUserIWantToRequestAMovie.

* Fixed redis expire & mock implmentation

* Replaced all fetch alternatives from source code and package.json

* Pass error from tmdb api back to client as errorMessage

* Updated authentication middleware to handle checks consitenctly

* Prevent assert error when checking request status, returns success 200

* Resolved merge conflicts

* Only build and publish docker container when branch master
This commit is contained in:
2022-08-20 17:41:46 +02:00
committed by GitHub
parent 1815a429b0
commit 628ed52012
44 changed files with 1248 additions and 1592 deletions

View File

@@ -1,63 +1,78 @@
const assert = require('assert');
const Config = require('src/config/configuration.js');
const assert = require("assert");
const Config = require("../../../src/config/configuration");
describe('Config', () => {
before(() => {
this.backedUpEnvironmentVariables = Object.assign({}, process.env);
this.backedUpConfigFields = Object.assign({}, Config.getInstance().fields);
describe("Config", () => {
beforeEach(() => {
this.backedUpEnvironmentVariables = { ...process.env };
this.backedUpConfigFields = { ...Config.getInstance().fields };
});
after(() => {
afterEach(() => {
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 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("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 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 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 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("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("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 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);
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

@@ -1,71 +1,77 @@
const assert = require('assert');
const Field = require('src/config/field.js');
const assert = require("assert");
const Field = require("../../../src/config/field");
describe('Field', () => {
it('should return an array if it is an array', () => {
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 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', () => {
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 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 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 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 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', () => {
it("should return undefined if the environment variable does not exist", () => {
const environmentVariables = { HTTP_PORT: 8080 };
const field = new Field('env|REDIS_URL', environmentVariables);
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);
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');
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 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);
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

@@ -1,34 +1,34 @@
const assert = require('assert');
const Filters = require('src/config/filters.js');
const assert = require("assert");
const Filters = require("../../../src/config/filters");
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']);
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 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');
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 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 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');
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

@@ -1,31 +1,38 @@
const assert = require('assert');
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')
const { Movie } = require("../../../src/tmdb/types");
const bladeRunnerQuerySuccess = require("../../fixtures/blade_runner_2049-info-success-response.json");
describe('Convert tmdb movieInfo to movie', () => {
beforeEach(() => [this.bladeRunnerTmdbMovie] = bladeRunnerQuerySuccess);
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);
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);
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 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);
it("should translate the tmdb vote_average to rating", () => {
const bladeRunner = Movie.convertFromTmdbResponse(
this.bladeRunnerTmdbMovie
);
assert.equal(bladeRunner.rating, 7.3);
});
});

View File

@@ -5,9 +5,9 @@ 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');
const emptyQuerySuccess = require('tests/fixtures/empty-query-success-response.json');
const interstellarQuerySuccess = require('tests/fixtures/arrival-info-success-response.json');
const popularMovieSuccessResponse = require('tests/fixtures/popular-movies-success-response.json');
describe('TMDB', function test() {
beforeEach(() => {