|
| 1 | +import db, { sequencerDB } from '../../../src/helpers/mysql'; |
| 2 | +import { action, verify } from '../../../src/writer/settings'; |
| 3 | +import payload from '../../fixtures/writer-payload/space.json'; |
| 4 | + |
| 5 | +const defaultSpace = JSON.parse(payload.msg).space; |
| 6 | +const spaceId = 'test-domain-uniqueness.eth'; |
| 7 | +const testDomainId = 'test-domain-uniqueness-2.eth'; |
| 8 | + |
| 9 | +function getInput(id = defaultSpace, msgPayload = {}) { |
| 10 | + const input = { ...payload }; |
| 11 | + const inputMsg = JSON.parse(input.msg); |
| 12 | + inputMsg.payload = { ...inputMsg.payload, ...msgPayload }; |
| 13 | + inputMsg.space = id; |
| 14 | + |
| 15 | + input.msg = JSON.stringify(inputMsg); |
| 16 | + |
| 17 | + return input; |
| 18 | +} |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + jest.restoreAllMocks(); |
| 22 | +}); |
| 23 | + |
| 24 | +const mockGetSpaceController = jest.fn((): any => { |
| 25 | + return ''; |
| 26 | +}); |
| 27 | +jest.mock('@snapshot-labs/snapshot.js', () => { |
| 28 | + const originalModule = jest.requireActual('@snapshot-labs/snapshot.js'); |
| 29 | + |
| 30 | + return { |
| 31 | + ...originalModule, |
| 32 | + utils: { |
| 33 | + ...originalModule.utils, |
| 34 | + getSpaceController: () => mockGetSpaceController() |
| 35 | + } |
| 36 | + }; |
| 37 | +}); |
| 38 | + |
| 39 | +describe.skip('writer/settings', () => { |
| 40 | + afterAll(async () => { |
| 41 | + await db.queryAsync('DELETE FROM snapshot_sequencer_test.spaces WHERE id IN (?)', [ |
| 42 | + spaceId, |
| 43 | + testDomainId |
| 44 | + ]); |
| 45 | + await db.endAsync(); |
| 46 | + await sequencerDB.endAsync(); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('verify()', () => { |
| 50 | + const DOMAIN = 'vote.snapshot.org'; |
| 51 | + |
| 52 | + beforeAll(async () => { |
| 53 | + await db.queryAsync( |
| 54 | + 'INSERT INTO snapshot_sequencer_test.spaces (id, name, created, updated, domain) VALUES (?, ?, 0, 0, ?)', |
| 55 | + [testDomainId, testDomainId, DOMAIN] |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + afterAll(async () => { |
| 60 | + await db.queryAsync('DELETE FROM snapshot_sequencer_test.spaces WHERE id = ?', [ |
| 61 | + testDomainId |
| 62 | + ]); |
| 63 | + }); |
| 64 | + |
| 65 | + it('rejects when domain is used by another space', async () => { |
| 66 | + const input = getInput(spaceId, { domain: DOMAIN }); |
| 67 | + |
| 68 | + mockGetSpaceController.mockResolvedValueOnce(input.address); |
| 69 | + await expect(verify(input)).rejects.toContain('domain already taken'); |
| 70 | + expect(mockGetSpaceController).toHaveBeenCalledTimes(1); |
| 71 | + }); |
| 72 | + |
| 73 | + it('resolves when domain is not used yet', async () => { |
| 74 | + const input = getInput(spaceId, { domain: 'vote1.snapshot.org' }); |
| 75 | + |
| 76 | + mockGetSpaceController.mockResolvedValueOnce(input.address); |
| 77 | + await expect(verify(input)).resolves.toBeUndefined(); |
| 78 | + expect(mockGetSpaceController).toHaveBeenCalledTimes(1); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('action()', () => { |
| 83 | + afterEach(async () => { |
| 84 | + await db.queryAsync('DELETE FROM snapshot_sequencer_test.spaces WHERE id IN (?)', [ |
| 85 | + spaceId, |
| 86 | + testDomainId |
| 87 | + ]); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('with a domain', () => { |
| 91 | + it('saves the domain in space settings and in the domain column', async () => { |
| 92 | + const domain = 'test-domain.eth'; |
| 93 | + const input = getInput(spaceId, { domain }); |
| 94 | + const save = await action(input); |
| 95 | + |
| 96 | + expect(save).resolves; |
| 97 | + |
| 98 | + const results = ( |
| 99 | + await db.queryAsync( |
| 100 | + "SELECT id, JSON_UNQUOTE(settings->'$.domain') as settingsDomain, domain as columnDomain FROM spaces WHERE id = ?", |
| 101 | + [spaceId] |
| 102 | + ) |
| 103 | + )[0]; |
| 104 | + |
| 105 | + expect(results.id).toBe(spaceId); |
| 106 | + expect(results.settingsDomain).toBe(domain); |
| 107 | + expect(results.columnDomain).toBe(domain); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('without domain', () => { |
| 112 | + it('sets the domain in space setting and column as NULL', async () => { |
| 113 | + const input = getInput(spaceId); |
| 114 | + const save = await action(input); |
| 115 | + |
| 116 | + expect(save).resolves; |
| 117 | + |
| 118 | + const results = ( |
| 119 | + await db.queryAsync( |
| 120 | + "SELECT id, JSON_UNQUOTE(settings->'$.domain') as settingsDomain, domain as columnDomain FROM spaces WHERE id = ?", |
| 121 | + [spaceId] |
| 122 | + ) |
| 123 | + )[0]; |
| 124 | + |
| 125 | + expect(results.id).toBe(spaceId); |
| 126 | + expect(results.settingsDomain).toBe(null); |
| 127 | + expect(results.columnDomain).toBe(null); |
| 128 | + }); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments