|
| 1 | +import "@databuddy/test/env"; |
| 2 | + |
| 3 | +import { profileAliases, profiles } from "@databuddy/db/schema"; |
| 4 | +import { appRouter, type Context } from "@databuddy/rpc"; |
| 5 | +import { |
| 6 | + addToOrganization, |
| 7 | + cleanup, |
| 8 | + db, |
| 9 | + expectCode, |
| 10 | + hasTestDb, |
| 11 | + insertOrganization, |
| 12 | + insertWebsite, |
| 13 | + reset, |
| 14 | + signUp, |
| 15 | + userContext, |
| 16 | +} from "@databuddy/test"; |
| 17 | +import { createProcedureClient } from "@orpc/server"; |
| 18 | +import { afterAll, beforeEach, describe, expect, it } from "vitest"; |
| 19 | + |
| 20 | +const iit = hasTestDb ? it : it.skip; |
| 21 | + |
| 22 | +function call<T>(procedure: T, ctx: Context) { |
| 23 | + return createProcedureClient(procedure as any, { context: ctx }); |
| 24 | +} |
| 25 | + |
| 26 | +beforeEach(() => reset()); |
| 27 | +afterAll(() => cleanup()); |
| 28 | + |
| 29 | +async function seedProfile( |
| 30 | + websiteId: string, |
| 31 | + profileId: string, |
| 32 | + overrides: Partial<typeof profiles.$inferInsert> = {} |
| 33 | +) { |
| 34 | + await db() |
| 35 | + .insert(profiles) |
| 36 | + .values({ |
| 37 | + websiteId, |
| 38 | + profileId, |
| 39 | + email: `${profileId}@example.com`, |
| 40 | + displayName: `User ${profileId}`, |
| 41 | + traits: { plan: "pro" }, |
| 42 | + ...overrides, |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +describe("profiles.getByIds", () => { |
| 47 | + iit("returns profiles for an organization member", async () => { |
| 48 | + const user = await signUp(); |
| 49 | + const org = await insertOrganization(); |
| 50 | + await addToOrganization(user.id, org.id, "member"); |
| 51 | + const website = await insertWebsite({ organizationId: org.id }); |
| 52 | + await seedProfile(website.id, "user_1"); |
| 53 | + await seedProfile(website.id, "user_2", { email: null }); |
| 54 | + |
| 55 | + const result = await call(appRouter.profiles.getByIds, { |
| 56 | + ...userContext(user, org.id), |
| 57 | + })({ |
| 58 | + websiteId: website.id, |
| 59 | + profileIds: ["user_1", "user_2", "user_missing"], |
| 60 | + }); |
| 61 | + |
| 62 | + expect(result).toHaveLength(2); |
| 63 | + const first = result.find((p) => p.profileId === "user_1"); |
| 64 | + expect(first?.email).toBe("user_1@example.com"); |
| 65 | + expect(first?.displayName).toBe("User user_1"); |
| 66 | + expect(first?.traits).toEqual({ plan: "pro" }); |
| 67 | + }); |
| 68 | + |
| 69 | + iit("does not leak profiles from another organization's website", async () => { |
| 70 | + const outsider = await signUp(); |
| 71 | + const outsiderOrg = await insertOrganization(); |
| 72 | + await addToOrganization(outsider.id, outsiderOrg.id, "member"); |
| 73 | + |
| 74 | + const org = await insertOrganization(); |
| 75 | + const website = await insertWebsite({ organizationId: org.id }); |
| 76 | + await seedProfile(website.id, "user_1"); |
| 77 | + |
| 78 | + await expectCode( |
| 79 | + call(appRouter.profiles.getByIds, { |
| 80 | + ...userContext(outsider, outsiderOrg.id), |
| 81 | + })({ |
| 82 | + websiteId: website.id, |
| 83 | + profileIds: ["user_1"], |
| 84 | + }), |
| 85 | + "FORBIDDEN" |
| 86 | + ); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe("profiles.get", () => { |
| 91 | + iit("returns a profile with its device aliases", async () => { |
| 92 | + const user = await signUp(); |
| 93 | + const org = await insertOrganization(); |
| 94 | + await addToOrganization(user.id, org.id, "member"); |
| 95 | + const website = await insertWebsite({ organizationId: org.id }); |
| 96 | + await seedProfile(website.id, "user_1"); |
| 97 | + await db().insert(profileAliases).values([ |
| 98 | + { websiteId: website.id, anonymousId: "anon_a", profileId: "user_1" }, |
| 99 | + { websiteId: website.id, anonymousId: "anon_b", profileId: "user_1" }, |
| 100 | + ]); |
| 101 | + |
| 102 | + const result = await call(appRouter.profiles.get, { |
| 103 | + ...userContext(user, org.id), |
| 104 | + })({ websiteId: website.id, profileId: "user_1" }); |
| 105 | + |
| 106 | + expect(result?.profileId).toBe("user_1"); |
| 107 | + expect(result?.anonymousIds?.sort()).toEqual(["anon_a", "anon_b"]); |
| 108 | + }); |
| 109 | + |
| 110 | + iit("returns null for an unknown profile", async () => { |
| 111 | + const user = await signUp(); |
| 112 | + const org = await insertOrganization(); |
| 113 | + await addToOrganization(user.id, org.id, "member"); |
| 114 | + const website = await insertWebsite({ organizationId: org.id }); |
| 115 | + |
| 116 | + const result = await call(appRouter.profiles.get, { |
| 117 | + ...userContext(user, org.id), |
| 118 | + })({ websiteId: website.id, profileId: "user_ghost" }); |
| 119 | + |
| 120 | + expect(result).toBeNull(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments