Test sign-in with test end users #
On this page
Prompt:
Test my app's signed-in pages without me signing in.
Also works:
- "Sign a test user in to the development environment and check the account page."
- "Run the native app's sign-in, refresh, and sign-out with no person."
- "My local run needs a signed-in session to test
/me."
A test end user is an end user your AI tool creates on one of your application's realms, at an address at synthetic.turnzero.ai, which receives no mail. Your AI tool reads its sign-in code through a management action instead of a mailbox. It signs in through your application's own sign-in pages, so your code runs exactly as it does for a person.
Your AI tool creates test end users with tzcloud_add_test_end_users and labels (step 2), and reads their codes with tzcloud_read_test_signin_code (step 3). It can instead run the line tzcloud_sign_in_test_end_user returns (step 4).
That line runs under a token you minted. On production, and under workforce in either environment, the token needs the test_end_users grant. It tests your signed-in routes. Test end users expire 48 hours after creation; tzcloud_add_test_end_users with remove: true removes them sooner (step 9).
What your AI tool does #
- Reads the
add-sign-inskill (read_context, idskill:add-sign-in) and follows its rule on test end users. - Signs a test end user in with
curl, from a browser, or with the commandtzcloud_sign_in_test_end_userreturns. It keeps any cookie jar outside the app folder and never prints the session's value. - Tests the signed-in routes you name, leaves the test end users to expire on their own unless you ask for them removed sooner, and deletes the jar whatever the tests' outcome.
- Creates test end users, reads their codes, and signs them in with
tzcloud_request_routeunder its own signed-in connection, in any environment, with no approval. - Before running
tzcloud_sign_in_test_end_user's line on production, asks you to mint a token with thetest_end_usersgrant. Outside theworkforceaudience, it first offers development instead. Underworkforce, the same holds for development. - Without such a token, signs in with the curl or browser route instead.
- For browser checks, uses development's
<application label>-dev.ai.hostwhere development is on. Applies its own rules before entering a sign-in code.
What you need #
- An application whose manifest declares the
accountsservice (Add sign-in to your app). - For
tzcloud_sign_in_test_end_user's line on production, and in either environment underworkforce, a token you minted with thetest_end_usersgrant (A token for test end users). Your AI coding tool's signed-in connection needs none.
Before your AI starts #
For the AI tool: Check the following before starting.
- A connected, signed-in AI tool (Connect your AI assistant), acting as the account that owns the application. No other account's credential reaches these actions.
- Whether the application has a development environment, from the environments
tzcloud_read_statuslists. - For backend tests that need no live sign-in, the Account package's double first (Test with the double). Test end users are the live half: the sign-in pages, the session cookie, and the verification.
Steps #
1. Choose the environment #
Most applications start with production alone. Your AI tool's own signed-in session, the connection you signed in to, creates test end users and reads their codes in any environment, with no approval. Your AI tool's MCP connection is that session where you signed it in through the browser. A connection set up with a token you minted holds that token instead, and a refusal's detail names the credential it read.
A token you minted with the test_end_users grant does the same, as A token for test end users says. On production, and on a workforce realm, each creation writes one line to the platform's record. The line gives the application, the environment, how many it created, and the credential that created them.
A token you minted without the grant is refused on production (Test on production from a pipeline). The line tzcloud_sign_in_test_end_user returns (step 4) runs under a token you minted, so on production that token needs the grant. Without it, sign in as step 3 or step 5 does, or with tzcloud_request_route (step 8).
You can test on development instead, once tzcloud_create_environment adds it (Test in development, then promote): outside the workforce audience, a development realm accepts every credential from its creation. Adding development removes no test end user from production, and your session still creates them there. Development lasts once added: every later deploy goes through it, so add it only where the account's owner wants it, never only to run a test.
Under the workforce audience, your AI tool's own signed-in session creates test end users and reads their codes in either environment with no approval, and so does a token with the test_end_users grant.
A token you minted without the grant is refused in either environment. A minted token is stored in a pipeline or on a machine. Only the grant you gave it lets it sign a test end user in past your organization's sign-in with no person present.
First set the realm's entra route to the tenant your audience declares (Add work-account sign-in): until it names that tenant, no one signs in, test end users included.
A test end user's address is at synthetic.turnzero.ai and has no source, so a route that admits only tenant addresses refuses it. Test with a route that admits any signed-in user. Prove your code's check for tenant addresses offline, over the Account package's double, as Test with the double shows.
2. Create the test end users #
Call tzcloud_add_test_end_users with application, environment, and one to twenty labels, such as tester-one. The tzcloud_add_test_end_users reference gives each label's form and every response member.
{ "application": "<application id>", "environment": "production", "labels": ["tester-one"] }
Each label becomes the verified address <label>@synthetic.turnzero.ai. The answer gives test_signin_page, where step 3 signs in, and each test end user's id, which steps 4, 8, and 9 use.
Each test end user lasts 48 hours from its creation, and the platform then removes it. A call naming expires_in_days is refused, because the lifetime is fixed.
Repeating the call is safe. A label an unexpired test end user already holds returns that user with created: false and creates nothing. A label whose test end user has expired is created again. Test end users are excluded from ordinary user limits, as Each environment's realm says.
3. Sign one in with curl #
A tool with no browser signs in with curl, keeping the cookies in a jar file. The jar holds a live session, so it goes in the system's temporary folder, never in your project folder, where it could be committed or uploaded. Set HOST to the origin test_signin_page begins with: https://<application label>.ai.host on production or https://<application label>-dev.ai.host on development. <application label> is your application's hostname label, not the test end user's. Send that origin with each POST, because the sign-in pages accept same-origin requests alone.
A tool's shell may keep no variables from one call to the next, so each block sets HOST and JAR again, and the jar keeps one fixed name. With --fail, a refused start or confirmation exits with an error that gives its status: stop there and find the status in Refusals.
HOST=https://<application label>.ai.host
JAR="${TMPDIR:-/tmp}/turnzero-test-signin-tester-one.jar"
curl -sS --fail -o /dev/null -c "$JAR" -b "$JAR" -H "Origin: $HOST" \
--data-urlencode "address=tester-one@synthetic.turnzero.ai" "$HOST/__account/email/start"
The platform holds the code instead of sending it. Read it with tzcloud_read_test_signin_code, which returns codes, newest first.
{ "application": "<application id>", "environment": "production", "email": "tester-one@synthetic.turnzero.ai" }
In place of email, you can pass end_user with the test end user's id or its label, here tester-one. A value that is one user's id and another user's label is refused invalid_request; pass email then.
A code lives ten minutes, allows five attempts, and confirms only with the cookies of the start that asked for it, which the jar keeps. Where several sign-ins are under way, pass binding, the base64url SHA-256 of that start's __Host-turnzero_cloud_signin_code cookie value, to read its code alone. This line prints the binding from the jar without printing the cookie's value.
JAR="${TMPDIR:-/tmp}/turnzero-test-signin-tester-one.jar"
awk '$6 == "__Host-turnzero_cloud_signin_code" { printf "%s", $7 }' "$JAR" | openssl dgst -sha256 -binary | openssl base64 -A | tr '+/' '-_' | tr -d '='
Confirm the newest code with the same jar, then call a route with the session.
HOST=https://<application label>.ai.host
JAR="${TMPDIR:-/tmp}/turnzero-test-signin-tester-one.jar"
curl -sS --fail -o /dev/null -c "$JAR" -b "$JAR" -H "Origin: $HOST" \
--data-urlencode "code=<code>" "$HOST/__account/email/verify"
curl -sS --fail -b "$JAR" "$HOST/__account/me"
The jar now holds the session cookie __Host-turnzero_cloud_realm, and /__account/me returns the test end user (Show the user and sign out). Send later requests with -b "$JAR". Never print the jar or the cookie's value.
On Windows, where step 4's call is admitted, its command_windows line does all of this in one line. In PowerShell, 7 or Windows PowerShell 5.1, step 3's start, binding, and confirmation read as below. Write curl.exe, since curl alone is Invoke-WebRequest in Windows PowerShell 5.1 (Windows PowerShell), and NUL for /dev/null. $Host is PowerShell's own variable, so the origin is $Origin, and the jar goes in $env:TEMP.
$Origin = 'https://<application label>.ai.host'
$Jar = Join-Path $env:TEMP 'turnzero-test-signin-tester-one.jar'
curl.exe -sS --fail -o NUL -c $Jar -b $Jar -H "Origin: $Origin" --data-urlencode 'address=tester-one@synthetic.turnzero.ai' "$Origin/__account/email/start"
This prints the binding from the jar without printing the cookie's value.
$Jar = Join-Path $env:TEMP 'turnzero-test-signin-tester-one.jar'
$Value = ((Select-String -Path $Jar -Pattern "`t__Host-turnzero_cloud_signin_code`t" | Select-Object -First 1).Line -split "`t")[6]
$Hash = [System.Security.Cryptography.SHA256]::Create().ComputeHash([System.Text.Encoding]::UTF8.GetBytes($Value))
[Convert]::ToBase64String($Hash).TrimEnd('=').Replace('+', '-').Replace('/', '_')
$Origin = 'https://<application label>.ai.host'
$Jar = Join-Path $env:TEMP 'turnzero-test-signin-tester-one.jar'
curl.exe -sS --fail -o NUL -c $Jar -b $Jar -H "Origin: $Origin" --data-urlencode 'code=<code>' "$Origin/__account/email/verify"
curl.exe -sS --fail -b $Jar "$Origin/__account/me"
4. Sign one in with the turnzero command #
The tzcloud_sign_in_test_end_user action returns a command that automates step 3, and the command runs under a token you minted. Outside the workforce audience, any such token runs it on development. On production, and under workforce in either environment, set TURNZERO_CLOUD_MINTED_TOKEN to a token with the test_end_users grant. Without it, use step 3 or step 5 instead.
This example uses development. Turn it on first if you chose to test there, and create tester-one in that environment as step 2 describes. Call tzcloud_sign_in_test_end_user with the application, the environment, and the test end user, by its id or by its label.
{ "application": "<application id>", "environment": "development", "end_user": "tester-one" }
The response contains no session. It returns command and command_windows for an installed turnzero, plus command_npx and command_npx_windows to run it through npx. Your AI tool first uses command_npx, or command_npx_windows on Windows, with or without a global installation.
Run the line once, exactly as returned, from your application's folder, under this computer's token or TURNZERO_CLOUD_MINTED_TOKEN. Running and writing lines explains what to do if npx can't run here. The line signs the test end user in and writes the session's cookie into the file after --out, in the form curl -b reads. The value never passes through your AI tool.
turnzero session --application <application id> --environment development --end-user tester-one --out ../turnzero-session-tester-one.txt --force
The line puts the file in the folder above your application's, ../turnzero-session-tester-one.txt, named for the test end user's label. Pass another --out to move it. The command refuses a path inside the folder it runs in if that folder holds package.json. Then send requests with curl -b and that path, as in step 3.
The session lasts at most 1 hour after sign-in. It ends sooner if the test end user expires or the realm token's lifetime ends. The line's --force lets it run again for a new session. Delete the file when the tests end, as step 9 does for the jar.
HOST=https://<application label>-dev.ai.host
curl -sS --fail -b ../turnzero-session-tester-one.txt "$HOST/__account/me"
In PowerShell, that call is curl.exe -sS --fail -b ../turnzero-session-tester-one.txt "https://<application label>-dev.ai.host/__account/me".
Where the token the line runs under needs the grant, the answer opens with a sentence saying so. That is production, and either environment under the workforce audience. Run the line there with TURNZERO_CLOUD_MINTED_TOKEN set to a token with the test_end_users grant: a token without it is refused test_end_users_not_admitted when the line runs. Otherwise sign in as step 3 or step 5 does.
5. Sign one in from a browser #
Open test_signin_page, which is /__account/signin/test on the environment's hostname. It shows the emailed-code form even where the realm offers only Google or other providers. Where the realm offers email, the ordinary sign-in page works too.
Enter the test end user's address, read the code with tzcloud_read_test_signin_code as in step 3, and type the newest code on the code page. The confirmation opens a real session, as it does for any end user. No passkey is offered, and a test end user cannot register one.
For your AI tool's browser check, use development's test_signin_page when development is on. The tool still follows its own rules for entering codes. With production alone, use tzcloud_request_route or tzcloud_write_route with test_end_user as in step 8. Follow that step's limits on writes. Report browser checks not run. API checks do not run the page's scripts.
6. Sign one in from a native app #
A native app's sign-in opens the authorization endpoint in the system's browser sheet, as Sign in from a native app describes. The endpoint redirects to /__account/signin?authorization=<reference>. On a realm without email, open /__account/signin/test with the same authorization query instead.
Sign in there as in step 5. The sheet then returns to the app's redirect URI with a code, and the app's own code exchange, refresh, and revocation run unchanged.
7. Test a local run #
With turnzero 0.38.0 or later, you can sign in through a browser on your machine. First provision the local run as Run your app on your computer describes, then start:
turnzero run --port 3000 --sign-in <hostname>
Use turnzero.cmd in Windows PowerShell. Name the application's development hostname where development is on, otherwise its production hostname or a served custom domain. The command checks that the hostname belongs to the realm your local run verifies against.
Wait for the local application's health path to return 200 before testing its own routes. Until the application listens, the command's listener responds with 502.
Open http://localhost:3000/__account/signin/test and use a test end user created in that environment. Read its code with tzcloud_read_test_signin_code, as for the hosted browser route. Open the local application's signed-in page and check that its own user read succeeds.
The test page uses an emailed code and offers no passkey. Federated sign-in and passkeys complete on the hosted hostname, so they cannot complete this local sign-in. Browser sign-in on localhost has not been checked in Chromium-family browsers, Firefox, or Safari.
The browser sends this session to every server you open at localhost, on any port. Use the route only while you trust the other local projects running on this computer. The command's sign-in listener binds only to loopback and closes when the application ends.
For scripts, an older command, or a run without --sign-in, sign in on the hosted realm instead:
- With development on, use the development hostname,
<application label>-dev.ai.host. - With one environment, use the production hostname and a test end user your AI tool's session created there (step 2). A local run of such an application verifies production's sessions.
Sign in with curl as in step 3, then hand the session from the jar to the local run as the request's Cookie header, or as a bearer where your backend reads one. Tell which user sent a request describes verification under the development credential the run already holds.
This block reads the session from the jar into SESSION and sends it to the local run's /me; never echo $SESSION.
JAR="${TMPDIR:-/tmp}/turnzero-test-signin-tester-one.jar"
SESSION=$(awk '$6 == "__Host-turnzero_cloud_realm" { print $7 }' "$JAR")
curl -sS -H "Cookie: __Host-turnzero_cloud_realm=$SESSION" "http://localhost:<port>/me"
In PowerShell, the same hand-off reads the session into $Session; never print it.
$Jar = Join-Path $env:TEMP 'turnzero-test-signin-tester-one.jar'
$Session = ((Select-String -Path $Jar -Pattern "`t__Host-turnzero_cloud_realm`t" | Select-Object -First 1).Line -split "`t")[6]
curl.exe -sS -H "Cookie: __Host-turnzero_cloud_realm=$Session" "http://localhost:<port>/me"
8. Test the signed-in paths #
Call your application's signed-in routes with the session.
A tool with no shell calls a deployed route with tzcloud_request_route instead, or tzcloud_write_route for a route that writes. It passes the test end user's id or label as test_end_user, with the environment. The platform signs that test end user in for that call, and ends the session when the call ends.
tzcloud_request_route and tzcloud_write_route send the session as a cookie by default. For a route that reads a bearer token, also pass session_as: "bearer". The platform sends Authorization: Bearer instead, including on same-host redirects, with no cookie.
Wherever the session's value appears as written in the answer's url, headers, or body, the platform replaces it, so the answer holds no copy of it. A value your application sends back encoded, such as in base64, is not replaced.
Your AI tool's signed-in session, and a token with the test_end_users grant, can do this in any environment. A token you minted without the grant is refused on production, and in either environment under the workforce audience. A call that also sets a cookie header is refused. With session_as: "bearer", also leave out the authorization header: the platform supplies it.
Without the grant on production and with nobody present, call each signed-in route with tzcloud_request_route without a session, test_end_user, or HTML Accept. Under invited or workforce, expect 401 outside session_free_paths: this proves only the gate refuses. Run signed-in checks on existing development outside workforce. Report each unrun signed-in check to the owner, with the remedy: a token they mint with the grant or your AI coding tool's own signed-in session.
Each call is a new sign-in, and it counts against the same limit as the sign-ins of steps 3 and 4: by default, five code starts an hour for one address. A sixth sign-in within the hour is refused signin_code_rate_limited. Sign another test end user in, or raise limits.code_sends_per_hour with tzcloud_configure_app_sign_in (The limits on emailed codes).
To check up to five routes with one sign-in, pass a routes list, each entry with its path and optional expect, an integer status from 100 to 599. Under tzcloud_write_route each entry also names its method, so reads and writes take a call each. The answer's results holds one result per entry. Each body comes back up to 12 KiB, and the whole list ends within 60 seconds.
failed counts results refused with route_unreachable, results whose status differs from expect, and results with a status of 400 or more where no expect was given. Set expect: 404 to check that a route is absent; a matching 404 does not count as failed. invalid_request refuses the whole call instead of appearing in results.
To check that a page redirects with 302, set follow_redirects: false and expect: 302 in its routes entry. The result shows the redirect instead of following it.
Each result whose request names expect carries expected: true when its status matches and false otherwise, including a refused result.
To test what a suspended user sees, call tzcloud_set_end_user_standing with the test end user's id as end_user and standing: suspended, then with standing: active to restore it.
The rows your routes write for a test end user are your application's own data, and they stay after the user is removed.
When verifying the first deploy, create a temporary test end user and use it for signed-in reads or the app's AI chat. These checks need no further confirmation, even if the app stores that user's own chat turns. Removing the data these checks wrote for that test user also needs no further confirmation. The test user expires after 48 hours. On an app with real users, ask the owner to confirm any other production write; a request to build, deploy, or test alone does not approve it.
On an app with no real users, test writes on development where available, otherwise freely on its live copy. Report the test rows you wrote so the person can remove them before launch. Email beyond the person's own test addresses and paid calls with stored keys still need their request for this run. Check the release defines when an app has no real users.
Before deleting the test end user, remove the rows through the application's own route that deletes them. Call it with tzcloud_write_route, naming the same test_end_user and environment and the method the route takes. With a shell, you can instead call that route with curl and the signed-in jar from step 3.
If the application has no route to delete those rows, leave them and tell the owner which remain and where, using the test end user's label and the route that wrote them. The platform's tzcloud_query_database reads data but cannot remove it.
The sign-in limits apply to test end users as to anyone, so a busy test suite can meet them. By default, one address gets five code starts an hour, and one source address thirty sign-in starts. Reuse a session across tests, create more test end users, or raise the realm's limits (The limits on emailed codes).
9. Remove them #
Call tzcloud_add_test_end_users with remove: true, application, environment, and either end_users, their identifiers, or all: true. It needs no approval, because a test end user holds no person's data.
Use the same environment you used to create and sign in the test end users. With all: true, every test end user in that environment is removed; use end_users to remove only your test's users.
For the production curl or browser example, call tzcloud_add_test_end_users with remove: true on production:
{ "remove": true, "application": "<application id>", "environment": "production", "all": true }
For step 4's development command example, call tzcloud_add_test_end_users with remove: true on development:
{ "remove": true, "application": "<application id>", "environment": "development", "all": true }
Each removal ends the user's sessions on every router within sixty seconds. It deletes the user's held codes and ends the sign-ins they could still confirm. A test end user also ends:
- within ten minutes after its
expires_at; - with
tzcloud_delete_end_user, or its own account deletion from the application; - with its realm, when you delete the environment or the application.
Then delete the jar, whatever the tests' outcome, and even where you keep the test end users. Where the steps run as one script, trap 'rm -f "$JAR"' EXIT near its start deletes the jar however the script ends.
For step 3's curl route, remove its temporary jar:
rm -f "${TMPDIR:-/tmp}/turnzero-test-signin-tester-one.jar"
In PowerShell, Remove-Item -Force -ErrorAction SilentlyContinue (Join-Path $env:TEMP 'turnzero-test-signin-tester-one.jar') deletes it, and in a script, that line in the finally of a try around the steps deletes it however the script ends.
For step 4's command route, remove the file its --out names. With the example's path, run:
rm -f ../turnzero-session-tester-one.txt
In PowerShell, use Remove-Item -LiteralPath '../turnzero-session-tester-one.txt' -Force -ErrorAction SilentlyContinue. If you changed --out, remove that file instead. Keep this cleanup in the script's trap or finally too.
Test on production from a pipeline #
A pipeline, a scheduled job, or an agent with no person present tests on production under a token minted with the test_end_users grant (A token for test end users). The person claims it with token claim, and the job runs under TURNZERO_CLOUD_MINTED_TOKEN. The token creates test end users, reads their codes, and signs them in, in either environment and under every audience, with no approval in the browser.
A change of the declared audience, or of the tenant under workforce, leaves the token admitted. Revoke it with revoke_token to end its admission. Its test end users remain until their expiry, or until tzcloud_add_test_end_users with remove: true removes them.
A call to admit_test_end_users answers 404 unknown_action, and no approval admits a token: mint one with the test_end_users grant.
Expected result #
tzcloud_add_test_end_users with labels returns test_signin_page and one entry per label with created: true and an expires_at 48 hours away. After confirmation, GET /__account/me returns the test end user, and tzcloud_list_end_users lists it with test: true. With remove: true, the call returns removed, the identifiers it removed.
An admitted tzcloud_sign_in_test_end_user call returns the command and its Windows and npx forms, with no session value. Running that command writes the cookie jar and prints its hostname, test end user, expiry, and file path. A request with that jar reaches the app as the test end user.
Refusals #
See the refusals reference for other platform errors.
| Refusal | Status | Cause | Remedy |
|---|---|---|---|
test_end_users_not_admitted |
409 | A token you minted without the test_end_users grant called on production, or in either environment under workforce. The detail names the credential it read and the remedy. |
Mint a token with the test_end_users grant (A token for test end users) and call under it, or call from your AI coding tool's session. That session is an MCP connection signed in through the browser, not one set up with a token. Adding a development environment is no remedy: it lasts, and every later deploy goes through it. |
test_end_users_ceiling_reached |
409 | The call would pass twenty unexpired test end users on the realm, or fifty created by the application's realms within the hour. Nothing is created. | Remove test end users you no longer need, pass fewer labels, or wait for the hour to pass. |
not_test_end_user |
409 | A label's address belongs to a real end user, or the named user is not an unexpired test end user of that realm. A deletion naming one is refused whole. | Use the test end users tzcloud_list_end_users marks test: true. Remove a real end user with tzcloud_delete_end_user. |
no_such_end_user |
404 | tzcloud_read_test_signin_code named an end_user, by id or by label, that no user of that realm has. |
Use the id or the label that tzcloud_add_test_end_users with labels returned for the same environment. |
route_not_enabled |
400 | On a realm that does not offer email, an emailed-code start named an address outside synthetic.turnzero.ai. |
Use a test end user's address, or add email to the realm's sign_in_methods. |
signin_code_rate_limited |
429 | One address started more emailed-code sign-ins in the hour than the realm allows. | Reuse a session, create more test end users, or raise limits.code_sends_per_hour. |
signin_rate_limited |
429 | One source address started more sign-ins in the hour than the realm allows. | Reuse a session, or raise limits.signin_starts_per_hour. |
test_end_user_sign_in_unavailable |
503 | The line of tzcloud_sign_in_test_end_user, or tzcloud_request_route's test_end_user, reached a platform service that cannot sign a test end user in. No one is signed in. |
Sign in as step 3 or step 5 does. |
command_too_old |
426 | The command is older than the minimum_version the refusal names. No one is signed in. |
Update the turnzero command, then call tzcloud_sign_in_test_end_user again and run its returned line. |
local_route_required |
409 | A call to tzcloud_sign_in_test_end_user included delivery outside the HTTP action route. Nothing is signed in. |
Omit delivery from the MCP call and run the returned turnzero session line. The command writes the session to the cookie jar; keep that value out of the conversation. |
Related #
- Manage end users lists, suspends, and deletes end users, and lists every realm setting.
- Add sign-in to your app turns on the accounts service and chooses the sign-in methods.
- Sign in from a native app runs a native app's sign-in, refresh, and revocation.
- Test your application locally tests the backend offline on the Account package's double.
- Tell which user sent a request shows how a backend verifies the session.
- tzcloud_add_test_end_users, tzcloud_read_test_signin_code, and tzcloud_sign_in_test_end_user in the generated reference give each action's arguments and result.