|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { getBaseEndpoint } from "./utils.js"; |
| 3 | + |
| 4 | +const baseEndpoint = getBaseEndpoint(); |
| 5 | + |
| 6 | +describe("CORS tests", async () => { |
| 7 | + test("Events: Known URL is preflight allowed in CORS", async () => { |
| 8 | + const response = await fetch(`${baseEndpoint}/api/v1/events`, { |
| 9 | + method: "OPTIONS", |
| 10 | + headers: { |
| 11 | + "Access-Control-Request-Method": "GET", |
| 12 | + Origin: "https://acmuiuc.pages.dev", |
| 13 | + }, |
| 14 | + }); |
| 15 | + expect(response.status).toBe(204); |
| 16 | + expect(response.headers.get("access-control-allow-origin")).toStrictEqual( |
| 17 | + "https://acmuiuc.pages.dev", |
| 18 | + ); |
| 19 | + }); |
| 20 | + test("Events: Known URL is allowed in CORS", async () => { |
| 21 | + const response = await fetch(`${baseEndpoint}/api/v1/events`, { |
| 22 | + headers: { |
| 23 | + Origin: "https://acmuiuc.pages.dev", |
| 24 | + }, |
| 25 | + }); |
| 26 | + expect(response.status).toBe(200); |
| 27 | + expect(response.headers.get("access-control-allow-origin")).toStrictEqual( |
| 28 | + "https://acmuiuc.pages.dev", |
| 29 | + ); |
| 30 | + }); |
| 31 | + test("Events: Unknown URL is preflight not allowed in CORS", async () => { |
| 32 | + const response = await fetch(`${baseEndpoint}/api/v1/events`, { |
| 33 | + method: "OPTIONS", |
| 34 | + headers: { |
| 35 | + "Access-Control-Request-Method": "GET", |
| 36 | + Origin: "https://google.com", |
| 37 | + }, |
| 38 | + }); |
| 39 | + expect(response.status).toBe(204); |
| 40 | + expect(response.headers).not.toHaveProperty("access-control-allow-origin"); |
| 41 | + }); |
| 42 | + test("Events: Unknown URL is not allowed in CORS", async () => { |
| 43 | + const response = await fetch(`${baseEndpoint}/api/v1/events`, { |
| 44 | + headers: { |
| 45 | + Origin: "https://google.com", |
| 46 | + }, |
| 47 | + }); |
| 48 | + expect(response.status).toBe(200); |
| 49 | + expect(response.headers).not.toHaveProperty("access-control-allow-origin"); |
| 50 | + }); |
| 51 | + test("Membership: Known URL is allowed in CORS", async () => { |
| 52 | + const response = await fetch(`${baseEndpoint}/api/v1/membership/zzzzzz`, { |
| 53 | + headers: { |
| 54 | + Origin: "https://acmuiuc.pages.dev", |
| 55 | + }, |
| 56 | + }); |
| 57 | + expect(response.status).toBe(200); |
| 58 | + expect(response.headers.get("access-control-allow-origin")).toStrictEqual( |
| 59 | + "https://acmuiuc.pages.dev", |
| 60 | + ); |
| 61 | + }); |
| 62 | + test("Membership: Known URL is preflight allowed in CORS", async () => { |
| 63 | + const response = await fetch(`${baseEndpoint}/api/v1/membership/zzzzzz`, { |
| 64 | + method: "OPTIONS", |
| 65 | + headers: { |
| 66 | + "Access-Control-Request-Method": "GET", |
| 67 | + Origin: "https://acmuiuc.pages.dev", |
| 68 | + }, |
| 69 | + }); |
| 70 | + expect(response.status).toBe(204); |
| 71 | + expect(response.headers.get("access-control-allow-origin")).toStrictEqual( |
| 72 | + "https://acmuiuc.pages.dev", |
| 73 | + ); |
| 74 | + }); |
| 75 | + test("Membership: Unknown URL is not allowed in CORS", async () => { |
| 76 | + const response = await fetch(`${baseEndpoint}/api/v1/membership/zzzzzz`, { |
| 77 | + headers: { |
| 78 | + Origin: "https://google.com", |
| 79 | + }, |
| 80 | + }); |
| 81 | + expect(response.status).toBe(200); |
| 82 | + expect(response.headers).not.toHaveProperty("access-control-allow-origin"); |
| 83 | + }); |
| 84 | + test("Membership: Unknown URL is preflight not allowed in CORS", async () => { |
| 85 | + const response = await fetch(`${baseEndpoint}/api/v1/membership/zzzzzz`, { |
| 86 | + method: "OPTIONS", |
| 87 | + headers: { |
| 88 | + "Access-Control-Request-Method": "GET", |
| 89 | + Origin: "https://google.com", |
| 90 | + }, |
| 91 | + }); |
| 92 | + expect(response.status).toBe(204); |
| 93 | + expect(response.headers).not.toHaveProperty("access-control-allow-origin"); |
| 94 | + }); |
| 95 | +}); |
0 commit comments