Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #580 from Giveth/f_565_fix_create_trace_with_repet…
Browse files Browse the repository at this point in the history
…itive_title

F 565 fix create trace with repetitive title
  • Loading branch information
aminlatifi authored Aug 12, 2021
2 parents 315460a + 1adb786 commit d9c4cd3
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 1 deletion.
109 changes: 109 additions & 0 deletions src/services/traces/checkTraceName.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const {
assertThrowsAsync,
assertNotThrowsAsync,
SAMPLE_DATA,
generateRandomMongoId,
} = require('../../../test/testUtility');
const { getFeatherAppInstance } = require('../../app');

const checkTraceName = require('./checkTraceName');

let app;
before(() => {
app = getFeatherAppInstance();
});

const checkTraceNameTestCases = () => {
it('should throw error for repetitive title', async () => {
const traceData = {
...SAMPLE_DATA.createTraceData(),
title: 'test-trace-unique',
ownerAddress: SAMPLE_DATA.USER_ADDRESS,
};
await app.service('traces').create(traceData);
const context = {
id: generateRandomMongoId(),
app,
data: {
title: traceData.title,
campaignId: traceData.campaignId,
},
};
const badFunc = async () => {
await checkTraceName()(context);
};
await assertThrowsAsync(
badFunc,
'Trace title is repetitive. Please select a different title for the trace.',
);
});
it('should not throw error for repetitive title but different campaign', async () => {
const traceData = {
...SAMPLE_DATA.createTraceData(),
ownerAddress: SAMPLE_DATA.USER_ADDRESS,
};
await app.service('traces').create(traceData);

const context = {
id: generateRandomMongoId(),
app,
data: {
title: traceData.title,
campaignId: generateRandomMongoId(),
},
};
const goodFunc = async () => {
await checkTraceName()(context);
};
await assertNotThrowsAsync(goodFunc);
});
it('should not throw error for similar title with extra spaces between words', async () => {
const title = 'test repetetive title with space between words';
const titleWithExtraSpaces = 'test repetetive title with space between words';
const traceData = {
...SAMPLE_DATA.createTraceData(),
title,
ownerAddress: SAMPLE_DATA.USER_ADDRESS,
};
await app.service('traces').create(traceData);

const context = {
id: generateRandomMongoId(),
app,
data: {
title: titleWithExtraSpaces,
campaignId: generateRandomMongoId(),
},
};
const goodFunc = async () => {
await checkTraceName()(context);
};
await assertNotThrowsAsync(goodFunc);
});
it('should not throw error for similar title with extra spaces at end of title', async () => {
const title = 'test repetetive title with extra spaces at end of title';
const titleWithExtraSpaces =
'test repetetive title with extra spaces at end of title ';
const traceData = {
...SAMPLE_DATA.createTraceData(),
title,
ownerAddress: SAMPLE_DATA.USER_ADDRESS,
};
await app.service('traces').create(traceData);

const context = {
id: generateRandomMongoId(),
app,
data: {
title: titleWithExtraSpaces,
campaignId: generateRandomMongoId(),
},
};
const goodFunc = async () => {
await checkTraceName()(context);
};
await assertNotThrowsAsync(goodFunc);
});
};

describe('test checkTraceName() functions', checkTraceNameTestCases);
65 changes: 65 additions & 0 deletions src/services/traces/traces.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,71 @@ function postMilestoneTestCases() {
assert.equal(response.statusCode, 201);
assert.equal(response.body.ownerAddress, SAMPLE_DATA.USER_ADDRESS);
});

it('should not create trace with repetitive title', async () => {
const traceData = SAMPLE_DATA.createTraceData();
const response = await request(baseUrl)
.post(relativeUrl)
.send(traceData)
.set({ Authorization: getJwt() });
assert.equal(response.statusCode, 201);
assert.equal(response.body.ownerAddress, SAMPLE_DATA.USER_ADDRESS);

const response2 = await request(baseUrl)
.post(relativeUrl)
.send(traceData)
.set({ Authorization: getJwt() });
assert.equal(response2.statusCode, 403);
assert.equal(
response2.body.message,
'Trace title is repetitive. Please select a different title for the trace.',
);
});
it('should not create trace with similar title with extra spaces between words', async () => {
const title = 'test similar title with extra spaces between words';
const titleWithSpace = 'test similar title with extra spaces between words';

const traceData = SAMPLE_DATA.createTraceData();
const response = await request(baseUrl)
.post(relativeUrl)
.send({ ...traceData, title })
.set({ Authorization: getJwt() });
assert.equal(response.statusCode, 201);
assert.equal(response.body.ownerAddress, SAMPLE_DATA.USER_ADDRESS);

const response2 = await request(baseUrl)
.post(relativeUrl)
.send({ ...traceData, title: titleWithSpace })
.set({ Authorization: getJwt() });
assert.equal(response2.statusCode, 403);
assert.equal(
response2.body.message,
'Trace title is repetitive. Please select a different title for the trace.',
);
});
it('should not create trace with similar title with extra spaces at end of title', async () => {
const title = 'test similar title with extra spaces at end of title';
const titleWithSpace = 'test similar title with extra spaces at end of title ';

const traceData = SAMPLE_DATA.createTraceData();
const response = await request(baseUrl)
.post(relativeUrl)
.send({ ...traceData, title })
.set({ Authorization: getJwt() });
assert.equal(response.statusCode, 201);
assert.equal(response.body.ownerAddress, SAMPLE_DATA.USER_ADDRESS);

const response2 = await request(baseUrl)
.post(relativeUrl)
.send({ ...traceData, title: titleWithSpace })
.set({ Authorization: getJwt() });
assert.equal(response2.statusCode, 403);
assert.equal(
response2.body.message,
'Trace title is repetitive. Please select a different title for the trace.',
);
});

it('non-campaign owner should not create trace with Pending status', async () => {
const user = await app.service('users').create({ address: generateRandomEtheriumAddress() });
const response = await request(baseUrl)
Expand Down
13 changes: 13 additions & 0 deletions src/utils/regexUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ const getSimilarTitleInTraceRegexTestCases = () => {
assert.isTrue(regex.test('Amin - Givether PAN Distribution'));
});

it('should return true for same title with some spaces between words', () => {
const title = 'Amin - Givether PAN Distribution';
const titleWithSpace = 'Amin - Givether PAN Distribution';
const regex = getSimilarTitleInTraceRegex(titleWithSpace);
assert.isTrue(regex.test(title));
});
it('should return true for same title with some spaces at end of title', () => {
const title = 'Amin - Givether PAN Distribution';
const titleWithSpace = 'Amin - Givether PAN Distribution ';
const regex = getSimilarTitleInTraceRegex(titleWithSpace);
assert.isTrue(regex.test(title));
});

it('should return false if there is character after the title', () => {
// this is a real case
const title = 'Amin - Givether PAN Distribution';
Expand Down
2 changes: 1 addition & 1 deletion test/testUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const SAMPLE_DATA = {
return {
fullyFunded: false,
mined: true,
title: `test-milestone-${new Date()}`,
title: `test-milestone-${new Date().getTime()}`,
description: '<p>give money for god sake</p>',
image: '',
reviewerAddress,
Expand Down

0 comments on commit d9c4cd3

Please sign in to comment.