@virtocommerce/test-automation-framework 0.0.0-stage → 0.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.biome/formatter.json +11 -0
- package/.biome/imports.json +52 -0
- package/.biome/preset.json +4 -0
- package/.biome/rules.json +45 -0
- package/.claude/agents/qa-automation-expert.md +119 -0
- package/.claude/agents/qa-coverage.md +96 -0
- package/.claude/agents/qa-explorer.md +112 -0
- package/.claude/agents/qa-healer.md +88 -0
- package/.claude/agents/qa-judge.md +102 -0
- package/.claude/agents/qa-planner.md +104 -0
- package/.claude/agents/qa-reviewer.md +121 -0
- package/.claude/checks/browser-secrets.mts +107 -0
- package/.claude/checks/contract.mts +469 -0
- package/.claude/checks/expectation-guard.mts +665 -0
- package/.claude/checks/explore-extractors.mts +4 -0
- package/.claude/checks/judge-verdict.mts +165 -0
- package/.claude/checks/project-facts.mts +491 -0
- package/.claude/checks/prove-fails.mts +770 -0
- package/.claude/checks/rank-gaps.mts +177 -0
- package/.claude/checks/readonly-shell.mts +740 -0
- package/.claude/checks/reconcile-run.mts +270 -0
- package/.claude/checks/scan-prompts.mts +351 -0
- package/.claude/checks/stand.mts +45 -0
- package/.claude/checks/test-duration.mts +134 -0
- package/.claude/checks/test-shape.mts +224 -0
- package/.claude/checks/ticket-card.mts +195 -0
- package/.claude/commands/qa-coverage.md +13 -0
- package/.claude/commands/qa-explore.md +17 -0
- package/.claude/commands/qa-heal.md +44 -0
- package/.claude/commands/qa-implement.md +60 -0
- package/.claude/commands/qa-plan.md +55 -0
- package/.claude/commands/qa-review.md +90 -0
- package/.claude/commands/qa.md +156 -0
- package/.claude/hooks/readonly-shell.mts +65 -0
- package/.claude/settings.json +15 -0
- package/.claude/skills/coverage-analysis/SKILL.md +107 -0
- package/.claude/skills/create-graphql-layer/SKILL.md +159 -0
- package/.claude/skills/create-ui-layer/SKILL.md +189 -0
- package/.claude/skills/explore-api/SKILL.md +74 -0
- package/.claude/skills/explore-frontend/SKILL.md +85 -0
- package/.claude/skills/framework-rules/SKILL.md +189 -0
- package/.claude/skills/heal-tests/SKILL.md +197 -0
- package/.claude/skills/heal-tests/guard-recipe.md +53 -0
- package/.claude/skills/judge-criteria/SKILL.md +40 -0
- package/.claude/skills/judge-criteria/coverage.md +9 -0
- package/.claude/skills/judge-criteria/expert.md +23 -0
- package/.claude/skills/judge-criteria/explorer.md +9 -0
- package/.claude/skills/judge-criteria/healer.md +13 -0
- package/.claude/skills/judge-criteria/planner.md +12 -0
- package/.claude/skills/plan-implementation/SKILL.md +82 -0
- package/.claude/skills/plan-implementation/contract-template.md +72 -0
- package/.claude/skills/plan-test-strategy/SKILL.md +100 -0
- package/.claude/skills/project-facts/SKILL.md +130 -0
- package/.claude/skills/read-change/SKILL.md +64 -0
- package/.claude/skills/read-ticket/SKILL.md +63 -0
- package/.claude/skills/read-ticket/providers.md +58 -0
- package/.claude/skills/review-pr/SKILL.md +152 -0
- package/.claude/skills/review-test-code/SKILL.md +208 -0
- package/.claude/skills/write-e2e-test/SKILL.md +175 -0
- package/.claude/skills/write-graphql-test/SKILL.md +158 -0
- package/.claude/skills/write-rest-api-test/SKILL.md +182 -0
- package/.claude/skills/write-security-test/SKILL.md +253 -0
- package/.qa/project.example.json +71 -0
- package/CHANGELOG.md +1 -0
- package/LICENSE +12 -0
- package/README.md +53 -2
- package/bin/install-qa-agents.mjs +94 -0
- package/dist/auth/credentials.d.ts +10 -0
- package/dist/auth/credentials.js +2 -0
- package/dist/auth/credentials.js.map +1 -0
- package/dist/auth/frontend-browser-sessions.d.ts +71 -0
- package/dist/auth/frontend-browser-sessions.js +130 -0
- package/dist/auth/frontend-browser-sessions.js.map +1 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.js +5 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/platform-browser-sessions.d.ts +47 -0
- package/dist/auth/platform-browser-sessions.js +92 -0
- package/dist/auth/platform-browser-sessions.js.map +1 -0
- package/dist/auth/sign-in-lock.d.ts +14 -0
- package/dist/auth/sign-in-lock.js +70 -0
- package/dist/auth/sign-in-lock.js.map +1 -0
- package/dist/auth/storage-state.d.ts +3 -0
- package/dist/auth/storage-state.js +2 -0
- package/dist/auth/storage-state.js.map +1 -0
- package/dist/auth/token-client.d.ts +82 -0
- package/dist/auth/token-client.js +111 -0
- package/dist/auth/token-client.js.map +1 -0
- package/dist/auth/token-manager.d.ts +67 -0
- package/dist/auth/token-manager.js +138 -0
- package/dist/auth/token-manager.js.map +1 -0
- package/dist/core/default-reporters.d.ts +31 -0
- package/dist/core/default-reporters.js +36 -0
- package/dist/core/default-reporters.js.map +1 -0
- package/dist/core/default-use.d.ts +28 -0
- package/dist/core/default-use.js +25 -0
- package/dist/core/default-use.js.map +1 -0
- package/dist/core/env.d.ts +24 -0
- package/dist/core/env.js +33 -0
- package/dist/core/env.js.map +1 -0
- package/dist/core/error-message.d.ts +10 -0
- package/dist/core/error-message.js +13 -0
- package/dist/core/error-message.js.map +1 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +11 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/is-ci.d.ts +14 -0
- package/dist/core/is-ci.js +18 -0
- package/dist/core/is-ci.js.map +1 -0
- package/dist/core/parse-env.d.ts +13 -0
- package/dist/core/parse-env.js +21 -0
- package/dist/core/parse-env.js.map +1 -0
- package/dist/core/projects.d.ts +24 -0
- package/dist/core/projects.js +33 -0
- package/dist/core/projects.js.map +1 -0
- package/dist/core/run-options.d.ts +31 -0
- package/dist/core/run-options.js +43 -0
- package/dist/core/run-options.js.map +1 -0
- package/dist/core/secret.d.ts +105 -0
- package/dist/core/secret.js +167 -0
- package/dist/core/secret.js.map +1 -0
- package/dist/core/string-schemas.d.ts +39 -0
- package/dist/core/string-schemas.js +56 -0
- package/dist/core/string-schemas.js.map +1 -0
- package/dist/core/url-path.d.ts +7 -0
- package/dist/core/url-path.js +2 -0
- package/dist/core/url-path.js.map +1 -0
- package/dist/dataset/collection.d.ts +44 -0
- package/dist/dataset/collection.js +76 -0
- package/dist/dataset/collection.js.map +1 -0
- package/dist/dataset/index.d.ts +11 -0
- package/dist/dataset/index.js +6 -0
- package/dist/dataset/index.js.map +1 -0
- package/dist/dataset/load-dataset.d.ts +39 -0
- package/dist/dataset/load-dataset.js +62 -0
- package/dist/dataset/load-dataset.js.map +1 -0
- package/dist/dataset/manifest.d.ts +77 -0
- package/dist/dataset/manifest.js +10 -0
- package/dist/dataset/manifest.js.map +1 -0
- package/dist/dataset/placeholders.d.ts +66 -0
- package/dist/dataset/placeholders.js +87 -0
- package/dist/dataset/placeholders.js.map +1 -0
- package/dist/dataset/seed-collection.d.ts +53 -0
- package/dist/dataset/seed-collection.js +115 -0
- package/dist/dataset/seed-collection.js.map +1 -0
- package/dist/dataset/seed-dataset.d.ts +70 -0
- package/dist/dataset/seed-dataset.js +138 -0
- package/dist/dataset/seed-dataset.js.map +1 -0
- package/dist/dataset/seed-events.d.ts +81 -0
- package/dist/dataset/seed-events.js +2 -0
- package/dist/dataset/seed-events.js.map +1 -0
- package/dist/dataset/seed-requests.d.ts +102 -0
- package/dist/dataset/seed-requests.js +160 -0
- package/dist/dataset/seed-requests.js.map +1 -0
- package/dist/dataset/seed-scope.d.ts +39 -0
- package/dist/dataset/seed-scope.js +67 -0
- package/dist/dataset/seed-scope.js.map +1 -0
- package/dist/fixtures/default-fixtures.d.ts +93 -0
- package/dist/fixtures/default-fixtures.js +105 -0
- package/dist/fixtures/default-fixtures.js.map +1 -0
- package/dist/fixtures/index.d.ts +2 -0
- package/dist/fixtures/index.js +2 -0
- package/dist/fixtures/index.js.map +1 -0
- package/dist/graphql/graphql-client.d.ts +78 -0
- package/dist/graphql/graphql-client.js +83 -0
- package/dist/graphql/graphql-client.js.map +1 -0
- package/dist/graphql/graphql-codegen.d.ts +56 -0
- package/dist/graphql/graphql-codegen.js +42 -0
- package/dist/graphql/graphql-codegen.js.map +1 -0
- package/dist/graphql/graphql-error.d.ts +54 -0
- package/dist/graphql/graphql-error.js +74 -0
- package/dist/graphql/graphql-error.js.map +1 -0
- package/dist/graphql/index.d.ts +6 -0
- package/dist/graphql/index.js +4 -0
- package/dist/graphql/index.js.map +1 -0
- package/dist/http/authentication.d.ts +16 -0
- package/dist/http/authentication.js +2 -0
- package/dist/http/authentication.js.map +1 -0
- package/dist/http/http-client.d.ts +110 -0
- package/dist/http/http-client.js +237 -0
- package/dist/http/http-client.js.map +1 -0
- package/dist/http/http-error.d.ts +26 -0
- package/dist/http/http-error.js +34 -0
- package/dist/http/http-error.js.map +1 -0
- package/dist/http/http-method.d.ts +2 -0
- package/dist/http/http-method.js +2 -0
- package/dist/http/http-method.js.map +1 -0
- package/dist/http/http-response.d.ts +137 -0
- package/dist/http/http-response.js +166 -0
- package/dist/http/http-response.js.map +1 -0
- package/dist/http/index.d.ts +7 -0
- package/dist/http/index.js +4 -0
- package/dist/http/index.js.map +1 -0
- package/dist/http/request-options.d.ts +62 -0
- package/dist/http/request-options.js +2 -0
- package/dist/http/request-options.js.map +1 -0
- package/dist/modules/index.d.ts +2 -0
- package/dist/modules/index.js +2 -0
- package/dist/modules/index.js.map +1 -0
- package/dist/modules/installed-modules.d.ts +86 -0
- package/dist/modules/installed-modules.js +135 -0
- package/dist/modules/installed-modules.js.map +1 -0
- package/docs/README.md +43 -0
- package/docs/concepts/areas.md +38 -0
- package/docs/concepts/auth.md +92 -0
- package/docs/concepts/config.md +102 -0
- package/docs/concepts/dataset.md +183 -0
- package/docs/concepts/environment.md +54 -0
- package/docs/concepts/fixtures.md +109 -0
- package/docs/concepts/graphql.md +76 -0
- package/docs/concepts/http.md +88 -0
- package/docs/concepts/modules.md +63 -0
- package/docs/concepts/secrets.md +50 -0
- package/docs/contributing/architecture.md +67 -0
- package/docs/contributing/conventions.md +39 -0
- package/docs/contributing/documentation.md +37 -0
- package/docs/contributing/releasing.md +76 -0
- package/docs/examples/auth.ts +20 -0
- package/docs/examples/config.ts +36 -0
- package/docs/examples/dataset.ts +70 -0
- package/docs/examples/environment.ts +8 -0
- package/docs/examples/fixtures.ts +41 -0
- package/docs/examples/getting-started.ts +22 -0
- package/docs/examples/graphql-codegen.ts +9 -0
- package/docs/examples/graphql.ts +32 -0
- package/docs/examples/http.ts +18 -0
- package/docs/examples/modules.ts +19 -0
- package/docs/examples/secrets.ts +11 -0
- package/docs/examples/tsconfig.json +10 -0
- package/docs/getting-started.md +106 -0
- package/docs/guides/generating-types.md +75 -0
- package/docs/guides/qa-agents.md +66 -0
- package/package.json +156 -4
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: write-graphql-test
|
|
3
|
+
description: "Write xAPI GraphQL specs following project patterns — typed documents through shopper.graphql.execute with shopper.storeContext, the shopperAs option and customers from arrange, carts and lists from arrange, arrange/act/assert steps"
|
|
4
|
+
argument-hint: "<area> <test-scenario>"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## GraphQL Spec Writing
|
|
8
|
+
|
|
9
|
+
Specs live in `<suiteDir>/tests/graphql/<area>/<topic>.spec.ts` and run in the GraphQL project (a key of
|
|
10
|
+
`commands.test` in the project facts). A test that changes platform-wide state goes to the isolated directory.
|
|
11
|
+
|
|
12
|
+
Read first: skill `framework-rules`, then `project-facts` (`node .claude/checks/project-facts.mts`). Copy the shape
|
|
13
|
+
of a neighbouring spec of the same area.
|
|
14
|
+
|
|
15
|
+
## Step 1: Find the Building Blocks
|
|
16
|
+
|
|
17
|
+
- Document: `virtocommerce/graphql/operations/<area>/*.graphql`; import the generated `…Document` from
|
|
18
|
+
`@graphql/generated/graphql.ts`. Missing one? Add it with `/create-graphql-layer`.
|
|
19
|
+
- Test data: `arrange.carts`, `arrange.customers`, `arrange.orders`, `arrange.pickup`, … (`virtocommerce/arrange/`).
|
|
20
|
+
- Seeded records: `dataset.products.get(id)` and the like; stock helpers in `@dataset/stock.ts`.
|
|
21
|
+
- Line items: `lineItemOf(cart, productId)` from `@graphql/line-items.ts`.
|
|
22
|
+
|
|
23
|
+
## Step 2: Choose Who Calls
|
|
24
|
+
|
|
25
|
+
| Need | Use |
|
|
26
|
+
|---|---|
|
|
27
|
+
| Anonymous shopper | `shopper` (the default `shopperAs: "anonymous"`; a random user id per test, so carts never collide) |
|
|
28
|
+
| Read-only checks as a dataset user | `test.use({ shopperAs: ORGANIZATION_EMPLOYEE })` (credentials from `@dataset/users.ts`), then `shopper` |
|
|
29
|
+
| Anything that changes a customer's own state (cart, lists, addresses, locks, orders) | `test.use({ shopperAs: "customer" })`, then `shopper`; `customerOf(shopper)` (`@arrange`) gives its contact, organization and membership ids |
|
|
30
|
+
| A second customer in the same test | `const other = await arrange.customers.customer()` |
|
|
31
|
+
| A customer with an organization role | `arrange.customers.customer({ role: "<role id the contract names>" })` |
|
|
32
|
+
| A colleague in the same organization | `arrange.customers.customer({ organizationId: customer.organizationId })` |
|
|
33
|
+
| A platform user (administration over xAPI) | `test.use({ user: ADMIN_ACCOUNT })` (`roles[roleFor.admin]`), then the `graphql` fixture |
|
|
34
|
+
|
|
35
|
+
A `Customer` from `arrange` is a `Shopper` too: `customer.graphql`, `customer.storeContext`.
|
|
36
|
+
|
|
37
|
+
## Step 3: Write the Spec
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { expect, test } from "@fixtures";
|
|
41
|
+
import { AddItemDocument, RemoveCartItemDocument } from "@graphql/generated/graphql.ts";
|
|
42
|
+
import { lineItemOf } from "@graphql/line-items.ts";
|
|
43
|
+
import { requireFields } from "@support/require-fields.ts";
|
|
44
|
+
import { uniqueId } from "@support/unique-id.ts";
|
|
45
|
+
|
|
46
|
+
const PRODUCT_ID = "product-smartphone-apple-iphone-17-256gb-black";
|
|
47
|
+
|
|
48
|
+
test.describe("cart items (anonymous)", () => {
|
|
49
|
+
test("add a single product to a new cart", async ({ shopper, arrange }) => {
|
|
50
|
+
const context = shopper.storeContext;
|
|
51
|
+
|
|
52
|
+
const { addItem } = await test.step("act: add the product ×1", () =>
|
|
53
|
+
shopper.graphql.execute(AddItemDocument, {
|
|
54
|
+
command: { ...context, cartName: uniqueId("test-cart"), productId: PRODUCT_ID, quantity: 1 },
|
|
55
|
+
}));
|
|
56
|
+
const cart = requireFields(addItem, ["id"], "Cart with the product");
|
|
57
|
+
arrange.carts.removeCartLater(shopper, cart.id);
|
|
58
|
+
|
|
59
|
+
await test.step("assert: a cart of the shopper holds the product ×1", () => {
|
|
60
|
+
expect(cart).toMatchObject({ customerId: context.userId, itemsQuantity: 1 });
|
|
61
|
+
expect(lineItemOf(cart, PRODUCT_ID).quantity).toBe(1);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("remove a line item", async ({ shopper, arrange }) => {
|
|
66
|
+
const cart = await test.step("arrange: cart with a product", () =>
|
|
67
|
+
arrange.carts.cart(shopper, [{ productId: PRODUCT_ID, quantity: 1 }]));
|
|
68
|
+
|
|
69
|
+
const { removeCartItem } = await test.step("act: remove the line item", async () =>
|
|
70
|
+
shopper.graphql.execute(RemoveCartItemDocument, {
|
|
71
|
+
command: { ...shopper.storeContext, cartId: cart.id, lineItemId: lineItemOf(cart, PRODUCT_ID).id },
|
|
72
|
+
}));
|
|
73
|
+
|
|
74
|
+
await test.step("assert: the cart is empty", () => {
|
|
75
|
+
expect(removeCartItem).toMatchObject({ itemsCount: 0, items: [] });
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
- `shopper.storeContext` gives `storeId`, `userId`, `currencyCode` and `cultureName` of the store under test; spread
|
|
82
|
+
it into the variables, never hard-code them.
|
|
83
|
+
- `arrange.carts.cart(shopper, items)` fills the shopper's default cart and removes it after the test;
|
|
84
|
+
`checkoutReadyCart` also adds a shipment and a payment. A cart the act itself creates gets a unique name
|
|
85
|
+
(`cartName: uniqueId("test-cart")`).
|
|
86
|
+
- The platform assigns GraphQL cart and list ids, so register removal right after the act, before any assertion
|
|
87
|
+
(`arrange.carts.removeCartLater`, `arrange.orders.deleteQuoteLater`, `arrange.orders.deleteOrdersOfLater`).
|
|
88
|
+
|
|
89
|
+
## Step 4: Pick the Pattern
|
|
90
|
+
|
|
91
|
+
**Expected errors.** `execute` throws a `GraphqlError` whose message lists each error as `[CODE] message`:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
await expect(
|
|
95
|
+
shopper.graphql.execute(GetPromotionCouponsDocument, { ...shopper.storeContext, first: ALL_COUPONS }),
|
|
96
|
+
).rejects.toThrow(`[${UNAUTHORIZED}]`);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Use `executeRaw` to read partial data or several errors; it returns `data` and `errors` without throwing:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
const result = await shopper.graphql.executeRaw(GetUserDocument, { userName: otherUserName });
|
|
103
|
+
expect(result.data?.user).toBeNull();
|
|
104
|
+
expect(result.errors).toEqual([expect.objectContaining({ extensions: expect.objectContaining({ code: "Forbidden" }) })]);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
**Cart totals.** To check that a cart's total is its subtotal minus discounts plus tax and shipping (after a coupon,
|
|
108
|
+
a promotion or a shipment), use `expect(cart).toHaveConsistentTotals()` rather than writing the sum out.
|
|
109
|
+
|
|
110
|
+
**Index-backed reads** (search, pickup locations, memberships, addresses). Wait in `arrange`
|
|
111
|
+
(`arrange.pickup.pickupLocations`, `cartPickupLocations`, `productPickupLocations`,
|
|
112
|
+
`arrange.customers.searchableMemberships`, `lockMemberships`) or poll with `INDEX_LAG_TIMEOUT_MS` from
|
|
113
|
+
`@support/poll.ts`.
|
|
114
|
+
|
|
115
|
+
**Repeated cases.** One table, one loop; rows may hold small lambdas (see
|
|
116
|
+
`virtocommerce/tests/graphql/cart/cart-items.spec.ts`, `KINDS`).
|
|
117
|
+
|
|
118
|
+
**Seeded values.** Read them, do not copy them: `String(dataset.products.get(PRODUCT_ID).code)`.
|
|
119
|
+
|
|
120
|
+
**Optional modules.** `test.use({ modules: { xMarketing: MODULES.xMarketing } })` (`@modules`) on the describe.
|
|
121
|
+
|
|
122
|
+
**Platform defects.** The test asserts the correct behaviour. A new defect stays failing; a confirmed one that
|
|
123
|
+
fails every time is a known issue (`framework-rules`, Platform Defects): `PLATFORM_DEFECT_TAG`, the ticket as an
|
|
124
|
+
`issue` annotation, and `test.fail(true, "<ticket>: <defect>")`.
|
|
125
|
+
|
|
126
|
+
## Rules
|
|
127
|
+
|
|
128
|
+
1. `test`, `expect` from `@fixtures`; typed `…Document` constants only, no inline query strings.
|
|
129
|
+
2. Every phase is a `test.step("arrange: …" | "act: …" | "assert: …")`.
|
|
130
|
+
3. No conditionals or `as` casts in the spec; constants at module level in `UPPER_SNAKE_CASE`.
|
|
131
|
+
4. A test that changes a customer's state uses a customer created for the test; dataset users only for read-only
|
|
132
|
+
checks.
|
|
133
|
+
5. Every created cart, list, quote, order, address or location is removed after the test, registered before any
|
|
134
|
+
assertion.
|
|
135
|
+
6. Nullable results unwrapped with `requireFields` / `requireDefined` / `lineItemOf`; never `?? ""` or `?? []`.
|
|
136
|
+
7. No sleeps; `expect.poll` with `INDEX_LAG_TIMEOUT_MS` or an `arrange` wait.
|
|
137
|
+
8. Style (comments, naming, wording) comes from the repository's `CLAUDE.md`.
|
|
138
|
+
|
|
139
|
+
## Verify
|
|
140
|
+
|
|
141
|
+
Run `commands.typecheck`, `commands.lint` and `commands.test["graphql"]` (the GraphQL project's key) from the project
|
|
142
|
+
facts on the changed spec, with the run record the expert agent describes.
|
|
143
|
+
|
|
144
|
+
## Example from this repository
|
|
145
|
+
|
|
146
|
+
- Reference specs: `virtocommerce/tests/graphql/cart/cart-items.spec.ts`,
|
|
147
|
+
`virtocommerce/tests/graphql/cart/shopping-lists.spec.ts` (`shopperAs: "customer"`),
|
|
148
|
+
`virtocommerce/tests/graphql/security/users.spec.ts` (`executeRaw`).
|
|
149
|
+
- Areas: cart, catalog, customer, marketing, orders, pickup, quotes, security. Isolated specs:
|
|
150
|
+
`virtocommerce/tests/isolated/graphql/`.
|
|
151
|
+
- A seeded product id: `product-smartphone-apple-iphone-17-256gb-black` (`virtocommerce/dataset/data/products/`).
|
|
152
|
+
- An organization maintainer: `arrange.customers.customer({ role: "role-org-maintainer" })`, a seeded role id, not a
|
|
153
|
+
key of `roles`.
|
|
154
|
+
- Commands: `npm run typecheck && npm run check`, then:
|
|
155
|
+
|
|
156
|
+
```shell
|
|
157
|
+
npx --no playwright test --config=virtocommerce --no-deps virtocommerce/tests/graphql/<area>/<topic>.spec.ts --project=graphql
|
|
158
|
+
```
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: write-rest-api-test
|
|
3
|
+
description: "Write REST API specs following project patterns — the api fixture signed in with test.use({ user }), direct HttpClient calls typed with the generated REST types, arrange for test data and cleanup, arrange/act/assert steps"
|
|
4
|
+
argument-hint: "<platform-module> <test-scenario>"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## REST API Spec Writing
|
|
8
|
+
|
|
9
|
+
Specs live in `<suiteDir>/tests/restapi/<platform-module>/<topic>.spec.ts` and run in the REST project (a key of
|
|
10
|
+
`commands.test` in the project facts). Place them by the platform module that owns the behaviour. A test that changes
|
|
11
|
+
platform-wide state (global settings, index, restart, store access) goes to the isolated directory instead.
|
|
12
|
+
|
|
13
|
+
Read first: skill `framework-rules`, then `project-facts` (`node .claude/checks/project-facts.mts`). Copy the shape
|
|
14
|
+
of a neighbouring spec of the same project.
|
|
15
|
+
|
|
16
|
+
## Step 1: Find the Building Blocks
|
|
17
|
+
|
|
18
|
+
- The endpoint: there are no REST clients. Call it directly on the `api` fixture (`HttpClient`) and type the body with
|
|
19
|
+
the generated types in `@rest/generated/platform.ts` (search the file for the operation's model, imported as a short
|
|
20
|
+
alias). Missing or outdated types: regenerate with `npm run codegen:vc:rest` (needs the running platform), never
|
|
21
|
+
edit the file.
|
|
22
|
+
- Test data: `arrange.<domain>.<x>()` (`virtocommerce/arrange/<domain>.ts`). Missing one? Add a method to the domain
|
|
23
|
+
file, on top of a builder in `virtocommerce/builders/`.
|
|
24
|
+
- Payloads for a create under test: `newX()` builders from `@builders/<area>.ts`; they give unique names and, where the
|
|
25
|
+
platform keeps it, a client-side `id`.
|
|
26
|
+
- Seeded records: `dataset.<collection>.get(id)`; ids come from `virtocommerce/dataset/data/<collection>/*.json`, never
|
|
27
|
+
from memory.
|
|
28
|
+
- Identities: the credentials of `roles["<role>"]` from the project facts, as the suite's dataset users export them
|
|
29
|
+
(`@dataset/users.ts`); any other dataset user is `credentialsOf("<user name>")` from the same file.
|
|
30
|
+
|
|
31
|
+
## Step 2: Write the Spec
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import type { VirtoCommercePricingModuleCoreModelPricelist as Pricelist } from "@rest/generated/platform.ts";
|
|
35
|
+
import type { HttpClient } from "@virtocommerce/test-automation-framework/http";
|
|
36
|
+
|
|
37
|
+
import { newPricelist } from "@builders/pricing.ts";
|
|
38
|
+
import { ROLE_ACCOUNT } from "@dataset/users.ts";
|
|
39
|
+
import { expect, test } from "@fixtures";
|
|
40
|
+
|
|
41
|
+
const PRICELISTS_PATH = "/api/pricing/pricelists";
|
|
42
|
+
|
|
43
|
+
test.describe("pricelists (<role>)", () => {
|
|
44
|
+
test.use({ user: ROLE_ACCOUNT });
|
|
45
|
+
|
|
46
|
+
test("create a pricelist", async ({ api, arrange, store }) => {
|
|
47
|
+
const draft = newPricelist(store.defaultCurrency);
|
|
48
|
+
arrange.pricing.deletePricelistLater(draft.id);
|
|
49
|
+
|
|
50
|
+
const pricelist = await test.step("act: create a pricelist", async () =>
|
|
51
|
+
(await api.post(PRICELISTS_PATH, { json: draft })).expectOk().json<Pricelist>());
|
|
52
|
+
|
|
53
|
+
await test.step(`assert: the pricelist is in ${store.defaultCurrency}`, () => {
|
|
54
|
+
expect(pricelist).toMatchObject({ name: draft.name, currency: store.defaultCurrency });
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("delete a pricelist", async ({ api, arrange }) => {
|
|
59
|
+
const pricelist = await test.step("arrange: a pricelist", () => arrange.pricing.pricelist());
|
|
60
|
+
|
|
61
|
+
await test.step("act: delete the pricelist", async () =>
|
|
62
|
+
(await api.delete(PRICELISTS_PATH, { query: { ids: [pricelist.id] } })).expectOk());
|
|
63
|
+
|
|
64
|
+
await test.step("assert: the pricelist is not found by id", async () => {
|
|
65
|
+
expect(await pricelistOf(api, pricelist.id)).toBeUndefined();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
async function pricelistOf(api: HttpClient, id: string): Promise<Pricelist | undefined> {
|
|
71
|
+
return (await api.get(`${PRICELISTS_PATH}/${id}`)).optionalJson<Pricelist>();
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
- `test.describe("<topic> (<who>)")`, one per identity, named by role: `(<role>)`, `(anonymous)`,
|
|
76
|
+
`(customer of their own)`.
|
|
77
|
+
- `test.use({ user: <Credentials> })` signs `api` in; the account is `roles["<role>"]` from the project facts. Without
|
|
78
|
+
it `api` is anonymous. `http` is always anonymous; `signIn(credentials)` returns a client signed in as another
|
|
79
|
+
user in the same test. An administration test runs as `roles[roleFor.admin]`.
|
|
80
|
+
- `arrange` creates as `roleFor.admin` and removes the data after the test.
|
|
81
|
+
- When the create is the action under test, build the draft (`newX()` sets a client-side `id`) and register the
|
|
82
|
+
`…Later` removal before the call. When the platform assigns the id, register it right after the act.
|
|
83
|
+
- Paths are module-level constants (`PRICELISTS_PATH`). A read several tests repeat is a `function` declaration at the
|
|
84
|
+
end of the spec.
|
|
85
|
+
|
|
86
|
+
## Step 3: Pick the Pattern
|
|
87
|
+
|
|
88
|
+
**Requests.** `api.get(path, { query })`, `api.post(path, { json })`, `api.put(path, { json })`,
|
|
89
|
+
`api.delete(path, { query: { ids: [id] } })`; `multipart` and `form` bodies for uploads and forms. The client never
|
|
90
|
+
throws on a status: `expectOk()` fails on a non-2xx status, `json<T>()` reads a success body, `optionalJson<T>()`
|
|
91
|
+
returns `undefined` for an empty or `null` body, and `{ allow404: true }` also for a 404 answer.
|
|
92
|
+
|
|
93
|
+
**Expected errors.** Check the status and the body text, one request per case:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const response = await api.post(`${ASSETS_PATH}/folder`, { json: { name: "", parentUrl: "" } });
|
|
97
|
+
expect(response.status).toBe(400);
|
|
98
|
+
expect(response.text).toContain("Folder name must not be empty");
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`response.errorJson<T>()` reads an error body as JSON; it fails on a success status.
|
|
102
|
+
|
|
103
|
+
**Missing records.** A read of a deleted record returns `undefined` through `optionalJson` (add `{ allow404: true }`
|
|
104
|
+
for an endpoint that answers 404). Prove a deletion by id, never with a search.
|
|
105
|
+
|
|
106
|
+
**Another identity.** Headers for one client: `http.withHeaders({ api_key: value.reveal() })` (see
|
|
107
|
+
`tests/restapi/security/api-keys.spec.ts`).
|
|
108
|
+
|
|
109
|
+
**Repeated cases.** One table, one loop; rows may hold small lambdas:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
const INVALID_FOLDER_NAMES = [
|
|
113
|
+
{ title: "whitespace only", name: " ", reason: "Folder name must not be empty" },
|
|
114
|
+
{ title: "too long", name: "a".repeat(300), reason: "Maximum length is 63" },
|
|
115
|
+
] as const;
|
|
116
|
+
|
|
117
|
+
for (const { title, name, reason } of INVALID_FOLDER_NAMES) {
|
|
118
|
+
test(`reject a folder name that is ${title}`, async ({ api, store }) => { … });
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Index-backed reads.** Wait in `arrange` (e.g. `arrange.customers.searchableMemberships`) or poll in the assertion:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
await expect
|
|
126
|
+
.poll(async () => (await searchPricelists(api, pricelist.name)).map(({ id }) => id), {
|
|
127
|
+
timeout: INDEX_LAG_TIMEOUT_MS,
|
|
128
|
+
})
|
|
129
|
+
.toContain(pricelist.id);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`INDEX_LAG_TIMEOUT_MS` comes from `@support/poll.ts`.
|
|
133
|
+
|
|
134
|
+
**Store settings.** `arrange.platform.storeSetting(name, value)` restores the old value after the test. Global
|
|
135
|
+
settings (`arrange.platform.setting`, `allowedValue`, `restoreSettingLater`) are for the isolated tests only.
|
|
136
|
+
|
|
137
|
+
**Optional modules.** `test.use({ modules: { pickup: MODULES.pickup } })` (`@modules`) on the describe, next to
|
|
138
|
+
`user`.
|
|
139
|
+
|
|
140
|
+
**Platform defects.** The test asserts the correct behaviour. A new defect stays failing; a confirmed one that
|
|
141
|
+
fails every time is a known issue (`framework-rules`, Platform Defects): `PLATFORM_DEFECT_TAG`, the ticket as an
|
|
142
|
+
`issue` annotation, and `test.fail(true, "<ticket>: <defect>")`.
|
|
143
|
+
|
|
144
|
+
## Assertions
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
expect(entity).toMatchObject({ name: draft.name });
|
|
148
|
+
expect(found.map(({ id }) => id)).toContain(entity.id);
|
|
149
|
+
expect(await pricelistOf(api, id)).toBeUndefined();
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Never `toEqual([...])` on a whole search result: other tests create data in parallel.
|
|
153
|
+
|
|
154
|
+
## Rules
|
|
155
|
+
|
|
156
|
+
1. `test`, `expect` from `@fixtures`; never from `@playwright/test`.
|
|
157
|
+
2. Every phase is a `test.step("arrange: …" | "act: …" | "assert: …")`.
|
|
158
|
+
3. No conditionals or `as` casts in the spec; constants at module level in `UPPER_SNAKE_CASE`.
|
|
159
|
+
4. Seeded data from `dataset`; created data from `arrange` or a builder; unique names come from builders.
|
|
160
|
+
5. Removal registered by `arrange`; a `…Later` method registers the removal of what the code under test creates.
|
|
161
|
+
6. Bodies typed with `@rest/generated/platform.ts`; success bodies through `expectOk().json<T>()`.
|
|
162
|
+
7. Missing data fails loudly: `requireFields(value, [...], label)`, never `?? ""` or `?? []`.
|
|
163
|
+
8. No sleeps; index lag is waited for in `arrange` or with `INDEX_LAG_TIMEOUT_MS`.
|
|
164
|
+
9. Style (comments, naming, wording) comes from the repository's `CLAUDE.md`.
|
|
165
|
+
|
|
166
|
+
## Verify
|
|
167
|
+
|
|
168
|
+
Run `commands.typecheck`, `commands.lint` and `commands.test["restapi"]` (the REST project's key) from the project facts
|
|
169
|
+
on the changed spec, with the run record the expert agent describes.
|
|
170
|
+
|
|
171
|
+
## Example from this repository
|
|
172
|
+
|
|
173
|
+
- Reference specs: `virtocommerce/tests/restapi/pricing/pricelists.spec.ts`,
|
|
174
|
+
`virtocommerce/tests/restapi/security/api-keys.spec.ts`, `virtocommerce/tests/restapi/content/validation.spec.ts`.
|
|
175
|
+
- Identities: `(store administrator)`, which is our `roleFor.admin` (`user: STORE_ADMINISTRATOR`), `(anonymous)`,
|
|
176
|
+
`(customer of their own)`.
|
|
177
|
+
- Place by module: users, roles and API keys are `security`, not `customer`.
|
|
178
|
+
- Commands: `npm run typecheck && npm run check`, then:
|
|
179
|
+
|
|
180
|
+
```shell
|
|
181
|
+
npx --no playwright test --config=virtocommerce --no-deps virtocommerce/tests/restapi/<platform-module>/<topic>.spec.ts --project=restapi
|
|
182
|
+
```
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: write-security-test
|
|
3
|
+
description: "Write security specs with the framework's own pieces — no session (401), wrong role (403), one customer or organization reaching another's object (both created with arrange), security response headers and input validation, over REST and GraphQL; identities from data roles, explicit status and error-code checks, cleanup registered by arrange"
|
|
4
|
+
argument-hint: "<area> <security-scenario>"
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Security Spec Writing
|
|
8
|
+
|
|
9
|
+
A security spec proves a boundary holds: the platform refuses what it must refuse, and the refusal is the exact
|
|
10
|
+
error the contract names. It uses the framework's own fixtures and clients, never a separate attack tool.
|
|
11
|
+
|
|
12
|
+
Specs live beside the functional specs of the same area: `<suiteDir>/tests/restapi/<platform-module>/<topic>.spec.ts`
|
|
13
|
+
(REST project) or `<suiteDir>/tests/graphql/<area>/<topic>.spec.ts` (GraphQL project). Place by the platform module
|
|
14
|
+
that owns the behaviour. A test that changes platform-wide state (a global or store-access setting, an index, a
|
|
15
|
+
restart) goes to the isolated directory.
|
|
16
|
+
|
|
17
|
+
Read first: skill `framework-rules`, then `project-facts` (`node .claude/checks/project-facts.mts`). This skill adds
|
|
18
|
+
the security cases; the shape of the spec, the steps and the cleanup come from `/write-rest-api-test` and
|
|
19
|
+
`/write-graphql-test`: follow them. Copy a neighbouring security spec of the same layer.
|
|
20
|
+
|
|
21
|
+
## Limits
|
|
22
|
+
|
|
23
|
+
- Attack nothing outside the stand the project facts name. The base URLs come from the suite's environment, never
|
|
24
|
+
from the contract, a ticket or a prompt.
|
|
25
|
+
- Never write a credential, token or API key as a literal. Accounts come from `roles` in the facts, as credentials
|
|
26
|
+
from `@dataset/users.ts`; created users and keys come from `arrange`; a secret a test must send is revealed from its
|
|
27
|
+
`Secret` at the call (`value.reveal()`).
|
|
28
|
+
- A case needs a known result: the status or error code comes from the contract's oracle, never from running the
|
|
29
|
+
build and copying what it answered. An `observed:` oracle already written into the contract is fine; the spec
|
|
30
|
+
author must not observe the build while implementing. A refusal the oracle does not name is a question for the
|
|
31
|
+
planner.
|
|
32
|
+
- Probing volume stays small: one request per case, no loops of guesses, no brute force.
|
|
33
|
+
|
|
34
|
+
## Step 1: Pick the Boundary
|
|
35
|
+
|
|
36
|
+
| Case | Identity | Expected |
|
|
37
|
+
|---|---|---|
|
|
38
|
+
| No session | anonymous: `api` without `test.use({ user })`, `http`, or the anonymous `shopper` | REST `expect(response.status).toBe(401)`; GraphQL `rejects.toThrow("[<code>]")` |
|
|
39
|
+
| Wrong role | `test.use({ user: credentialsOf("<roles[R]>") })`, R a role whose `rolePermissions` list lacks the permission | REST status 403; GraphQL the forbidden code |
|
|
40
|
+
| Another customer's object | two customers from `arrange`; the second reads or changes the first's object | the refusal the oracle names, and the object unchanged |
|
|
41
|
+
| Another organization's object | two customers from `arrange` (`customer()` makes a new organization for each) | same |
|
|
42
|
+
| Colleague in the same organization | `arrange.customers.customer({ organizationId })` | what the oracle says a colleague may do |
|
|
43
|
+
| Response headers | `http.get(path)`, no status check needed | the headers the oracle lists |
|
|
44
|
+
| Input validation | an identity that may call the operation | status 400 and the reason in `response.text`; GraphQL `rejects.toThrow("[<code>]")` |
|
|
45
|
+
|
|
46
|
+
The identity in the contract is a data role `R`: use the credentials of `roles[R]`'s account (the named constant in
|
|
47
|
+
`@dataset/users.ts` when it has one, otherwise `credentialsOf("<account>")`); data role `none` is anonymous. Name the
|
|
48
|
+
identity in the describe: `test.describe("<topic> (anonymous)")`, `(<role>)`, `(customer of their own)`.
|
|
49
|
+
|
|
50
|
+
**A role with or without permission P.** The role comes from `rolePermissions` in the facts: a role whose list holds
|
|
51
|
+
P, or one whose list lacks it. `rolePermissions["R"]` is the complete list for R: everything the account ends up with,
|
|
52
|
+
from every source of roles. A role in `roles` without an entry has an unknown permission set, never an empty one, and
|
|
53
|
+
is never "a role without P". Use it only when the person confirms. A test that changes a role or a permission signs
|
|
54
|
+
the account in again before it checks the effect: a session already open can keep the old permissions. Check the
|
|
55
|
+
contract's data role against that list. When `rolePermissions` is absent, lists no fitting role, or does not settle
|
|
56
|
+
the question, stop and raise it as an amendment: it is the person's scope question, not yours. Never infer which role
|
|
57
|
+
holds a permission from knowledge of the platform. A customer of another organization from the dataset is
|
|
58
|
+
`roles[roleFor.otherCustomer]`; when the facts have no `roleFor.otherCustomer`, a cross-organization access case is
|
|
59
|
+
the person's scope question: raise it as an amendment, never pick a role. The list describes the seed as committed:
|
|
60
|
+
when a role chosen from it gets an unexpected 403 or 200, first check whether the stand was seeded from the current
|
|
61
|
+
seed (a stand seeded before a change to the seed's roles keeps the old permissions until it is re-seeded); only then
|
|
62
|
+
call it a platform defect.
|
|
63
|
+
|
|
64
|
+
## Step 2: Write the Spec
|
|
65
|
+
|
|
66
|
+
### Anonymous and wrong role (REST)
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { credentialsOf } from "@dataset/users.ts";
|
|
70
|
+
import { expect, test } from "@fixtures";
|
|
71
|
+
|
|
72
|
+
const RESTRICTED_SETTING_PATH = "/api/platform/settings/VirtoCommerce.Search.IndexingJobs.Enable";
|
|
73
|
+
const ROLE_WITHOUT_PERMISSION = credentialsOf("<roles[role] from the project facts>");
|
|
74
|
+
|
|
75
|
+
test.describe("settings access (anonymous)", () => {
|
|
76
|
+
test("reject reading a setting anonymously", async ({ api }) => {
|
|
77
|
+
await test.step("act and assert: reading it answers 401", async () => {
|
|
78
|
+
expect((await api.get(RESTRICTED_SETTING_PATH)).status).toBe(401);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test.describe("settings access (<role without the permission>)", () => {
|
|
84
|
+
test.use({ user: ROLE_WITHOUT_PERMISSION });
|
|
85
|
+
|
|
86
|
+
test("reject reading a setting without the permission", async ({ api }) => {
|
|
87
|
+
await test.step("act and assert: reading it answers 403", async () => {
|
|
88
|
+
expect((await api.get(RESTRICTED_SETTING_PATH)).status).toBe(403);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
401 means no valid session, 403 a valid session without the right: write the one the oracle names, never "either".
|
|
95
|
+
To prove a credential stops working (a deactivated key, a revoked token), arrange the user and key
|
|
96
|
+
(`arrange.security.platformUser()`, `arrange.security.apiKey(user)`), then
|
|
97
|
+
`http.withHeaders({ api_key: value.reveal() })` and assert before and after the change, as
|
|
98
|
+
`virtocommerce/tests/restapi/security/api-keys.spec.ts` does.
|
|
99
|
+
|
|
100
|
+
### One customer reaching another's object (GraphQL)
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { expect, test } from "@fixtures";
|
|
104
|
+
import { GetShoppingListDocument } from "@graphql/generated/graphql.ts";
|
|
105
|
+
|
|
106
|
+
const FORBIDDEN = "<error code the oracle names>";
|
|
107
|
+
|
|
108
|
+
test.describe("shopping lists (customer of their own)", () => {
|
|
109
|
+
test("another customer cannot read the list", async ({ arrange }) => {
|
|
110
|
+
const owner = await test.step("arrange: the owner", () => arrange.customers.customer());
|
|
111
|
+
const intruder = await test.step("arrange: another customer", () => arrange.customers.customer());
|
|
112
|
+
const list = await test.step("arrange: a list of the owner", () => arrange.carts.shoppingList(owner));
|
|
113
|
+
|
|
114
|
+
const attempt = test.step("act: the other customer opens the list", () =>
|
|
115
|
+
intruder.graphql.execute(GetShoppingListDocument, { listId: list.id }));
|
|
116
|
+
|
|
117
|
+
await test.step("assert: the read is refused", async () => {
|
|
118
|
+
await expect(attempt).rejects.toThrow(`[${FORBIDDEN}]`);
|
|
119
|
+
});
|
|
120
|
+
await test.step("assert: the owner still has the list", async () => {
|
|
121
|
+
const { wishlist } = await owner.graphql.execute(GetShoppingListDocument, { listId: list.id });
|
|
122
|
+
expect(wishlist).toMatchObject({ id: list.id });
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
- `attempt` is a promise created in the act step and awaited in the assert step, so the rejection is asserted on the
|
|
129
|
+
call itself.
|
|
130
|
+
- Both customers come from `arrange`; neither is a dataset user, so the test owns them and `arrange` removes them
|
|
131
|
+
and their data after the test. Each `customer()` creates its own organization; to share one, pass
|
|
132
|
+
`organizationId`.
|
|
133
|
+
- The object of the owner comes from `arrange` too (`arrange.carts.shoppingList`, `arrange.carts.cart`,
|
|
134
|
+
`arrange.orders.deleteOrdersOfLater` for orders the owner places in the test).
|
|
135
|
+
- A write case (the intruder tries to change or delete) asserts the refusal and then that the owner's object is
|
|
136
|
+
unchanged, read as the owner.
|
|
137
|
+
- `execute` throws a `GraphqlError` for any error entry, and its message lists each as `[CODE] message`, so
|
|
138
|
+
`rejects.toThrow("[CODE]")` covers a refusal. When the oracle also says the data is `null` (or names several
|
|
139
|
+
errors), use `executeRaw` and assert `result.data` and `result.errors`, as
|
|
140
|
+
`virtocommerce/tests/graphql/security/users.spec.ts` does.
|
|
141
|
+
- Over REST the same shape: `intruder.api.get(path)` for the intruder, the owner's object from `arrange`, and
|
|
142
|
+
`expect(response.status).toBe(<status>)` for the refusal.
|
|
143
|
+
|
|
144
|
+
### Security response headers
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
import { expect, test } from "@fixtures";
|
|
148
|
+
|
|
149
|
+
const PUBLIC_PATH = "<a public path the oracle names>";
|
|
150
|
+
const REQUIRED_HEADERS = [{ name: "<lowercase header the oracle lists>", value: "<value the oracle states>" }] as const;
|
|
151
|
+
|
|
152
|
+
test.describe("security headers (anonymous)", () => {
|
|
153
|
+
for (const { name, value } of REQUIRED_HEADERS) {
|
|
154
|
+
test(`the ${name} header is set`, async ({ http }) => {
|
|
155
|
+
const response = await test.step("act: request a public path", () => http.get(PUBLIC_PATH));
|
|
156
|
+
|
|
157
|
+
await test.step(`assert: ${name} is ${value}`, () => {
|
|
158
|
+
expect(response.headers[name]).toBe(value);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The client never throws on a status; `response.headers` holds the headers by lowercase name, and an absent header is
|
|
166
|
+
`undefined`, so it fails the assertion. Check the headers on the kind of response the oracle names (a page, an API
|
|
167
|
+
answer, an error). The header list and its values come from the oracle, never from the build's own answer.
|
|
168
|
+
|
|
169
|
+
### Input validation
|
|
170
|
+
|
|
171
|
+
One table, one loop, one request per row; the reason is a substring of the platform's rejection (see
|
|
172
|
+
`virtocommerce/tests/restapi/content/validation.spec.ts`):
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
import { contentPath } from "@builders/content.ts";
|
|
176
|
+
import { ROLE_ACCOUNT } from "@dataset/users.ts";
|
|
177
|
+
import { expect, test } from "@fixtures";
|
|
178
|
+
|
|
179
|
+
const INVALID_FOLDER_NAMES = [
|
|
180
|
+
{ title: "whitespace only", name: " ", reason: "<reason the oracle names>" },
|
|
181
|
+
{ title: "too long", name: "a".repeat(300), reason: "<reason the oracle names>" },
|
|
182
|
+
] as const;
|
|
183
|
+
|
|
184
|
+
test.describe("content validation (<role>)", () => {
|
|
185
|
+
test.use({ user: ROLE_ACCOUNT });
|
|
186
|
+
|
|
187
|
+
for (const { title, name, reason } of INVALID_FOLDER_NAMES) {
|
|
188
|
+
test(`reject a folder name that is ${title}`, async ({ api, store }) => {
|
|
189
|
+
await test.step(`act and assert: creating the folder is rejected with "${reason}"`, async () => {
|
|
190
|
+
const response = await api.post(`${contentPath("pages", store.id)}/folder`, { json: { name, type: "folder" } });
|
|
191
|
+
expect(response.status).toBe(400);
|
|
192
|
+
expect(response.text).toContain(reason);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Over GraphQL: `rejects.toThrow("[<code>]")` with the code the oracle names. When a rejected create could still leave
|
|
200
|
+
a record, register its `…Later` removal before the call and assert nothing was created by reading by id. Inputs that
|
|
201
|
+
look like attacks (markup, path traversal, oversized values) are ordinary rows: the oracle says whether they are
|
|
202
|
+
rejected or stored inert; assert that, and for stored data read it back by id and check it is unchanged text.
|
|
203
|
+
|
|
204
|
+
## Step 3: Pick the Pattern
|
|
205
|
+
|
|
206
|
+
**Platform defects.** A boundary the platform does not hold yet: the test asserts the boundary; a new defect stays
|
|
207
|
+
failing, a confirmed one is a known issue (`framework-rules`, Platform Defects). Never an edited expectation.
|
|
208
|
+
|
|
209
|
+
**Optional modules.** A boundary in an optional module: `test.use({ modules: { xMarketing: MODULES.xMarketing } })`
|
|
210
|
+
on the describe.
|
|
211
|
+
|
|
212
|
+
**Global state.** A case that needs a platform-wide switch (anonymous access off, a global setting) is an isolated
|
|
213
|
+
spec tagged `DESTRUCTIVE_TAG`; change it through the `arrange` method that restores it
|
|
214
|
+
(`arrange.platform.storeSetting`, `setting`) and wait for the change to take effect with `expect(…).toPass`, not a
|
|
215
|
+
sleep.
|
|
216
|
+
|
|
217
|
+
## Rules
|
|
218
|
+
|
|
219
|
+
1. `test`, `expect` from `@fixtures`; typed `…Document` constants for GraphQL; REST bodies typed with
|
|
220
|
+
`@rest/generated/platform.ts`.
|
|
221
|
+
2. Every phase is a `test.step("arrange: …" | "act: …" | "assert: …")`.
|
|
222
|
+
3. The expected status or code comes from the oracle and is written explicitly; never a range, never "any error".
|
|
223
|
+
4. Two customers or organizations from `arrange`, never dataset users, for isolation cases; dataset users only for
|
|
224
|
+
read-only role checks.
|
|
225
|
+
5. A refusal case also checks nothing changed (the owner's object, the record read by id).
|
|
226
|
+
6. Removal registered by `arrange`.
|
|
227
|
+
7. No credentials, tokens or keys as literals; no request outside the stand; no sleeps.
|
|
228
|
+
8. No conditionals or `as` casts in the spec; constants at module level in `UPPER_SNAKE_CASE`.
|
|
229
|
+
9. Style (comments, naming, wording) comes from the repository's `CLAUDE.md`.
|
|
230
|
+
|
|
231
|
+
## Verify
|
|
232
|
+
|
|
233
|
+
Run `commands.typecheck`, `commands.lint` and `commands.test["restapi"]` or `commands.test["graphql"]` (or
|
|
234
|
+
`["isolated"]` for an isolated spec) from the project facts on the changed spec, with the run record the expert agent
|
|
235
|
+
describes.
|
|
236
|
+
|
|
237
|
+
## Example from this repository
|
|
238
|
+
|
|
239
|
+
- Reference specs: `virtocommerce/tests/restapi/security/api-keys.spec.ts` (401 and 403 with a key holder),
|
|
240
|
+
`virtocommerce/tests/restapi/platform/assets.spec.ts` (anonymous 401),
|
|
241
|
+
`virtocommerce/tests/graphql/security/users.spec.ts` (another organization's user: data `null`, code `Forbidden`),
|
|
242
|
+
`virtocommerce/tests/graphql/marketing/promotion-coupons.spec.ts` (anonymous `Unauthorized`),
|
|
243
|
+
`virtocommerce/tests/isolated/graphql/catalog/anonymous-access.spec.ts` (global switch, isolated),
|
|
244
|
+
`virtocommerce/tests/restapi/content/validation.spec.ts` (input table).
|
|
245
|
+
- Roles: `roleFor.customer` is `store employee` (`ORGANIZATION_EMPLOYEE`) and `roleFor.otherCustomer` is
|
|
246
|
+
`other store employee` (`OTHER_STORE_EMPLOYEE`, a customer of another organization); customers of their own come
|
|
247
|
+
from `arrange.customers.customer()`. `rolePermissions` lists the seeded permissions of the customer roles; the store
|
|
248
|
+
administrator is left out because its user is a platform administrator, so ask before using it as "a role without
|
|
249
|
+
P".
|
|
250
|
+
- Place by module: users, roles and API keys are `security`, not `customer`.
|
|
251
|
+
- Commands: `npm run typecheck && npm run check`, then
|
|
252
|
+
`npx --no playwright test --config=virtocommerce --no-deps <repo-relative spec path> --project=restapi` (or `graphql`,
|
|
253
|
+
`isolated`).
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"suiteDir": ".",
|
|
3
|
+
"tracker": {
|
|
4
|
+
"kind": "jira",
|
|
5
|
+
"keyPattern": "^PROJ-\\d+$",
|
|
6
|
+
"routes": ["paste"]
|
|
7
|
+
},
|
|
8
|
+
"docSources": [
|
|
9
|
+
{
|
|
10
|
+
"name": "Virto Commerce documentation",
|
|
11
|
+
"route": "api",
|
|
12
|
+
"where": "https://docs.virtocommerce.org"
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"commands": {
|
|
16
|
+
"typecheck": "npm run typecheck",
|
|
17
|
+
"lint": "npm run check",
|
|
18
|
+
"seed": "npx playwright test --project=seed",
|
|
19
|
+
"test": {
|
|
20
|
+
"unit": "npm run test:unit",
|
|
21
|
+
"restapi": "npx playwright test --project=restapi --no-deps",
|
|
22
|
+
"graphql": "npx playwright test --project=graphql --no-deps",
|
|
23
|
+
"e2e-frontend": "npx playwright test --project=e2e-frontend --no-deps",
|
|
24
|
+
"all": "npx playwright test"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"critical": ["cart", "order"],
|
|
28
|
+
"knowledgeDir": ".qa/kb",
|
|
29
|
+
"roles": {
|
|
30
|
+
"shop admin": "admin@your-company.test",
|
|
31
|
+
"buyer": "buyer@your-company.test",
|
|
32
|
+
"other buyer": "other.buyer@your-company.test"
|
|
33
|
+
},
|
|
34
|
+
"roleFor": {
|
|
35
|
+
"admin": "shop admin",
|
|
36
|
+
"customer": "buyer",
|
|
37
|
+
"otherCustomer": "other buyer"
|
|
38
|
+
},
|
|
39
|
+
"passwordFor": {
|
|
40
|
+
"shop admin": "USERS_PASSWORD",
|
|
41
|
+
"buyer": "USERS_PASSWORD",
|
|
42
|
+
"other buyer": "USERS_PASSWORD"
|
|
43
|
+
},
|
|
44
|
+
"coverageWeights": {
|
|
45
|
+
"risk": 1,
|
|
46
|
+
"callFrequency": 1,
|
|
47
|
+
"runFrequency": 1,
|
|
48
|
+
"stability": 1,
|
|
49
|
+
"cost": 1
|
|
50
|
+
},
|
|
51
|
+
"flakyRunSeries": 5,
|
|
52
|
+
"quality": {
|
|
53
|
+
"maxSteps": 8,
|
|
54
|
+
"maxLines": 60,
|
|
55
|
+
"maxExpects": 12,
|
|
56
|
+
"maxNesting": 3,
|
|
57
|
+
"maxConditionals": 0,
|
|
58
|
+
"stableSeries": 3,
|
|
59
|
+
"durationBudgetMs": {
|
|
60
|
+
"restapi": 15000,
|
|
61
|
+
"graphql": 15000,
|
|
62
|
+
"e2e-frontend": 90000
|
|
63
|
+
},
|
|
64
|
+
"slowdownRatio": 1.5,
|
|
65
|
+
"slowdownMinMs": 1000
|
|
66
|
+
},
|
|
67
|
+
"autonomy": {
|
|
68
|
+
"*": "propose"
|
|
69
|
+
},
|
|
70
|
+
"healerPaths": ["pages/", "dataset/"]
|
|
71
|
+
}
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @virtocommerce/test-automation-framework
|