|
| 1 | +# Integration Tests |
| 2 | + |
| 3 | +The `da-admin` worker includes a suite of integration tests designed as "smoke tests". These tests validate: |
| 4 | + |
| 5 | +- **Deployment Integrity**: Ensures the worker can be successfully deployed to the Cloudflare Workers runtime. |
| 6 | +- **Core Functionality**: Verifies critical features such as authentication, read/write operations, and permission handling function correctly end-to-end. |
| 7 | + |
| 8 | +## Architecture |
| 9 | + |
| 10 | +The test entry point is [`./test/it/smoke.test.js`](./test/it/smoke.test.js), which sets up the environment and executes the test suite defined in [`./test/it/it-tests.js`](./test/it/it-tests.js). The tests can run in two modes: |
| 11 | + |
| 12 | +1. **Local Mode**: Runs entirely on the local machine using mocks and local servers. |
| 13 | +2. **Stage Mode**: Runs against a deployed version of the worker on Cloudflare (used in CI). |
| 14 | + |
| 15 | +### 1. Local Mode |
| 16 | + |
| 17 | +**Local Mode** is the default for development. It orchestrates a local environment consisting of: |
| 18 | +- **`wrangler dev`**: Runs the `da-admin` worker locally. |
| 19 | +- **`S3rver`**: A local S3-compatible object storage server to mock R2/S3. |
| 20 | +- **Mock IMS Server**: A local HTTP server simulating Adobe IMS for authentication. |
| 21 | + |
| 22 | +**Configuration:** |
| 23 | +- Environment variables are automatically loaded from [`./.dev.vars.it`](./.dev.vars.it). |
| 24 | +- No manual configuration is typically required. |
| 25 | +- **Note**: In **Local Mode**, the DA configuration is ephemeral and set up before each test run. |
| 26 | + |
| 27 | +**How to Run:** |
| 28 | +```bash |
| 29 | +npm run test:it |
| 30 | +``` |
| 31 | + |
| 32 | +### 2. Stage Mode (CI/CD) |
| 33 | + |
| 34 | +In **Stage Mode**, tests execute against a live worker deployed to Cloudflare. This verifies the actual deployment artifacts and Cloudflare environment behavior. |
| 35 | + |
| 36 | +#### CI/CD Pipeline Flow |
| 37 | + |
| 38 | +The GitHub Actions workflow executes these tests in two phases: |
| 39 | + |
| 40 | +1. **Deployment**: |
| 41 | + - `npm run deploy:ci`: Uploads a new version of the worker (tagged with the branch name) to the `ci` environment. |
| 42 | + - Generates a `.deployment-env` file containing the `WORKER_VERSION_ID`, `WORKER_PREVIEW_URL` and `WORKER_PREVIEW_BRANCH`. |
| 43 | + |
| 44 | +2. **Verification**: |
| 45 | + - `npm run test:postdeploy`: Sources the `.deployment-env` file and runs the test suite. |
| 46 | + - When `WORKER_PREVIEW_URL` is present in the environment, [`smoke.test.js`](./test/it/smoke.test.js) switches to **Stage Mode**. |
| 47 | + - Tests authenticates against a real IMS environment (Stage/Prod) and requests are sent to the deployed worker. |
| 48 | + |
| 49 | +#### Running Stage Tests Locally |
| 50 | + |
| 51 | +To debug CI failures or test against a deployed worker from your local machine: |
| 52 | + |
| 53 | +1. **Deploy the Worker**: |
| 54 | + ```bash |
| 55 | + npm run deploy:ci |
| 56 | + ``` |
| 57 | + This script will generate the `.deployment-env` file in your root directory. |
| 58 | + |
| 59 | +2. **Configure Credentials**: |
| 60 | + Create a `.env` file (or set environment variables) with the required IMS credentials for the test account: |
| 61 | + ```env |
| 62 | + IT_IMS_STAGE_ENDPOINT=https://ims-na1.adobelogin.com |
| 63 | + IT_IMS_STAGE_CLIENT_ID=<client-id> |
| 64 | + IT_IMS_STAGE_CLIENT_SECRET=<client-secret> |
| 65 | + IT_IMS_STAGE_SCOPES=openid,AdobeID,aem.frontend.all,read_organizations,additional_info.projectedProductContext |
| 66 | + ``` |
| 67 | + |
| 68 | +3. **Run the Tests**: |
| 69 | + ```bash |
| 70 | + # Loads the deployment vars and runs the tests |
| 71 | + npm run test:postdeploy2 |
| 72 | + ``` |
| 73 | + |
| 74 | +### Persistence & Configuration |
| 75 | + |
| 76 | +In **Stage Mode**, the tests rely on the `DA_CONFIG_STAGE` KV storage for permissions. This configuration is persistent. |
| 77 | + |
| 78 | +If the configuration is lost or needs to be reset, the expected permission model is: |
| 79 | + |
| 80 | +```json |
| 81 | +{ |
| 82 | + "total": 2, |
| 83 | + "limit": 2, |
| 84 | + "offset": 0, |
| 85 | + "data": [ |
| 86 | + { |
| 87 | + "path": "CONFIG", |
| 88 | + "groups": "<test-user-email>", |
| 89 | + "actions": "write" |
| 90 | + }, |
| 91 | + { |
| 92 | + "path": "/+**", |
| 93 | + "groups": "<test-user-email>", |
| 94 | + "actions": "write" |
| 95 | + } |
| 96 | + ], |
| 97 | + ":type": "sheet", |
| 98 | + ":sheetname": "permissions" |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## IMS Configuration |
| 103 | + |
| 104 | +In **Stage Mode**, tests execute against the **IMS Stage** environment. |
| 105 | + |
| 106 | +### Prerequisites |
| 107 | + |
| 108 | +1. **Worker Configuration**: The `IMS_ORIGIN` secret for the `da-admin` worker (CI environment) must point to the IMS Stage endpoint. |
| 109 | +2. **User Existence**: Test users must exist and belong to an IMS Stage organization. No specific organization permissions are required beyond basic membership. |
| 110 | +3. **DA Configuration**: The test users must be explicitly granted permissions in the `DA_CONFIG` (as shown in the [Persistence & Configuration](#persistence--configuration) section). |
| 111 | + |
| 112 | +### Test Users Setup |
| 113 | + |
| 114 | +The integration tests use dedicated service accounts defined in the [Adobe Stage Developer Console](https://developer-stage.adobe.com/) under the `Document Authoring Stage` organization. Two distinct projects were created to simulate different user roles: |
| 115 | + |
| 116 | +- **Authenticated User Project**: |
| 117 | + - **Purpose**: Simulates a user who is logged in but may not have specific permissions (used for negative testing or basic access). |
| 118 | + - **Credentials**: Defined in CI secrets as `IT_IMS_STAGE_CLIENT_ID` / `IT_IMS_STAGE_CLIENT_SECRET`. |
| 119 | + - **API**: Uses `Edge Delivery Service` to create OAuth Server-to-Server credentials. |
| 120 | + |
| 121 | +- **Authorized User Project**: |
| 122 | + - **Purpose**: Simulates a user with full read/write permissions. |
| 123 | + - **Credentials**: (Currently the tests primarily use one set of credentials which are authorized in the config). |
| 124 | + - **API**: Uses `Edge Delivery Service` to create OAuth Server-to-Server credentials. |
| 125 | + |
| 126 | +> **Notes on Setup:** |
| 127 | +> 1. **Multiple Projects**: Two separate projects were created because generating multiple independent credentials within a single project was not supported. |
| 128 | +> 2. **Role Distinction**: The distinction between "authenticated" and "authorized" is managed entirely within the `DA_CONFIG` permissions sheet, not in IMS. The project naming reflects the intended use case. |
| 129 | +> 3. **API Selection**: The `Edge Delivery Service` API was selected for convenience. Any API service can be used provided it: |
| 130 | +> - Supports IMS connection. |
| 131 | +> - Includes the `read_organizations` scope. |
0 commit comments