From af8d693a0fbb58e033e9d47128c52493bb2cee32 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Mon, 20 Nov 2023 12:48:14 -0500 Subject: [PATCH] Use document fixtures --- .gitignore | 3 + tests/anonymous/viewer/document.spec.js | 68 +++++----- tests/fixtures/production.json | 158 ++++++++++++++++++++++++ tests/fixtures/staging.json | 60 +++++++++ 4 files changed, 251 insertions(+), 38 deletions(-) create mode 100644 tests/fixtures/production.json create mode 100644 tests/fixtures/staging.json diff --git a/.gitignore b/.gitignore index 86da8a731..7fad13404 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,9 @@ public/assets/ playwright-report test-results +# local fixtures, everyone should generate their own +tests/fixtures/development.json + .vscode scratch/ diff --git a/tests/anonymous/viewer/document.spec.js b/tests/anonymous/viewer/document.spec.js index fb9a4001f..01c11840b 100644 --- a/tests/anonymous/viewer/document.spec.js +++ b/tests/anonymous/viewer/document.spec.js @@ -1,44 +1,34 @@ // @ts-check - -import { test, expect } from "@playwright/test"; - -const DC_BASE = process.env.DC_BASE; - -let document, text; - -test.describe("document tests", () => { - // fetch the first available public document as a test case - test.beforeAll(async () => { - const endpoint = new URL( - "api/documents.json?access=public&per_page=1", - DC_BASE, +import fs from "node:fs/promises"; +import { test as base, expect } from "@playwright/test"; + +const { + DC_BASE = "https://api.dev.documentcloud.org", + NODE_ENV = "development", +} = process.env; + +const test = base.extend({ + document: async ({ page }, use) => { + const filename = new URL( + `../../fixtures/${NODE_ENV}.json`, + import.meta.url, ); - const { results } = await fetch(endpoint) - .then((r) => r.json()) - .catch(console.error); - - document = results[0]; - - console.log(`Using test document: ${document.title}`); + const documents = await fs + .readFile(filename) + .then((s) => JSON.parse(s.toString())); - const textEndpoint = new URL( - `documents/${document.id}/${document.slug}.txt.json`, - document.asset_url, - ); - - text = await fetch(textEndpoint) - .then((r) => r.json()) - .catch((e) => { - console.error(e); - console.error(textEndpoint); - }); - }); + await use(documents[0]); + }, +}); - test("basic document test", async ({ page }) => { - await page.goto(document.canonical_url); +test.describe("document tests", () => { + test("basic document test", async ({ page, document }) => { + // canonical will point to a URL that might not exist on staging + const path = new URL(document.canonical_url).pathname; + await page.goto(path).catch(console.error); - expect(page.url()).toBe(document.canonical_url); + expect(new URL(page.url()).pathname).toBe(path); await expect(page.locator(".sidebar").getByRole("heading")).toHaveText( document.title, @@ -59,9 +49,11 @@ test.describe("document tests", () => { .selectOption("text"); // check that text view loaded - await expect(page.locator(".text").first()).toHaveText( - text.pages[0].contents, - ); + /* + await expect(page.locator(".text").first()).toHaveText( + text.pages[0].contents, + ); + */ // switch to thumbnail view, click the first image await page.getByRole("combobox").selectOption("thumbnail"); diff --git a/tests/fixtures/production.json b/tests/fixtures/production.json new file mode 100644 index 000000000..8de6c2d71 --- /dev/null +++ b/tests/fixtures/production.json @@ -0,0 +1,158 @@ +[ + { + "id": 1, + "access": "public", + "admin_noindex": false, + "asset_url": "https://s3.documentcloud.org/", + "canonical_url": "https://www.documentcloud.org/documents/1-a-i-g-bailout-the-inspector-generals-report", + "created_at": "2010-02-22T19:48:08.738905Z", + "data": {}, + "description": "Neil Barofsky's report concludes that officials overseeing the rescue of the American International Group might have overpaid other banks to wrap up A.I.G.'s financial obligations.", + "edit_access": false, + "file_hash": "", + "noindex": false, + "language": "eng", + "organization": 1, + "original_extension": "pdf", + "page_count": 47, + "page_spec": "612.0x792.0:0-46", + "projects": [ + 46386, + 6 + ], + "publish_at": null, + "published_url": "", + "related_article": "", + "revision_control": false, + "slug": "a-i-g-bailout-the-inspector-generals-report", + "source": "Office of the Special Inspector General for T.A.R.P.", + "status": "success", + "title": "A.I.G. Bailout: The Inspector General's Report", + "updated_at": "2020-11-10T16:23:31.154198Z", + "user": 1 + }, + { + "id": 2, + "access": "public", + "admin_noindex": false, + "asset_url": "https://s3.documentcloud.org/", + "canonical_url": "https://www.documentcloud.org/documents/2-president-obamas-health-care-proposal", + "created_at": "2010-02-22T19:57:44.131650Z", + "data": {}, + "description": "On Feb. 22, 2010, the Obama Administration released a detailed proposal outlining the President's plan for a compromise among the House and Senate versions of a health care bill, and Republican concerns.", + "edit_access": false, + "file_hash": "", + "noindex": false, + "language": "eng", + "organization": 1, + "original_extension": "pdf", + "page_count": 11, + "page_spec": "612.0x792.0:0-10", + "projects": [], + "publish_at": null, + "published_url": "", + "related_article": "", + "revision_control": false, + "slug": "president-obamas-health-care-proposal", + "source": "whitehouse.gov", + "status": "success", + "title": "President Obama's Health Care Proposal", + "updated_at": "2020-11-10T16:23:31.180653Z", + "user": 1 + }, + { + "id": 3, + "access": "public", + "admin_noindex": false, + "asset_url": "https://s3.documentcloud.org/", + "canonical_url": "https://www.documentcloud.org/documents/3-shaping-the-next-economic-expansion", + "created_at": "2010-02-22T20:13:31.180904Z", + "data": {}, + "description": "A speech delivered by Lawrence Summers to the New York Economic Club on October 29, 2009. ", + "edit_access": false, + "file_hash": "", + "noindex": false, + "language": "eng", + "organization": 1, + "original_extension": "pdf", + "page_count": 7, + "page_spec": "612.0x792.0:0-6", + "projects": [ + 46386, + 6 + ], + "publish_at": null, + "published_url": "", + "related_article": "", + "revision_control": false, + "slug": "shaping-the-next-economic-expansion", + "source": "New York Economic Club", + "status": "success", + "title": "Shaping the Next Economic Expansion", + "updated_at": "2020-11-10T16:23:31.160644Z", + "user": 1 + }, + { + "id": 4, + "access": "public", + "admin_noindex": false, + "asset_url": "https://s3.documentcloud.org/", + "canonical_url": "https://www.documentcloud.org/documents/4-inspector-generals-report-on-medicare-prescription-fraud", + "created_at": "2010-02-22T20:16:51.842446Z", + "data": {}, + "description": "Daniel Levinson's report concludes that federal health officials need to cooperate better with fraud investigations, and that the government should require private insurers to report all instances of suspected fraud.", + "edit_access": false, + "file_hash": "", + "noindex": false, + "language": "eng", + "organization": 1, + "original_extension": "pdf", + "page_count": 31, + "page_spec": "612.0x792.0:0-26,28-30;616.7999877929688x795.1199951171875:27", + "projects": [ + 2, + 9352 + ], + "publish_at": null, + "published_url": "", + "related_article": "", + "revision_control": false, + "slug": "inspector-generals-report-on-medicare-prescription-fraud", + "source": "Department of Health and Human Services", + "status": "success", + "title": "Inspector General's Report on Medicare Prescription Fraud", + "updated_at": "2020-11-10T16:23:31.160353Z", + "user": 1 + }, + { + "id": 5, + "access": "public", + "admin_noindex": false, + "asset_url": "https://s3.documentcloud.org/", + "canonical_url": "https://www.documentcloud.org/documents/5-wiretapping-lawsuits-thrown-out", + "created_at": "2010-02-22T21:22:53.524125Z", + "data": {}, + "description": "In this ruling, Judge Vaughn R. Walker throws out dozens of lawsuits claiming that the nation's largest telecommunications companies illegally assisted in the government's wiretapping program.", + "edit_access": false, + "file_hash": "", + "noindex": false, + "language": "eng", + "organization": 1, + "original_extension": "pdf", + "page_count": 46, + "page_spec": "612.0x792.0:0-45", + "projects": [ + 2 + ], + "publish_at": null, + "published_url": "", + "related_article": "", + "revision_control": false, + "slug": "wiretapping-lawsuits-thrown-out", + "source": "United States District Court for Northern California", + "status": "success", + "title": "Wiretapping Lawsuits Thrown Out", + "updated_at": "2020-11-10T16:23:31.159780Z", + "user": 1 + } +] diff --git a/tests/fixtures/staging.json b/tests/fixtures/staging.json new file mode 100644 index 000000000..2311e7adb --- /dev/null +++ b/tests/fixtures/staging.json @@ -0,0 +1,60 @@ +[ + { + "id": 20005908, + "access": "public", + "admin_noindex": false, + "asset_url": "https://documentcloud-staging-files.s3.amazonaws.com/", + "canonical_url": "https://www.staging.documentcloud.org/documents/20005908-finalseasonal_allergies_pollen_and_mold_2023__en", + "created_at": "2023-03-21T20:33:35.857475Z", + "data": {}, + "description": "", + "edit_access": false, + "file_hash": "f776f2148c685d7d67aef0082f1d430dfe4bbe17", + "noindex": false, + "language": "eng", + "organization": 1, + "original_extension": "pdf", + "page_count": 9, + "page_spec": "612.0x792.0000610351562:0-8", + "projects": [], + "publish_at": null, + "published_url": "", + "related_article": "", + "revision_control": false, + "slug": "finalseasonal_allergies_pollen_and_mold_2023__en", + "source": "", + "status": "success", + "title": "FINALSeasonal_allergies_pollen_and_mold_2023__EN_", + "updated_at": "2023-09-18T16:56:04.839073Z", + "user": 100012 + }, + { + "id": 20006188, + "access": "public", + "admin_noindex": false, + "asset_url": "https://documentcloud-staging-files.s3.amazonaws.com/", + "canonical_url": "https://www.staging.documentcloud.org/documents/20006188-change-assessment_w_mjw-letter", + "created_at": "2023-10-25T17:39:22.192849Z", + "data": {}, + "description": "", + "edit_access": false, + "file_hash": "0ee61c995723d434b710f89d95a2681b56a0f30c", + "noindex": false, + "language": "eng", + "organization": 1, + "original_extension": "pdf", + "page_count": 24, + "page_spec": "612.0x792.0:0-23", + "projects": [], + "publish_at": null, + "published_url": "", + "related_article": "", + "revision_control": false, + "slug": "change-assessment_w_mjw-letter", + "source": "", + "status": "success", + "title": "Change-assessment_w_MJW-letter", + "updated_at": "2023-11-20T16:02:35.301271Z", + "user": 100012 + } +]