Skip to content

Commit

Permalink
Add test cases to make sure cant create trace repetitive title
Browse files Browse the repository at this point in the history
related to #565
  • Loading branch information
mohammadranjbarz committed Aug 12, 2021
1 parent 77c417f commit 1adb786
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 40 deletions.
7 changes: 0 additions & 7 deletions src/repositories/traceRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ const findVerifiedTraces = async app => {
});
};

const findTraceByQuery = async (app, query) => {
const tracesService = app.service('traces');
const tracesModel = tracesService.Model;
return tracesModel.find(query);
};

module.exports = {
findVerifiedTraces,
findTraceByQuery,
};
36 changes: 6 additions & 30 deletions src/services/traces/checkTraceName.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const errors = require('@feathersjs/errors');
const logger = require('winston');
const {findTraceByQuery} = require('../../repositories/traceRepository')
const { getSimilarTitleInTraceRegex } = require('../../utils/regexUtils');
/**
* This function checks if traces name is unique in the campaign scope
Expand All @@ -21,38 +20,15 @@ const checkIfTraceNameIsUnique = () => async context => {
const query = {
_id: { $ne: context.id },
campaignId: data.campaignId,
// title: getSimilarTitleInTraceRegex(title),
title,
title: getSimilarTitleInTraceRegex(title),
};
// const traceWithSameName = await traceService.find({
// query,
// });
// if (traceWithSameName.total > 0) {
// logger.info('checkIfTraceNameIsUnique ', {
// query,
// foundTraces: traceWithSameName.data.map(trace => {
// return {
// title: trace.title,
// id: trace.id,
// _id: trace._id,
// };
// }),
// });
// // trace titles are supposed to be unique
// throw new errors.Forbidden(
// 'Trace title is repetitive. Please select a different title for the trace.',
// );
// }
const traceWithSameName = await findTraceByQuery(app, query);
if (traceWithSameName.length > 0) {
const traceWithSameName = await traceService.find({
query,
});
if (traceWithSameName.total > 0) {
logger.info('checkIfTraceNameIsUnique ', {
query,
foundTraces: traceWithSameName.map(trace => {
return {
title: trace.title,
_id: trace._id,
};
}),
traceWithSameName,
});
// trace titles are supposed to be unique
throw new errors.Forbidden(
Expand Down
53 changes: 50 additions & 3 deletions src/services/traces/checkTraceName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ 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,
Expand All @@ -30,7 +30,7 @@ const checkTraceNameTestCases = () => {
},
};
const badFunc = async () => {
await checkTraceName(context);
await checkTraceName()(context);
};
await assertThrowsAsync(
badFunc,
Expand All @@ -53,7 +53,54 @@ const checkTraceNameTestCases = () => {
},
};
const goodFunc = async () => {
await checkTraceName(context);
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);
});
Expand Down
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

0 comments on commit 1adb786

Please sign in to comment.