Impersonate user / create test content
I'm working on a site that has a lot of user-generated content. I'm wondering what strategy I can follow to generate test content? Using the import command looks difficult, as my schema has a lot of relations, so i would need to carefully craft the import data making sure to always reference the correct IDs. I would prefer to just call the regular mutations to create data, but most mutations are requiring a logged in user.
Are there any best practices/suggestions how to approach this? Maybe there is some way to impersonate a user? So i would create a bunch of test users via import of json data, then call the regular mutations to create content, impersonating one of the previously created test users.
5 Replies
We're working on making the import command support this case, so stay tuned. If you want to impersonate a user, you can do so in the dashboard function runner: https://docs.convex.dev/dashboard/deployments/functions#assuming-a-user-identity
Functions | Convex Developer Hub
Functions Dashboard View The functions view shows
For importing seed data for testing, I would do something like here: gist is you have
npx convex run init
where you have a convex/init.ts
file with a default export of an internalMutation
which creates the users or fetches them from the DB, then calls the underlying functions passing in the user. If your code passes ctx
around and does the ctx.auth -> User lookup deep in the stack, then that would require refactoring your code to look up the authed user earlier and pass that around. If you're passing ctx around, you could make an AuthedCtx
that those funcitons take, which has an extra user
field instead of relying on auth
. Then you can do the lookup in one place but retrieve it in deeper stacks, and enforce with types that you won't call it from a non-authed context. Such a ctx could be made like https://stack.convex.dev/custom-functions for public mutationsCustomizing serverless functions without middleware
Re-use code and centralize request handler definitions with discoverability and type safety and without the indirection of middleware or nesting of wr...
Seeding Data for Preview Deployments
Now that we've launched Preview Deployments on Convex, you can test out backend changes easier than ever. But you may want to seed your project with d...
That sounds like a good approach. I'm doing the ctx.auth->User lookup already as the first thing in all mutations. But this means i always need to create an underlying function that does the actual work, passing in the user and other arguments, right? So far I only have this for mutations that are called both directly or implicitly from other mutations.
Yes I don’t believe you can patch the auth object from another action/mutation currently
@TripleSpeeder , somewhat off the record since this isn't strictly encouraged, but you can also call your mutation's function directly, and provide your own
ctx
argument to it. E.g. for testing you could pass { auth: { getUserIdentity: () => ({ // my fields })}}
... since it's all just javascript at the end of the day.Quick update here,
npx convex import
can now import relational data, and it can also seed data in preview deployments https://news.convex.dev/announcing-convex-1-9/Convex News
Announcing Convex 1.9
Big Import and Export Improvements
When we released ZIP file imports and exports in Convex 1.7, it wasn't long before industrious developers started hitting the limitations. We’ve got some big improvements for you.
* File storage can now be included in Snapshot Export
* Snapshot import via npx convex import