hocinoH
Convex Community15mo ago
33 replies
hocino

Understand how Context works in test environment with Identity

Hi Everyone,

I'm a bit confused about the context in my test. Here's what I'm trying to achieve:

I want to check if a user can access a document created by another user. To do this:

1 - I created an identity (asViewer) for the user (viewer) who wants to access the document.
2 - Then, I verified that the owner of the document can retrieve this user by using t.run. This part works fine.
3 - Next, I called my mutation, which is responsible for adding permissions to the document for the viewer. I executed this mutation with the asOwner identity, but it throws an error. The mutation is unable to retrieve the viewer user.

Here is the relevant part of my code:

const { asUser: asViewer, user: viewer } = await createIdentity(t);
const getViewerAsOwner = await asOwner.run(async (ctx) => {
    return await ctx.db.get(viewer._id);
});

asOwner.mutation(api.core.permissions.insert, {
    subjectId: viewer._id,
    entityId: ownedApplication._id,
    entityType: 'applications',
    role: 'viewer',
});


I don't understand why I'm able to retrieve the viewer user when I execute asOwner.run, but not when I execute asOwner.mutation. Did I miss something? Could someone explain this behavior to me?

Thank you
Was this page helpful?