|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import sinon from 'sinon'; |
| 6 | +import { expect } from 'chai'; |
| 7 | +import { CartCleanup } from '../../scripts/cleanup-old-carts/cleanup-old-carts'; |
| 8 | +import Sinon from 'sinon'; |
| 9 | + |
| 10 | +describe('CartCleanup', () => { |
| 11 | + let cartCleanup: CartCleanup; |
| 12 | + let dbStub: { |
| 13 | + deleteFrom: Sinon.SinonSpy; |
| 14 | + where: Sinon.SinonSpy; |
| 15 | + execute: Sinon.SinonSpy; |
| 16 | + updateTable: Sinon.SinonSpy; |
| 17 | + set: Sinon.SinonSpy; |
| 18 | + }; |
| 19 | + |
| 20 | + const deleteBefore = new Date('2024-01-01T00:00:00Z'); |
| 21 | + const anonymizeBefore = new Date('2023-06-01T00:00:00Z'); |
| 22 | + const anonymizeFields = new Set(['email', 'taxAddress'] as const); |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + dbStub = { |
| 26 | + deleteFrom: sinon.stub().returnsThis(), |
| 27 | + where: sinon.stub().returnsThis(), |
| 28 | + execute: sinon.stub().resolves(), |
| 29 | + updateTable: sinon.stub().returnsThis(), |
| 30 | + set: sinon.stub().returnsThis(), |
| 31 | + }; |
| 32 | + |
| 33 | + cartCleanup = new CartCleanup( |
| 34 | + deleteBefore, |
| 35 | + anonymizeBefore, |
| 36 | + anonymizeFields, |
| 37 | + dbStub |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + sinon.restore(); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('run', () => { |
| 46 | + it('deletes old carts', async () => { |
| 47 | + await cartCleanup.run(); |
| 48 | + |
| 49 | + expect(dbStub.deleteFrom.calledWith('carts')).to.be.true; |
| 50 | + expect(dbStub.where.calledWith('updatedAt', '<', deleteBefore.getTime())) |
| 51 | + .to.be.true; |
| 52 | + expect(dbStub.execute.called).to.be.true; |
| 53 | + }); |
| 54 | + |
| 55 | + it('anonymizes fields within carts', async () => { |
| 56 | + await cartCleanup.run(); |
| 57 | + |
| 58 | + expect(dbStub.updateTable.calledWith('carts')).to.be.true; |
| 59 | + expect( |
| 60 | + dbStub.where.calledWith('updatedAt', '<', anonymizeBefore.getTime()) |
| 61 | + ).to.be.true; |
| 62 | + expect(dbStub.set.calledWith('email', null)).to.be.true; |
| 63 | + expect(dbStub.set.calledWith('taxAddress', null)).to.be.true; |
| 64 | + expect(dbStub.execute.calledTwice).to.be.true; |
| 65 | + }); |
| 66 | + |
| 67 | + it('does not anonymize if no fields are provided', async () => { |
| 68 | + cartCleanup = new CartCleanup( |
| 69 | + deleteBefore, |
| 70 | + anonymizeBefore, |
| 71 | + new Set(), |
| 72 | + dbStub |
| 73 | + ); |
| 74 | + await cartCleanup.run(); |
| 75 | + |
| 76 | + expect(dbStub.updateTable.called).to.be.false; |
| 77 | + }); |
| 78 | + |
| 79 | + it('does not anonymize if anonymizeBefore is null', async () => { |
| 80 | + cartCleanup = new CartCleanup( |
| 81 | + deleteBefore, |
| 82 | + null, |
| 83 | + anonymizeFields, |
| 84 | + dbStub |
| 85 | + ); |
| 86 | + await cartCleanup.run(); |
| 87 | + |
| 88 | + expect(dbStub.updateTable.called).to.be.false; |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments