From 977fca855a2e1eaf22aa03984907f0d51b850882 Mon Sep 17 00:00:00 2001 From: A Cottrill Date: Wed, 25 Sep 2024 17:41:24 -0400 Subject: [PATCH 1/6] chore: create twitch proxy tests --- test/twitch-proxy.test.js | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/twitch-proxy.test.js diff --git a/test/twitch-proxy.test.js b/test/twitch-proxy.test.js new file mode 100644 index 000000000..a47fe2e18 --- /dev/null +++ b/test/twitch-proxy.test.js @@ -0,0 +1,65 @@ +// TODO: Remove once types for Jest are recognized +/* eslint-disable no-undef */ +const axios = require('axios').default; +const portMap = require('../port-map.json'); +const twitchProxyPort = portMap['twitch-proxy']; + +describe('helix api', () => { + it('should return freecodecamp user data', async () => { + const response = await axios.get( + `http://localhost:${twitchProxyPort}/helix/users?login=freecodecamp` + ); + // eslint-disable-next-line no-undef + expect(response.status).toBe(200); + // eslint-disable-next-line no-undef + expect(response.data.data).toHaveLength(1); + }); + + it('should return ESL_SC2 stream data', async () => { + const response = await axios.get( + `http://localhost:${twitchProxyPort}/helix/streams?user_login=ESL_SC2` + ); + expect(response.status).toBe(200); + expect(response.data.data).toHaveLength(1); + }); + + it('should return Farmer Simulator 19 (Game ID: 504689) game data', async () => { + const response = await axios.get( + `http://localhost:${twitchProxyPort}/helix/streams?user_login=ESL_SC2` + ); + expect(response.status).toBe(200); + expect(response.data.data).toHaveLength(1); + }); +}); + +describe('kraken api', () => { + it('should return freecodecamp user data', async () => { + const response = await axios.get( + `http://localhost:${twitchProxyPort}/twitch-api/users/freecodecamp` + ); + const user = response.data; + expect(response.status).toBe(200); + expect(user.name).toBe('freecodecamp'); + expect(user.display_name).toBe('FreeCodeCamp'); + }); + + it('should return freecodecamp channel data', async () => { + const response = await axios.get( + `http://localhost:${twitchProxyPort}/twitch-api/channels/freecodecamp` + ); + const channel = response.data; + expect(response.status).toBe(200); + expect(channel.name).toBe('freecodecamp'); + expect(channel.display_name).toBe('FreeCodeCamp'); + expect(channel.url).toBe('https://www.twitch.tv/freecodecamp'); + }); + + it("should return esl_sc2's stream data", async () => { + const response = await axios.get( + `http://localhost:${twitchProxyPort}/twitch-api/streams/ESL_SC2` + ); + const stream = response.data.stream; + expect(response.status).toBe(200); + expect(stream._id).toBe(23366709968); + }); +}); From 57884494928ac666576dda9ed0810a984cc79834 Mon Sep 17 00:00:00 2001 From: A Cottrill Date: Tue, 22 Oct 2024 18:20:56 -0400 Subject: [PATCH 2/6] log results --- test/twitch-proxy.test.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/twitch-proxy.test.js b/test/twitch-proxy.test.js index a47fe2e18..f669607f0 100644 --- a/test/twitch-proxy.test.js +++ b/test/twitch-proxy.test.js @@ -1,6 +1,6 @@ // TODO: Remove once types for Jest are recognized /* eslint-disable no-undef */ -const axios = require('axios').default; +const axios = require('axios'); const portMap = require('../port-map.json'); const twitchProxyPort = portMap['twitch-proxy']; @@ -12,7 +12,9 @@ describe('helix api', () => { // eslint-disable-next-line no-undef expect(response.status).toBe(200); // eslint-disable-next-line no-undef - expect(response.data.data).toHaveLength(1); + const result = response.data; + console.log(result); + expect(result.data).toHaveLength(1); }); it('should return ESL_SC2 stream data', async () => { @@ -20,7 +22,9 @@ describe('helix api', () => { `http://localhost:${twitchProxyPort}/helix/streams?user_login=ESL_SC2` ); expect(response.status).toBe(200); - expect(response.data.data).toHaveLength(1); + const result = response.data; + console.log(result); + expect(result.data).toHaveLength(1); }); it('should return Farmer Simulator 19 (Game ID: 504689) game data', async () => { @@ -28,7 +32,9 @@ describe('helix api', () => { `http://localhost:${twitchProxyPort}/helix/streams?user_login=ESL_SC2` ); expect(response.status).toBe(200); - expect(response.data.data).toHaveLength(1); + const result = response.data; + console.log(result); + expect(result.data).toHaveLength(1); }); }); From b2bef65d4913d859b1c1f634e7bde908cba9fcc5 Mon Sep 17 00:00:00 2001 From: A Cottrill Date: Tue, 22 Oct 2024 18:29:07 -0400 Subject: [PATCH 3/6] bypass broken api key call --- apps/twitch-proxy/package.json | 2 +- test/twitch-proxy.test.js | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/apps/twitch-proxy/package.json b/apps/twitch-proxy/package.json index be3c4f599..5e7b346b8 100644 --- a/apps/twitch-proxy/package.json +++ b/apps/twitch-proxy/package.json @@ -4,7 +4,7 @@ "description": "fCC Twitch API passthrough server", "main": "server.js", "scripts": { - "prestart": "node kill-node-process-cron.js & node update-api-key.mjs", + "prestart": "node kill-node-process-cron.js", "start": "node server.js" }, "dependencies": { diff --git a/test/twitch-proxy.test.js b/test/twitch-proxy.test.js index f669607f0..bf2a902f8 100644 --- a/test/twitch-proxy.test.js +++ b/test/twitch-proxy.test.js @@ -13,7 +13,6 @@ describe('helix api', () => { expect(response.status).toBe(200); // eslint-disable-next-line no-undef const result = response.data; - console.log(result); expect(result.data).toHaveLength(1); }); @@ -23,7 +22,6 @@ describe('helix api', () => { ); expect(response.status).toBe(200); const result = response.data; - console.log(result); expect(result.data).toHaveLength(1); }); @@ -33,7 +31,6 @@ describe('helix api', () => { ); expect(response.status).toBe(200); const result = response.data; - console.log(result); expect(result.data).toHaveLength(1); }); }); From a5b962cea8ffe8ddd6b2455447e1bbce79492000 Mon Sep 17 00:00:00 2001 From: A Cottrill Date: Tue, 22 Oct 2024 18:34:00 -0400 Subject: [PATCH 4/6] restore update function and remove helix tests --- apps/twitch-proxy/package.json | 2 +- test/twitch-proxy.test.js | 31 ------------------------------- 2 files changed, 1 insertion(+), 32 deletions(-) diff --git a/apps/twitch-proxy/package.json b/apps/twitch-proxy/package.json index 5e7b346b8..be3c4f599 100644 --- a/apps/twitch-proxy/package.json +++ b/apps/twitch-proxy/package.json @@ -4,7 +4,7 @@ "description": "fCC Twitch API passthrough server", "main": "server.js", "scripts": { - "prestart": "node kill-node-process-cron.js", + "prestart": "node kill-node-process-cron.js & node update-api-key.mjs", "start": "node server.js" }, "dependencies": { diff --git a/test/twitch-proxy.test.js b/test/twitch-proxy.test.js index bf2a902f8..50c545cb8 100644 --- a/test/twitch-proxy.test.js +++ b/test/twitch-proxy.test.js @@ -4,37 +4,6 @@ const axios = require('axios'); const portMap = require('../port-map.json'); const twitchProxyPort = portMap['twitch-proxy']; -describe('helix api', () => { - it('should return freecodecamp user data', async () => { - const response = await axios.get( - `http://localhost:${twitchProxyPort}/helix/users?login=freecodecamp` - ); - // eslint-disable-next-line no-undef - expect(response.status).toBe(200); - // eslint-disable-next-line no-undef - const result = response.data; - expect(result.data).toHaveLength(1); - }); - - it('should return ESL_SC2 stream data', async () => { - const response = await axios.get( - `http://localhost:${twitchProxyPort}/helix/streams?user_login=ESL_SC2` - ); - expect(response.status).toBe(200); - const result = response.data; - expect(result.data).toHaveLength(1); - }); - - it('should return Farmer Simulator 19 (Game ID: 504689) game data', async () => { - const response = await axios.get( - `http://localhost:${twitchProxyPort}/helix/streams?user_login=ESL_SC2` - ); - expect(response.status).toBe(200); - const result = response.data; - expect(result.data).toHaveLength(1); - }); -}); - describe('kraken api', () => { it('should return freecodecamp user data', async () => { const response = await axios.get( From 62c75cb987d66feb8e946624b6208f21a833fe14 Mon Sep 17 00:00:00 2001 From: Shaun Hamilton Date: Thu, 24 Oct 2024 08:27:03 +0000 Subject: [PATCH 5/6] chore: set url for gitpod testing --- .gitpod.yml | 8 ++++++++ jest.config.js | 3 ++- test/jest-utils.js | 18 ++++++++++++++++++ test/ports.test.js | 10 +++++----- test/twitch-proxy.test.js | 22 ++++++++++++---------- 5 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 test/jest-utils.js diff --git a/.gitpod.yml b/.gitpod.yml index 8ebef2db2..b8b9fadec 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -2,8 +2,16 @@ image: gitpod/workspace-mongodb ports: - port: 27017 onOpen: ignore + - port: 50000-50200 + onOpen: ignore + # Set to public for tests to have access across ports + # Technically, could be configured with `credentials: true` in fetch calls too + visibility: public tasks: + - before: | + export DEMO_APPS_DOMAIN=$(gp url) - init: | + cp sample.env .env npm ci mkdir /workspace/log mongod --fork --dbpath /data/db --logpath /workspace/log/mongod.log diff --git a/jest.config.js b/jest.config.js index 0ab81955e..5523f1b2c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,6 @@ const config = { - roots: ['./test/'] + roots: ['./test/'], + setupFiles: ['dotenv/config'] }; module.exports = config; diff --git a/test/jest-utils.js b/test/jest-utils.js new file mode 100644 index 000000000..1196ca455 --- /dev/null +++ b/test/jest-utils.js @@ -0,0 +1,18 @@ +const baseUrl = port => { + if (process.env.DEMO_APPS_DOMAIN === 'localhost') { + const url = new URL('https://localhost'); + url.port = port; + return url; + } + + if (process.env.GITPOD_HOST) { + const url = new URL( + `https://${port}-${process.env.GITPOD_WORKSPACE_ID}.${process.env.GITPOD_WORKSPACE_CLUSTER_HOST}` + ); + return url; + } +}; + +module.exports = { + baseUrl +}; diff --git a/test/ports.test.js b/test/ports.test.js index 19ec5d1c3..146316a95 100644 --- a/test/ports.test.js +++ b/test/ports.test.js @@ -1,6 +1,5 @@ // TODO: Remove eslint-disable-next-line no-undef the minute expect can be labelled as defined const { readdirSync } = require('fs'); -const axios = require('axios'); const { join } = require('path'); const portMap = require('../port-map.json'); @@ -10,6 +9,8 @@ const names = readdirSync(join(__dirname, '../apps')).filter( name => !/(^|\/)\.[^/.]/g.test(name) && name !== 'nightlife-coordination-app' ); +const { baseUrl } = require('./jest-utils.js'); + describe('portMap', () => { it('should have unique ports', () => { const ports = Object.values(portMap); @@ -35,10 +36,11 @@ describe('Project statuses', () => { for (const name of projectNames) { const portNum = portMap[name]; + const BASE_URL = baseUrl(portNum); it(`${name} should be running on port ${portNum}`, async () => { try { - const response = await axios.get(`http://localhost:${portNum}`); + const response = await fetch(BASE_URL); // eslint-disable-next-line no-undef expect(response.status).toBe(200); @@ -49,9 +51,7 @@ describe('Project statuses', () => { }); it(`Pinging ${name} should return a status code of 200 `, async () => { try { - const response = await axios.get( - `http://localhost:${portNum}/status/ping` - ); + const response = await fetch(new URL('/status/ping', BASE_URL)); // eslint-disable-next-line no-undef expect(response.status).toBe(200); diff --git a/test/twitch-proxy.test.js b/test/twitch-proxy.test.js index 50c545cb8..d2be46471 100644 --- a/test/twitch-proxy.test.js +++ b/test/twitch-proxy.test.js @@ -1,25 +1,27 @@ // TODO: Remove once types for Jest are recognized /* eslint-disable no-undef */ -const axios = require('axios'); const portMap = require('../port-map.json'); const twitchProxyPort = portMap['twitch-proxy']; +const { baseUrl } = require('./jest-utils.js'); + +const BASE_URL = baseUrl(twitchProxyPort); describe('kraken api', () => { it('should return freecodecamp user data', async () => { - const response = await axios.get( - `http://localhost:${twitchProxyPort}/twitch-api/users/freecodecamp` + const response = await fetch( + new URL('/twitch-api/users/freecodecamp', BASE_URL) ); - const user = response.data; + const user = await response.json(); expect(response.status).toBe(200); expect(user.name).toBe('freecodecamp'); expect(user.display_name).toBe('FreeCodeCamp'); }); it('should return freecodecamp channel data', async () => { - const response = await axios.get( - `http://localhost:${twitchProxyPort}/twitch-api/channels/freecodecamp` + const response = await fetch( + new URL('/twitch-api/channels/freecodecamp', BASE_URL) ); - const channel = response.data; + const channel = await response.json(); expect(response.status).toBe(200); expect(channel.name).toBe('freecodecamp'); expect(channel.display_name).toBe('FreeCodeCamp'); @@ -27,10 +29,10 @@ describe('kraken api', () => { }); it("should return esl_sc2's stream data", async () => { - const response = await axios.get( - `http://localhost:${twitchProxyPort}/twitch-api/streams/ESL_SC2` + const response = await fetch( + new URL('/twitch-api/streams/ESL_SC2', BASE_URL) ); - const stream = response.data.stream; + const stream = (await response.json()).stream; expect(response.status).toBe(200); expect(stream._id).toBe(23366709968); }); From 3f92ca07a197c6ffd0d29847462c6748cdb523d9 Mon Sep 17 00:00:00 2001 From: Shaun Hamilton Date: Thu, 24 Oct 2024 11:38:50 +0200 Subject: [PATCH 6/6] fix: shaun forgets localhost is not secure Co-authored-by: Oliver Eyton-Williams --- test/jest-utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/jest-utils.js b/test/jest-utils.js index 1196ca455..75bc8c4b3 100644 --- a/test/jest-utils.js +++ b/test/jest-utils.js @@ -1,6 +1,6 @@ const baseUrl = port => { if (process.env.DEMO_APPS_DOMAIN === 'localhost') { - const url = new URL('https://localhost'); + const url = new URL('http://localhost'); url.port = port; return url; }