|
| 1 | +import { migrateProviderConnections } from "@scripts/commands/migrate-connections/migrate"; |
| 2 | +import { ObjectId } from "mongodb"; |
| 3 | +import { |
| 4 | + cleanupCollections, |
| 5 | + cleanupTestDb, |
| 6 | + setupTestDb, |
| 7 | +} from "@backend/__tests__/helpers/mock.db.setup"; |
| 8 | +import mongoService from "@backend/common/services/mongo.service"; |
| 9 | +import { setupSyncStorage } from "@sync/__tests__/helpers/storage"; |
| 10 | +import { GOOGLE_SCOPES } from "@sync/providers/google/google.scopes"; |
| 11 | +import { SYNC_COLLECTIONS } from "@sync/storage/collections"; |
| 12 | +import { CredentialRepository } from "@sync/storage/repositories/credential.repository"; |
| 13 | +import { ProviderConnectionRepository } from "@sync/storage/repositories/provider-connection.repository"; |
| 14 | +import { afterAll, afterEach, beforeAll, describe, expect, it } from "bun:test"; |
| 15 | + |
| 16 | +const NOW = new Date("2026-07-25T04:00:00.000Z"); |
| 17 | + |
| 18 | +describe("migrate-connections (db)", () => { |
| 19 | + const syncStorage = setupSyncStorage(import.meta.url); |
| 20 | + |
| 21 | + beforeAll(() => setupTestDb(import.meta.url)); |
| 22 | + afterEach(async () => { |
| 23 | + await cleanupCollections(); |
| 24 | + await mongoService.user.deleteMany({}); |
| 25 | + }); |
| 26 | + afterAll(cleanupTestDb); |
| 27 | + |
| 28 | + it("dry-run does not write Sync rows; apply then rerun is idempotent", async () => { |
| 29 | + const userId = new ObjectId(); |
| 30 | + await mongoService.user.insertOne({ |
| 31 | + _id: userId, |
| 32 | + email: "migrate@example.com", |
| 33 | + firstName: "Mig", |
| 34 | + lastName: "Rate", |
| 35 | + name: "Mig Rate", |
| 36 | + locale: "en", |
| 37 | + google: { |
| 38 | + googleId: "google-subject-migrate", |
| 39 | + picture: "", |
| 40 | + gRefreshToken: "legacy-refresh-token", |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + const connections = new ProviderConnectionRepository(syncStorage.db()); |
| 45 | + const credentials = new CredentialRepository(syncStorage.db()); |
| 46 | + const users = await mongoService.user.find({}).toArray(); |
| 47 | + |
| 48 | + const dry = await migrateProviderConnections( |
| 49 | + { connections, credentials }, |
| 50 | + users, |
| 51 | + { dryRun: true, now: NOW }, |
| 52 | + ); |
| 53 | + expect(dry.counts.wouldCreate).toBe(1); |
| 54 | + expect( |
| 55 | + await syncStorage |
| 56 | + .db() |
| 57 | + .collection(SYNC_COLLECTIONS.providerConnections) |
| 58 | + .countDocuments(), |
| 59 | + ).toBe(0); |
| 60 | + expect( |
| 61 | + await syncStorage |
| 62 | + .db() |
| 63 | + .collection(SYNC_COLLECTIONS.credentials) |
| 64 | + .countDocuments(), |
| 65 | + ).toBe(0); |
| 66 | + |
| 67 | + // Source credential must remain untouched. |
| 68 | + const sourceBefore = await mongoService.user.findOne({ _id: userId }); |
| 69 | + expect(sourceBefore?.google?.gRefreshToken).toBe("legacy-refresh-token"); |
| 70 | + |
| 71 | + const first = await migrateProviderConnections( |
| 72 | + { connections, credentials }, |
| 73 | + users, |
| 74 | + { dryRun: false, now: NOW }, |
| 75 | + ); |
| 76 | + expect(first.counts.created).toBe(1); |
| 77 | + expect(first.results[0]?.credentialVerified).toBe(true); |
| 78 | + const connectionId = first.results[0]?.connectionId; |
| 79 | + expect(connectionId).toMatch(/^[0-9a-f]{24}$/); |
| 80 | + |
| 81 | + const stored = await credentials.findByConnection(connectionId as never); |
| 82 | + expect(stored?.refreshToken).toBe("legacy-refresh-token"); |
| 83 | + expect(stored?.scopes).toEqual([...GOOGLE_SCOPES]); |
| 84 | + expect(stored?.accessToken).toBeNull(); |
| 85 | + |
| 86 | + const listed = await connections.listByPrincipal( |
| 87 | + userId.toHexString() as never, |
| 88 | + userId.toHexString() as never, |
| 89 | + ); |
| 90 | + expect(listed).toHaveLength(1); |
| 91 | + expect(listed[0]?.state).toBe("importing"); |
| 92 | + expect(listed[0]?.account.providerAccountId).toBe("google-subject-migrate"); |
| 93 | + |
| 94 | + const second = await migrateProviderConnections( |
| 95 | + { connections, credentials }, |
| 96 | + users, |
| 97 | + { dryRun: false, now: NOW }, |
| 98 | + ); |
| 99 | + expect(second.counts.updated).toBe(1); |
| 100 | + expect(second.results[0]?.connectionId).toBe(connectionId); |
| 101 | + expect( |
| 102 | + await syncStorage |
| 103 | + .db() |
| 104 | + .collection(SYNC_COLLECTIONS.providerConnections) |
| 105 | + .countDocuments(), |
| 106 | + ).toBe(1); |
| 107 | + |
| 108 | + const sourceAfter = await mongoService.user.findOne({ _id: userId }); |
| 109 | + expect(sourceAfter?.google?.gRefreshToken).toBe("legacy-refresh-token"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("migrates an OAuth user and skips a password-only user", async () => { |
| 113 | + const oauthId = new ObjectId(); |
| 114 | + const passwordId = new ObjectId(); |
| 115 | + await mongoService.user.insertMany([ |
| 116 | + { |
| 117 | + _id: oauthId, |
| 118 | + email: "oauth@example.com", |
| 119 | + firstName: "O", |
| 120 | + lastName: "Auth", |
| 121 | + name: "O Auth", |
| 122 | + locale: "en", |
| 123 | + google: { |
| 124 | + googleId: "google-oauth", |
| 125 | + picture: "", |
| 126 | + gRefreshToken: "oauth-refresh", |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + _id: passwordId, |
| 131 | + email: "password@example.com", |
| 132 | + firstName: "P", |
| 133 | + lastName: "Word", |
| 134 | + name: "P Word", |
| 135 | + locale: "en", |
| 136 | + }, |
| 137 | + ]); |
| 138 | + |
| 139 | + const report = await migrateProviderConnections( |
| 140 | + { |
| 141 | + connections: new ProviderConnectionRepository(syncStorage.db()), |
| 142 | + credentials: new CredentialRepository(syncStorage.db()), |
| 143 | + }, |
| 144 | + await mongoService.user.find({}).toArray(), |
| 145 | + { dryRun: false, now: NOW }, |
| 146 | + ); |
| 147 | + |
| 148 | + expect(report.counts.created).toBe(1); |
| 149 | + expect(report.counts.skipped).toBe(1); |
| 150 | + expect( |
| 151 | + report.results.find((r) => r.userId === passwordId.toHexString()) |
| 152 | + ?.skipCategory, |
| 153 | + ).toBe("no_google_identity"); |
| 154 | + }); |
| 155 | +}); |
0 commit comments