| 1 | import { AccountType } from '@yikart/common' |
| 2 | import { describe, expect, it, vi } from 'vitest' |
| 3 | import { AccountStatus } from '../schemas' |
| 4 | import { OAuth2CredentialRepository } from './oauth2-credential.repository' |
| 5 | |
| 6 | vi.mock('../schemas', () => ({ |
| 7 | AccountStatus: { |
| 8 | NORMAL: 1, |
| 9 | ABNORMAL: 0, |
| 10 | }, |
| 11 | OAuth2Credential: class OAuth2Credential {}, |
| 12 | })) |
| 13 | |
| 14 | describe('oAuth2CredentialRepository', () => { |
| 15 | it('lists expiring credentials only for normal existing accounts', async () => { |
| 16 | const records = [{ |
| 17 | accountId: 'account-1', |
| 18 | platform: AccountType.Facebook, |
| 19 | accessTokenExpiresAt: 1767225600, |
| 20 | }] |
| 21 | const exec = vi.fn(async () => records) |
| 22 | const model = { |
| 23 | aggregate: vi.fn(() => ({ exec })), |
| 24 | } |
| 25 | const repo = new OAuth2CredentialRepository(model as never) |
| 26 | |
| 27 | await expect(repo.listByAccessTokenExpiresAtAndNormalAccount(1767229200, 100)) |
| 28 | .resolves |
| 29 | .toBe(records) |
| 30 | |
| 31 | expect(model.aggregate).toHaveBeenCalledWith([ |
| 32 | { |
| 33 | $match: { |
| 34 | accessTokenExpiresAt: { $type: 'number', $lte: 1767229200 }, |
| 35 | refreshToken: { $type: 'string', $ne: '' }, |
| 36 | }, |
| 37 | }, |
| 38 | { $sort: { accessTokenExpiresAt: 1, _id: 1 } }, |
| 39 | { |
| 40 | $lookup: { |
| 41 | from: 'account', |
| 42 | localField: 'accountId', |
| 43 | foreignField: '_id', |
| 44 | as: 'account', |
| 45 | }, |
| 46 | }, |
| 47 | { $unwind: '$account' }, |
| 48 | { $match: { 'account.status': AccountStatus.NORMAL } }, |
| 49 | { $limit: 100 }, |
| 50 | { $addFields: { cursorId: '$_id' } }, |
| 51 | { $project: { account: 0 } }, |
| 52 | ]) |
| 53 | }) |
| 54 | |
| 55 | it('continues listing expiring credentials from the previous expiry cursor', async () => { |
| 56 | const exec = vi.fn(async () => []) |
| 57 | const model = { |
| 58 | aggregate: vi.fn(() => ({ exec })), |
| 59 | } |
| 60 | const repo = new OAuth2CredentialRepository(model as never) |
| 61 | |
| 62 | await repo.listByAccessTokenExpiresAtAndNormalAccount(1767229200, 100, { |
| 63 | accessTokenExpiresAt: 1767225600, |
| 64 | cursorId: 'cursor-id', |
| 65 | }) |
| 66 | |
| 67 | expect(model.aggregate).toHaveBeenCalledWith([ |
| 68 | { |
| 69 | $match: { |
| 70 | accessTokenExpiresAt: { $type: 'number', $lte: 1767229200 }, |
| 71 | refreshToken: { $type: 'string', $ne: '' }, |
| 72 | $or: [ |
| 73 | { accessTokenExpiresAt: { $gt: 1767225600 } }, |
| 74 | { |
| 75 | accessTokenExpiresAt: 1767225600, |
| 76 | _id: { $gt: 'cursor-id' }, |
| 77 | }, |
| 78 | ], |
| 79 | }, |
| 80 | }, |
| 81 | { $sort: { accessTokenExpiresAt: 1, _id: 1 } }, |
| 82 | { |
| 83 | $lookup: { |
| 84 | from: 'account', |
| 85 | localField: 'accountId', |
| 86 | foreignField: '_id', |
| 87 | as: 'account', |
| 88 | }, |
| 89 | }, |
| 90 | { $unwind: '$account' }, |
| 91 | { $match: { 'account.status': AccountStatus.NORMAL } }, |
| 92 | { $limit: 100 }, |
| 93 | { $addFields: { cursorId: '$_id' } }, |
| 94 | { $project: { account: 0 } }, |
| 95 | ]) |
| 96 | }) |
| 97 | }) |
| 98 |