From 294ec36cc36d4258ab770819a739578dec3d2401 Mon Sep 17 00:00:00 2001 From: Stevche Radevski Date: Fri, 31 May 2024 16:12:04 +0200 Subject: [PATCH] Move few test suites from api to http folder (#7558) * chore: Move api key tests to http folder * chore: Move some of the product category tests to http * chore: Move collection tests to http * chore: Remove unused database test --- .../api/__tests__/database/index.js | 56 ----------------- .../__tests__/api-key}/admin/api-key.spec.ts | 41 +++++------- .../collection/admin}/collections.ts | 4 +- .../admin/product-category.spec.ts | 63 +++++++++++-------- 4 files changed, 53 insertions(+), 111 deletions(-) delete mode 100644 integration-tests/api/__tests__/database/index.js rename integration-tests/{api/__tests__ => http/__tests__/api-key}/admin/api-key.spec.ts (93%) rename integration-tests/{api/__tests__/store => http/__tests__/collection/admin}/collections.ts (96%) rename integration-tests/{api/__tests__ => http/__tests__/product-category}/admin/product-category.spec.ts (60%) diff --git a/integration-tests/api/__tests__/database/index.js b/integration-tests/api/__tests__/database/index.js deleted file mode 100644 index 1e3dcc3bff..0000000000 --- a/integration-tests/api/__tests__/database/index.js +++ /dev/null @@ -1,56 +0,0 @@ -const path = require("path") - -const setupServer = require("../../../environment-helpers/setup-server") -const { initDb, useDb } = require("../../../environment-helpers/use-db") - -jest.setTimeout(30000) - -describe("Database options", () => { - let medusaProcess - let dbConnection - - beforeAll(async () => { - const cwd = path.resolve(path.join(__dirname, "..", "..")) - dbConnection = await initDb({ - cwd, - databaseExtra: { idle_in_transaction_session_timeout: 1000 }, - }) - medusaProcess = await setupServer({ cwd }) - }) - - afterAll(async () => { - const db = useDb() - await db.shutdown() - medusaProcess.kill() - }) - - describe("idle_in_transaction_session_timeout", () => { - it("Resolves successfully", async () => { - expect.assertions(1) - - let queryRunner - - try { - queryRunner = dbConnection.createQueryRunner() - await queryRunner.connect() - - await queryRunner.startTransaction() - - await queryRunner.query(`select * from product`) - - // Idle time is 1000 ms so this should timeout - await new Promise((resolve) => - setTimeout(() => resolve(undefined), 2000) - ) - - // This query should fail with a QueryRunnerAlreadyReleasedError - await queryRunner.commitTransaction() - } catch (error) { - // Query runner will be released in case idle_in_transaction_session_timeout kicks in - expect(error?.type || error.name).toEqual( - "QueryRunnerAlreadyReleasedError" - ) - } - }) - }) -}) diff --git a/integration-tests/api/__tests__/admin/api-key.spec.ts b/integration-tests/http/__tests__/api-key/admin/api-key.spec.ts similarity index 93% rename from integration-tests/api/__tests__/admin/api-key.spec.ts rename to integration-tests/http/__tests__/api-key/admin/api-key.spec.ts index 2d5bf251da..b193127340 100644 --- a/integration-tests/api/__tests__/admin/api-key.spec.ts +++ b/integration-tests/http/__tests__/api-key/admin/api-key.spec.ts @@ -1,26 +1,18 @@ import { ApiKeyType, ContainerRegistrationKeys } from "@medusajs/utils" import { medusaIntegrationTestRunner } from "medusa-test-utils" -import { createAdminUser } from "../../../helpers/create-admin-user" +import { + adminHeaders, + createAdminUser, +} from "../../../../helpers/create-admin-user" jest.setTimeout(50000) -const env = { MEDUSA_FF_MEDUSA_V2: true } -const adminHeaders = { - headers: { "x-medusa-access-token": "test_token" }, -} - medusaIntegrationTestRunner({ - env, + env: {}, testSuite: ({ dbConnection, getContainer, api }) => { describe("API Keys - Admin", () => { - let container - - beforeAll(async () => { - container = getContainer() - }) - beforeEach(async () => { - await createAdminUser(dbConnection, adminHeaders, container) + await createAdminUser(dbConnection, adminHeaders, getContainer()) }) it("should correctly implement the entire lifecycle of an api key", async () => { @@ -70,7 +62,7 @@ medusaIntegrationTestRunner({ expect(revoked.data.api_key).toEqual( expect.objectContaining({ id: created.data.api_key.id, - revoked_by: "admin_user", + revoked_by: expect.stringMatching(/^user_*/), }) ) expect(revoked.data.api_key.revoked_at).toBeTruthy() @@ -432,19 +424,14 @@ medusaIntegrationTestRunner({ expect(deletedApiKeys.data.api_keys).toHaveLength(0) - const remoteQuery = container.resolve( - ContainerRegistrationKeys.REMOTE_QUERY - ) + const fetchedSalesChannel = ( + await api.get( + `/admin/sales-channels/${sales_channel.id}?fields=*publishable_api_keys`, + adminHeaders + ) + ).data.sales_channel - // Not the prettiest, but an easy way to check if the link was removed - const channels = await remoteQuery({ - publishable_api_key_sales_channels: { - __args: { sales_channel_id: [sales_channel.id] }, - fields: ["id", "sales_channel_id", "publishable_key_id"], - }, - }) - - expect(channels).toHaveLength(0) + expect(fetchedSalesChannel.publishable_api_keys).toEqual([]) }) }) }, diff --git a/integration-tests/api/__tests__/store/collections.ts b/integration-tests/http/__tests__/collection/admin/collections.ts similarity index 96% rename from integration-tests/api/__tests__/store/collections.ts rename to integration-tests/http/__tests__/collection/admin/collections.ts index 0dd40404cf..8431d4b551 100644 --- a/integration-tests/api/__tests__/store/collections.ts +++ b/integration-tests/http/__tests__/collection/admin/collections.ts @@ -2,12 +2,12 @@ import { medusaIntegrationTestRunner } from "medusa-test-utils" import { createAdminUser, adminHeaders, -} from "../../../helpers/create-admin-user" +} from "../../../../helpers/create-admin-user" jest.setTimeout(30000) medusaIntegrationTestRunner({ - env: { MEDUSA_FF_PRODUCT_CATEGORIES: true }, + env: {}, testSuite: ({ dbConnection, getContainer, api }) => { let baseCollection let baseCollection1 diff --git a/integration-tests/api/__tests__/admin/product-category.spec.ts b/integration-tests/http/__tests__/product-category/admin/product-category.spec.ts similarity index 60% rename from integration-tests/api/__tests__/admin/product-category.spec.ts rename to integration-tests/http/__tests__/product-category/admin/product-category.spec.ts index c7173b6b1e..9d893ae524 100644 --- a/integration-tests/api/__tests__/admin/product-category.spec.ts +++ b/integration-tests/http/__tests__/product-category/admin/product-category.spec.ts @@ -1,40 +1,51 @@ import { medusaIntegrationTestRunner } from "medusa-test-utils" -import { createAdminUser } from "../../../helpers/create-admin-user" +import { + adminHeaders, + createAdminUser, +} from "../../../../helpers/create-admin-user" jest.setTimeout(50000) -const env = { MEDUSA_FF_MEDUSA_V2: true } -const adminHeaders = { - headers: { "x-medusa-access-token": "test_token" }, -} - medusaIntegrationTestRunner({ - env, + env: {}, testSuite: ({ dbConnection, getContainer, api }) => { describe("Product categories - Admin", () => { - let container - - beforeAll(async () => { - container = getContainer() - }) - beforeEach(async () => { - await createAdminUser(dbConnection, adminHeaders, container) + await createAdminUser(dbConnection, adminHeaders, getContainer()) }) it("should correctly query categories by q", async () => { - const productService = container.resolve("productModuleService") - const categoryOne = await productService.createCategory({ - name: "Category One", - }) - const categoryTwo = await productService.createCategory({ - name: "Category Two", - parent_category_id: categoryOne.id, - }) - const categoryThree = await productService.createCategory({ - name: "Category Three", - parent_category_id: categoryTwo.id, - }) + const categoryOne = ( + await api.post( + "/admin/product-categories", + { + name: "Category One", + }, + adminHeaders + ) + ).data.product_category + + const categoryTwo = ( + await api.post( + "/admin/product-categories", + { + name: "Category Two", + parent_category_id: categoryOne.id, + }, + adminHeaders + ) + ).data.product_category + + const categoryThree = ( + await api.post( + "/admin/product-categories", + { + name: "Category Three", + parent_category_id: categoryTwo.id, + }, + adminHeaders + ) + ).data.product_category const response = await api.get( "/admin/product-categories?q=Category",