|
1 | 1 | import { EscrowRepository } from './escrow.repository'; |
2 | 2 | import { PrismaService } from '../prisma/prisma.service'; |
| 3 | +import { encryptContact } from '../common/sanitization/contact-encryption.util'; |
| 4 | + |
| 5 | +// Required by the encryption util |
| 6 | +process.env.CONTACT_ENCRYPTION_KEY = 'a'.repeat(64); |
3 | 7 |
|
4 | 8 | function makeDto() { |
5 | 9 | return { |
@@ -56,6 +60,103 @@ describe('EscrowRepository', () => { |
56 | 60 | }); |
57 | 61 | }); |
58 | 62 |
|
| 63 | + // ── #205: cursor-based pagination ───────────────────────────────────────── |
| 64 | + describe('findByVendor() — pagination (#205)', () => { |
| 65 | + beforeEach(async () => { |
| 66 | + await repo.create({ ...makeDto(), itemRef: 'A' }, 'v-page'); |
| 67 | + await repo.create({ ...makeDto(), itemRef: 'B' }, 'v-page'); |
| 68 | + await repo.create({ ...makeDto(), itemRef: 'C' }, 'v-page'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns up to `take` records for the first page', async () => { |
| 72 | + const results = await repo.findByVendor('v-page', undefined, 2); |
| 73 | + expect(results).toHaveLength(2); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns remaining records after a cursor', async () => { |
| 77 | + const first = await repo.findByVendor('v-page', undefined, 2); |
| 78 | + const second = await repo.findByVendor('v-page', first[first.length - 1].id, 10); |
| 79 | + expect(second.length).toBeGreaterThanOrEqual(1); |
| 80 | + expect(second.map((e) => e.id)).not.toContain(first[0].id); |
| 81 | + }); |
| 82 | + |
| 83 | + it('returns an empty array when no more records exist after cursor', async () => { |
| 84 | + const all = await repo.findByVendor('v-page', undefined, 100); |
| 85 | + const last = all[all.length - 1]; |
| 86 | + const next = await repo.findByVendor('v-page', last.id, 10); |
| 87 | + expect(next).toHaveLength(0); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('findByBuyer() — pagination (#205)', () => { |
| 92 | + beforeEach(async () => { |
| 93 | + await repo.create({ ...makeDto(), itemRef: 'P', buyerAddress: 'b-page' }, 'v1'); |
| 94 | + await repo.create({ ...makeDto(), itemRef: 'Q', buyerAddress: 'b-page' }, 'v1'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returns up to `take` records', async () => { |
| 98 | + const results = await repo.findByBuyer('b-page', undefined, 1); |
| 99 | + expect(results).toHaveLength(1); |
| 100 | + }); |
| 101 | + |
| 102 | + it('uses default take of 20 when not specified', async () => { |
| 103 | + const results = await repo.findByBuyer('b-page'); |
| 104 | + expect(results.length).toBeLessThanOrEqual(20); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + // ── #206: findFirst instead of findMany + index ──────────────────────────── |
| 109 | + describe('findByVendorAndItem() — findFirst determinism (#206)', () => { |
| 110 | + it('returns the earliest record when multiple share the same (vendorAddress, itemRef)', async () => { |
| 111 | + const first = await repo.create({ ...makeDto(), itemRef: 'DUP' }, 'v-dup'); |
| 112 | + await repo.create({ ...makeDto(), itemRef: 'DUP' }, 'v-dup'); |
| 113 | + const found = await repo.findByVendorAndItem('v-dup', 'DUP'); |
| 114 | + expect(found?.id).toBe(first.id); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + // ── #208: plaintext buyer contact rejected by prisma guard ──────────────── |
| 119 | + describe('saveBuyerContact() — encryption guard (#208)', () => { |
| 120 | + it('stores encrypted contact without throwing', async () => { |
| 121 | + const escrow = await repo.create(makeDto(), 'v-enc'); |
| 122 | + const encEmail = encryptContact('test@example.com'); |
| 123 | + const encPhone = encryptContact('+2348001234567'); |
| 124 | + await expect( |
| 125 | + repo.saveBuyerContact(escrow.id, encEmail, encPhone), |
| 126 | + ).resolves.toBeDefined(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('throws when plaintext email is passed directly to the repository', async () => { |
| 130 | + const escrow = await repo.create(makeDto(), 'v-enc2'); |
| 131 | + await expect( |
| 132 | + repo.saveBuyerContact(escrow.id, 'plaintext@example.com', null), |
| 133 | + ).rejects.toThrow(/Security violation.*buyerContactEmail/); |
| 134 | + }); |
| 135 | + |
| 136 | + it('throws when plaintext phone is passed directly to the repository', async () => { |
| 137 | + const escrow = await repo.create(makeDto(), 'v-enc3'); |
| 138 | + await expect( |
| 139 | + repo.saveBuyerContact(escrow.id, null, '+2348001234567'), |
| 140 | + ).rejects.toThrow(/Security violation.*buyerContactPhone/); |
| 141 | + }); |
| 142 | + |
| 143 | + it('allows null values (contact not provided)', async () => { |
| 144 | + const escrow = await repo.create(makeDto(), 'v-enc4'); |
| 145 | + await expect( |
| 146 | + repo.saveBuyerContact(escrow.id, null, null), |
| 147 | + ).resolves.toBeDefined(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('stored value differs from plaintext input', async () => { |
| 151 | + const escrow = await repo.create(makeDto(), 'v-enc5'); |
| 152 | + const plain = 'secret@test.com'; |
| 153 | + const enc = encryptContact(plain); |
| 154 | + const updated = await repo.saveBuyerContact(escrow.id, enc, null); |
| 155 | + expect(updated.buyerContactEmail).not.toBe(plain); |
| 156 | + expect(updated.buyerContactEmail).toBe(enc); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
59 | 160 | describe('findVendorEscrows()', () => { |
60 | 161 | beforeEach(async () => { |
61 | 162 | await repo.create({ ...makeDto(), amount: 300, itemRef: 'A' }, 'v1'); |
|
0 commit comments