Skip to content

Commit

Permalink
chore: create twitch proxy tests (#617)
Browse files Browse the repository at this point in the history
* chore: create twitch proxy tests

* log results

* bypass broken api key call

* restore update function and remove helix tests

* chore: set url for gitpod testing

* fix: shaun forgets localhost is not secure

Co-authored-by: Oliver Eyton-Williams <[email protected]>

---------

Co-authored-by: Shaun Hamilton <[email protected]>
Co-authored-by: Oliver Eyton-Williams <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2024
1 parent cfb93c7 commit 5be11a9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const config = {
roots: ['./test/']
roots: ['./test/'],
setupFiles: ['dotenv/config']
};

module.exports = config;
18 changes: 18 additions & 0 deletions test/jest-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const baseUrl = port => {
if (process.env.DEMO_APPS_DOMAIN === 'localhost') {
const url = new URL('http://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
};
10 changes: 5 additions & 5 deletions test/ports.test.js
Original file line number Diff line number Diff line change
@@ -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');

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions test/twitch-proxy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// TODO: Remove once types for Jest are recognized
/* eslint-disable no-undef */
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 fetch(
new URL('/twitch-api/users/freecodecamp', BASE_URL)
);
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 fetch(
new URL('/twitch-api/channels/freecodecamp', BASE_URL)
);
const channel = await response.json();
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 fetch(
new URL('/twitch-api/streams/ESL_SC2', BASE_URL)
);
const stream = (await response.json()).stream;
expect(response.status).toBe(200);
expect(stream._id).toBe(23366709968);
});
});

0 comments on commit 5be11a9

Please sign in to comment.