Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import { validateUrl } from './url.js';
2
3describe('URL Tests', () => {
4 describe('Deny Prod URLs', () => {
5 beforeEach(() => {
6 // This absolutely is unsafe mutation of a global that'll be visible
7 // across test suites. However, this env var should only be relied upon
8 // by this module, so it should be ok.
9 // eslint-disable-next-line better-mutation/no-mutation
10 process.env.ALLOW_USER_INPUT_LOCALHOST_URIS = 'false';
11 });
12
13 afterEach(() => {
14 delete process.env.ALLOW_USER_INPUT_LOCALHOST_URIS;
15 });
16
17 test('Deny Coop domains', () => {
18 expect(() => validateUrl('https://www.coopapi.com')).toThrow();
19 expect(() => validateUrl('https://www.trycoop.co')).toThrow();
20 expect(() => validateUrl('https://www.getcoop.com')).toThrow();
21 });
22
23 test('Deny localhost domains', () => {
24 // This absolutely is unsafe mutation of a global that'll be visible
25 // across test suites. However, this env var should only be relied upon
26 // by this module, so it should be ok.
27 // eslint-disable-next-line better-mutation/no-mutation
28 process.env.ALLOW_USER_INPUT_LOCALHOST_URIS = 'false';
29
30 expect(() => validateUrl('https://localhost:3000')).toThrow();
31 expect(() => validateUrl('https://127.0.0.1')).toThrow();
32 });
33 });
34
35 describe('Allow development URLs', () => {
36 beforeEach(() => {
37 // This absolutely is unsafe mutation of a global that'll be visible
38 // across test suites. However, this env var should only be relied upon
39 // by this module, so it should be ok.
40 // eslint-disable-next-line better-mutation/no-mutation
41 process.env.ALLOW_USER_INPUT_LOCALHOST_URIS = 'true';
42 });
43
44 afterEach(() => {
45 delete process.env.ALLOW_USER_INPUT_LOCALHOST_URIS;
46 });
47
48 test('Deny Coop domains', () => {
49 expect(() => validateUrl('https://www.coopapi.com')).toThrow();
50 expect(() => validateUrl('https://www.trycoop.co')).toThrow();
51 expect(() => validateUrl('https://www.getcoop.com')).toThrow();
52 });
53
54 test('Allow localhost domains', () => {
55 expect(() => validateUrl('https://localhost:3000')).not.toThrow();
56 expect(() => validateUrl('https://127.0.0.1')).not.toThrow();
57 });
58 });
59});