Skip to content

Commit

Permalink
fix gitcoin bugs, add query tests and update .envs
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed May 28, 2023
1 parent 74df117 commit fa1d7dc
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 19 deletions.
5 changes: 5 additions & 0 deletions config/example.env
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,8 @@ OPTIMISM_NODE_HTTP_URL=https://optimism-mainnet.public.blastapi.io/
ENABLE_INSTANT_BOOSTING_UPDATE=true
# OPTIONAL - default: Every 5 minutes
INSTANT_BOOSTING_UPDATE_CRONJOB_EXPRESSION=0 */5 * * * *

// Gitcoin API
GITCOIN_ADAPTER=mock
GITCOIN_PASSPORT_API=
GITCOIN_SCORER_ID=
2 changes: 2 additions & 0 deletions config/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,5 @@ DONATION_VERIFICAITON_EXPIRATION_HOURS=24
# We need it for monoswap
POLYGON_MAINNET_NODE_HTTP_URL=https://polygon-rpc.com
OPTIMISM_NODE_HTTP_URL=https://optimism-mainnet.public.blastapi.io

GITCOIN_ADAPTER=mock
2 changes: 1 addition & 1 deletion src/adapters/adaptersFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const gitcoinAdapter = new GitcoinAdapter();
const mockGitcoinAdapter = new GitcoinMockAdapter();

export const getGitcoinAdapter = () => {
switch (process.env.GITCOINT_ADAPTER) {
switch (process.env.GITCOIN_ADAPTER) {
case 'gitcoin':
return gitcoinAdapter;
case 'mock':
Expand Down
10 changes: 5 additions & 5 deletions src/adapters/gitcoin/gitcoinAdapterInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export interface SubmitPassportInput {

export interface SubmittedPassportResponse {
address: string;
score: string | undefined;
score: string | undefined | null;
status: string;
last_score_timestamp: string;
evidence: any | undefined;
error: string | undefined;
evidence: any | undefined | null;
error: string | undefined | null;
}

export interface SubmittedPassportsResponse {
Expand All @@ -30,8 +30,8 @@ interface StampInterface {
}

export interface GetPassportStampsResponse {
next: string;
prev: string;
next: string | null | undefined;
prev: string | null | undefined;
items: StampInterface[];
}

Expand Down
52 changes: 40 additions & 12 deletions src/adapters/gitcoin/gitcoinMockAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { generateRandomEtheriumAddress } from '../../../test/testUtils';
import {
GitcoinAdapterInterface,
SigningMessageAndNonceResponse,
Expand All @@ -7,17 +8,29 @@ import {
GetPassportStampsResponse,
} from './gitcoinAdapterInterface';

export const cachedReferralAddresses = {};

export class GitcoinMockAdapter implements GitcoinAdapterInterface {
async getWalletAddressScore(
address: string,
): Promise<SubmittedPassportResponse> {
if (cachedReferralAddresses[address]) {
return Promise.resolve({
address,
score: '10',
status: 'ok',
last_score_timestamp: 'string',
evidence: undefined,
error: undefined,
});
}
return Promise.resolve({
address: 'string',
score: 'string',
status: 'string',
address,
score: undefined,
status: 'Error',
last_score_timestamp: 'string',
evidence: undefined,
error: undefined,
error: 'Invalid address',
});
}
async getListOfScores(): Promise<SubmittedPassportsResponse> {
Expand All @@ -41,9 +54,13 @@ export class GitcoinMockAdapter implements GitcoinAdapterInterface {
nonce: 'string',
});
}
// Use this method to register in the cache the address for testing
async submitPassport(
params: SubmitPassportInput,
): Promise<SubmittedPassportResponse> {
if (!cachedReferralAddresses[params.address]) {
cachedReferralAddresses[params.address] = params.address;
}
return Promise.resolve({
address: 'string',
score: 'string',
Expand All @@ -54,15 +71,26 @@ export class GitcoinMockAdapter implements GitcoinAdapterInterface {
});
}
async getPassportStamps(address: string): Promise<GetPassportStampsResponse> {
if (cachedReferralAddresses[address]) {
return Promise.resolve({
next: 'string',
prev: 'string',
items: [
{
version: '1',
credential: 'any',
},
{
version: '1',
credential: 'any',
},
],
});
}
return Promise.resolve({
next: 'string',
prev: 'string',
items: [
{
version: 'string',
credential: 'any',
},
],
next: null,
prev: null,
items: [],
});
}
}
63 changes: 62 additions & 1 deletion src/resolvers/userResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,78 @@ import {
SEED_DATA,
} from '../../test/testUtils';
import axios from 'axios';
import { updateUser, userByAddress } from '../../test/graphqlQueries';
import {
refreshUserScores,
updateUser,
userByAddress,
} from '../../test/graphqlQueries';
import { assert } from 'chai';
import { errorMessages } from '../utils/errorMessages';
import { insertSinglePowerBoosting } from '../repositories/powerBoostingRepository';
import { create } from 'domain';
import { DONATION_STATUS } from '../entities/donation';
import { getGitcoinAdapter } from '../adapters/adaptersFactory';

describe('updateUser() test cases', updateUserTestCases);
describe('userByAddress() test cases', userByAddressTestCases);
describe('refreshUserScores() test cases', refreshUserScoresTestCases);
// TODO I think we can delete addUserVerification query
// describe('addUserVerification() test cases', addUserVerificationTestCases);
function refreshUserScoresTestCases() {
it('should refresh user scores if the user has registered passport', async () => {
const userData = {
firstName: 'firstName',
lastName: 'lastName',
email: '[email protected]',
avatar: 'pinata address',
url: 'website url',
loginType: 'wallet',
walletAddress: generateRandomEtheriumAddress(),
};
const user = await User.create(userData).save();
await getGitcoinAdapter().submitPassport({
address: userData.walletAddress,
scorer: '200',
signature: 'any',
nonce: 'any',
});
const result = await axios.post(graphqlUrl, {
query: refreshUserScores,
variables: {
address: userData.walletAddress,
},
});

const updatedUser = result.data.data.refreshUserScores;
assert.equal(updatedUser.walletAddress, user.walletAddress);
assert.isTrue(updatedUser.passportScore > 0);
assert.isTrue(updatedUser.passportStamps > 0);
});
it('should not refresh user scores if not registered to gitcoin', async () => {
const userData = {
firstName: 'firstName',
lastName: 'lastName',
email: '[email protected]',
avatar: 'pinata address',
url: 'website url',
loginType: 'wallet',
walletAddress: generateRandomEtheriumAddress(),
};
const user = await User.create(userData).save();
const result = await axios.post(graphqlUrl, {
query: refreshUserScores,
variables: {
address: userData.walletAddress,
},
});

const updatedUser = result.data.data.refreshUserScores;
assert.equal(updatedUser.walletAddress, user.walletAddress);
// if score remains null the user has not registered
assert.isNull(updatedUser.passportScore);
assert.equal(updatedUser.passportStamps, 0);
});
}

function userByAddressTestCases() {
it('Get non-sensitive fields of a user', async () => {
Expand Down
22 changes: 22 additions & 0 deletions test/graphqlQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,28 @@ export const userByAddress = `
}
`;

export const refreshUserScores = `
query ($address: String!) {
refreshUserScores(address: $address) {
id
firstName
lastName
name
email
avatar
walletAddress
url
location
boostedProjectsCount
likedProjectsCount
donationsCount
projectsCount
passportScore
passportStamps
}
}
`;

export const uploadImageToIpfsQuery = `
mutation ($fileUpload: FileUploadInputType!) {
upload(fileUpload: $fileUpload)
Expand Down

0 comments on commit fa1d7dc

Please sign in to comment.