From 40510a407d5553a56b61013dd92de4f3c79eff75 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 13 Jan 2021 11:14:24 +0100 Subject: [PATCH] merge api.spec.js and parse.spec.js and lint code --- .eslintignore | 1 + .gitignore | 1 + .prettierignore | 1 + spec/api.spec.js | 164 +++++++++++++++++++++++++++++++++++++++------ spec/helper.js | 138 +++++++++++--------------------------- spec/parse.spec.js | 135 ------------------------------------- 6 files changed, 185 insertions(+), 255 deletions(-) delete mode 100644 spec/parse.spec.js diff --git a/.eslintignore b/.eslintignore index d27bab8..ffdec1c 120000 --- a/.eslintignore +++ b/.eslintignore @@ -10,3 +10,4 @@ build perf.csv .vscode .eslintcache +globalConfig.json diff --git a/.gitignore b/.gitignore index 46976d3..9e790f0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ build perf.csv .vscode .eslintcache +globalConfig.json diff --git a/.prettierignore b/.prettierignore index d27bab8..ffdec1c 120000 --- a/.prettierignore +++ b/.prettierignore @@ -10,3 +10,4 @@ build perf.csv .vscode .eslintcache +globalConfig.json diff --git a/spec/api.spec.js b/spec/api.spec.js index 725154e..15efc98 100644 --- a/spec/api.spec.js +++ b/spec/api.spec.js @@ -1,20 +1,24 @@ -/* eslint-disable */ const axios = require('axios'); -const { bindAll, getJwtToken } = require(`${SPEC_PATH}/helper`); +const { bindAll, getJwtToken } = require('./helper'); +const { API_URL } = require('./__mock__/config'); -process.env.TEST_SUITE = 'api-server'; +bindAll(); describe('api server', () => { - bindAll(); - + let jwtToken; let applicationId; - it('Create Application', async () => { - const jwtToken = await getJwtToken(); + it('Get JSON web token', async () => { + jwtToken = await getJwtToken(); + + expect(jwtToken).toBeDefined(); + expect.assertions(1); + }); + it('Create Application', async () => { const response = await axios({ method: 'post', - url: 'http://localhost:3000/api/application', + url: `${API_URL}/api/application`, headers: { Authorization: `Bearer ${jwtToken}`, 'Content-Type': 'application/json', @@ -39,11 +43,9 @@ describe('api server', () => { }); it('List Application', async () => { - const jwtToken = await getJwtToken(); - const response = await axios({ method: 'get', - url: 'http://localhost:3000/api/application', + url: `${API_URL}/api/application`, headers: { Authorization: `Bearer ${jwtToken}`, 'Content-Type': 'application/json', @@ -55,11 +57,9 @@ describe('api server', () => { }); it('Get Application', async () => { - const jwtToken = await getJwtToken(); - const getResponse = await axios({ method: 'get', - url: `http://localhost:3000/api/application/${applicationId}`, + url: `${API_URL}/api/application/${applicationId}`, headers: { Authorization: `Bearer ${jwtToken}`, 'Content-Type': 'application/json', @@ -71,11 +71,9 @@ describe('api server', () => { }); it('Update Application', async () => { - const jwtToken = await getJwtToken(); - const response = await axios({ method: 'put', - url: `http://localhost:3000/api/application/${applicationId}`, + url: `${API_URL}/api/application/${applicationId}`, headers: { Authorization: `Bearer ${jwtToken}`, 'Content-Type': 'application/json', @@ -96,11 +94,9 @@ describe('api server', () => { }); it('Delete Developer', async () => { - const jwtToken = await getJwtToken(); - const response = await axios({ method: 'delete', - url: 'http://localhost:3000/api/developer', + url: `${API_URL}/api/developer`, headers: { Authorization: `Bearer ${jwtToken}`, 'Content-Type': 'application/json', @@ -114,3 +110,131 @@ describe('api server', () => { expect.assertions(4); }); }); + +describe('parse custom server', () => { + let sessionToken; + let objectId; + + it('Get session token', async () => { + const jwtToken = await getJwtToken(); + + const response = await axios({ + method: 'post', + url: `${API_URL}/api/application`, + headers: { + Authorization: `Bearer ${jwtToken}`, + 'Content-Type': 'application/json', + }, + data: { + name: 'toto', + description: 'truc', + apple_store_link: 'https://apple.fr', + google_market_link: 'https://google.fr', + }, + }); + + const parseResponse = await axios({ + method: 'get', + url: `${API_URL}/parse/login`, + headers: { + 'x-parse-application-id': `test`, + 'x-parse-revocable-session': '1', + }, + data: { + username: response.data.parse_name, + password: response.data.token, + }, + }); + + expect(parseResponse.data.sessionToken).toBeDefined(); + expect.assertions(1); + + sessionToken = parseResponse.data.sessionToken; + }); + + it('Create Item', async () => { + const parseResponse = await axios({ + method: 'post', + url: `${API_URL}/parse/classes/GameScore`, + headers: { + 'Content-Type': 'application/json', + 'x-parse-application-id': `test`, + 'x-parse-session-token': sessionToken, + }, + data: { + score: 1337, + playerName: 'test9', + cheatMode: false, + }, + }); + + expect(parseResponse.data.score).toBe(1337); + expect(parseResponse.data.playerName).toBe('test9'); + expect(parseResponse.data.cheatMode).toBe(false); + expect(parseResponse.data.createdAt).toBeDefined(); + expect(parseResponse.data.updatedAt).toBeDefined(); + expect(parseResponse.data.objectId).toBeDefined(); + expect(parseResponse.data.ACL).toBeUndefined(); + expect(parseResponse.data.owner).toBeUndefined(); + expect.assertions(8); + + objectId = parseResponse.data.objectId; + }); + + it('Get Items', async () => { + const parseResponse = await axios({ + method: 'get', + url: `${API_URL}/parse/classes/GameScore`, + headers: { + 'Content-Type': 'application/json', + 'x-parse-application-id': `test`, + 'x-parse-session-token': sessionToken, + }, + }); + + expect(parseResponse.data.results.length).toBe(1); + expect(parseResponse.data.results[0].playerName).toBe('test9'); + expect(parseResponse.data.results[0].objectId).toBe(objectId); + expect(parseResponse.data.results[0].ACL).toBeUndefined(); + expect(parseResponse.data.results[0].owner).toBeUndefined(); + expect.assertions(5); + }); + + it('Get Item', async () => { + const parseResponse = await axios({ + method: 'get', + url: `${API_URL}/parse/classes/GameScore/${objectId}`, + headers: { + 'Content-Type': 'application/json', + 'x-parse-application-id': `test`, + 'x-parse-session-token': sessionToken, + }, + }); + + expect(parseResponse.data.playerName).toBe('test9'); + expect(parseResponse.data.objectId).toBe(objectId); + expect(parseResponse.data.ACL).toBeUndefined(); + expect(parseResponse.data.owner).toBeUndefined(); + expect.assertions(4); + }); + + it('Update Item', async () => { + const parseResponse = await axios({ + method: 'put', + url: `${API_URL}/parse/classes/GameScore/${objectId}`, + headers: { + 'Content-Type': 'application/json', + 'x-parse-application-id': `test`, + 'x-parse-session-token': sessionToken, + }, + data: { + cheatMode: true, + }, + }); + + expect(parseResponse.data.cheatMode).toBe(true); + expect(parseResponse.data.ACL).toBeUndefined(); + expect(parseResponse.data.owner).toBeUndefined(); + expect.assertions(3); + }); +}); diff --git a/spec/helper.js b/spec/helper.js index 045aa41..a1155c0 100644 --- a/spec/helper.js +++ b/spec/helper.js @@ -1,121 +1,60 @@ -/* eslint-disable */ const mongoose = require('mongoose'); const nock = require('nock'); -const getClasses = require(`${SPEC_PATH}/../src/parse/schema/getClasses`); const { EventEmitter } = require('events'); const configMock = require('./__mock__/config'); -// Set up a default API server for testing with default configuration. -let ConnectServer; - -mongoose.set('useFindAndModify', false); -mongoose.set('useCreateIndex', true); -mongoose.set('useNewUrlParser', true); -mongoose.set('useUnifiedTopology', true); +configMock.MONGO_URI = 'mongodb://localhost:46347/connect'; +process.env = Object.assign(process.env, configMock); +jest.mock('../src/config', () => configMock); -const Parse = require('parse/node'); -Parse.serverURL = 'http://localhost:3000/parse'; +// Set up a default API server for testing with default configuration. +const ConnectServer = require('../src/connectServer'); +const AuthService = require('../src/api/services/auth'); function bindServer() { let server; - let parse; - - // Allows testing specific configurations of Parse Server - const reconfigureServer = () => { - return new Promise((resolve, reject) => { - if (server) { - return server.server.close(() => { - server = undefined; - reconfigureServer().then(resolve, reject); - }); - } - try { - const event = new EventEmitter(); - ConnectServer.start(configMock.APP_PORT, event).then( - (connectServer) => { - server = connectServer; - let expressFinish = false; - let parseInit = false; - - event.once('parse-init', function (Parse) { - parse = Parse; - parseInit = true; - if (parseInit && expressFinish) { - resolve(); - } - }); - - server.server.once('listening', function () { - expressFinish = true; - - if (parseInit && expressFinish) { - resolve(); - } - }); - }, - ); - } catch (error) { - return reject(error); - } - }); - }; - - beforeAll(async () => { - process.env.TESTING = true; - const connectionName = `mongodb://localhost:46347/${process.env.TEST_SUITE}`; - const parseConnect = mongoose.createConnection(connectionName); - const apiConnect = mongoose.createConnection(`${connectionName}-api`); - const parseSandboxConnect = mongoose.createConnection( - `${connectionName}-sandbox`, - ); - - jest.mock(`${SPEC_PATH}/../src/db/client`, () => { - return () => { - return { - parseConnect, - apiConnect, - parseSandboxConnect, - }; - }; - }); - configMock.MONGO_URI = connectionName; - process.env = Object.assign(process.env, configMock); - jest.mock(`${SPEC_PATH}/../src/config`, () => configMock); - await getClasses(); - ConnectServer = require('./../src/connectServer'); + afterAll(async () => { + await mongoose.disconnect(); }); - afterAll(() => { - mongoose.disconnect().then(() => { - if (server) { - return server.server.close(); - } - }); - }); + beforeAll(() => { + return new Promise((resolve) => { + const event = new EventEmitter(); + ConnectServer.start(configMock.APP_PORT, event).then((connectServer) => { + server = connectServer; + let expressFinish = false; + let parseInit = false; + + event.once('parse-init', () => { + parseInit = true; + if (parseInit && expressFinish) { + resolve(); + } + }); - beforeEach(() => { - return reconfigureServer(); - }); + server.server.once('listening', () => { + expressFinish = true; - afterEach(async () => { - return new Promise((resolve) => { - if (!server) { - resolve(); - } - - parse.Cloud._removeAllHooks(); - server.server.close(() => { - server = undefined; - resolve(); + if (parseInit && expressFinish) { + resolve(); + } + }); }); }); }); + + afterAll(async () => { + if (server) { + await server.server.close(); + server = undefined; + } + }); } function bindGithub() { - beforeEach(() => { + beforeAll(() => { nock(`https://github.com`) .persist() .post('/login/oauth/access_token') @@ -131,7 +70,7 @@ function bindGithub() { }); }); - afterEach(() => nock.cleanAll()); + afterAll(() => nock.cleanAll()); } function bindAll() { @@ -140,9 +79,8 @@ function bindAll() { } async function getJwtToken() { - const AuthService = require(`${SPEC_PATH}/../src/api/services/auth`); const authService = new AuthService(); - return await authService.connectUser('lambda_github_code'); + return authService.connectUser('lambda_github_code'); } module.exports = { diff --git a/spec/parse.spec.js b/spec/parse.spec.js deleted file mode 100644 index 97e1658..0000000 --- a/spec/parse.spec.js +++ /dev/null @@ -1,135 +0,0 @@ -/* eslint-disable */ -const axios = require('axios'); -const { bindAll, getJwtToken } = require(`${SPEC_PATH}/helper`); - -process.env.TEST_SUITE = 'parse-custom-server'; - -describe('parse custom server', () => { - bindAll(); - - let sessionToken; - let objectId; - - it('Get Token', async () => { - const jwtToken = await getJwtToken(); - - const response = await axios({ - method: 'post', - url: 'http://localhost:3000/api/application', - headers: { - Authorization: `Bearer ${jwtToken}`, - 'Content-Type': 'application/json', - }, - data: { - name: 'toto', - description: 'truc', - apple_store_link: 'https://apple.fr', - google_market_link: 'https://google.fr', - }, - }); - - const parseResponse = await axios({ - method: 'get', - url: 'http://localhost:3000/parse/login', - headers: { - 'x-parse-application-id': `test`, - 'x-parse-revocable-session': '1', - }, - data: { - username: response.data.parse_name, - password: response.data.token, - }, - }); - - expect(parseResponse.data.sessionToken).toBeDefined(); - expect.assertions(1); - - sessionToken = parseResponse.data.sessionToken; - }); - - it('Create Item', async () => { - const parseResponse = await axios({ - method: 'post', - url: 'http://localhost:3000/parse/classes/GameScore', - headers: { - 'Content-Type': 'application/json', - 'x-parse-application-id': `test`, - 'x-parse-session-token': sessionToken, - }, - data: { - score: 1337, - playerName: 'test9', - cheatMode: false, - }, - }); - - expect(parseResponse.data.score).toBe(1337); - expect(parseResponse.data.playerName).toBe('test9'); - expect(parseResponse.data.cheatMode).toBe(false); - expect(parseResponse.data.createdAt).toBeDefined(); - expect(parseResponse.data.updatedAt).toBeDefined(); - expect(parseResponse.data.objectId).toBeDefined(); - expect(parseResponse.data.ACL).toBeUndefined(); - expect(parseResponse.data.owner).toBeUndefined(); - expect.assertions(8); - - objectId = parseResponse.data.objectId; - }); - - it('Get Items', async () => { - const parseResponse = await axios({ - method: 'get', - url: 'http://localhost:3000/parse/classes/GameScore', - headers: { - 'Content-Type': 'application/json', - 'x-parse-application-id': `test`, - 'x-parse-session-token': sessionToken, - }, - }); - - expect(parseResponse.data.results.length).toBe(1); - expect(parseResponse.data.results[0].playerName).toBe('test9'); - expect(parseResponse.data.results[0].objectId).toBe(objectId); - expect(parseResponse.data.results[0].ACL).toBeUndefined(); - expect(parseResponse.data.results[0].owner).toBeUndefined(); - expect.assertions(5); - }); - - it('Get Item', async () => { - const parseResponse = await axios({ - method: 'get', - url: `http://localhost:3000/parse/classes/GameScore/${objectId}`, - headers: { - 'Content-Type': 'application/json', - 'x-parse-application-id': `test`, - 'x-parse-session-token': sessionToken, - }, - }); - - expect(parseResponse.data.playerName).toBe('test9'); - expect(parseResponse.data.objectId).toBe(objectId); - expect(parseResponse.data.ACL).toBeUndefined(); - expect(parseResponse.data.owner).toBeUndefined(); - expect.assertions(4); - }); - - it('Update Item', async () => { - const parseResponse = await axios({ - method: 'put', - url: `http://localhost:3000/parse/classes/GameScore/${objectId}`, - headers: { - 'Content-Type': 'application/json', - 'x-parse-application-id': `test`, - 'x-parse-session-token': sessionToken, - }, - data: { - cheatMode: true, - }, - }); - - expect(parseResponse.data.cheatMode).toBe(true); - expect(parseResponse.data.ACL).toBeUndefined(); - expect(parseResponse.data.owner).toBeUndefined(); - expect.assertions(3); - }); -});