Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

control description length <= 280 #62

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions smart-contracts/assembly/contracts/dns/__tests__/dns.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { contractOwnerKey, blackListKey } from './../dns';
import {
setResolver,
isDescriptionValid,
resolver,
addWebsiteToBlackList,
addWebsitesToBlackList,
Expand Down Expand Up @@ -49,6 +50,19 @@ describe('DNS contract tests', () => {
expect(Storage.get(contractOwnerKey)).toStrictEqual(serializedDnsAdmin);
});

test('description length', () => {
const validDescriptionLessThan280 =
'Valid description with less than 280 characters.';
const invalidDescriptionMoreThan280 =
'Invalid description exceeding the maximum limit of 280 characters.' +
'x'.repeat(300);
const validDescriptionExactly280 = 'x'.repeat(280);

expect(isDescriptionValid(validDescriptionLessThan280)).toBe(true);
expect(isDescriptionValid(invalidDescriptionMoreThan280)).toBe(false);
expect(isDescriptionValid(validDescriptionExactly280)).toBe(true);
});

test('invalid dns entry', () => {
expect(() => {
const setResolverArgs = new Args()
Expand Down
13 changes: 13 additions & 0 deletions smart-contracts/assembly/contracts/dns/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ function isDnsValid(input: string): bool {
return true;
}

export function isDescriptionValid(description: string): boolean {
B-Naoufal marked this conversation as resolved.
Show resolved Hide resolved
// Check if the length exceeds the maximum limit of 280 characters
if (description.length > 280) {
return false;
}

return true;
}

/**
* This function is meant to be called only one time: when the contract is deployed.
*
Expand Down Expand Up @@ -122,6 +131,10 @@ export function setResolver(binaryArgs: StaticArray<u8>): void {
.nextString()
.expect('website description is missing or invalid');

if (!isDescriptionValid(description)) {
triggerError('INVALID_DESCRIPTION_ENTRY');
}

if (Storage.has(websiteNameBytes)) {
triggerError('Try another website name, this one is already taken.');
}
Expand Down