diff --git a/src/services/authorizationService.test.ts b/src/services/authorizationService.test.ts index a86dfdb59..34f589693 100644 --- a/src/services/authorizationService.test.ts +++ b/src/services/authorizationService.test.ts @@ -2,6 +2,7 @@ import { assert } from 'chai'; import { generateRandomEtheriumAddress, generateTestAccessToken, + saveUserDirectlyToDb, } from '../../test/testUtils'; import { User } from '../entities/user'; import Axios from 'axios'; @@ -20,15 +21,7 @@ const origin = 'https://serve.giveth.io'; function authorizationHandlerTestCases() { it('should decode user jwt with current impact graph authorization', async () => { - const userData = { - firstName: 'firstName', - lastName: 'lastName', - email: `${new Date().getTime()}-giveth@giveth.com`, - url: 'website url', - loginType: 'wallet', - walletAddress: generateRandomEtheriumAddress(), - }; - const user = await User.create(userData).save(); + const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress()); const accessToken = await generateTestAccessToken(user.id); const jwtUser = await authorizationHandler('1', accessToken); assert.equal(jwtUser.userId, user.id); @@ -36,15 +29,8 @@ function authorizationHandlerTestCases() { it('should decode user jwt with the auth microservice', async () => { const privateKey = process.env.PRIVATE_ETHERS_TEST_KEY as string; const publicKey = process.env.PUBLIC_ETHERS_TEST_KEY as string; - const userData = { - firstName: 'firstName', - lastName: 'lastName', - email: `${new Date().getTime()}-giveth@giveth.com`, - url: 'website url', - loginType: 'wallet', - walletAddress: publicKey, - }; - const user = await User.create(userData).save(); + + const user = await saveUserDirectlyToDb(publicKey); const nonceRoute = config.get('AUTH_MICROSERVICE_NONCE_URL') as string; const nonceResult = await Axios.get(nonceRoute); const wallet = new ethers.Wallet(privateKey); @@ -73,6 +59,7 @@ function authorizationHandlerTestCases() { const accessToken = authenticationResult.data.jwt; const jwtUser = await authorizationHandler('2', accessToken); assert.equal(jwtUser.userId, user.id); + await User.delete(user.id); }); it('should decode jwt and create user if it is nonexistent', async () => { const privateKey = process.env.PRIVATE_ETHERS_SECONDARY_TEST_KEY as string; diff --git a/src/services/donationService.test.ts b/src/services/donationService.test.ts index ad9f81a3d..0f1b83eeb 100644 --- a/src/services/donationService.test.ts +++ b/src/services/donationService.test.ts @@ -12,6 +12,7 @@ import { createDonationData, createProjectData, DONATION_SEED_DATA, + generateRandomEtheriumAddress, saveDonationDirectlyToDb, saveProjectDirectlyToDb, saveUserDirectlyToDb, @@ -25,6 +26,7 @@ import { errorMessages } from '../utils/errorMessages'; import { findDonationById } from '../repositories/donationRepository'; import { findProjectById } from '../repositories/projectRepository'; import { CHAIN_ID } from '@giveth/monoswap/dist/src/sdk/sdkFactory'; +import { findUserById } from '../repositories/userRepository'; describe('isProjectAcceptToken test cases', isProjectAcceptTokenTestCases); describe( @@ -64,7 +66,7 @@ function sendSegmentEventForDonationTestCases() { } function syncDonationStatusWithBlockchainNetworkTestCases() { - it('should verify a goerli donation', async () => { + it('should verify a goerli donation and update donor.totalDonated and projectOwner.totalReceived', async () => { // https://goerli.etherscan.io/tx/0x43cb1c61a81f007abd3de766a6029ffe62d0324268d7781469a3d7879d487cb1 const transactionInfo = { @@ -78,10 +80,16 @@ function syncDonationStatusWithBlockchainNetworkTestCases() { timestamp: 1661114988, }; const user = await saveUserDirectlyToDb(transactionInfo.fromAddress); - const project = await saveProjectDirectlyToDb({ - ...createProjectData(), - walletAddress: transactionInfo.toAddress, - }); + const projectOwner = await saveUserDirectlyToDb( + generateRandomEtheriumAddress(), + ); + const project = await saveProjectDirectlyToDb( + { + ...createProjectData(), + walletAddress: transactionInfo.toAddress, + }, + projectOwner, + ); const donation = await saveDonationDirectlyToDb( { amount: transactionInfo.amount, @@ -105,6 +113,12 @@ function syncDonationStatusWithBlockchainNetworkTestCases() { assert.equal(updateDonation.id, donation.id); assert.isTrue(updateDonation.segmentNotified); assert.equal(updateDonation.status, DONATION_STATUS.VERIFIED); + + const donor = await findUserById(user.id); + assert.equal(donor?.totalDonated, 100); + + const updatedProjectOwner = await findUserById(projectOwner.id); + assert.equal(updatedProjectOwner?.totalReceived, 100); }); it('should verify a Polygon donation', async () => { diff --git a/src/services/donationService.ts b/src/services/donationService.ts index 7041df901..9097ff8f1 100644 --- a/src/services/donationService.ts +++ b/src/services/donationService.ts @@ -323,7 +323,8 @@ export const syncDonationStatusWithBlockchainNetwork = async (params: { // After updating price we update totalDonations await updateTotalDonationsOfProject(donation.projectId); - await updateUserTotalReceived(donation.userId); + const project = await findProjectById(donation.projectId); + await updateUserTotalReceived(project!.adminUser.id); await sendSegmentEventForDonation({ donation, });