Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create twitch proxy tests #617

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('https://localhost');
ShaunSHamilton marked this conversation as resolved.
Show resolved Hide resolved
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', () => {
a2937 marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
Loading