From 32a6cfbad4efe1dac967ffcb068db6f862f93e4c Mon Sep 17 00:00:00 2001 From: "mike.rode" Date: Sat, 23 Feb 2019 18:29:18 -0600 Subject: [PATCH] Add test for authentication endpoint. --- package-lock.json | 98 ++++++++++++++++++- package.json | 3 +- server/index.js | 11 ++- server/models/index.js | 2 +- server/routes/index.js | 6 ++ server/services/plexApi/auth.js | 7 +- server/services/plexApi/importData.js | 2 - server/services/plexApi/index.js | 1 - server/services/plexApi/plexApi.js | 2 - test/server/services/plexApi/auth.test.js | 32 ++++++ test/server/services/plexApi/index.test.js | 2 - .../services/plexApi/mocks/authResponse.xml | 85 ++++++++++++++++ .../services/plexApi/mocks/plexResponses.js | 85 ---------------- 13 files changed, 230 insertions(+), 106 deletions(-) create mode 100644 test/server/services/plexApi/auth.test.js create mode 100644 test/server/services/plexApi/mocks/authResponse.xml diff --git a/package-lock.json b/package-lock.json index 0c26bec..059e85c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -266,6 +266,37 @@ "any-observable": "^0.3.0" } }, + "@sinonjs/commons": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-1.3.0.tgz", + "integrity": "sha512-j4ZwhaHmwsCb4DlDOIWnI5YyKDNMoNThsmwEpfHx6a1EpsGZ9qYLxP++LMlmBRjtGptGHFsGItJ768snllFWpA==", + "requires": { + "type-detect": "4.0.8" + } + }, + "@sinonjs/formatio": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@sinonjs/formatio/-/formatio-3.1.0.tgz", + "integrity": "sha512-ZAR2bPHOl4Xg6eklUGpsdiIJ4+J1SNag1DHHrG/73Uz/nVwXqjgUtRPLoS+aVyieN9cSbc0E4LsU984tWcDyNg==", + "requires": { + "@sinonjs/samsam": "^2 || ^3" + } + }, + "@sinonjs/samsam": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-3.1.1.tgz", + "integrity": "sha512-ILlwvQUwAiaVBzr3qz8oT1moM7AIUHqUc2UmEjQcH9lLe+E+BZPwUMuc9FFojMswRK4r96x5zDTTrowMLw/vuA==", + "requires": { + "@sinonjs/commons": "^1.0.2", + "array-from": "^2.1.1", + "lodash": "^4.17.11" + } + }, + "@sinonjs/text-encoding": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/@sinonjs/text-encoding/-/text-encoding-0.7.1.tgz", + "integrity": "sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ==" + }, "@types/chai": { "version": "4.1.7", "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.1.7.tgz", @@ -420,6 +451,11 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "array-from": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/array-from/-/array-from-2.1.1.tgz", + "integrity": "sha1-z+nYwmYoudxa7MYqn12PHzUsEZU=" + }, "array-union": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/array-union/-/array-union-1.0.2.tgz", @@ -1924,8 +1960,7 @@ "diff": { "version": "3.5.0", "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", - "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", - "dev": true + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==" }, "doctrine": { "version": "2.1.0", @@ -4289,6 +4324,11 @@ "verror": "1.10.0" } }, + "just-extend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/just-extend/-/just-extend-4.0.2.tgz", + "integrity": "sha512-FrLwOgm+iXrPV+5zDU6Jqu4gCRXbWEQg2O3SKONsWE4w7AXFRkryS53bpWdaL9cNol+AmR3AEYz6kn+o0fCPnw==" + }, "kind-of": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", @@ -4581,6 +4621,11 @@ "wrap-ansi": "^3.0.1" } }, + "lolex": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/lolex/-/lolex-3.1.0.tgz", + "integrity": "sha512-zFo5MgCJ0rZ7gQg69S4pqBsLURbFw11X68C18OcJjJQbqaXm2NoTrGl1IMM3TIz0/BnN1tIs2tzmmqvCsOMMjw==" + }, "loose-envify": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", @@ -4887,6 +4932,38 @@ "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==" }, + "nise": { + "version": "1.4.10", + "resolved": "https://registry.npmjs.org/nise/-/nise-1.4.10.tgz", + "integrity": "sha512-sa0RRbj53dovjc7wombHmVli9ZihXbXCQ2uH3TNm03DyvOSIQbxg+pbqDKrk2oxMK1rtLGVlKxcB9rrc6X5YjA==", + "requires": { + "@sinonjs/formatio": "^3.1.0", + "@sinonjs/text-encoding": "^0.7.1", + "just-extend": "^4.0.2", + "lolex": "^2.3.2", + "path-to-regexp": "^1.7.0" + }, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "lolex": { + "version": "2.7.5", + "resolved": "https://registry.npmjs.org/lolex/-/lolex-2.7.5.tgz", + "integrity": "sha512-l9x0+1offnKKIzYVjyXU2SiwhXDLekRzKyhnbyldPHvC7BvLPVpdNUNR2KeMAiCN2D/kLNttZgQD5WjSxuBx3Q==" + }, + "path-to-regexp": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.7.0.tgz", + "integrity": "sha1-Wf3g9DW62suhA6hOnTvGTpa5k30=", + "requires": { + "isarray": "0.0.1" + } + } + } + }, "nock": { "version": "10.0.6", "resolved": "https://registry.npmjs.org/nock/-/nock-10.0.6.tgz", @@ -10678,6 +10755,20 @@ } } }, + "sinon": { + "version": "7.2.4", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-7.2.4.tgz", + "integrity": "sha512-FGlcfrkiBRfaEIKRw8s/9mk4nP4AMGswvKFixLo+AzsOhskjaBCHAHGLMd8pCJpQGS+9ZI71px6qoQUyvADeyA==", + "requires": { + "@sinonjs/commons": "^1.3.0", + "@sinonjs/formatio": "^3.1.0", + "@sinonjs/samsam": "^3.1.1", + "diff": "^3.5.0", + "lolex": "^3.1.0", + "nise": "^1.4.10", + "supports-color": "^5.5.0" + } + }, "slash": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/slash/-/slash-1.0.0.tgz", @@ -11479,8 +11570,7 @@ "type-detect": { "version": "4.0.8", "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", - "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", - "dev": true + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==" }, "type-is": { "version": "1.6.16", diff --git a/package.json b/package.json index 7830ef9..a9f6852 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "test": "PLEX_API_TOKEN=testPlexApiToken mocha --compilers js:babel-core/register ./test/**/**/**/*.test.js --exit", + "test": "PLEX_API_TOKEN=testPlexApiToken mocha --require babel-core/register ./test/**/**/**/*.test.js --exit", "dev": "nodemon --exec babel-node --inspect index.js", "debug": "nodemon --exec babel-node --inspect index.js", "start": "nodemon --exec babel-node index.js" @@ -27,6 +27,7 @@ "plex-api": "^5.2.1", "request-promise": "^4.2.4", "sequelize": "^4.42.0", + "sinon": "^7.2.4", "tdaw": "^1.3.0", "uuid": "^3.3.2", "xml2json": "^0.11.2" diff --git a/server/index.js b/server/index.js index 4e01b50..eb4bb19 100644 --- a/server/index.js +++ b/server/index.js @@ -28,15 +28,18 @@ export default () => { const port = server.get('port'); sequelize.sync().then(() => { - server.listen(port, () => { - console.log(`Express server listening on - http://${hostname}:${port}`); - }); + if (!module.parent) { + server.listen(port, () => { + console.log( + `Express server listening on - http://${hostname}:${port}`, + ); + }); + } }); }; process.on('unhandledRejection', (reason, p) => { console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); - // application specific logging, throwing an error, or other logic here }); return {create, start}; diff --git a/server/models/index.js b/server/models/index.js index 33e34d2..1d0e2e7 100644 --- a/server/models/index.js +++ b/server/models/index.js @@ -7,7 +7,7 @@ const sequelize = new Sequelize( { dialect: 'postgres', host: 'localhost', - // logging: false, + logging: false, }, ); diff --git a/server/routes/index.js b/server/routes/index.js index 17e1f32..1ae36df 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -7,6 +7,12 @@ const init = server => { }); server.use('/api', apiRoute); + + server.use((err, req, res, next) => { + if (err) { + console.log('Error in routes/index', err); + } + }); }; export default { diff --git a/server/services/plexApi/auth.js b/server/services/plexApi/auth.js index 2bac11b..a5a1cff 100644 --- a/server/services/plexApi/auth.js +++ b/server/services/plexApi/auth.js @@ -1,10 +1,10 @@ -import uuid from 'uuid/v1'; +import uuid from 'uuid'; import btoa from 'btoa'; import request from 'request-promise'; const rxAuthToken = /authenticationToken="([^"]+)"/; -const options = (username, password) => ({ +const urlParams = (username, password) => ({ url: 'https://plex.tv/users/sign_in.xml', headers: { 'X-Plex-Client-Identifier': uuid(), @@ -18,8 +18,7 @@ const encryptUserCreds = (username, password) => { }; const fetchToken = async (username, password) => { - const res = await request.post(options(username, password)); - console.log(res); + const res = await request.post(urlParams(username, password)); const token = res.match(rxAuthToken)[1]; return token; }; diff --git a/server/services/plexApi/importData.js b/server/services/plexApi/importData.js index fd33f67..da760a1 100644 --- a/server/services/plexApi/importData.js +++ b/server/services/plexApi/importData.js @@ -30,7 +30,6 @@ const importLibraries = async () => { const importMostWatched = async req => { const plexApi = plexApiClient(); const mostWatched = await plexApi.getMostWatched(req); - console.log('6========', mostWatched); mostWatched.forEach(async libraryData => { await updateLibrary([libraryData]); }); @@ -70,7 +69,6 @@ const updateLibrary = libraryData => { }; const createLibrary = libraryData => { - console.log(libraryData[1]); libraryData.forEach(async data => { await models.PlexLibrary.upsert( { diff --git a/server/services/plexApi/index.js b/server/services/plexApi/index.js index d307b0e..bcc52e3 100644 --- a/server/services/plexApi/index.js +++ b/server/services/plexApi/index.js @@ -8,7 +8,6 @@ const getAuthToken = async (req, res) => { const {password} = req.query; auth(username, password).then(data => { - console.log(data); return res.json(data); }); }; diff --git a/server/services/plexApi/plexApi.js b/server/services/plexApi/plexApi.js index b7cbdf6..b88257e 100644 --- a/server/services/plexApi/plexApi.js +++ b/server/services/plexApi/plexApi.js @@ -74,7 +74,6 @@ const request = async function(url) { }) .catch(error => { if (error.response) { - // console.log('data', error.response.data); console.log('status', error.response.status); console.log('headers', error.response.headers); return reject(error.response); @@ -100,7 +99,6 @@ const getMostWatched = async function(req) { const urlParams = mostWatchedUrlParams(req); const mostWatchedUrl = buildUrl(urlParams); const response = await request(mostWatchedUrl); - console.log(response.MediaContainer.Metadata); return response.MediaContainer.Metadata; }; diff --git a/test/server/services/plexApi/auth.test.js b/test/server/services/plexApi/auth.test.js new file mode 100644 index 0000000..a8adfd8 --- /dev/null +++ b/test/server/services/plexApi/auth.test.js @@ -0,0 +1,32 @@ +import chai from 'chai'; +import chaiHttp from 'chai-http'; +import nock from 'nock'; +import sinon from 'sinon'; +import responses from './mocks/plexResponses'; +import app from '../../../../index'; + +chai.use(chaiHttp); +const should = chai.should(); +describe('Users', () => { + describe('GET /api/v1/plex/auth', async () => { + it('should get plex auth token', (done) => { + const response = `${__dirname}/mocks/authResponse.xml`; + nock('https://plex.tv') + .post(uri => uri.includes('/users/sign_in.xml')) + .replyWithFile(200, response, { + 'Content-Type': 'text/xml', + }); + + chai + .request(app) + .get('/api/v1/plex/auth') + .query({ username: 'username', password: 'password' }) + .end((err, res) => { + console.log('mike', res.body); + res.should.have.status(200); + res.body.should.equal('testPlexApiToken'); + done(); + }); + }); + }); +}); diff --git a/test/server/services/plexApi/index.test.js b/test/server/services/plexApi/index.test.js index cc74564..0b047e3 100644 --- a/test/server/services/plexApi/index.test.js +++ b/test/server/services/plexApi/index.test.js @@ -4,8 +4,6 @@ import nock from 'nock'; import responses from './mocks/plexResponses'; import app from '../../../../index'; -nock.enableNetConnect; - chai.use(chaiHttp); const should = chai.should(); diff --git a/test/server/services/plexApi/mocks/authResponse.xml b/test/server/services/plexApi/mocks/authResponse.xml new file mode 100644 index 0000000..fcf0983 --- /dev/null +++ b/test/server/services/plexApi/mocks/authResponse.xml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + mjrode + michaelrode44@gmail.com + 2014-01-03 03:05:51 UTC + testPlexApiToken + \ No newline at end of file diff --git a/test/server/services/plexApi/mocks/plexResponses.js b/test/server/services/plexApi/mocks/plexResponses.js index a0605f7..e881ae6 100644 --- a/test/server/services/plexApi/mocks/plexResponses.js +++ b/test/server/services/plexApi/mocks/plexResponses.js @@ -3794,89 +3794,4 @@ export default { ], }, }, - authResponse: ` - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - mjrode - michaelrode44@gmail.com - 2014-01-03 03:05:51 UTC - testPlexApiToken - `, };