Skip to content

Commit

Permalink
Merge pull request #106 from Giveth/4194-add-unit-test-and-debug
Browse files Browse the repository at this point in the history
Add unit tests for notify reward amount
  • Loading branch information
ae2079 authored Jul 14, 2024
2 parents 1e979b9 + c3f34ed commit 259147f
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 3 deletions.
3 changes: 3 additions & 0 deletions config/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ GIVETH_IO_THIRD_PARTY_MICRO_SERVICE=givethio
GIV_ECONOMY_THIRD_PARTY_SECRET=secret
GIV_ECONOMY_THIRD_PARTY_MICRO_SERVICE=giveconomy-notification-service

NOTIFY_REWARD_THIRD_PARTY_SECRET=secret
NOTIFY_REWARD_THIRD_PARTY_MICRO_SERVICE=notifyreward

# OPTIONAL - force logging to stdout when the value is true
LOG_STDOUT=false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const NotifyRewardAmountNotificationType = [
name: NOTIFICATION_TYPE_NAMES.NOTIFY_REWARD_AMOUNT,
description: NOTIFICATION_TYPE_NAMES.NOTIFY_REWARD_AMOUNT,
microService: MICRO_SERVICES.givethio,
category: NOTIFICATION_CATEGORY.NOTIFY_REWARD_AMOUNT,
category: NOTIFICATION_CATEGORY.GENERAL,
schemaValidator: SCHEMA_VALIDATORS_NAMES.NOTIFY_REWARD_AMOUNT,
title: "Notify reward report",
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MICRO_SERVICES } from '../src/utils/utils';
import { NOTIFICATION_CATEGORY } from '../src/types/general';

export class changeMicroserviceAndCategoryOfNotifyRewardNotificationType1720553769343 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
UPDATE notification_type
SET "microService" = '${MICRO_SERVICES.notifyReward}',
category = '${NOTIFICATION_CATEGORY.ORTTO}'
WHERE name = 'Notify reward amount';
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
UPDATE notification_type
SET "microService" = '${MICRO_SERVICES.givethio}',
categoty = '${NOTIFICATION_CATEGORY.GENERAL}'
WHERE name = 'Notify reward amount';
`);
}
}
27 changes: 27 additions & 0 deletions migrations/1720828190666-seedThirdPartyForNotifyReward.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class seedThirdPartyForNotifyReward1720828190666 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
if (
process.env.NODE_ENV === 'test' ||
process.env.NODE_ENV === 'development'
) {
// Create third part record for notifyreward in development and test ENVs
await queryRunner.query(`
INSERT INTO third_party(
"microService", secret, "isActive")
VALUES
('notifyreward', 'secret', true)
;
`);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DELETE FROM third_party
WHERE "microService" = 'notifyreward';
`);
}

}
72 changes: 72 additions & 0 deletions src/routes/v1/notificationRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getAccessTokenForMockAuthMicroService,
getGivEconomyBasicAuth,
getGivethIoBasicAuth,
getNotifyRewardBasicAuth,
serverUrl,
sleep,
} from '../../../test/testUtils';
Expand Down Expand Up @@ -2092,6 +2093,77 @@ function sendNotificationTestCases() {
const createdNotification = await findNotificationByTrackId(trackId);
assert.equal(createdNotification?.createdAt.getTime(), creationTime);
});

it('should create *Notify reward amount* notification, success', async () => {
const data = {
eventName: "Notify reward amount",
sendEmail: true,
sendSegment: true,
creationTime: 1667992708000,
email: "[email protected]",
segment: {
payload: {
round: 10,
date: "1667992708000",
amount: "12134",
contractAddress: "0xsfglsjfdflk",
farm: "test farm",
message: "test message",
network: "ethereum",
script: "test script",
transactionHash: "test txhash"
}
}
};

const result = await axios.post(sendNotificationUrl, data, {
headers: {
authorization: getNotifyRewardBasicAuth(),
},
});

assert.equal(result.status, 200);
assert.isOk(result.data);
assert.isTrue(result.data.success);
});
it('should create *Notify reward amount* notification, failed invalid payload', async () => {
try {
const data = {
eventName: "Notify reward amount",
sendEmail: true,
sendSegment: true,
creationTime: 1667992708000,
email: "[email protected]",
segment: {
payload: {
round: 10,
date: "1667992708000",
amount: "12134",
contractAddress: "0xsfglsjfdflk",
farm: "test farm",
message: "test message",
network: "ethereum",
script: "test script",
transactionHash: "test txhash",
invalidField: "invalid data"
}
}
};
await axios.post(sendNotificationUrl, data, {
headers: {
authorization: getNotifyRewardBasicAuth(),
},
});
// If request doesn't fail, it means this test failed
assert.isTrue(false);
} catch (e: any) {
assert.equal(
e.response.data.message,
errorMessagesEnum.IMPACT_GRAPH_VALIDATION_ERROR.message,
);
assert.equal(e.response.data.description, '"segment.payload.invalidField" is not allowed');
}
});
}

function sendBulkNotificationsTestCases() {
Expand Down
43 changes: 43 additions & 0 deletions src/services/notificationService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { activityCreator } from './notificationService';
import { NOTIFICATIONS_EVENT_NAMES } from '../types/notifications';
import { expect } from 'chai';

describe('activityCreator', () => {
it('should create attributes for NOTIFY_REWARD_AMOUNT', () => {
const payload = {
round: 1,
date: '2024-06-01',
amount: '1000',
contractAddress: '0x123',
farm: 'Test Farm',
message: 'Test Message',
network: 'Test Network',
script: 'Test Script',
transactionHash: '0xabc',
email: '[email protected]'
};
const result = activityCreator(payload, NOTIFICATIONS_EVENT_NAMES.NOTIFY_REWARD_AMOUNT);
expect(JSON.stringify(result)).equal(JSON.stringify({
activities: [
{
activity_id: "act:cm:notify-reward-amount",
attributes: {
'int:cm:round': payload.round,
'str:cm:date': payload.date,
'str:cm:amount': payload.amount,
'str:cm:contractaddress': payload.contractAddress,
'str:cm:farm': payload.farm,
'str:cm:message': payload.message,
'str:cm:network': payload.network,
'str:cm:script': payload.script,
'str:cm:transactionhash': payload.transactionHash,
},
fields: {
'str::email': payload.email,
},
},
],
merge_by: ['str::email'],
}));
})
});
2 changes: 1 addition & 1 deletion src/services/notificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { NOTIFICATIONS_EVENT_NAMES, ORTTO_EVENT_NAMES } from '../types/notificat
import { getEmailAdapter } from '../adapters/adapterFactory';
import { NOTIFICATION_CATEGORY } from '../types/general';

const activityCreator = (payload: any, orttoEventName: NOTIFICATIONS_EVENT_NAMES) : any=> {
export const activityCreator = (payload: any, orttoEventName: NOTIFICATIONS_EVENT_NAMES) : any=> {
let attributes;
switch (orttoEventName) {
case NOTIFICATIONS_EVENT_NAMES.SUBSCRIBE_ONBOARDING:
Expand Down
1 change: 0 additions & 1 deletion src/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export enum NOTIFICATION_CATEGORY {
SUPPORTED_PROJECTS = 'supportedProjects',
GIV_POWER = 'givPower',
ORTTO = 'ortto',
NOTIFY_REWARD_AMOUNT = 'notifyRewardAmount',
}

export enum NOTIFICATION_TYPE_NAMES {
Expand Down
1 change: 1 addition & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const MICRO_SERVICES = {
givethio: 'givethio',
givEconomyNotificationMicroService: 'giveconomy-notification-service',
trace: 'trace',
notifyReward: 'notifyreward',
};

// Need to define trace, blockchain and miscellaneos events
Expand Down
6 changes: 6 additions & 0 deletions src/validators/schemaValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ export const sendNotificationValidator = Joi.object({
update: Joi.string(),

// Notify reward attributes
round: Joi.number(),
date: Joi.string(),
contractAddress: Joi.string(),
farm: Joi.string(),
message: Joi.string(),
network: Joi.string(),
script: Joi.string(),
transactionHash: Joi.string(),
}),
}),
});
Expand Down
7 changes: 7 additions & 0 deletions test/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export const getGivEconomyBasicAuth = () => {
});
};

export const getNotifyRewardBasicAuth = () => {
return createBasicAuthentication({
secret: process.env.NOTIFY_REWARD_THIRD_PARTY_SECRET as string,
username: process.env.NOTIFY_REWARD_THIRD_PARTY_MICRO_SERVICE as string,
});
};

export const getAccessTokenForMockAuthMicroService = (
walletAddress: string,
) => {
Expand Down

0 comments on commit 259147f

Please sign in to comment.