|
1 | 1 | import { isExistingAlias } from '../../../src/helpers/alias'; |
2 | 2 | import db, { sequencerDB } from '../../../src/helpers/mysql'; |
| 3 | +import { action, verify } from '../../../src/writer/alias'; |
3 | 4 | import { aliasesSqlFixtures } from '../../fixtures/alias'; |
4 | 5 |
|
5 | 6 | describe('alias', () => { |
6 | 7 | const seed = Date.now().toFixed(0); |
7 | 8 |
|
8 | | - beforeAll(async () => { |
9 | | - await db.queryAsync('DELETE from snapshot_sequencer_test.aliases'); |
| 9 | + beforeEach(async () => { |
| 10 | + await db.queryAsync('DELETE from snapshot_sequencer_test.aliases WHERE ipfs = ?', seed); |
10 | 11 | await Promise.all( |
11 | 12 | aliasesSqlFixtures.map(alias => { |
12 | | - const values = { |
13 | | - ...alias, |
14 | | - ipfs: seed |
15 | | - }; |
| 13 | + const values = { ...alias, ipfs: seed }; |
16 | 14 | return db.queryAsync('INSERT INTO snapshot_sequencer_test.aliases SET ?', values); |
17 | 15 | }) |
18 | 16 | ); |
19 | 17 | }); |
20 | 18 |
|
21 | | - afterEach(async () => { |
22 | | - await db.queryAsync('DELETE from snapshot_sequencer_test.aliases where ipfs = ?', seed); |
23 | | - }); |
24 | | - |
25 | 19 | afterAll(async () => { |
| 20 | + await db.queryAsync('DELETE from snapshot_sequencer_test.aliases WHERE ipfs = ?', seed); |
26 | 21 | await db.endAsync(); |
27 | 22 | await sequencerDB.endAsync(); |
28 | 23 | }); |
29 | 24 |
|
| 25 | + describe('verify()', () => { |
| 26 | + it('should pass when alias pair already exists (allows renewal)', async () => { |
| 27 | + const { address, alias } = aliasesSqlFixtures[0]; |
| 28 | + const msg = { |
| 29 | + version: '0.1.4', |
| 30 | + timestamp: Math.floor(Date.now() / 1000), |
| 31 | + type: 'alias', |
| 32 | + payload: { alias } |
| 33 | + }; |
| 34 | + |
| 35 | + await expect(verify({ address, msg: JSON.stringify(msg) })).resolves.toBeTruthy(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should bump created date when re-submitting existing alias', async () => { |
| 39 | + const { address, alias } = aliasesSqlFixtures[0]; |
| 40 | + const newTimestamp = Math.floor(Date.now() / 1000) + 1000; |
| 41 | + const msg = { |
| 42 | + version: '0.1.4', |
| 43 | + timestamp: newTimestamp, |
| 44 | + type: 'alias', |
| 45 | + payload: { alias } |
| 46 | + }; |
| 47 | + |
| 48 | + await action({ address, msg: JSON.stringify(msg) }, 'ipfs-new', '', 'new-id'); |
| 49 | + |
| 50 | + const [row] = await db.queryAsync( |
| 51 | + 'SELECT created FROM aliases WHERE address = ? AND alias = ?', |
| 52 | + [address, alias] |
| 53 | + ); |
| 54 | + expect(row.created).toBe(newTimestamp); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should pass when alias does not exist', async () => { |
| 58 | + const address = '0x0000000000000000000000000000000000000001'; |
| 59 | + const msg = { |
| 60 | + version: '0.1.4', |
| 61 | + timestamp: Math.floor(Date.now() / 1000), |
| 62 | + type: 'alias', |
| 63 | + payload: { alias: '0x0000000000000000000000000000000000000002' } |
| 64 | + }; |
| 65 | + |
| 66 | + await expect(verify({ address, msg: JSON.stringify(msg) })).resolves.toBeTruthy(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
30 | 70 | describe('isExistingAlias()', () => { |
31 | 71 | it('should return true for valid alias', () => { |
32 | 72 | expect( |
|
0 commit comments