Skip to content

Implement getUserIdentity syscall and fix authentication context propagation in nested contexts #50

@faramos

Description

@faramos

Problem Description

When using convex-test with libraries like convex-helpers that execute triggers, two critical authentication issues occur:

  1. Missing getUserIdentity syscall support: convex-test throws "convexTest does not support async syscall: 1.0/getUserIdentity" when functions try to access user identity
  2. Authentication context not propagated to triggers: Triggers receive contexts with _userIdentity: null instead of the mocked identity set with withIdentity()

Root Cause Analysis

Issue 1: Missing getUserIdentity syscall

The asyncSyscallImpl() function in index.ts doesn't implement the "1.0/getUserIdentity" case, causing authentication-related functions to fail.

Issue 2: Context propagation problem

In withAuth() function (lines 1773 and 1801), the context creation overwrites the original auth context:

// Line 1773 - runTransaction
const testCtx = { ...ctx, auth, ...extraCtx };

// Line 1801 - queryFromPath  
const testCtx = { ...ctx, auth };

This means when triggers execute, they don't have access to the authentication context that was set up with withIdentity().

Expected Behavior

  1. Functions should be able to call ctx.auth.getUserIdentity() without errors
  2. Triggers should have access to the same authentication context that was configured with withIdentity()
  3. Tests should replicate real-world authentication scenarios

Proposed Solution

Fix 1: Add getUserIdentity syscall support

Add the missing case in asyncSyscallImpl():

case "1.0/getUserIdentity": {
  const auth = getCurrentAuth(); // Need to implement this
  const identity = await auth.getUserIdentity();
  return JSON.stringify(convexToJson(identity));
}

Fix 2: Preserve authentication context in nested contexts

Modify context creation to preserve original auth when it exists:

// Line 1773 - runTransaction
const testCtx = { ...ctx, auth: ctx.auth || auth, ...extraCtx };

// Line 1801 - queryFromPath  
const testCtx = { ...ctx, auth: ctx.auth || auth };

Environment

  • convex-test version: 0.0.38
  • convex-helpers version: 0.1.104
  • convex version: 1.27.3

Impact

This affects any project using convex-test with:

  • Authentication-based functions
  • Trigger-based workflows (like convex-helpers)
  • Complex nested function calls that require authentication context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions