Skip to content

Commit

Permalink
Add playwright create/login test for all db
Browse files Browse the repository at this point in the history
  • Loading branch information
Timshel committed Jul 25, 2024
1 parent b428481 commit 55601d1
Show file tree
Hide file tree
Showing 17 changed files with 1,056 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ web-vault

# Vaultwarden Resources
resources

# Playwright tests
playwright
6 changes: 6 additions & 0 deletions playwright/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
logs
node_modules/
/test-results/
/playwright-report/
/playwright/.cache/
temp
27 changes: 27 additions & 0 deletions playwright/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM docker.io/library/debian:bookworm-slim

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN apt-get update \
&& apt-get install -y --no-install-recommends docker.io docker-compose git nodejs npm \
&& apt-get install -y --no-install-recommends \
ca-certificates \
curl \
libmariadb-dev-compat \
libpq5 \
openssl

RUN mkdir /playwright
WORKDIR /playwright

COPY package.json .
RUN npm install && npx playwright install-deps && npx playwright install firefox

COPY docker-compose.yml Dockerfile test.env ./
COPY compose ./compose

COPY *.ts test.env ./
COPY tests ./tests

ENTRYPOINT ["/usr/bin/npx", "playwright"]
CMD ["test"]
88 changes: 88 additions & 0 deletions playwright/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Integration tests

This allows running integration tests using [Playwright](https://playwright.dev/).
\
It usse its own [test.env](/test/scenarios/test.env) with different ports to not collide with a running dev instance.

## Install

This rely on `docker` and `docker-compose`.
Databases (`Mariadb`, `Mysql` and `Postgres`) and `Playwright` will run in containers.

### Running Playwright outside docker

It's possible to run `Playwright` outside of the container, this remove the need to rebuild the image for each change.
You'll additionally need `nodejs` then run:

```bash
npm install
npx playwright install-deps
npx playwright install firefox
```

## Usage

To run all the tests:

```bash
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright
```

To force a rebuild of the Playwright image:
```bash
DOCKER_BUILDKIT=1 docker-compose --env-file test.env build Playwright
```

To access the ui to easily run test individually and debug if needed (will not work in docker):

```bash
npx playwright test --ui
```

### DB

Projects are configured to allow to run tests only on specific database.
\
You can use:

```bash
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project mariadb
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project mysql
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project postgres
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite
```

### Running specific tests

To run a whole file you can :

```bash
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite tests/login.spec.ts
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite login
```

To run only a specifc test (It might fail if it has dependency):

```bash
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite -g "Account creation"
DOCKER_BUILDKIT=1 docker-compose --env-file test.env run Playwright test --project sqlite tests/login.spec.ts:16
```

## Writing scenario

When creating new scenario use the recorder to more easily identify elements (in general try to rely on visible hint to identify elements and not hidden ids).
This does not start the server, you will need to start it manually.

```bash
npx playwright codegen "http://127.0.0.1:8000"
```

## Override web-vault

It's possible to change the `web-vault` used by referencing a different `bw_web_builds` commit.

```bash
export PW_WV_REPO_URL=https://github.com/Timshel/oidc_web_builds.git
export PW_WV_COMMIT_HASH=8707dc76df3f0cceef2be5bfae37bb29bd17fae6
DOCKER_BUILDKIT=1 docker-compose --env-file test.env build Playwright
```
40 changes: 40 additions & 0 deletions playwright/compose/vaultwarden/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM playwright_vaultwarden_prebuilt AS vaultwarden

FROM node:18-bookworm AS build

arg REPO_URL
arg COMMIT_HASH

ENV REPO_URL=$REPO_URL
ENV COMMIT_HASH=$COMMIT_HASH

COPY --from=vaultwarden /web-vault /web-vault
COPY build.sh /build.sh
RUN /build.sh

######################## RUNTIME IMAGE ########################
FROM docker.io/library/debian:bookworm-slim

ENV DEBIAN_FRONTEND=noninteractive

# Create data folder and Install needed libraries
RUN mkdir /data && \
apt-get update && apt-get install -y \
--no-install-recommends \
ca-certificates \
curl \
libmariadb-dev-compat \
libpq5 \
openssl && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Copies the files from the context (Rocket.toml file and web-vault)
# and the binary from the "build" stage to the current stage
WORKDIR /

COPY --from=vaultwarden /start.sh .
COPY --from=vaultwarden /vaultwarden .
COPY --from=build /web-vault ./web-vault

ENTRYPOINT ["/start.sh"]
24 changes: 24 additions & 0 deletions playwright/compose/vaultwarden/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

echo $REPO_URL
echo $COMMIT_HASH

if [[ ! -z "$REPO_URL" ]] && [[ ! -z "$COMMIT_HASH" ]] ; then
rm -rf /web-vault

mkdir bw_web_builds;
cd bw_web_builds;

git -c init.defaultBranch=main init
git remote add origin "$REPO_URL"
git fetch --depth 1 origin "$COMMIT_HASH"
git -c advice.detachedHead=false checkout FETCH_HEAD

export VAULT_VERSION=$(cat Dockerfile | grep "ARG VAULT_VERSION" | cut -d "=" -f2)
./scripts/checkout_web_vault.sh
./scripts/patch_web_vault.sh
./scripts/build_web_vault.sh
printf '{"version":"%s"}' "$COMMIT_HASH" > ./web-vault/apps/web/build/vw-version.json

mv ./web-vault/apps/web/build /web-vault
fi
75 changes: 75 additions & 0 deletions playwright/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
version: '3.8'

services:
VaultwardenPrebuild:
container_name: playwright_vaultwarden_prebuilt
image: playwright_vaultwarden_prebuilt
build:
context: ..
dockerfile: Dockerfile
entrypoint: /bin/bash
restart: "no"

Vaultwarden:
container_name: playwright_vaultwarden-${ENV:-dev}
image: playwright_vaultwarden-${ENV:-dev}
network_mode: "host"
build:
context: compose/vaultwarden
dockerfile: Dockerfile
args:
REPO_URL: ${PW_WV_REPO_URL:-}
COMMIT_HASH: ${PW_WV_COMMIT_HASH:-}
env_file: ${ENV}.env
environment:
- DATABASE_URL=${DATABASE_URL:-dummy}
- I_REALLY_WANT_VOLATILE_STORAGE=${I_REALLY_WANT_VOLATILE_STORAGE:-}
restart: "no"

Playwright:
container_name: playwright_playwright
image: playwright_playwright
network_mode: "host"
build:
context: .
dockerfile: Dockerfile
environment:
- PW_WV_REPO_URL=${PW_WV_REPO_URL:-}
- PW_WV_COMMIT_HASH=${PW_WV_COMMIT_HASH:-}
restart: "no"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ..:/project

Mariadb:
container_name: playwright_mariadb
image: mariadb:11.2.4
env_file: test.env
healthcheck:
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
start_period: 10s
interval: 10s
ports:
- ${MARIADB_PORT}:3306

Mysql:
container_name: playwright_mysql
image: mysql:8.4.1
env_file: test.env
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
start_period: 10s
interval: 10s
ports:
- ${MYSQL_PORT}:3306

Postgres:
container_name: playwright_postgres
image: postgres:16.3
env_file: test.env
healthcheck:
test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
start_period: 20s
interval: 30s
ports:
- ${POSTGRES_PORT}:5432
22 changes: 22 additions & 0 deletions playwright/global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { firefox, type FullConfig } from '@playwright/test';
import { execSync } from 'node:child_process';
import fs from 'fs';

const utils = require('./global-utils');

utils.loadEnv();

async function globalSetup(config: FullConfig) {
// Are we running in docker and the project is mounted ?
const path = (fs.existsSync("/project/playwright/playwright.config.ts") ? "/project/playwright" : ".");
execSync(`docker-compose --project-directory ${path} --env-file test.env build VaultwardenPrebuild`, {
env: { ...process.env },
stdio: "inherit"
});
execSync(`docker-compose --project-directory ${path} --env-file test.env build Vaultwarden`, {
env: { ...process.env },
stdio: "inherit"
});
}

export default globalSetup;
Loading

0 comments on commit 55601d1

Please sign in to comment.