Skip to content

Commit 5be11a9

Browse files
a2937ShaunSHamiltonojeytonwilliams
authored
chore: create twitch proxy tests (#617)
* 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 <ojeytonwilliams@gmail.com> --------- Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
1 parent cfb93c7 commit 5be11a9

File tree

5 files changed

+72
-6
lines changed

5 files changed

+72
-6
lines changed

.gitpod.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ image: gitpod/workspace-mongodb
22
ports:
33
- port: 27017
44
onOpen: ignore
5+
- port: 50000-50200
6+
onOpen: ignore
7+
# Set to public for tests to have access across ports
8+
# Technically, could be configured with `credentials: true` in fetch calls too
9+
visibility: public
510
tasks:
11+
- before: |
12+
export DEMO_APPS_DOMAIN=$(gp url)
613
- init: |
14+
cp sample.env .env
715
npm ci
816
mkdir /workspace/log
917
mongod --fork --dbpath /data/db --logpath /workspace/log/mongod.log

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const config = {
2-
roots: ['./test/']
2+
roots: ['./test/'],
3+
setupFiles: ['dotenv/config']
34
};
45

56
module.exports = config;

test/jest-utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const baseUrl = port => {
2+
if (process.env.DEMO_APPS_DOMAIN === 'localhost') {
3+
const url = new URL('http://localhost');
4+
url.port = port;
5+
return url;
6+
}
7+
8+
if (process.env.GITPOD_HOST) {
9+
const url = new URL(
10+
`https://${port}-${process.env.GITPOD_WORKSPACE_ID}.${process.env.GITPOD_WORKSPACE_CLUSTER_HOST}`
11+
);
12+
return url;
13+
}
14+
};
15+
16+
module.exports = {
17+
baseUrl
18+
};

test/ports.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// TODO: Remove eslint-disable-next-line no-undef the minute expect can be labelled as defined
22
const { readdirSync } = require('fs');
3-
const axios = require('axios');
43
const { join } = require('path');
54
const portMap = require('../port-map.json');
65

@@ -10,6 +9,8 @@ const names = readdirSync(join(__dirname, '../apps')).filter(
109
name => !/(^|\/)\.[^/.]/g.test(name) && name !== 'nightlife-coordination-app'
1110
);
1211

12+
const { baseUrl } = require('./jest-utils.js');
13+
1314
describe('portMap', () => {
1415
it('should have unique ports', () => {
1516
const ports = Object.values(portMap);
@@ -35,10 +36,11 @@ describe('Project statuses', () => {
3536

3637
for (const name of projectNames) {
3738
const portNum = portMap[name];
39+
const BASE_URL = baseUrl(portNum);
3840

3941
it(`${name} should be running on port ${portNum}`, async () => {
4042
try {
41-
const response = await axios.get(`http://localhost:${portNum}`);
43+
const response = await fetch(BASE_URL);
4244

4345
// eslint-disable-next-line no-undef
4446
expect(response.status).toBe(200);
@@ -49,9 +51,7 @@ describe('Project statuses', () => {
4951
});
5052
it(`Pinging ${name} should return a status code of 200 `, async () => {
5153
try {
52-
const response = await axios.get(
53-
`http://localhost:${portNum}/status/ping`
54-
);
54+
const response = await fetch(new URL('/status/ping', BASE_URL));
5555

5656
// eslint-disable-next-line no-undef
5757
expect(response.status).toBe(200);

test/twitch-proxy.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// TODO: Remove once types for Jest are recognized
2+
/* eslint-disable no-undef */
3+
const portMap = require('../port-map.json');
4+
const twitchProxyPort = portMap['twitch-proxy'];
5+
const { baseUrl } = require('./jest-utils.js');
6+
7+
const BASE_URL = baseUrl(twitchProxyPort);
8+
9+
describe('kraken api', () => {
10+
it('should return freecodecamp user data', async () => {
11+
const response = await fetch(
12+
new URL('/twitch-api/users/freecodecamp', BASE_URL)
13+
);
14+
const user = await response.json();
15+
expect(response.status).toBe(200);
16+
expect(user.name).toBe('freecodecamp');
17+
expect(user.display_name).toBe('FreeCodeCamp');
18+
});
19+
20+
it('should return freecodecamp channel data', async () => {
21+
const response = await fetch(
22+
new URL('/twitch-api/channels/freecodecamp', BASE_URL)
23+
);
24+
const channel = await response.json();
25+
expect(response.status).toBe(200);
26+
expect(channel.name).toBe('freecodecamp');
27+
expect(channel.display_name).toBe('FreeCodeCamp');
28+
expect(channel.url).toBe('https://www.twitch.tv/freecodecamp');
29+
});
30+
31+
it("should return esl_sc2's stream data", async () => {
32+
const response = await fetch(
33+
new URL('/twitch-api/streams/ESL_SC2', BASE_URL)
34+
);
35+
const stream = (await response.json()).stream;
36+
expect(response.status).toBe(200);
37+
expect(stream._id).toBe(23366709968);
38+
});
39+
});

0 commit comments

Comments
 (0)