|
| 1 | +import db, { envelopDB, sequencerDB } from '../../../src/helpers/mysql'; |
| 2 | +import { action, verify } from '../../../src/writer/update-subscription'; |
| 3 | + |
| 4 | +describe('writer/update-subscription', () => { |
| 5 | + const TEST_PREFIX = 'test-update-subscription'; |
| 6 | + |
| 7 | + afterAll(async () => { |
| 8 | + await envelopDB.queryAsync('DELETE FROM subscribers'); |
| 9 | + await envelopDB.endAsync(); |
| 10 | + await db.endAsync(); |
| 11 | + await sequencerDB.endAsync(); |
| 12 | + }); |
| 13 | + |
| 14 | + describe('verify()', () => { |
| 15 | + const msg = JSON.stringify({ payload: { subscriptions: ['closedProposal'] } }); |
| 16 | + const msgWithInvalidSubscriptions = JSON.stringify({ |
| 17 | + payload: { subscriptions: ['test'] } |
| 18 | + }); |
| 19 | + |
| 20 | + beforeAll(async () => { |
| 21 | + await Promise.all([ |
| 22 | + envelopDB.queryAsync( |
| 23 | + 'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?', |
| 24 | + [`${TEST_PREFIX}-0x0`, '[email protected]', '[]', 0, 0] |
| 25 | + ), |
| 26 | + envelopDB.queryAsync( |
| 27 | + 'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?', |
| 28 | + [`${TEST_PREFIX}-0x1`, '[email protected]', '[]', 0, 1] |
| 29 | + ) |
| 30 | + ]); |
| 31 | + }); |
| 32 | + |
| 33 | + it('rejects when the address is not subscribed', () => { |
| 34 | + return expect(verify({ address: '0x0', msg })).rejects.toEqual(`user not subscribed`); |
| 35 | + }); |
| 36 | + |
| 37 | + it('rejects when the address is not verified', () => { |
| 38 | + return expect(verify({ address: `${TEST_PREFIX}-0x0`, msg })).rejects.toEqual( |
| 39 | + `email not verified` |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it('rejects when subscription values are not valid', () => { |
| 44 | + return expect( |
| 45 | + verify({ address: `${TEST_PREFIX}-0x1`, msg: msgWithInvalidSubscriptions }) |
| 46 | + ).rejects.toEqual(`invalid subscription value`); |
| 47 | + }); |
| 48 | + |
| 49 | + it('resolves when all args are valid', () => { |
| 50 | + return expect(verify({ address: `${TEST_PREFIX}-0x1`, msg })).resolves.toHaveProperty( |
| 51 | + 'address' |
| 52 | + ); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('action()', () => { |
| 57 | + const address = `${TEST_PREFIX}-0x3`; |
| 58 | + const subscriptions = ['newProposal', 'closedProposal']; |
| 59 | + |
| 60 | + beforeAll(async () => { |
| 61 | + await envelopDB.queryAsync( |
| 62 | + 'INSERT INTO subscribers SET address = ?, email = ?, subscriptions = ?, created = ?, verified = ?', |
| 63 | + [address, '[email protected]', '["summary"]', 0, 1] |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it('updates the subscription', async () => { |
| 68 | + await action({ |
| 69 | + address: address, |
| 70 | + msg: JSON.stringify({ payload: { subscriptions } }) |
| 71 | + }); |
| 72 | + |
| 73 | + const result = await envelopDB.queryAsync( |
| 74 | + `SELECT subscriptions FROM subscribers WHERE address = ? LIMIT 1`, |
| 75 | + [address] |
| 76 | + ); |
| 77 | + |
| 78 | + expect(JSON.parse(result[0].subscriptions)).toEqual(subscriptions); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments