Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/scripts/build-demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
# The demo of this pack's own suite: a line of the framework on the official Ruby image by its digest, its app made in
# the image (demo/rails-demo-app.sh), in development mode, capped at 512 MB, its mail to a mail catcher beside it
# (Mailpit, by digest, capped at 128 MB), run as the image's own user (the app is the image's), its secret_key_base from the environment into a file only you can read
# (never from a command line; it refuses to start without one).
# RAILS_DEMO_SECRET_KEY_BASE=… bash .github/scripts/build-demo.sh <line> (8.1.4.1 or 8.0.5.2)
set -uo pipefail
LINE="${1:-8.1.4.1}"
RUBY='docker.io/library/ruby@sha256:5ac6b9f959a3356af5fc4f6a80a30106d99fd51785633f8a365be73cbe84e199' # 4.0.10-slim-bookworm
MAILPIT='docker.io/axllent/mailpit@sha256:1c0beb8db617aa61841cfcbb166529a6c246f606059038e3b0bf08d609fbfb3d' # v1.34.1
case "${LINE}" in
8.1.4.1) PORT=8780 ; MAIL=8790 ;;
8.0.5.2) PORT=8781 ; MAIL=8791 ;;
*) echo "No line ${LINE}: the demo has 8.1.4.1 and 8.0.5.2." ; exit 1 ;;
esac
if [ "${#RAILS_DEMO_SECRET_KEY_BASE}" -lt 64 ] ; then
echo "Set RAILS_DEMO_SECRET_KEY_BASE in the environment (64 characters or more): the demo never runs without its secret." ; exit 1
fi
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
TAG="${LINE//./-}" ; NAME="rails-steps-${TAG}" ; DIR="${ROOT}/demo-app/${TAG}" ; NET="${NAME}-net"
docker rm -f "${NAME}" "${NAME}-mail" > /dev/null 2>&1 ; docker network rm "${NET}" > /dev/null 2>&1
rm -rf "${DIR}" ; mkdir -p "${DIR}" ; chmod 700 "${DIR}"
( umask 077 ; printf '%s' "${RAILS_DEMO_SECRET_KEY_BASE}" > "${DIR}/secret_key_base" )
docker image inspect "${NAME}" > /dev/null 2>&1 || docker build -q -t "${NAME}" --build-arg RAILS_VERSION="${LINE}" \
--build-arg RUBY_IMAGE="${RUBY}" "${ROOT}/demo" > /dev/null || { echo "The demo's image could not be built." ; exit 1 ; }
docker network create "${NET}" > /dev/null || exit 1
docker run -d --name "${NAME}-mail" --network "${NET}" --network-alias mail --memory 128m --memory-swap 128m \
-p "127.0.0.1:${MAIL}:8025" "${MAILPIT}" > /dev/null || exit 1
docker run -d --name "${NAME}" --network "${NET}" --memory 512m --memory-swap 512m -p "127.0.0.1:${PORT}:3000" \
-e APP_HOST="127.0.0.1:${PORT}" -e APP_PROTOCOL=http -e SMTP_HOST=mail -e SMTP_PORT=1025 -e SIGN_IN_RATE_LIMIT=500 \
-e SECRET_KEY_BASE_FILE=/run/demo/secret_key_base -v "${DIR}/secret_key_base:/run/demo/secret_key_base:ro" \
"${NAME}" > /dev/null || exit 1
for i in $(seq 1 90) ; do
if [ "$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:${PORT}/up")" = 200 ] && curl -fs -o /dev/null "http://127.0.0.1:${MAIL}/api/v1/info" ; then
echo "The demo of Rails ${LINE} is up at http://127.0.0.1:${PORT} (its mail at http://127.0.0.1:${MAIL})." ; exit 0
fi
sleep 1
done
echo "The demo of Rails ${LINE} did not answer." ; docker logs --tail 40 "${NAME}" 2>&1 | sed -E 's/[0-9a-fA-F]{32,}/…/g' ; exit 1
10 changes: 10 additions & 0 deletions .github/scripts/remove-demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# Remove the demo of a line (its containers, its network, its files): bash .github/scripts/remove-demo.sh <line> [--image]
set -uo pipefail
LINE="${1:-8.1.4.1}"
case "${LINE}" in 8.1.4.1|8.0.5.2) ;; *) echo "No line ${LINE}: the demo has 8.1.4.1 and 8.0.5.2." ; exit 1 ;; esac
ROOT="$(cd "$(dirname "$0")/../.." && pwd)" ; TAG="${LINE//./-}" ; NAME="rails-steps-${TAG}"
docker rm -f "${NAME}" "${NAME}-mail" > /dev/null 2>&1 ; docker network rm "${NAME}-net" > /dev/null 2>&1
rm -rf "${ROOT}/demo-app/${TAG}"
[ "${2:-}" = --image ] && docker rmi "${NAME}" > /dev/null 2>&1
echo "The demo of Rails ${LINE} is removed."
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
- name: The secret of the run
run: |
for name in RAILS_DEMO_SECRET_KEY_BASE; do
secret="$(openssl rand -hex 24)"
secret="$(openssl rand -hex 64)"
echo "::add-mask::${secret}"
echo "${name}=${secret}" >> "$GITHUB_ENV"
done
Expand Down
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node_modules/
.claude/
tests/reports/*.json
tests/reports/*.html
tests/reports/*.pdf
screenshots/
videos/
traces/
test-results/
*.har
*.zip
*.log
.DS_Store
demo-app/
270 changes: 268 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,269 @@
# Ruby on Rails steps
# rails-steps

Cucumber step definitions for Ruby on Rails web apps, for webship-js
<p align="center"><img src="https://raw.githubusercontent.com/webship/rails-steps/2.0.x/assets/logo.svg" alt="Ruby on Rails logo: this pack supports Ruby on Rails" width="96"></p>

Cucumber step definitions for Ruby on Rails apps, for [webship-js](https://webship.co/docs/webship-js/2.0.x), a BDD
browser automation harness built on Playwright and Cucumber-js.

A Rails app is built from the same parts again and again: a scaffold's resources, the authentication generator's
sign-in and password reset, Turbo's forms and confirms, its CSRF guard and its health check. The steps follow those
conventions, so a scenario reads the same for any app made the Rails way, and they keep every password, session cookie,
secret_key_base and reset token out of their words, their messages, their failures and the reports:

- **users:** made in the app through its own console (`bin/rails runner -`, the code on its input, never on a command
line), with a random password kept in memory, and removed with their sessions at the scenario's end;
- **signing in and out:** the generator's sign-in page, a wrong password refused, the app's "Sign out", and the check
that a page then asks to sign in again;
- **a scaffold's resources:** made, edited and destroyed through their pages (Turbo's confirm answered, confirm or
cancel), listed or not, the notices the app sets, and the errors that refuse a record;
- **the app's guards:** its session cookies' flags for the page's scheme, a form posted without its CSRF token refused,
its health check;
- **a password reset by mail:** asked for, the mail read from the app's mail catcher (never its log), the link followed
and a new password chosen, the used link refused;
- **in development:** the app's error page for a missing record, without a secret on it;
- **no request outside the app's address.**

No step phrase names the platform: a scenario reads `When I create a "note" in the web application with:` and
`Then the web application should have mailed "reader@example.com" a password reset link`.

## Requirements

- Node.js 24 or newer.
- webship-js 2.0.20 or newer.
- A Rails app with the authentication generator's sign-in and password reset, and a console the suite can run Ruby in
(`bin/rails runner -`). Rails 8.1.4.1 and 8.0.5.2 are the two lines this pack is proven on, both on Ruby 4.0.10.
- For the password-reset steps, a mail catcher with Mailpit's API that the app's mail goes to.

## Install

```bash
npm install --save-dev @webship-js/rails-steps
```

List its step definitions in `cucumber.js`, after webship-js's own, and tell it the app's console and mail catcher:

```js
require: [
'node_modules/webship-js/tests/step-definitions/**/*.js',
'node_modules/@webship-js/rails-steps/tests/step-definitions/**/*.js',
'tests/step-definitions/**/*.js',
],
worldParameters: {
launchUrl: 'https://notes.ddev.site',
webApp: {
console: ['ddev', 'exec', 'bin/rails', 'runner', '-'], // or ['docker', 'exec', '-i', '<container>', 'bin/rails', 'runner', '-']
mailUrl: 'https://notes.ddev.site:8026', // Mailpit
secretKeyBase: process.env.WEB_APP_SECRET_KEY_BASE, // only to keep it out of the output
},
},
```

## Quick start

```gherkin
Feature: Notes
Background:
Given the web application has a user "editor@example.com"
And I am signed in to the web application as "editor@example.com"

Scenario: Made, refused, destroyed after a confirm
When I create a "note" in the web application with:
| Title | The quiet garden |
Then the web application should say "Note was successfully created."
When I try to create a "note" in the web application with:
| Title | |
Then the web application should show the error "Title can't be blank"
When I destroy the "note" "The quiet garden" in the web application and confirm
Then the web application should not list the "note" "The quiet garden"
And the web application should have sent no request outside its address
```

## From nothing to a green run

```bash
git clone https://github.com/webship/rails-steps && cd rails-steps
npm ci && npx playwright install chromium
export RAILS_DEMO_SECRET_KEY_BASE="$(openssl rand -hex 64)"
bash .github/scripts/build-demo.sh 8.1.4.1 # http://127.0.0.1:8780, its mail at :8790 (8.0.5.2: :8781, :8791)
npx cucumber-js # LINE=8.0.5.2 for the other line
npm run test:must-fail
bash .github/scripts/remove-demo.sh 8.1.4.1
```

### The demo app

It is made at the build, never shipped: `demo/rails-demo-app.sh` runs `rails new` (SQLite; no Kamal, Thruster, Docker,
CI, RuboCop or Brakeman files), a scaffold of notes (a title and a body), and the authentication generator, on the
official Ruby image by its digest:

| Rails line | Ruby | Image |
| --- | --- | --- |
| 8.1.4.1 | 4.0.10 | `docker.io/library/ruby@sha256:5ac6b9f959a3356af5fc4f6a80a30106d99fd51785633f8a365be73cbe84e199` (4.0.10-slim-bookworm) |
| 8.0.5.2 | 4.0.10 | the same |

Rails 8.0 was to run on Ruby 3.4.13 (`sha256:b9ca1b2b1f19a50b4f496c5c3af813151dd03a19ee053c157c4ee569c1e581bb`), so the
two lines covered two Rubies; its image's build tools could not be fetched (Debian's mirror stalled, twice), so both
lines run on Ruby 4.0.10, where both were proven.

Then a few additions of ours, each because a step or a person needs it:

- `root "notes#index"`: the generator sends a signed-in person to `root_url`, which `rails new` has none of;
- a title presence validation on a note;
- a "Sign out" button: the generator has the route (`DELETE /session`) and no control for it;
- a confirm on "Destroy this note" (Turbo's `data-turbo-confirm`, the browser's own dialog);
- the mail's links at the app's address (`APP_HOST`, `APP_PROTOCOL`), its mail to a mail catcher (`SMTP_HOST`);
- cookies Secure on https and plain on http: a middleware flags them on a secure request only (a TLS proxy's
`X-Forwarded-Proto` counts, from a trusted proxy). Not `force_ssl`: it redirects to https, and makes the session
cookie Secure on every request, so a plain-http port would keep no session (every form then failed its CSRF check,
422). Proven on the demo:

```
$ curl -s -D - -o /dev/null http://127.0.0.1:8780/session/new | grep -i set-cookie
set-cookie: _notes_session=…; path=/; httponly; samesite=lax
$ curl -s -D - -o /dev/null -H 'X-Forwarded-Proto: https' http://127.0.0.1:8780/session/new | grep -i set-cookie
set-cookie: _notes_session=…; path=/; httponly; samesite=lax; secure
```

The step `Then the web application's session cookies should be safe for its scheme` checks it for the page's own scheme;
- secret_key_base from a file only its user can read (`SECRET_KEY_BASE_FILE`), never from a command line;
- the generator's rate limits on signing in (both lines) and on reset requests (8.1; 8.0's generator has none), 10 in 3
minutes per address, from the environment (`SIGN_IN_RATE_LIMIT`, 10 when unset): **only this demo and the Webship
Workspace's development build set 500**, as a suite signs in more often than a person. A production app keeps the
generator's 10: never ship 500 to a public site;
- three made-up notes of ours as seeds ("The harbour walk", "The lighthouse steps", "The market list").

The demo runs in **development**, capped at 512 MB, beside a mail catcher (Mailpit 1.34.1 by digest, MIT, capped at
128 MB). Development, because one step needs it: the app's error page for a missing record (`@development`; a production
app answers a plain 404, and the step's scenario is left out with `WEB_APP_ENV=production`). Every other step runs on a
production app too.

### What the suite tests

Three features, seven scenarios: signing in and out (the cookies' flags, a wrong password, a password reset by mail
used once), notes (made, edited, a destroy cancelled then confirmed, a note refused without its title), and the app (its
health check, forms refused without their CSRF token, its development error page). Eleven must-fail scenarios check that
each assertion fails when it should, with its message and a hint (one against an address nothing answers); the check
then scans their output, their JSON and HTML reports and every file the runs left for the app's secret_key_base, a
session cookie's value and a reset token (`/passwords/<token>`), and fails on any trace or HAR file.

## What needs the app's console

The console (`worldParameters.webApp.console`: a command that runs the Ruby on its input in the app,
`bin/rails runner -`) is how the pack makes and removes what a scenario needs without a back door into the app:

- `Given the web application has a user "<email>"`, and so every step that signs that user in or resets its password;
- the clean-up at a scenario's end: the users it made (with their sessions) and the records it made through
`When I create a "<resource>" …`.

Every other step needs only the app's address (and, for the reset mail, its mail catcher). Against an outside Rails app,
give the pack a console that reaches it (`ddev exec`, `docker exec -i <container>`, `kubectl exec -i deploy/<name> --`,
each followed by `bin/rails runner -`). The Webship Workspace's core runs a suite off DDEV in a tests container of its
own that cannot reach an app's container yet ([webship/workspace#1056](https://github.com/webship/workspace/issues/1056)):
run such a suite from the host there.

## Settings

`worldParameters.webApp` in `cucumber.js`:

| Setting | Default | What it is |
| --- | --- | --- |
| `console` | none | The words of a command that runs the Ruby on its input in the app (`bin/rails runner -`). |
| `mailUrl` | none | The app's mail catcher (Mailpit's API). |
| `secretKeyBase` | none | The app's secret_key_base, from the environment, only to keep it out of the output. |

## Steps

22 step definitions; each has five examples in its JSDoc block.

### `rails-session.steps.js` (5)

- `Given the web application has a user "<email>"`: removed with its sessions at the scenario's end.
- `Given I am signed in to the web application as "<email>"`
- `When I try to sign in to the web application as "<email>" with a wrong password`
- `When I sign out of the web application`
- `Then I should be signed out of the web application`

### `rails-resources.steps.js` (8)

- `When I create a "<resource>" in the web application with:` (a table of `| label | value |`): removed at the scenario's end.
- `When I try to create a "<resource>" in the web application with:`: refused (422); the browser's console line for that
answer is allowed in this step only, for that path and status.
- `When I edit the "<resource>" "<text>" in the web application with:`
- `When I destroy the "<resource>" "<text>" in the web application and confirm` / `and cancel`
- `Then the web application should list the "<resource>" "<text>"` / `should not list`
- `Then the web application should say "<text>"`
- `Then the web application should show the error "<text>"`

### `rails-app.steps.js` (9)

- `Then the web application's session cookies should be safe for its scheme`: HttpOnly, SameSite Lax, Secure exactly on https.
- `Then the web application should refuse a "<METHOD> <path>" without its CSRF token`
- `Then the web application's health check should answer 200`
- `When I ask the web application to reset the password of "<email>"`: only for a user the scenario made.
- `Then the web application should have mailed "<email>" a password reset link`: from the mail catcher; the mail removed at
the scenario's end.
- `When I follow the password reset link and choose a new password`
- `Then the password reset link should be refused by the web application`
- `Then the web application should show its error page for a missing "<resource>"` (`@development`)
- `Then the web application should have sent no request outside its address`

## Secrets

A user's password is made for the scenario and lives in memory; it reaches the app inside the Ruby its console reads
on its input, and the page through the password field set by its id, never through a step's words or a command line.
The app's secret_key_base is only known to the pack to take it out of any text.

A password-reset token sits in a link's path, `/passwords/<token>/edit`, and a page there carries it in its address:
in the browser's messages, and in the `Page:` line webship-js adds to a failure. The pack masks the path and every
cookie value in its failures, in its messages and in webship-js's list of the browser's errors (until webship-js does
it: [webship/webship-js#707](https://github.com/webship/webship-js/issues/707)), and never turns on a trace or a HAR.

## What a run leaves behind

Nothing a later run sees: every user (with its sessions), record and mail a scenario makes is removed at its end, even
when it fails. The app's seeds stay as they were.

## The console

The suite runs in webship-js's `fail` mode with nothing let through for the whole suite. Two answers of the app show in
the browser's console at known moments, and the step of each moment allows only its own, then counts it in the report:

- a refused form (422): "Failed to load resource: … 422 (Unprocessable Content) @ <address>/<resources>", in the step
that sends it;
- the development error page (404): "Failed to load resource: … 404 … @ <address>/<resources>/999999999", in its step.

## Licence of the framework

Ruby on Rails is MIT. This pack carries no code of Rails: the demo installs it at the build.

## Phrases shared with other packs

None. Every phrase says "the web application" ("the web app" is the Flask pack's). Checked before the release against
the 71 published packs and the Jellyfin, Meilisearch, Express, Superset, Flask, Vaultwarden and Gitea packs: no example
or feature step of this pack matches another pack's definition, and no step of theirs matches one of this pack's.

## Testing this package

```bash
npm run test:examples # every example of every step matches its own definition only; every feature step one
npm run test:paths # a feature or a scenario named on the command line runs alone
npx cucumber-js --dry-run
```

CI, for each line (8.1.4.1 and 8.0.5.2), builds the demo and runs the suite twice and then the must-fail assertions.

## License

MIT.

## Logo

The logo is Simple Icons' Ruby on Rails icon (`icons/rubyonrails.svg` of simple-icons 16.39.0), in the colour the set
lists for it (`#D30001`); its path is unmodified. Simple Icons gives it no licence of its own and points to the Rails
trademark policy as its guidelines (rubyonrails.org/trademarks), which allows the logo "to say 'we support Ruby on
Rails'": that is its only use here. It is not covered by this package's licence.

---

Rails, Ruby on Rails, and the Rails logo are trademarks of the Rails Foundation. This project is not endorsed by or
affiliated with Rails, the trademark owner, or the Rails Foundation.
Loading
Loading