From f772b1e183098dc613ebc9bc81a9eaea34639696 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Tue, 16 Apr 2024 14:07:22 -0400 Subject: [PATCH] Check expected arguments --- src/lib/api/documents.test.ts | 54 ++++++++++++++++++++++++++--------- src/lib/api/documents.ts | 1 - 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/lib/api/documents.test.ts b/src/lib/api/documents.test.ts index 06619fd0b..9f6fef088 100644 --- a/src/lib/api/documents.test.ts +++ b/src/lib/api/documents.test.ts @@ -1,15 +1,7 @@ import type { Document, DocumentUpload, Sizes } from "./types"; -import { - vi, - test as base, - describe, - it, - expect, - beforeEach, - afterEach, -} from "vitest"; -import { APP_URL, IMAGE_WIDTHS_ENTRIES } from "@/config/config.js"; +import { vi, test as base, describe, expect, afterEach } from "vitest"; +import { APP_URL, DC_BASE, IMAGE_WIDTHS_ENTRIES } from "@/config/config.js"; import * as documents from "./documents"; @@ -61,14 +53,28 @@ describe("document uploads and processing", () => { documents.create(docs, "token", mockFetch).then((d) => { expect(d).toMatchObject(created); }); + + expect(mockFetch).toHaveBeenCalledWith( + new URL("/api/documents/", DC_BASE), + { + body: JSON.stringify(docs), + credentials: "include", + headers: { + "Content-type": "application/json", + Referer: APP_URL, + "X-CSRFToken": "token", + }, + method: "POST", + }, + ); }); test("documents.upload", async ({ created }) => { - const mockFetch = vi.fn().mockImplementation(async () => ({ - ok: true, - })); - created.forEach(async (doc) => { + const mockFetch = vi.fn().mockImplementation(async () => ({ + ok: true, + })); + const file = new File( ["test content"], "finalseasonal-allergies-pollen-and-mold-2023-en.pdf", @@ -82,6 +88,13 @@ describe("document uploads and processing", () => { ); expect(resp.ok).toBeTruthy(); + expect(mockFetch).toHaveBeenCalledWith(new URL(doc.presigned_url), { + body: file, + headers: { + "Content-Type": file.type, + }, + method: "PUT", + }); }); }); @@ -100,6 +113,19 @@ describe("document uploads and processing", () => { ); expect(resp.ok).toBeTruthy(); + expect(mockFetch).toHaveBeenCalledWith( + new URL("/api/documents/process/", DC_BASE), + { + body: JSON.stringify(created.map((d) => ({ id: d.id }))), + credentials: "include", + headers: { + "Content-type": "application/json", + Referer: APP_URL, + "X-CSRFToken": "csrf_token", + }, + method: "POST", + }, + ); }); test.todo("documents.cancel"); diff --git a/src/lib/api/documents.ts b/src/lib/api/documents.ts index b06745b99..87b3c50d6 100644 --- a/src/lib/api/documents.ts +++ b/src/lib/api/documents.ts @@ -96,7 +96,6 @@ export async function create( Referer: APP_URL, }, body: JSON.stringify(documents), - referrerPolicy: "origin", }); if (isErrorCode(resp.status)) {