Skip to content

Commit

Permalink
feat: enable domain edition only on turbo and legacy spaces (#488)
Browse files Browse the repository at this point in the history
* feat: enable domain edition only on turbo and legacy spaces

* Update src/writer/settings.ts

Co-authored-by: Chaitanya <[email protected]>

---------

Co-authored-by: Chaitanya <[email protected]>
  • Loading branch information
wa0x6e and ChaituVR authored Jan 11, 2025
1 parent 0776492 commit bf2b8de
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/writer/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export async function verify(body): Promise<any> {
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,
Expand Down
66 changes: 65 additions & 1 deletion test/unit/writer/settings.test.ts
Original file line number Diff line number Diff line change
@@ -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) };
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit bf2b8de

Please sign in to comment.