Seed data and logins
Describe your users and starting data once, and every sandbox comes up with both so a test can sign in and start straight away.
You list a user for each role your tests need, and the scripts that create the starting data. Every sandbox (a live copy of your app, also called a test environment) then comes up with both. Cofactor generates each password when it builds the snapshot, so none live in your repository.
Add logins and data
-
Declare a persona for each role. A persona is a named login. Only its email is fixed.
yamlpersonas: owner: { email: owner@example.test } member: { email: member@example.test } -
Make each login work. Your
preparecommands receive the logins asCOFACTOR_PERSONAS, a JSON map of persona name to email and password. Create or update that user in your app with that password.yamllifecycle: prepare: - bash sandbox/personas.shprepareruns while your app is up, so the app must read accounts on each sign-in. Don't load them once at startup. -
Write an action that creates data. An action is a script with typed inputs and outputs. It signs in as a persona and calls your app's own API. See the worked example in Pushed images for a full script.
yamlactions: create-organization: description: A new organization, created by the given persona. run: sandbox/actions/create-organization.sh input: name: { type: string, required: true } as: { type: persona, required: true } output: organization_id: { type: string }Inputs arrive as
INPUT_<NAME>variables. The script prints its result as JSON on the last stdout line that starts with{. Send logs to stderr. -
Seed it. An environment's seeds call actions in order, once per version.
yamlenvironments: default: description: Organization "Acme", owned by the owner persona. seeds: - { id: acme, action: create-organization, input: { name: Acme, as: owner } }Seeds are shared base data, so keep them small and generic. A test that needs its own data calls an action to create it.
-
Let tests sign in as a persona. Add an authentication script. When a test asks to sign in as a persona, Cofactor runs it in that run's own copy.
yamlauthentication: run: sandbox/auth.sh
Run
cofactor sandbox guide authenticationfor a complete script. -
Claim and check. Validate the file first:
cofactor sandbox validate, orcofactor sandbox validate --upload cofactor.sandbox.yamlfor a pushed image.bashcofactor sandbox claim --application "Acme App" --wait cofactor sandbox show <sandbox id>
The sandbox page showing its URLs, one login per role with the password hidden, and the seed outputs.
Check it worked
cofactor sandbox auth-check <sandbox id> --persona owner cofactor sandbox verify-env --application "Acme App"
verify-env claims each environment, runs its seeds, signs in every persona through your authentication script and destroys the copy. Each persona prints signed in. Open the URL, sign in as each role and confirm the seeded data is there.
Go deeper
- Seeds and actions
cofactor.sandbox.yaml- Emulators for Google, OpenID Connect and Cognito sign-in
- Receiving email
More environments, brand-new test users and common errors.
More environments. Add an environment when many tests need a starting state default can't give them, such as a plan or a feature flag. It can extend another, which runs the parent's seeds first.
environments:
free-plan:
extends: default
description: Same as default, with Acme on the Free plan.
seeds:
- { id: downgrade, action: set-plan, input: { organization_id: "${acme.organization_id}", plan: free } }
A brand-new user per run. For signup, invites or onboarding, a seed flow with a credential output mints a unique email with its own inbox for every run. Cofactor doesn't create the account in your app. The seed does. See Receiving email.
Passwords. Cofactor generates each persona's password when it builds a version's snapshot. Every sandbox claimed from that snapshot has the same password. Keep sensitive data out of sandboxes.
Errors.
| What you see | What to do |
|---|---|
| A persona can't sign in | prepare didn't set the login, or the app reads accounts only at startup. |
| A seed fails | The error names the step and the script's stderr. Claim an environment that stops before it, then run the action with cofactor sandbox run <id> <action> --input k=v. |
validate says a step "has not run yet" | Seeds run in order. Reference only earlier steps. |
| Seeds hit your login rate limit | Raise the limit for the sandbox in the top-level env:. |