#Convex-test withIdentity fails for mutation inside mutation

1 messages · Page 1 of 1 (latest)

umbral burrow
#

I have a test, it simply calls a mutation which checks for auth. It works. But if that mutation calls another mutation which checks for auth, it fails. I mean of course I do t.withIdentity, and it works if not deep. how to fix.

tranquil pebbleBOT
#

Hi @umbral burrow. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Short answer: only the top-level call gets the test identity. Nested Convex function calls don’t automatically inherit auth unless you pass it through or avoid calling public functions from other functions.

What you can do:

  • Refactor so public functions (with auth checks) call shared helpers, and only internal functions are invoked from other functions. Then the single top-level authed call is enough. [Best practices]

  • If you must invoke another function, make it internal and pass needed data (e.g., user id) as arguments instead of relying on ctx.auth in the nested call. [Scheduled auth note]

  • For tests, wrap the top-level call with t.withIdentity and ensure nested logic doesn’t perform another public mutation/query that expects ctx.auth; instead, call internal functions or helpers. [t.withIdentity]

If your current structure calls a public mutation from another mutation, that second mutation won’t see the identity from the test harness. Converting the inner one to an internal function (and doing auth only at the boundary) is the recommended fix. [Best practices]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: