|
| 1 | +import { Kysely } from 'kysely'; |
| 2 | +import { randomBytes } from 'node:crypto'; |
| 3 | +import { SharedLinkType } from 'src/enum'; |
| 4 | +import { AccessRepository } from 'src/repositories/access.repository'; |
| 5 | +import { DatabaseRepository } from 'src/repositories/database.repository'; |
| 6 | +import { LoggingRepository } from 'src/repositories/logging.repository'; |
| 7 | +import { SharedLinkRepository } from 'src/repositories/shared-link.repository'; |
| 8 | +import { StorageRepository } from 'src/repositories/storage.repository'; |
| 9 | +import { DB } from 'src/schema'; |
| 10 | +import { SharedLinkService } from 'src/services/shared-link.service'; |
| 11 | +import { newMediumService } from 'test/medium.factory'; |
| 12 | +import { factory } from 'test/small.factory'; |
| 13 | +import { getKyselyDB } from 'test/utils'; |
| 14 | + |
| 15 | +let defaultDatabase: Kysely<DB>; |
| 16 | + |
| 17 | +const setup = (db?: Kysely<DB>) => { |
| 18 | + return newMediumService(SharedLinkService, { |
| 19 | + database: db || defaultDatabase, |
| 20 | + real: [AccessRepository, DatabaseRepository, SharedLinkRepository], |
| 21 | + mock: [LoggingRepository, StorageRepository], |
| 22 | + }); |
| 23 | +}; |
| 24 | + |
| 25 | +beforeAll(async () => { |
| 26 | + defaultDatabase = await getKyselyDB(); |
| 27 | +}); |
| 28 | + |
| 29 | +describe(SharedLinkService.name, () => { |
| 30 | + describe('get', () => { |
| 31 | + it('should return the correct dates on the shared link album', async () => { |
| 32 | + const { sut, ctx } = setup(); |
| 33 | + |
| 34 | + const { user } = await ctx.newUser(); |
| 35 | + const auth = factory.auth({ user }); |
| 36 | + const { album } = await ctx.newAlbum({ ownerId: user.id }); |
| 37 | + |
| 38 | + const dates = ['2021-01-01T00:00:00.000Z', '2022-01-01T00:00:00.000Z', '2020-01-01T00:00:00.000Z']; |
| 39 | + |
| 40 | + for (const date of dates) { |
| 41 | + const { asset } = await ctx.newAsset({ fileCreatedAt: date, localDateTime: date, ownerId: user.id }); |
| 42 | + await ctx.newExif({ assetId: asset.id, make: 'Canon' }); |
| 43 | + await ctx.newAlbumAsset({ albumId: album.id, assetId: asset.id }); |
| 44 | + } |
| 45 | + |
| 46 | + const sharedLinkRepo = ctx.get(SharedLinkRepository); |
| 47 | + |
| 48 | + const sharedLink = await sharedLinkRepo.create({ |
| 49 | + key: randomBytes(16), |
| 50 | + id: factory.uuid(), |
| 51 | + userId: user.id, |
| 52 | + albumId: album.id, |
| 53 | + allowUpload: true, |
| 54 | + type: SharedLinkType.Album, |
| 55 | + }); |
| 56 | + |
| 57 | + await expect(sut.get(auth, sharedLink.id)).resolves.toMatchObject({ |
| 58 | + album: expect.objectContaining({ |
| 59 | + startDate: '2020-01-01T00:00:00+00:00', |
| 60 | + endDate: '2022-01-01T00:00:00+00:00', |
| 61 | + }), |
| 62 | + }); |
| 63 | + }); |
| 64 | + }); |
| 65 | +}); |
0 commit comments