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

Commit

Permalink
Merge branch 'develop' into f_2462_set_ratelimit_for_endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
aminlatifi authored Aug 9, 2021
2 parents d587d49 + 4985776 commit 2f592c0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 125 deletions.
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
"createAuthenticationTtlSeconds":60,
"createAuthenticationThreshold":10
},
"givethIoUrl": "https://mainnet.serve.giveth.io/graphql",

"givethIoUrl": "https://serve.giveth.io/graphql",
"givethIoProjectsReviewerAddress": "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"
}
100 changes: 0 additions & 100 deletions scripts/utils/eventProcessingHelper.json

This file was deleted.

24 changes: 23 additions & 1 deletion src/adapters/givethIo/givethIoAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,26 @@ const getProjectInfoBySLug = async slug => {
}
};

module.exports = { getProjectInfoBySLug };
const getUserByUserId = async userId => {
try {
const query = gql`
query user($userId: Int!) {
user(userId: $userId) {
name
firstName
lastName
email
avatar
walletAddress
}
}
`;
const result = await client.request(query, { userId: Number(userId) });
return result.user;
} catch (e) {
logger.error('getUserByUserId error', e);
throw e;
}
};

module.exports = { getProjectInfoBySLug, getUserByUserId };
15 changes: 11 additions & 4 deletions src/adapters/givethIo/givethIoMockAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const getProjectInfoBySLug = slug => {
id: String(Math.floor(Math.random() * 100000)),
slug,
title: slug.split('-').join(' '),

// Please dont change this it's needed for tests
walletAddress: '0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0',
description: 'test description',
admin: '25',
Expand All @@ -22,5 +20,14 @@ const getProjectInfoBySLug = slug => {
],
};
};

module.exports = { getProjectInfoBySLug };
const getUserByUserId = _userId => {
return {
// Please dont change this it's needed for tests
walletAddress: '0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0',
name: 'John Smith',
firstName: 'James',
lastName: 'Smith',
email: '[email protected]',
};
};
module.exports = { getProjectInfoBySLug, getUserByUserId };
4 changes: 3 additions & 1 deletion src/app.hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const auth = require('@feathersjs/authentication');
const config = require('config');
const { discard } = require('feathers-hooks-common');
const { NotAuthenticated } = require('@feathersjs/errors');
const { DonationStatus } = require('./models/donations.model');
const { isRequestInternal } = require('./utils/feathersUtils');
const { responseLoggerHook, startMonitoring } = require('./hooks/logger');
const { rateLimit } = require('./utils/rateLimit');
Expand Down Expand Up @@ -30,7 +31,8 @@ const authenticate = () => context => {
if (
context.params.provider === 'socketio' &&
context.path === 'donations' &&
context.method === 'create'
context.method === 'create' &&
context.data.status === DonationStatus.PENDING
) {
// for creating donations it's not needed to be authenticated, anonymous users can donate
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function GetCreateCampaignForGivethioProjectsTestCases() {
);
assert.equal(response.statusCode, 200);
assert.equal(response.body.slug, slug);
assert.exists(response.body.owner);
assert.equal(response.body.owner.walletAddress, SAMPLE_DATA.GIVETH_IO_PROJECT_OWNER_ADDRESS);
assert.exists(response.body.id);
});
it('Get 403 for getting projectInfo with invalid walletAddress', async () => {
Expand All @@ -32,7 +34,7 @@ function CreateCampaignForGivethioProjectsTestCases() {
.send({
slug,
txHash: generateRandomTxHash(),
image: '/ipfs/dshdkjsahdkahkdsa',
url: '/ipfs/dshdkjsahdkahkdsa',
})
.set({ Authorization: getJwt(SAMPLE_DATA.GIVETH_IO_PROJECT_OWNER_ADDRESS) });
assert.equal(response.statusCode, 201);
Expand All @@ -46,7 +48,7 @@ function CreateCampaignForGivethioProjectsTestCases() {
.send({
slug,
txHash: generateRandomTxHash(),
image: 'testImage',
url: '/ipfs/kjlfkjsdlkfjdksl',
})
.set({ Authorization: getJwt(SAMPLE_DATA.USER_ADDRESS) });

Expand All @@ -60,7 +62,7 @@ function CreateCampaignForGivethioProjectsTestCases() {
.send({
slug,
txHash: generateRandomTxHash(),
image: 'testImage',
url: '/ipfs/dkjalkdjaslkjdas',
});

assert.equal(response.statusCode, 401);
Expand Down
34 changes: 19 additions & 15 deletions src/services/verifiedCampaigns/verifiedCampaigns.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,48 @@ module.exports = function verifiedCampaigns() {

const service = {
async create(data, params) {
const { txHash, image, slug } = data;
const { txHash, url, slug } = data;

const projectInfo = await givethIoAdapter.getProjectInfoBySLug(slug);
const {
id: givethIoProjectId,
title,
description,
walletAddress: ownerAddress,
} = projectInfo;
if (params.user.address.toLowerCase() !== ownerAddress.toLowerCase()) {
const { id: givethIoProjectId, title, description, image } = projectInfo;
const owner = await givethIoAdapter.getUserByUserId(projectInfo.admin);
if (params.user.address.toLowerCase() !== owner.walletAddress.toLowerCase()) {
throw new errors.Forbidden('The owner of project in givethIo is not you');
}
let campaign = await findCampaignByGivethIoProjectId(app, givethIoProjectId);
if (campaign) {
throw new errors.BadRequest('Campaign with this givethIo projectId exists');
}
const imageIpfsPath = image.match(/\/ipfs\/.*/);
campaign = await app.service('campaigns').create({
title,
url,
slug,
reviewerAddress: config.givethIoProjectsReviewerAddress,
description,
txHash,
image,
ownerAddress,
image: imageIpfsPath ? imageIpfsPath[0] : image,
ownerAddress: owner.walletAddress,
givethIoProjectId,
});
return campaign;
},

async find({ query }) {
const { slug, userAddress } = query;
const projectInfo = await givethIoAdapter.getProjectInfoBySLug(slug);
const { id: givethIoProjectId, walletAddress: ownerAddress } = projectInfo;
if (ownerAddress !== userAddress) {
logger.error('The owner of givethIo project is ', ownerAddress);
const { id: givethIoProjectId } = projectInfo;
const owner = await givethIoAdapter.getUserByUserId(projectInfo.admin);

if (owner.walletAddress !== userAddress) {
logger.error('The owner of givethIo project is ', owner.walletAddress);
throw new errors.Forbidden('The owner of project in givethIo is not you');
}
const campaign = await findCampaignByGivethIoProjectId(app, givethIoProjectId);
if (campaign) {
throw new errors.BadRequest('Campaign with this givethIo projectId exists');
}
return projectInfo;
return { ...projectInfo, owner };
},
};
service.docs = {
Expand All @@ -66,11 +67,13 @@ module.exports = function verifiedCampaigns() {
type: 'string',
in: 'query',
description: 'The slug of project in givethIo',
default: 'testing-another-project',
name: 'slug',
},
{
type: 'string',
in: 'query',
default: '0x826976d7c600d45fb8287ca1d7c76fc8eb732030',
name: 'userAddress',
},
],
Expand All @@ -88,7 +91,8 @@ module.exports = function verifiedCampaigns() {
txHash: {
type: 'string',
},
image: {
url: {
description: 'ipfs url for project',
type: 'string',
},
},
Expand Down

0 comments on commit 2f592c0

Please sign in to comment.