From e4668065115a80f6780670a900238dd5f01ee374 Mon Sep 17 00:00:00 2001 From: Patryk Andrzejewski Date: Wed, 16 Oct 2024 10:28:29 +0200 Subject: [PATCH] Login before test 320 (#5219) * Login before tests (#5196) * Login before tests * Changeset * Composite actions * Composite actions * Apply in other workflows * Apply in other workflows * Don't run export tests on PRs (#5206) * Revert Don't run export tests on PRs (#5206) (#5208) This reverts commit 3ec3cf35ffe35fa14b62a75836ab52b5130ca285. * Apply in other workflows * Apply in other workflows * Apply in other workflows * Apply in other workflows * Apply in other workflows * Apply in other workflows --------- Co-authored-by: Wojciech Mista * Add deps installation (#5213) * Port * Missing encode pass (#5218) * Add missing encode pass * Add missing encode pass * Update .changeset/two-zebras-check.md Co-authored-by: Mikail <6186720+NyanKiyoshi@users.noreply.github.com> --------- Co-authored-by: Mikail <6186720+NyanKiyoshi@users.noreply.github.com> * Add missing encode pass --------- Co-authored-by: Wojciech Mista Co-authored-by: Mikail <6186720+NyanKiyoshi@users.noreply.github.com> --- .eslintrc.json | 2 +- .github/actions/prepare-accounts/action.yml | 48 +++++ .github/actions/restore-accounts/action.yml | 32 +++ .github/actions/run-pw-tests/action.yml | 18 +- .github/workflows/pr-automation.yml | 18 +- .github/workflows/run-test-cron.yml | 34 +++- .github/workflows/run-test-manual.yml | 35 +++- .github/workflows/run-tests-on-release.yml | 34 ++++ playwright/auth.js | 191 ++++++++++++++++++ playwright/data/userPermissions.ts | 4 +- playwright/tests/apps.spec.ts | 5 +- playwright/tests/attributes.spec.ts | 5 +- playwright/tests/categories.spec.ts | 5 +- playwright/tests/channels.spec.ts | 5 +- playwright/tests/collections.spec.ts | 5 +- playwright/tests/customers.spec.ts | 5 +- playwright/tests/discounts.spec.ts | 5 +- playwright/tests/giftCards.spec.ts | 5 +- playwright/tests/home.spec.ts | 6 +- playwright/tests/navigation.spec.ts | 5 +- playwright/tests/orders.spec.ts | 5 +- playwright/tests/pageTypes.spec.ts | 5 +- playwright/tests/permissionGroup.spec.ts | 5 +- playwright/tests/product.spec.ts | 5 +- playwright/tests/productTypes.spec.ts | 5 +- playwright/tests/shippingMethods.spec.ts | 5 +- .../tests/singlePermissions/apps.spec.ts | 5 +- .../channelsWebhooks.spec.ts | 5 +- .../singlePermissions/contentPage.spec.ts | 5 +- .../tests/singlePermissions/customer.spec.ts | 6 +- .../tests/singlePermissions/discount.spec.ts | 5 +- .../tests/singlePermissions/orders.spec.ts | 5 +- .../tests/singlePermissions/plugins.spec.ts | 5 +- .../tests/singlePermissions/product.spec.ts | 5 +- .../singlePermissions/productType.spec.ts | 6 +- .../readonlyAppAccess.spec.ts | 6 +- .../settingsConfiguration.spec.ts | 6 +- .../tests/singlePermissions/shipping.spec.ts | 6 +- .../singlePermissions/staffMembers.spec.ts | 5 +- .../singlePermissions/translations.spec.ts | 6 +- playwright/tests/siteSettings.spec.ts | 5 +- playwright/tests/staffMembers.spec.ts | 5 +- playwright/tests/taxes.spec.ts | 5 +- playwright/tests/translations.spec.ts | 5 +- playwright/tests/vouchers.spec.ts | 5 +- playwright/tests/warehouse.spec.ts | 5 +- playwright/utils/auth.ts | 76 +++++++ playwright/utils/testWithPermission.ts | 18 ++ 48 files changed, 618 insertions(+), 79 deletions(-) create mode 100644 .github/actions/prepare-accounts/action.yml create mode 100644 .github/actions/restore-accounts/action.yml create mode 100644 playwright/auth.js create mode 100644 playwright/utils/auth.ts create mode 100644 playwright/utils/testWithPermission.ts diff --git a/.eslintrc.json b/.eslintrc.json index c4a5baf31d5..f676344cfc7 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -105,5 +105,5 @@ "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-unused-vars": "off" }, - "ignorePatterns": ["node_modules/", "**/types/**/*", "type-policies.ts"] + "ignorePatterns": ["node_modules/", "**/types/**/*", "type-policies.ts", "playwright/auth.js"] } diff --git a/.github/actions/prepare-accounts/action.yml b/.github/actions/prepare-accounts/action.yml new file mode 100644 index 00000000000..af864f038a4 --- /dev/null +++ b/.github/actions/prepare-accounts/action.yml @@ -0,0 +1,48 @@ +name: Prepare accounts +description: Prepare accounts that are used by Playwright +inputs: + BASE_URL: + description: "Dashboard base url" + required: true + API_URL: + description: "API url" + required: true + E2E_USER_NAME: + description: "Username for e2e tests" + required: true + E2E_USER_PASSWORD: + description: "Password for e2e tests" + required: true + E2E_PERMISSIONS_USERS_PASSWORD: + description: "Permissions user password for e2e tests" + required: true + E2E_ENCODE_PASS: + description: "Password for encoding credentials" + required: true + +outputs: + ACCOUNTS: + description: "Encrypted accounts" + value: ${{ steps.accounts.outputs.ACCOUNTS }} + + +runs: + using: "composite" + steps: + - uses: actions/setup-node@v3 + with: + node-version-file: ".nvmrc" + + - name: Prepare accounts + shell: bash + id: accounts + env: + BASE_URL: ${{ inputs.BASE_URL }} + API_URL: ${{ inputs.API_URL }} + E2E_USER_NAME: ${{ inputs.E2E_USER_NAME }} + E2E_USER_PASSWORD: ${{ inputs.E2E_USER_PASSWORD }} + E2E_ENCODE_PASS: ${{ inputs.E2E_ENCODE_PASS }} + E2E_PERMISSIONS_USERS_PASSWORD: ${{ inputs.E2E_PERMISSIONS_USERS_PASSWORD }} + run: | + ACCOUNTS=$(node playwright/auth.js login) + echo "ACCOUNTS=${ACCOUNTS}" >> "$GITHUB_OUTPUT" diff --git a/.github/actions/restore-accounts/action.yml b/.github/actions/restore-accounts/action.yml new file mode 100644 index 00000000000..d05958214ef --- /dev/null +++ b/.github/actions/restore-accounts/action.yml @@ -0,0 +1,32 @@ +name: Restore accounts +description: Restore accounts to be consumed by Playwright +inputs: + ACCOUNTS: + description: "Accounts encrypted string" + required: true + E2E_ENCODE_PASS: + description: "Password for encoding credentials" + required: true + BASE_URL: + description: "Dashboard base url" + required: true + API_URL: + description: "API url" + required: true + +runs: + using: "composite" + steps: + - uses: actions/setup-node@v3 + with: + node-version-file: ".nvmrc" + + - name: Restore accounts + shell: bash + env: + E2E_ENCODE_PASS: ${{ inputs.E2E_ENCODE_PASS }} + ACCOUNTS: ${{ inputs.ACCOUNTS }} + API_URL: ${{ inputs.API_URL }} + BASE_URL: ${{ inputs.BASE_URL }} + run: | + node playwright/auth.js restore "$ACCOUNTS" diff --git a/.github/actions/run-pw-tests/action.yml b/.github/actions/run-pw-tests/action.yml index 184a309838d..f20b108fdf0 100644 --- a/.github/actions/run-pw-tests/action.yml +++ b/.github/actions/run-pw-tests/action.yml @@ -32,9 +32,15 @@ inputs: description: "Playwright retries" required: true PROJECT: - description: "Project name form playwright config" + description: "Project name from playwright config" required: false default: "e2e" + ACCOUNTS: + description: "Accounts hash" + required: true + E2E_ENCODE_PASS: + description: "Password for encoding credentials" + required: true runs: using: "composite" @@ -47,6 +53,14 @@ runs: shell: bash run: npm ci + - name: Restore accounts + uses: ./.github/actions/restore-accounts + with: + BASE_URL: ${{ inputs.BASE_URL }} + API_URL: ${{ inputs.API_URL }} + ACCOUNTS: ${{ inputs.ACCOUNTS }} + E2E_ENCODE_PASS: ${{ inputs.E2E_ENCODE_PASS }} + - name: Install Playwright Browsers shell: bash run: npx playwright install --with-deps @@ -82,4 +96,4 @@ runs: with: name: all-blob-reports path: blob-report - retention-days: 1 + retention-days: 1 \ No newline at end of file diff --git a/.github/workflows/pr-automation.yml b/.github/workflows/pr-automation.yml index d6127a4588e..83fb0e809fd 100644 --- a/.github/workflows/pr-automation.yml +++ b/.github/workflows/pr-automation.yml @@ -71,6 +71,8 @@ jobs: deploy-dashboard: if: github.event.pull_request.head.repo.full_name == 'saleor/saleor-dashboard' + outputs: + ACCOUNTS: ${{ steps.accounts.outputs.ACCOUNTS }} runs-on: ubuntu-22.04 needs: initialize-cloud permissions: @@ -151,6 +153,17 @@ jobs: deployment_id: ${{ steps.deployment.outputs.deployment_id }} env: ${{ needs.initialize-cloud.outputs.POOL_NAME }} + - name: Prepare accounts + id: accounts + uses: ./.github/actions/prepare-accounts + with: + BASE_URL: ${{ needs.initialize-cloud.outputs.BASE_URL }} + API_URL: ${{ needs.initialize-cloud.outputs.API_URL }} + E2E_USER_NAME: ${{ secrets.E2E_USER_NAME }} + E2E_USER_PASSWORD: ${{ secrets.E2E_USER_PASSWORD }} + E2E_ENCODE_PASS: ${{ secrets.E2E_ENCODE_PASS }} + E2E_PERMISSIONS_USERS_PASSWORD: ${{ secrets.E2E_PERMISSIONS_USERS_PASSWORD }} + run-tests: if: ${{ contains(github.event.pull_request.labels.*.name, 'run pw-e2e') }} runs-on: ubuntu-22.04 @@ -159,6 +172,7 @@ jobs: fail-fast: false matrix: shard: ${{ fromJson(needs.initialize-cloud.outputs.SHARD_MATRIX) }} + steps: - uses: actions/checkout@v4 @@ -174,6 +188,8 @@ jobs: MAILPITURL: ${{ secrets.MAILPITURL }} PW_WORKERS: ${{ vars.PW_WORKERS }} PW_RETRIES: ${{ vars.PW_RETRIES }} + ACCOUNTS: ${{ needs.deploy-dashboard.outputs.ACCOUNTS }} + E2E_ENCODE_PASS: ${{ secrets.E2E_ENCODE_PASS }} merge-reports: if: "!cancelled() && contains(github.event.pull_request.labels.*.name, 'run pw-e2e')" @@ -184,4 +200,4 @@ jobs: - uses: actions/checkout@v4 - name: Merge playwright reports - uses: ./.github/actions/merge-pw-reports + uses: ./.github/actions/merge-pw-reports \ No newline at end of file diff --git a/.github/workflows/run-test-cron.yml b/.github/workflows/run-test-cron.yml index c9d6c455a27..c0b0ae86412 100644 --- a/.github/workflows/run-test-cron.yml +++ b/.github/workflows/run-test-cron.yml @@ -20,10 +20,29 @@ jobs: BACKUP_ID: ${{ steps.cloud_variables.outputs.BACKUP_ID }} BACKUP_VER: ${{ steps.cloud_variables.outputs.BACKUP_VER }} BACKUP_NAME: ${{ steps.cloud_variables.outputs.BACKUP_NAME }} + ACCOUNTS: ${{ steps.accounts.outputs.ACCOUNTS }} steps: - uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v3 with: - sparse-checkout: ./.github/actions + node-version-file: ".nvmrc" + + - name: Cache node modules + uses: actions/cache@v3 + env: + cache-name: cache-node-modules + with: + path: ~/.npm + key: ${{ runner.os }}-qa-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-qa-${{ env.cache-name }}- + ${{ runner.os }}-qa- + ${{ runner.os }}- + + - name: Install deps + run: npm ci - name: Generate variables id: cloud_variables @@ -44,6 +63,17 @@ jobs: POOL_INSTANCE: ${{ steps.cloud_variables.outputs.POOL_INSTANCE }} BACKUP_ID: ${{ steps.cloud_variables.outputs.BACKUP_ID }} + - name: Prepare accounts + id: accounts + uses: ./.github/actions/prepare-accounts + with: + BASE_URL: ${{ needs.cloud_variables.outputs.BASE_URL }} + API_URL: ${{ needs.cloud_variables.outputs.API_URL }} + E2E_USER_NAME: ${{ secrets.E2E_USER_NAME }} + E2E_USER_PASSWORD: ${{ secrets.E2E_USER_PASSWORD }} + E2E_ENCODE_PASS: ${{ secrets.E2E_ENCODE_PASS }} + E2E_PERMISSIONS_USERS_PASSWORD: ${{ secrets.E2E_PERMISSIONS_USERS_PASSWORD }} + create-run-on-testmo: runs-on: ubuntu-22.04 needs: initialize-cloud @@ -76,6 +106,8 @@ jobs: API_URL: ${{ needs.initialize-cloud.outputs.API_URL }} E2E_USER_NAME: ${{ secrets.E2E_USER_NAME }} E2E_USER_PASSWORD: ${{ secrets.E2E_USER_PASSWORD }} + E2E_ENCODE_PASS: ${{ secrets.E2E_ENCODE_PASS }} + ACCOUNTS: ${{ needs.initialize-cloud.outputs.ACCOUNTS }} E2E_PERMISSIONS_USERS_PASSWORD: ${{ secrets.E2E_PERMISSIONS_USERS_PASSWORD }} MAILPITURL: ${{ secrets.MAILPITURL }} PW_WORKERS: ${{ vars.PW_WORKERS }} diff --git a/.github/workflows/run-test-manual.yml b/.github/workflows/run-test-manual.yml index dabf6b8d4ce..dbd318a1c24 100644 --- a/.github/workflows/run-test-manual.yml +++ b/.github/workflows/run-test-manual.yml @@ -17,10 +17,29 @@ jobs: BACKUP_ID: ${{ steps.cloud_variables.outputs.BACKUP_ID }} BACKUP_VER: ${{ steps.cloud_variables.outputs.BACKUP_VER }} BACKUP_NAME: ${{ steps.cloud_variables.outputs.BACKUP_NAME }} + ACCOUNTS: ${{ steps.accounts.outputs.ACCOUNTS }} steps: - uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version-file: ".nvmrc" + + - name: Cache node modules + uses: actions/cache@v3 + env: + cache-name: cache-node-modules with: - sparse-checkout: ./.github/actions + path: ~/.npm + key: ${{ runner.os }}-qa-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-qa-${{ env.cache-name }}- + ${{ runner.os }}-qa- + ${{ runner.os }}- + + - name: Install deps + run: npm ci - name: Set variables mode id: set_variables_mode @@ -51,6 +70,18 @@ jobs: POOL_INSTANCE: ${{ steps.cloud_variables.outputs.POOL_INSTANCE }} BACKUP_ID: ${{ steps.cloud_variables.outputs.BACKUP_ID }} + - name: Prepare accounts + id: accounts + uses: ./.github/actions/prepare-accounts + with: + BASE_URL: ${{ steps.cloud_variables.outputs.BASE_URL }} + API_URL: ${{ steps.cloud_variables.outputs.API_URL }} + E2E_USER_NAME: ${{ secrets.E2E_USER_NAME }} + E2E_USER_PASSWORD: ${{ secrets.E2E_USER_PASSWORD }} + E2E_ENCODE_PASS: ${{ secrets.E2E_ENCODE_PASS }} + E2E_PERMISSIONS_USERS_PASSWORD: ${{ secrets.E2E_PERMISSIONS_USERS_PASSWORD }} + + create-run-on-testmo: runs-on: ubuntu-22.04 needs: initialize-cloud @@ -84,6 +115,8 @@ jobs: E2E_USER_NAME: ${{ secrets.E2E_USER_NAME }} E2E_USER_PASSWORD: ${{ secrets.E2E_USER_PASSWORD }} E2E_PERMISSIONS_USERS_PASSWORD: ${{ secrets.E2E_PERMISSIONS_USERS_PASSWORD }} + E2E_ENCODE_PASS: ${{ secrets.E2E_ENCODE_PASS }} + ACCOUNTS: ${{ needs.initialize-cloud.outputs.ACCOUNTS }} MAILPITURL: ${{ secrets.MAILPITURL }} PW_WORKERS: ${{ vars.PW_WORKERS }} PW_RETRIES: ${{ vars.PW_RETRIES }} diff --git a/.github/workflows/run-tests-on-release.yml b/.github/workflows/run-tests-on-release.yml index 5eee30ca1ea..f227a3c1263 100644 --- a/.github/workflows/run-tests-on-release.yml +++ b/.github/workflows/run-tests-on-release.yml @@ -61,9 +61,30 @@ jobs: CHECK_ID: ${{ steps.create-check.outputs.CHECK_ID }} FRAMEWORK: ${{ steps.check-framework.outputs.result }} IS_OLD_VERSION: ${{ steps.get-environment-variables.outputs.IS_OLD_VERSION }} + ACCOUNTS: ${{ steps.accounts.outputs.ACCOUNTS }} steps: - uses: actions/checkout@v4 + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version-file: ".nvmrc" + + - name: Cache node modules + uses: actions/cache@v3 + env: + cache-name: cache-node-modules + with: + path: ~/.npm + key: ${{ runner.os }}-qa-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-qa-${{ env.cache-name }}- + ${{ runner.os }}-qa- + ${{ runner.os }}- + + - name: Install deps + run: npm ci + - id: check-framework uses: actions/github-script@v7 env: @@ -127,6 +148,17 @@ jobs: --repo_token "$REPO_TOKEN" \ --project "$PROJECT" + - name: Prepare accounts + id: accounts + uses: ./.github/actions/prepare-accounts + with: + BASE_URL: ${{ steps.cloud_variables.outputs.BASE_URL }} + API_URL: ${{ steps.cloud_variables.outputs.API_URL }} + E2E_USER_NAME: ${{ secrets.E2E_USER_NAME }} + E2E_USER_PASSWORD: ${{ secrets.E2E_USER_PASSWORD }} + E2E_ENCODE_PASS: ${{ secrets.E2E_ENCODE_PASS }} + E2E_PERMISSIONS_USERS_PASSWORD: ${{ secrets.E2E_PERMISSIONS_USERS_PASSWORD }} + run-cy-tests: if: needs.add-check-and-prepare-instance.outputs.FRAMEWORK == '"cypress"' needs: add-check-and-prepare-instance @@ -211,6 +243,8 @@ jobs: E2E_USER_NAME: ${{ secrets.E2E_USER_NAME }} E2E_USER_PASSWORD: ${{ secrets.E2E_USER_PASSWORD }} E2E_PERMISSIONS_USERS_PASSWORD: ${{ secrets.E2E_PERMISSIONS_USERS_PASSWORD }} + E2E_ENCODE_PASS: ${{ secrets.E2E_ENCODE_PASS }} + ACCOUNTS: ${{ needs.add-check-and-prepare-instance.outputs.ACCOUNTS }} MAILPITURL: ${{ secrets.MAILPITURL }} URL_TO_RUN: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} PW_WORKERS: ${{ vars.PW_WORKERS }} diff --git a/playwright/auth.js b/playwright/auth.js new file mode 100644 index 00000000000..de6310b1aff --- /dev/null +++ b/playwright/auth.js @@ -0,0 +1,191 @@ +/* eslint-disable */ + +const { request } = require("@playwright/test"); +const dotenv = require("dotenv"); +const process = require("process"); +const crypto = require("crypto"); +const path = require("path"); +const fs = require("fs"); + +dotenv.config(); + +const algorithm = 'aes-256-ctr'; + +const PERMISSIONS = [ + "admin", + "app", + "discount", + "order", + "channel", + "customer", + "giftCard", + "page", + "plugin", + "productTypeAndAttribute", + "product", + "settings", + "staff", + "shipping", + "translations", +] + +const ACCOUNT_EMAILS = { + channel: "channel.manager@example.com", + shipping: "shipping.manager@example.com", + giftCard: "gift.card.manager@example.com", + app: "app.manager@example.com", + settings: "setting.manager@example.com", + page: "page.manager@example.com", + order: "order.manager@example.com", + translations: "translation.manager@example.com", + staff: "staff.manager@example.com", + customer: "user.manager@example.com", + productTypeAndAttribute: "product.type.and.attribute.manager@example.com", + discount: "discount.manager@example.com", + plugin: "plugin.manager@example.com", + product: "product.manager@example.com", +}; + +const createQuery = (email, password) => { + const query = ` + mutation TokenAuth($email: String!, $password: String!) { + tokenCreate(email: $email, password: $password) { + token + refreshToken + errors { + code + message + } + user { + id + } + } + } + ` + + const variables = { email, password } + + return { query, variables } +}; + + +const getEmailForPermission = (permission) => { + if (permission === "admin") { + return process.env.E2E_USER_NAME; + } + + return ACCOUNT_EMAILS[permission]; +}; + +const getPasswordForPermission = (permission) => { + if (permission === "admin") { + return process.env.E2E_USER_PASSWORD; + } + + return process.env.E2E_PERMISSIONS_USERS_PASSWORD; +}; + +const getAuthForPermission = async (permissionName) => { + const apiRequestContext = await request.newContext({ + baseURL: process.env.BASE_URL, + }); + + const email = getEmailForPermission(permissionName); + const password = getPasswordForPermission(permissionName); + + const response = await apiRequestContext.post(process.env.API_URL || "", { + data: createQuery(email, password), + }); + const resposnseObj = await response.json() + const { errors } = resposnseObj.data.tokenCreate + + if (errors && errors.length > 0) { + const errorMessages = errors + .map(e => e.message) + .join(", "); + + throw new Error(`Login failed for permission ${permissionName}: ${errorMessages}`); + } + + const loginJsonInfo = await apiRequestContext.storageState(); + + return loginJsonInfo +} + +const encrypt = (password, text) => { + const key = Buffer.concat([Buffer.from(password), Buffer.alloc(32)], 32); + const iv = crypto.randomBytes(16); + const cipher = crypto.createCipheriv(algorithm, key, iv); + let encrypted = cipher.update(text); + encrypted = Buffer.concat([encrypted, cipher.final()]); + return iv.toString('hex') + encrypted.toString('hex'); +} + +const decrypt = (password, text) => { + const key = Buffer.concat([Buffer.from(password), Buffer.alloc(32)], 32); + const iv = Buffer.from(text.substring(0, 32), 'hex'); + const encryptedText = Buffer.from(text.substring(32), 'hex'); + const decipher = crypto.createDecipheriv(algorithm, key, iv); + let decrypted = decipher.update(encryptedText); + decrypted = Buffer.concat([decrypted, decipher.final()]); + return decrypted.toString(); +} + +(async () => { + const command = process.argv[2] + + if (command == "login") { + let authString = '' + + for (const permissionName of PERMISSIONS) { + + const auth = await getAuthForPermission(permissionName) + + + authString = `${authString}${JSON.stringify(auth)}|` + } + + const encodedString = encrypt(process.env.E2E_ENCODE_PASS, authString) + + process.stdout.write(encodedString) + } + + + if (command == "restore") { + const tempDir = path.join(__dirname, "../playwright/.auth"); + + if (!fs.existsSync(tempDir)) { + fs.mkdirSync(tempDir, { recursive: true }); + } + + + const encodedInput = process.argv[3] + console.log("Restoring...", encodedInput.length) + + + const decodedString = decrypt(process.env.E2E_ENCODE_PASS, encodedInput) + decodedString + .split("|") + .filter(Boolean) + .map(JSON.parse) + .map(obj => { + obj.origins.push({ + origin: process.env.BASE_URL, + localStorage: [ + { + name: "_saleorRefreshToken", + value: obj.cookies[0].value, + }, + ], + }); + + return obj + }) + .map(JSON.stringify) + .map((str, index) => ({ name: PERMISSIONS[index], str })) + .forEach(token => { + const storageStatePath = path.join(tempDir, `${token.name}.json`); + fs.writeFileSync(storageStatePath, token.str); + }) + } +})() \ No newline at end of file diff --git a/playwright/data/userPermissions.ts b/playwright/data/userPermissions.ts index bb7029c3463..d1e22ed74f4 100644 --- a/playwright/data/userPermissions.ts +++ b/playwright/data/userPermissions.ts @@ -32,7 +32,9 @@ export const USER_PERMISSION: UserPermissionType = { product: "product.manager@example.com", }; -export const permissions: Array = [ +export type UserPermission = keyof UserPermissionType; + +export const permissions: Array = [ "app", "discount", "order", diff --git a/playwright/tests/apps.spec.ts b/playwright/tests/apps.spec.ts index 139cf4ef223..46b6d343308 100644 --- a/playwright/tests/apps.spec.ts +++ b/playwright/tests/apps.spec.ts @@ -2,9 +2,10 @@ import { APPS } from "@data/e2eTestData"; import { AppInstallationPage } from "@pages/appInstallationPage"; import { AppPage } from "@pages/appPageThirdparty"; import { AppsPage } from "@pages/appsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let appsPage: AppsPage; let installationPage: AppInstallationPage; diff --git a/playwright/tests/attributes.spec.ts b/playwright/tests/attributes.spec.ts index 979ddba0835..f7a45ea78b1 100644 --- a/playwright/tests/attributes.spec.ts +++ b/playwright/tests/attributes.spec.ts @@ -1,10 +1,11 @@ import { ATTRIBUTES } from "@data/e2eTestData"; import { AttributesPage } from "@pages/attributesPage"; import { ConfigurationPage } from "@pages/configurationPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; import faker from "faker"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let attributesPage: AttributesPage; let configurationPage: ConfigurationPage; diff --git a/playwright/tests/categories.spec.ts b/playwright/tests/categories.spec.ts index 6a80660058a..157ec65fc5d 100644 --- a/playwright/tests/categories.spec.ts +++ b/playwright/tests/categories.spec.ts @@ -1,8 +1,9 @@ import { CATEGORIES } from "@data/e2eTestData"; import { CategoriesPage } from "@pages/categoriesPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let categoriesPage: CategoriesPage; diff --git a/playwright/tests/channels.spec.ts b/playwright/tests/channels.spec.ts index 727d68db732..d8fb7a94491 100644 --- a/playwright/tests/channels.spec.ts +++ b/playwright/tests/channels.spec.ts @@ -1,9 +1,10 @@ import { CHANNELS } from "@data/e2eTestData"; import { ChannelPage } from "@pages/channelsPage"; import { ConfigurationPage } from "@pages/configurationPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json", locale: "en" }); +test.use({ permissionName: "admin" }); let configurationPage: ConfigurationPage; let channelPage: ChannelPage; diff --git a/playwright/tests/collections.spec.ts b/playwright/tests/collections.spec.ts index dfc8b404284..9a56d79bb0b 100644 --- a/playwright/tests/collections.spec.ts +++ b/playwright/tests/collections.spec.ts @@ -1,8 +1,9 @@ import { COLLECTIONS } from "@data/e2eTestData"; import { CollectionsPage } from "@pages/collectionsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let collectionsPage: CollectionsPage; diff --git a/playwright/tests/customers.spec.ts b/playwright/tests/customers.spec.ts index 0f6a70427ff..f3310f29623 100644 --- a/playwright/tests/customers.spec.ts +++ b/playwright/tests/customers.spec.ts @@ -6,10 +6,11 @@ import { AddAddressDialog } from "@pages/dialogs/addAddressDialog"; import { DeleteAddressDialog } from "@pages/dialogs/deleteAddressDialog"; import { AddressForm } from "@pages/forms/addressForm"; import { GiftCardsPage } from "@pages/giftCardsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; import faker from "faker"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let customersPage: CustomersPage; let giftCardsPage: GiftCardsPage; diff --git a/playwright/tests/discounts.spec.ts b/playwright/tests/discounts.spec.ts index c731a626bb1..3bcd8ae8175 100644 --- a/playwright/tests/discounts.spec.ts +++ b/playwright/tests/discounts.spec.ts @@ -1,9 +1,10 @@ import { CATEGORIES, CHANNELS, COLLECTIONS, DISCOUNTS, PRODUCTS } from "@data/e2eTestData"; import { DiscountsPage } from "@pages/discountsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; import faker from "faker"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let discounts: DiscountsPage; diff --git a/playwright/tests/giftCards.spec.ts b/playwright/tests/giftCards.spec.ts index 357be0c43f3..40db2157b49 100644 --- a/playwright/tests/giftCards.spec.ts +++ b/playwright/tests/giftCards.spec.ts @@ -1,9 +1,10 @@ import { MailpitService } from "@api/mailpit"; import { GIFT_CARDS } from "@data/e2eTestData"; import { GiftCardsPage } from "@pages/giftCardsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let giftCardsPage: GiftCardsPage; let mailpitService: MailpitService; diff --git a/playwright/tests/home.spec.ts b/playwright/tests/home.spec.ts index 2c8449045a5..609c27c5122 100644 --- a/playwright/tests/home.spec.ts +++ b/playwright/tests/home.spec.ts @@ -1,8 +1,10 @@ import { URL_LIST } from "@data/url"; import { HomePage } from "@pages/homePage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; + +test.use({ permissionName: "admin" }); -test.use({ storageState: "./playwright/.auth/admin.json" }); test("TC: SALEOR_29 Correct information on dashboard home page @e2e", async ({ page }) => { const homePage = new HomePage(page); diff --git a/playwright/tests/navigation.spec.ts b/playwright/tests/navigation.spec.ts index b676f701e7a..3f301517dd9 100644 --- a/playwright/tests/navigation.spec.ts +++ b/playwright/tests/navigation.spec.ts @@ -3,10 +3,11 @@ import { ConfigurationPage } from "@pages/configurationPage"; import { AddNavigationMenuItemDialog } from "@pages/dialogs/addNavigationMenuItemDialog"; import { NavigationDetailsPage } from "@pages/navigationDetailsPage"; import { NavigationPage } from "@pages/navigationPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; import faker from "faker"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let navigation: NavigationPage; let navigationDetailsPage: NavigationDetailsPage; diff --git a/playwright/tests/orders.spec.ts b/playwright/tests/orders.spec.ts index a59a31f74ae..91447165ee0 100644 --- a/playwright/tests/orders.spec.ts +++ b/playwright/tests/orders.spec.ts @@ -7,10 +7,11 @@ import { AddressForm } from "@pages/forms/addressForm"; import { FulfillmentPage } from "@pages/fulfillmentPage"; import { OrdersPage } from "@pages/ordersPage"; import { RefundPage } from "@pages/refundPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; import * as faker from "faker"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let ordersPage: OrdersPage; let draftOrdersPage: DraftOrdersPage; diff --git a/playwright/tests/pageTypes.spec.ts b/playwright/tests/pageTypes.spec.ts index d7b8f4972b9..daa86eef67e 100644 --- a/playwright/tests/pageTypes.spec.ts +++ b/playwright/tests/pageTypes.spec.ts @@ -1,9 +1,10 @@ import { ATTRIBUTES, PAGE_TYPES } from "@data/e2eTestData"; import { PageTypesPage } from "@pages/pageTypesPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; import * as faker from "faker"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); const pageTypeName = `e2e-page-type-${faker.datatype.number()}`; diff --git a/playwright/tests/permissionGroup.spec.ts b/playwright/tests/permissionGroup.spec.ts index 3a7a5a96b8f..b9c745347a1 100644 --- a/playwright/tests/permissionGroup.spec.ts +++ b/playwright/tests/permissionGroup.spec.ts @@ -4,10 +4,11 @@ import { AssignPermissionGroupMembersDialog } from "@pages/dialogs/assignPermiss import { UnassignPermissionGroupMembersDialog } from "@pages/dialogs/unassignPermissionGroupMembersDialog"; import { PermissionGroupDetailsPage } from "@pages/permissionGroupDetailsPage"; import { PermissionGroupsPage } from "@pages/permissionGroupsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; import faker from "faker"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let permissions: PermissionGroupsPage; let permissionDetails: PermissionGroupDetailsPage; diff --git a/playwright/tests/product.spec.ts b/playwright/tests/product.spec.ts index a24d156dc04..dfb41122e32 100644 --- a/playwright/tests/product.spec.ts +++ b/playwright/tests/product.spec.ts @@ -4,9 +4,10 @@ import { PRODUCTS } from "@data/e2eTestData"; import { ProductCreateDialog } from "@pages/dialogs/productCreateDialog"; import { ProductPage } from "@pages/productPage"; import { VariantsPage } from "@pages/variantsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let productPage: ProductPage; let productCreateDialog: ProductCreateDialog; diff --git a/playwright/tests/productTypes.spec.ts b/playwright/tests/productTypes.spec.ts index 261e3907dd6..2d02ae42dc5 100644 --- a/playwright/tests/productTypes.spec.ts +++ b/playwright/tests/productTypes.spec.ts @@ -1,9 +1,10 @@ import { PRODUCT_TYPES } from "@data/e2eTestData"; import { ProductTypePage } from "@pages/productTypePage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; import * as faker from "faker"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); const productTypeName = `e2e-product-type-${faker.datatype.number()}`; diff --git a/playwright/tests/shippingMethods.spec.ts b/playwright/tests/shippingMethods.spec.ts index a95a8bea6c3..b9212bf203f 100644 --- a/playwright/tests/shippingMethods.spec.ts +++ b/playwright/tests/shippingMethods.spec.ts @@ -1,9 +1,10 @@ import { CHANNELS, SHIPPING_METHODS, WAREHOUSES } from "@data/e2eTestData"; import { ShippingMethodsPage } from "@pages/shippingMethodsPage"; import { ShippingRatesPage } from "@pages/shippingRatesPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let shippingMethodsPage: ShippingMethodsPage; let shippingRatesPage: ShippingRatesPage; diff --git a/playwright/tests/singlePermissions/apps.spec.ts b/playwright/tests/singlePermissions/apps.spec.ts index efa1ab1567e..d06257134c9 100644 --- a/playwright/tests/singlePermissions/apps.spec.ts +++ b/playwright/tests/singlePermissions/apps.spec.ts @@ -1,9 +1,10 @@ import { AppsPage } from "@pages/appsPage"; import { HomePage } from "@pages/homePage"; import { MainMenuPage } from "@pages/mainMenuPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/app.json" }); +test.use({ permissionName: "app" }); let mainMenuPage: MainMenuPage; let appsPage: AppsPage; diff --git a/playwright/tests/singlePermissions/channelsWebhooks.spec.ts b/playwright/tests/singlePermissions/channelsWebhooks.spec.ts index 888910af582..906d180110c 100644 --- a/playwright/tests/singlePermissions/channelsWebhooks.spec.ts +++ b/playwright/tests/singlePermissions/channelsWebhooks.spec.ts @@ -3,9 +3,10 @@ import { ConfigurationPage } from "@pages/configurationPage"; import { HomePage } from "@pages/homePage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { WebhooksEventsPage } from "@pages/webhooksEventsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/channel.json" }); +test.use({ permissionName: "channel" }); let channelPage: ChannelPage; let mainMenuPage: MainMenuPage; diff --git a/playwright/tests/singlePermissions/contentPage.spec.ts b/playwright/tests/singlePermissions/contentPage.spec.ts index e0bd80af930..344be848dbe 100644 --- a/playwright/tests/singlePermissions/contentPage.spec.ts +++ b/playwright/tests/singlePermissions/contentPage.spec.ts @@ -4,9 +4,10 @@ import { ContentPage } from "@pages/contentPage"; import { HomePage } from "@pages/homePage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { PageTypesPage } from "@pages/pageTypesPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/page.json" }); +test.use({ permissionName: "page" }); let basePage: BasePage; let mainMenuPage: MainMenuPage; diff --git a/playwright/tests/singlePermissions/customer.spec.ts b/playwright/tests/singlePermissions/customer.spec.ts index 468645947aa..1bd88947fb2 100644 --- a/playwright/tests/singlePermissions/customer.spec.ts +++ b/playwright/tests/singlePermissions/customer.spec.ts @@ -2,9 +2,11 @@ import { URL_LIST } from "@data/url"; import { BasePage } from "@pages/basePage"; import { CustomersPage } from "@pages/customersPage"; import { MainMenuPage } from "@pages/mainMenuPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; + +test.use({ permissionName: "customer" }); -test.use({ storageState: "playwright/.auth/customer.json" }); test("TC: SALEOR_13 User should be able to navigate to customer list as a staff member using CUSTOMER permission @e2e", async ({ page, }) => { diff --git a/playwright/tests/singlePermissions/discount.spec.ts b/playwright/tests/singlePermissions/discount.spec.ts index 215981e3421..921e473c38e 100644 --- a/playwright/tests/singlePermissions/discount.spec.ts +++ b/playwright/tests/singlePermissions/discount.spec.ts @@ -3,9 +3,10 @@ import { DiscountsPage } from "@pages/discountsPage"; import { HomePage } from "@pages/homePage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { VouchersPage } from "@pages/vouchersPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/discount.json" }); +test.use({ permissionName: "discount" }); let mainMenuPage: MainMenuPage; let home: HomePage; diff --git a/playwright/tests/singlePermissions/orders.spec.ts b/playwright/tests/singlePermissions/orders.spec.ts index f878143fd50..fd5e6cc8ae7 100644 --- a/playwright/tests/singlePermissions/orders.spec.ts +++ b/playwright/tests/singlePermissions/orders.spec.ts @@ -2,9 +2,10 @@ import { DraftOrdersPage } from "@pages/draftOrdersPage"; import { HomePage } from "@pages/homePage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { OrdersPage } from "@pages/ordersPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/order.json" }); +test.use({ permissionName: "order" }); let home: HomePage; let draftOrdersPage: DraftOrdersPage; diff --git a/playwright/tests/singlePermissions/plugins.spec.ts b/playwright/tests/singlePermissions/plugins.spec.ts index aae0c39df10..304a294b93a 100644 --- a/playwright/tests/singlePermissions/plugins.spec.ts +++ b/playwright/tests/singlePermissions/plugins.spec.ts @@ -1,9 +1,10 @@ import { ConfigurationPage } from "@pages/configurationPage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { PluginsPage } from "@pages/pluginsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/plugin.json" }); +test.use({ permissionName: "plugin" }); let configurationPage: ConfigurationPage; let mainMenuPage: MainMenuPage; diff --git a/playwright/tests/singlePermissions/product.spec.ts b/playwright/tests/singlePermissions/product.spec.ts index 09d1acba260..c3107fc3920 100644 --- a/playwright/tests/singlePermissions/product.spec.ts +++ b/playwright/tests/singlePermissions/product.spec.ts @@ -3,9 +3,10 @@ import { CollectionsPage } from "@pages/collectionsPage"; import { HomePage } from "@pages/homePage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { ProductPage } from "@pages/productPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/product.json" }); +test.use({ permissionName: "product" }); let home: HomePage; let mainMenuPage: MainMenuPage; diff --git a/playwright/tests/singlePermissions/productType.spec.ts b/playwright/tests/singlePermissions/productType.spec.ts index c08815159aa..1283dd874ac 100644 --- a/playwright/tests/singlePermissions/productType.spec.ts +++ b/playwright/tests/singlePermissions/productType.spec.ts @@ -2,9 +2,11 @@ import { URL_LIST } from "@data/url"; import { ConfigurationPage } from "@pages/configurationPage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { ProductTypePage } from "@pages/productTypePage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; + +test.use({ permissionName: "productTypeAndAttribute" }); -test.use({ storageState: "playwright/.auth/productTypeAndAttribute.json" }); test("TC: SALEOR_17 User should be able to navigate to product type list as a staff member using PRODUCT TYPE permission @e2e", async ({ page, }) => { diff --git a/playwright/tests/singlePermissions/readonlyAppAccess.spec.ts b/playwright/tests/singlePermissions/readonlyAppAccess.spec.ts index 049a00dcb4f..f95680a5e83 100644 --- a/playwright/tests/singlePermissions/readonlyAppAccess.spec.ts +++ b/playwright/tests/singlePermissions/readonlyAppAccess.spec.ts @@ -4,13 +4,15 @@ import { AppDetailsPage } from "@pages/appDetailsPage"; import { AppPage } from "@pages/appPageThirdparty"; import { AppsPage } from "@pages/appsPage"; import { MainMenuPage } from "@pages/mainMenuPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; const permissionToExclude = "app"; const permissionList = permissions.filter(item => item !== permissionToExclude); for (const permission of permissionList) { - test.use({ storageState: `playwright/.auth/${permission}.json` }); + test.use({ permissionName: permission }); + test(`TC: SALEOR_131 User with ${permission} permissions should have readonly access to Apps @e2e @apps`, async ({ page, }) => { diff --git a/playwright/tests/singlePermissions/settingsConfiguration.spec.ts b/playwright/tests/singlePermissions/settingsConfiguration.spec.ts index 6085397df4e..5a9a60adc7d 100644 --- a/playwright/tests/singlePermissions/settingsConfiguration.spec.ts +++ b/playwright/tests/singlePermissions/settingsConfiguration.spec.ts @@ -2,9 +2,11 @@ import { URL_LIST } from "@data/url"; import { ConfigurationPage } from "@pages/configurationPage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { SiteSettingsPage } from "@pages/siteSettingsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; + +test.use({ permissionName: "settings" }); -test.use({ storageState: "playwright/.auth/settings.json" }); test("TC: SALEOR_18 User should be able to navigate to configuration as a staff member using SETTINGS permission @e2e", async ({ page, }) => { diff --git a/playwright/tests/singlePermissions/shipping.spec.ts b/playwright/tests/singlePermissions/shipping.spec.ts index 1d493cf78fd..e08b32c1789 100644 --- a/playwright/tests/singlePermissions/shipping.spec.ts +++ b/playwright/tests/singlePermissions/shipping.spec.ts @@ -2,9 +2,11 @@ import { URL_LIST } from "@data/url"; import { ConfigurationPage } from "@pages/configurationPage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { ShippingMethodsPage } from "@pages/shippingMethodsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; + +test.use({ permissionName: "shipping" }); -test.use({ storageState: "playwright/.auth/shipping.json" }); test("TC: SALEOR_21 User should be able to navigate to shipping zones page as a staff member using SHIPPING permission @e2e", async ({ page, }) => { diff --git a/playwright/tests/singlePermissions/staffMembers.spec.ts b/playwright/tests/singlePermissions/staffMembers.spec.ts index 5f1df45fee8..3b6ed148ca4 100644 --- a/playwright/tests/singlePermissions/staffMembers.spec.ts +++ b/playwright/tests/singlePermissions/staffMembers.spec.ts @@ -2,9 +2,10 @@ import { ConfigurationPage } from "@pages/configurationPage"; import { MainMenuPage } from "@pages/mainMenuPage"; import { PermissionGroupsPage } from "@pages/permissionGroupsPage"; import { StaffMembersPage } from "@pages/staffMembersPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/staff.json" }); +test.use({ permissionName: "staff" }); let staffMembersPage: StaffMembersPage; let mainMenuPage: MainMenuPage; diff --git a/playwright/tests/singlePermissions/translations.spec.ts b/playwright/tests/singlePermissions/translations.spec.ts index ba5d44cb6ce..e44aa49c0c5 100644 --- a/playwright/tests/singlePermissions/translations.spec.ts +++ b/playwright/tests/singlePermissions/translations.spec.ts @@ -1,9 +1,11 @@ import { URL_LIST } from "@data/url"; import { BasePage } from "@pages/basePage"; import { MainMenuPage } from "@pages/mainMenuPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; + +test.use({ permissionName: "translations" }); -test.use({ storageState: "playwright/.auth/translations.json" }); test("TC: SALEOR_22 User should be able to navigate to translations page as a staff member using TRANSLATION permission @e2e", async ({ page, }) => { diff --git a/playwright/tests/siteSettings.spec.ts b/playwright/tests/siteSettings.spec.ts index 522b3a41f92..bf9ced4931a 100644 --- a/playwright/tests/siteSettings.spec.ts +++ b/playwright/tests/siteSettings.spec.ts @@ -1,8 +1,9 @@ import { SiteSettingsPage } from "@pages/siteSettingsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; import faker from "faker"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let siteSettingsPage: SiteSettingsPage; diff --git a/playwright/tests/staffMembers.spec.ts b/playwright/tests/staffMembers.spec.ts index 3f82142630a..d8dab6a7532 100644 --- a/playwright/tests/staffMembers.spec.ts +++ b/playwright/tests/staffMembers.spec.ts @@ -3,10 +3,11 @@ import { USERS } from "@data/e2eTestData"; import { ConfigurationPage } from "@pages/configurationPage"; import { PermissionGroupsPage } from "@pages/permissionGroupsPage"; import { StaffMembersPage } from "@pages/staffMembersPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; import faker from "faker"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let staffMembersPage: StaffMembersPage; let config: ConfigurationPage; diff --git a/playwright/tests/taxes.spec.ts b/playwright/tests/taxes.spec.ts index b2278e8f569..14ebe69ca0c 100644 --- a/playwright/tests/taxes.spec.ts +++ b/playwright/tests/taxes.spec.ts @@ -1,9 +1,10 @@ import { CHANNELS, COUNTRIES } from "@data/e2eTestData"; import { ConfigurationPage } from "@pages/configurationPage"; import { TaxesPage } from "@pages/taxesPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let configurationPage: ConfigurationPage; let taxesPage: TaxesPage; diff --git a/playwright/tests/translations.spec.ts b/playwright/tests/translations.spec.ts index 8397fba68e8..6ee1a7dce02 100644 --- a/playwright/tests/translations.spec.ts +++ b/playwright/tests/translations.spec.ts @@ -1,8 +1,9 @@ import { TRANSLATIONS } from "@data/e2eTestData"; import { TranslationsPage } from "@pages/translationsPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let translationsPage: TranslationsPage; diff --git a/playwright/tests/vouchers.spec.ts b/playwright/tests/vouchers.spec.ts index ee4ba0a2b71..9ad8d0be126 100644 --- a/playwright/tests/vouchers.spec.ts +++ b/playwright/tests/vouchers.spec.ts @@ -1,9 +1,10 @@ import { AVAILABILITY } from "@data/copy"; import { VOUCHERS } from "@data/e2eTestData"; import { VouchersPage } from "@pages/vouchersPage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let vouchersPage: VouchersPage; diff --git a/playwright/tests/warehouse.spec.ts b/playwright/tests/warehouse.spec.ts index 1e295efea83..8795c8c4668 100644 --- a/playwright/tests/warehouse.spec.ts +++ b/playwright/tests/warehouse.spec.ts @@ -1,8 +1,9 @@ import { WAREHOUSES } from "@data/e2eTestData"; import { WarehousePage } from "@pages/warehousePage"; -import { expect, test } from "@playwright/test"; +import { expect } from "@playwright/test"; +import { test } from "utils/testWithPermission"; -test.use({ storageState: "./playwright/.auth/admin.json" }); +test.use({ permissionName: "admin" }); let warehousePage: WarehousePage; diff --git a/playwright/utils/auth.ts b/playwright/utils/auth.ts new file mode 100644 index 00000000000..a90975082e5 --- /dev/null +++ b/playwright/utils/auth.ts @@ -0,0 +1,76 @@ +import { BasicApiService } from "@api/basics"; +import { USER_PERMISSION, UserPermission } from "@data/userPermissions"; +import { request } from "@playwright/test"; +import fs from "fs"; +import path from "path"; + +export const getStorageState = async (permission: UserPermission | "admin"): Promise => { + const tempDir = path.join(__dirname, "../.auth"); + const storageStatePath = path.join(tempDir, `${permission}.json`); + + if (!fs.existsSync(tempDir)) { + fs.mkdirSync(tempDir, { recursive: true }); + } + + if (!fs.existsSync(storageStatePath)) { + const apiRequestContext = await request.newContext({ + baseURL: process.env.BASE_URL!, + }); + + const basicApiService = new BasicApiService(apiRequestContext); + + const email = getEmailForPermission(permission); + const password = getPasswordForPermission(permission); + + console.log("getStorageState login external api", permission); + + try { + await basicApiService.logInUserViaApi({ email, password }); + } catch (error: unknown) { + console.log("Err", error); + + if (!(error instanceof Error)) { + throw new Error("An unknown error occurred while logging in the user via API"); + } + + const message = `logInUserViaApi failed for ${email}: ${error.message}`; + + console.error(message); + throw new Error(message); + } + + const loginJsonInfo = await apiRequestContext.storageState(); + + loginJsonInfo.origins.push({ + origin: process.env.BASE_URL!, + localStorage: [ + { + name: "_saleorRefreshToken", + value: loginJsonInfo.cookies[0].value, + }, + ], + }); + + fs.writeFileSync(storageStatePath, JSON.stringify(loginJsonInfo, null, 2)); + + await apiRequestContext.dispose(); + } + + return storageStatePath; +}; + +const getEmailForPermission = (permission: UserPermission | "admin"): string => { + if (permission === "admin") { + return process.env.E2E_USER_NAME!; + } else { + return USER_PERMISSION[permission]; + } +}; + +const getPasswordForPermission = (permission: UserPermission | "admin"): string => { + if (permission === "admin") { + return process.env.E2E_USER_PASSWORD!; + } else { + return process.env.E2E_PERMISSIONS_USERS_PASSWORD!; + } +}; diff --git a/playwright/utils/testWithPermission.ts b/playwright/utils/testWithPermission.ts new file mode 100644 index 00000000000..10eecbbb4de --- /dev/null +++ b/playwright/utils/testWithPermission.ts @@ -0,0 +1,18 @@ +import { UserPermission } from "@data/userPermissions"; +import { test as base } from "@playwright/test"; + +import { getStorageState } from "./auth"; + +interface PermissionOptions { + permissionName: UserPermission | "admin"; +} + +export const test = base.extend({ + permissionName: ["admin", { option: true }], + + storageState: async ({ permissionName }, use) => { + const storageStatePath = await getStorageState(permissionName); + + await use(storageStatePath); + }, +});