Docs/Seed data and logins
#

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.

Add logins and seed data to my sandbox

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

  1. Declare a persona for each role. A persona is a named login. Only its email is fixed.

    yaml
    personas:
      owner:  { email: owner@example.test }
      member: { email: member@example.test }
    
  2. Make each login work. Your prepare commands receive the logins as COFACTOR_PERSONAS, a JSON map of persona name to email and password. Create or update that user in your app with that password.

    yaml
    lifecycle:
      prepare:
        - bash sandbox/personas.sh
    

    prepare runs while your app is up, so the app must read accounts on each sign-in. Don't load them once at startup.

  3. 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.

    yaml
    actions:
      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.

  4. Seed it. An environment's seeds call actions in order, once per version.

    yaml
    environments:
      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.

  5. 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.

    yaml
    authentication:
      run: sandbox/auth.sh
    

    Run cofactor sandbox guide authentication for a complete script.

  6. Claim and check. Validate the file first: cofactor sandbox validate, or cofactor sandbox validate --upload cofactor.sandbox.yaml for a pushed image.

    bash
    cofactor 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

bash
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

Advanced

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.

yaml
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 seeWhat to do
A persona can't sign inprepare didn't set the login, or the app reads accounts only at startup.
A seed failsThe 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 limitRaise the limit for the sandbox in the top-level env:.