hocino
hocino
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
Excellent. Thank you @sshader 😊
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
Thank you
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
Oh I found the problem Just a missing 'await' 😱 Sorry for the time
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
here the initCtxAndUser code async function initCtxAndUser() { const t = convexTest(schema); const componentModules = import.meta.glob( '../../../node_modules/@convex-dev/sharded-counter/src/component/**/*.ts' ); t.registerComponent('shardedCounter', shardedSchema, componentModules); const { asUser, user } = await createIdentity(t); return { t, asUser, user }; }
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
Yes it's from a single run
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
I read many times my code I didn't find anything bad but if it works fine in your case something must be wrong in my way to implement this test
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
When I call inserttest asOwner, the mutation doesn't know the viewer user.
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
Here my mutation (a non-custom mutation) export const inserttest = mutation({ args: { subjectId: v.union(v.id(USERS_TABLE), v.id(TEAMS_TABLE)) }, handler: async (ctx, args) => { const f = await ctx.db.query('users').collect(); console.log('users', f) /* => users [ { _creationTime: 1733946485287, _id: '10000;users', email: 'Div9H@test.com', name: 'Div9H' } ] */ const subject = await ctx.db.get(args.subjectId); if (!subject) throw 'Subject not found TEST'; } });
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
I confirm I don't have the same database in function of where I question the database. Here all my code : test( testName( '#030 SERVER Retrieve an object if the current user has a role of viewer, contributor, admin, or owner on the object.' ), async () => { const { t, asUser: asOwner } = await initCtxAndUser(); const ownedApp = { name: 'My app', description: 'My app description' }; const ownedAppId = await asOwner.mutation( api.services.applications.insert, ownedApp ); const ownedApplication = await asOwner.query( api.services.applications.findOne, { id: ownedAppId } ); // Test for owner role expect(ownedApplication).toBeDefined(); expect(ownedApplication).not.toBeNull(); const { asUser: asViewer, user: viewer } = await createIdentity(t); await expect( asViewer.query(api.services.applications.findOne, { id: ownedApplication._id }) ).rejects.toThrow('entity.getEntityById - Method not allowed.'); const users = await t.run(async (ctx) => { return await ctx.db.query('users').collect(); }); console.log('users', users); /*=> users [ { _creationTime: 1733946484096, _id: '10000;users', email: 'PumeX@test.com', name: 'PumeX' }, { _creationTime: 1733946485074, _id: '10003;users', email: 'czMzP@test.com', name: 'czMzP' } ]*/ asOwner.mutation(api.core.permissions.inserttest, { subjectId: viewer._id }); const viewedApplication = await asViewer.query( api.services.applications.findOne, { id: ownedApplication._id } ); // Test for viewer role expect(viewedApplication).toBeDefined(); expect(viewedApplication).not.toBeNull(); } );
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
Indeed 😅 the non-custom mutation not returns the document.
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
What do you think about my custom Mutation export const SecureMutationBuilder = customMutation(mutation, { args: {}, input: async (ctx, args) => { const { userId, userPermissions } = await getPermissions(ctx); const db = wrapDatabaseWriter( ctx, ctx.db, await rlsRules(ctx, userId, userPermissions) ); return { ctx: { db, userId, userPermissions }, args }; } }); Do you notice something wrong or a bad approach ? Thank you for your help
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
Ok, I use the same convex-test vesion
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
Can the withIdentity could be the cause ?
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
it's just an helper to create an identity export async function createUser(t: TestConvex<any>) { const name = generateRandomString(5); return await t.run(async (ctx) => { const id = await ctx.db.insert(USERS_TABLE, { email: ${name}@test.com, name }); return (await ctx.db.get(id)) as Doc<'users'>; }); } export async function createIdentity(t: TestConvex<any>) { const user = await createUser(t); const asUser = t.withIdentity({ subject: user!._id, tokenIdentifier: user!._id, name: user.name }); return { user, asUser }; }
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
it's a 'classic' convex mutation not a custom function
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
And I have the same error.
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
So, I created a mutation just with the db.get as follow : export const inserttest = mutation({ args: { subjectId: v.union(v.id(USERS_TABLE), v.id(TEAMS_TABLE)) }, handler: async (ctx, args) => { const subject = await ctx.db.get(args.subjectId); if (!subject) throw 'Subject not found TEST'; } });
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
Yes, my mutation is a customFunction which returns a customCtx
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
So I execute 2 times the same query but I don't have the same result.
34 replies
CCConvex Community
Created by hocino on 12/11/2024 in #support-community
Understand how Context works in test environment with Identity
const getViewerAsOwner = await asOwner.run(async (ctx) => { return await ctx.db.get(viewer._id); }); console.log('getViewerAsOwner', getViewerAsOwner ) => getViewerAsOwner { _creationTime: 1733937360453, _id: '10003;users', email: 'dFVWE@test.com', name: 'dFVWE' } In my mutation const subject = await ctx.db.get(args.subjectId); console.log(subject) => null (subjectId has the same value than viewerId)
34 replies