feat: sync auth user (#20067)

This commit is contained in:
Jason Rasmussen 2025-07-23 09:59:33 -04:00 committed by GitHub
parent ab597155fa
commit 92384c28de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 507 additions and 41 deletions

View file

@ -0,0 +1,87 @@
import { Kysely } from 'kysely';
import { SyncEntityType, SyncRequestType } from 'src/enum';
import { UserRepository } from 'src/repositories/user.repository';
import { DB } from 'src/schema';
import { SyncTestContext } from 'test/medium.factory';
import { getKyselyDB } from 'test/utils';
let defaultDatabase: Kysely<DB>;
const setup = async (db?: Kysely<DB>) => {
const ctx = new SyncTestContext(db || defaultDatabase);
const { auth, user, session } = await ctx.newSyncAuthUser();
return { auth, user, session, ctx };
};
beforeAll(async () => {
defaultDatabase = await getKyselyDB();
});
describe(SyncEntityType.AuthUserV1, () => {
it('should detect and sync the first user', async () => {
const { auth, user, ctx } = await setup(await getKyselyDB());
const response = await ctx.syncStream(auth, [SyncRequestType.AuthUsersV1]);
expect(response).toHaveLength(1);
expect(response).toEqual([
{
ack: expect.any(String),
data: {
id: user.id,
isAdmin: user.isAdmin,
deletedAt: user.deletedAt,
name: user.name,
avatarColor: user.avatarColor,
email: user.email,
pinCode: user.pinCode,
hasProfileImage: false,
profileChangedAt: (user.profileChangedAt as Date).toISOString(),
oauthId: user.oauthId,
quotaSizeInBytes: user.quotaSizeInBytes,
quotaUsageInBytes: user.quotaUsageInBytes,
storageLabel: user.storageLabel,
},
type: 'AuthUserV1',
},
]);
await ctx.syncAckAll(auth, response);
await expect(ctx.syncStream(auth, [SyncRequestType.AuthUsersV1])).resolves.toEqual([]);
});
it('should sync a change and then another change to that same user', async () => {
const { auth, user, ctx } = await setup(await getKyselyDB());
const userRepo = ctx.get(UserRepository);
const response = await ctx.syncStream(auth, [SyncRequestType.AuthUsersV1]);
expect(response).toHaveLength(1);
expect(response).toEqual([
{
ack: expect.any(String),
data: expect.objectContaining({
id: user.id,
isAdmin: false,
}),
type: 'AuthUserV1',
},
]);
await ctx.syncAckAll(auth, response);
await userRepo.update(user.id, { isAdmin: true });
const newResponse = await ctx.syncStream(auth, [SyncRequestType.AuthUsersV1]);
expect(newResponse).toHaveLength(1);
expect(newResponse).toEqual([
{
ack: expect.any(String),
data: expect.objectContaining({
id: user.id,
isAdmin: true,
}),
type: 'AuthUserV1',
},
]);
});
});

View file

@ -37,6 +37,7 @@ describe(SyncEntityType.UserV1, () => {
email: user.email,
id: user.id,
name: user.name,
avatarColor: user.avatarColor,
},
type: 'UserV1',
},
@ -49,8 +50,7 @@ describe(SyncEntityType.UserV1, () => {
it('should detect and sync a soft deleted user', async () => {
const { auth, ctx } = await setup(await getKyselyDB());
const deletedAt = new Date().toISOString();
const { user: deleted } = await ctx.newUser({ deletedAt });
const { user: deleted } = await ctx.newUser({ deletedAt: new Date().toISOString() });
const response = await ctx.syncStream(auth, [SyncRequestType.UsersV1]);
@ -59,22 +59,12 @@ describe(SyncEntityType.UserV1, () => {
expect.arrayContaining([
{
ack: expect.any(String),
data: {
deletedAt: null,
email: auth.user.email,
id: auth.user.id,
name: auth.user.name,
},
data: expect.objectContaining({ id: auth.user.id }),
type: 'UserV1',
},
{
ack: expect.any(String),
data: {
deletedAt,
email: deleted.email,
id: deleted.id,
name: deleted.name,
},
data: expect.objectContaining({ id: deleted.id }),
type: 'UserV1',
},
]),
@ -85,7 +75,7 @@ describe(SyncEntityType.UserV1, () => {
});
it('should detect and sync a deleted user', async () => {
const { auth, ctx } = await setup(await getKyselyDB());
const { auth, user: authUser, ctx } = await setup(await getKyselyDB());
const userRepo = ctx.get(UserRepository);
@ -104,12 +94,7 @@ describe(SyncEntityType.UserV1, () => {
},
{
ack: expect.any(String),
data: {
deletedAt: null,
email: auth.user.email,
id: auth.user.id,
name: auth.user.name,
},
data: expect.objectContaining({ id: authUser.id }),
type: 'UserV1',
},
]);
@ -119,7 +104,7 @@ describe(SyncEntityType.UserV1, () => {
});
it('should sync a user and then an update to that same user', async () => {
const { auth, ctx } = await setup(await getKyselyDB());
const { auth, user, ctx } = await setup(await getKyselyDB());
const userRepo = ctx.get(UserRepository);
@ -128,12 +113,7 @@ describe(SyncEntityType.UserV1, () => {
expect(response).toEqual([
{
ack: expect.any(String),
data: {
deletedAt: null,
email: auth.user.email,
id: auth.user.id,
name: auth.user.name,
},
data: expect.objectContaining({ id: user.id }),
type: 'UserV1',
},
]);
@ -147,12 +127,7 @@ describe(SyncEntityType.UserV1, () => {
expect(newResponse).toEqual([
{
ack: expect.any(String),
data: {
deletedAt: null,
email: auth.user.email,
id: auth.user.id,
name: updated.name,
},
data: expect.objectContaining({ id: user.id, name: updated.name }),
type: 'UserV1',
},
]);