diff --git a/src/writer/settings.ts b/src/writer/settings.ts index 1b801810..fce91419 100644 --- a/src/writer/settings.ts +++ b/src/writer/settings.ts @@ -82,6 +82,10 @@ export async function verify(body): Promise { const isAdmin = admins.includes(body.address.toLowerCase()); const newAdmins = (msg.payload.admins || []).map(admin => admin.toLowerCase()); + if (msg.payload.domain && !space?.turbo && !space?.domain) { + return Promise.reject('domain is a turbo feature only'); + } + const anotherSpaceWithDomain = ( await db.queryAsync('SELECT 1 FROM spaces WHERE domain = ? AND id != ? LIMIT 1', [ msg.payload.domain, diff --git a/test/unit/writer/settings.test.ts b/test/unit/writer/settings.test.ts index 618d5f00..8f1627d3 100644 --- a/test/unit/writer/settings.test.ts +++ b/test/unit/writer/settings.test.ts @@ -1,7 +1,7 @@ +import SpaceSchema from '@snapshot-labs/snapshot.js/src/schemas/space.json'; import { verify } from '../../../src/writer/settings'; import { spacesGetSpaceFixtures } from '../../fixtures/space'; import input from '../../fixtures/writer-payload/space.json'; -import SpaceSchema from '@snapshot-labs/snapshot.js/src/schemas/space.json'; function editedInput(payload = {}) { const result = { ...input, msg: JSON.parse(input.msg) }; @@ -105,6 +105,70 @@ describe('writer/settings', () => { ) ).rejects.toContain('wrong space format'); }); + + describe('when the space has an existing custom domain', () => { + it('accepts a new domain for non-turbo spaces', () => { + mockGetSpace.mockResolvedValueOnce({ + ...spacesGetSpaceFixtures, + turbo: false, + domain: 'test.com' + }); + return expect( + verify( + editedInput({ + domain: 'test2.com' + }) + ) + ).resolves.toBeUndefined(); + }); + + it('accepts a new domain for turbo spaces', () => { + mockGetSpace.mockResolvedValueOnce({ + ...spacesGetSpaceFixtures, + turbo: true, + domain: 'test.com' + }); + return expect( + verify( + editedInput({ + domain: 'test2.com' + }) + ) + ).resolves.toBeUndefined(); + }); + }); + + describe('when the space does not have an existing custom domain', () => { + it('rejects a new domain for non-turbo spaces', () => { + mockGetSpace.mockResolvedValueOnce({ + ...spacesGetSpaceFixtures, + turbo: false, + domain: undefined + }); + return expect( + verify( + editedInput({ + domain: 'test2.com' + }) + ) + ).rejects.toContain('domain is a turbo feature only'); + }); + + it('accepts a new domain for turbo spaces', () => { + mockGetSpace.mockResolvedValueOnce({ + ...spacesGetSpaceFixtures, + turbo: true, + domain: undefined + }); + return expect( + verify( + editedInput({ + domain: 'test2.com' + }) + ) + ).resolves.toBeUndefined(); + }); + }); }); describe('on valid data', () => {