Skip to content

Commit

Permalink
feat(user): finished user class methods and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed May 8, 2024
1 parent ce60e7b commit 549e539
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 26 deletions.
28 changes: 28 additions & 0 deletions __mocks__/v3/user.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { User } from '../../src/v3/user/user';
import { activitiesStub, userStub } from '../../test/stubs/v3/user.stub';

export const UserMockService = (user: User) => ({
meMock: jest.spyOn(user.sdk, 'me_query').mockResolvedValue({
me: userStub(),
}),
getSingleUserMock: jest.spyOn(user.sdk, 'user_query').mockResolvedValue({
user: userStub(),
}),
myPDACountMock: jest.spyOn(user.sdk, 'myPDACount_query').mockResolvedValue({
myPDACount: 10,
}),
myPDAsMock: jest.spyOn(user.sdk, 'myPDAs_query').mockResolvedValue({
myPDAs: userStub().issuedPDAs,
}),
myDataModelsCountMock: jest
.spyOn(user.sdk, 'dataModelsCount_query')
.mockResolvedValue({
dataModelsCount: 10,
}),
myActivitiesCountMock: jest
.spyOn(user.sdk, 'myActivitiesCount_query')
.mockResolvedValue({ myActivitiesCount: 10 }),
myActivitiesMock: jest
.spyOn(user.sdk, 'myActivities_query')
.mockResolvedValue({ myActivities: activitiesStub() }),
});
17 changes: 14 additions & 3 deletions scripts/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ function processFile(inputFile, outputFile) {
}

const filesToProcess = [
{ input: 'dist/gatewaySdk/index.d.ts', output: 'dist/gatewaySdk/index.d.ts' },
{
input: 'dist/gatewaySdk/sources/GatewaySDK/types.d.ts',
output: 'dist/gatewaySdk/sources/GatewaySDK/types.d.ts',
input: 'dist/gatewaySdk/sources/GatewayV2/index.d.ts',
output: 'dist/gatewaySdk/sources/GatewayV2/index.d.ts',
},
{
input: 'dist/gatewaySdk/sources/GatewayV2/types.d.ts',
output: 'dist/gatewaySdk/sources/GatewayV2/types.d.ts',
},
{
input: 'dist/gatewaySdk/sources/GatewayV3/index.d.ts',
output: 'dist/gatewaySdk/sources/GatewayV3/index.d.ts',
},
{
input: 'dist/gatewaySdk/sources/GatewayV3/types.d.ts',
output: 'dist/gatewaySdk/sources/GatewayV3/types.d.ts',
},
];

Expand Down
4 changes: 1 addition & 3 deletions scripts/compress.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/bin/sh



files="dist/gatewaySdk/index.js dist/src/auth/auth.js dist/src/data-model/data-model.js dist/src/dataRequestsTemplate/dataRequestsTemplate.js dist/src/organization/organization.js dist/src/pda/pda.js dist/src/proof/proof.js dist/src/request/request.js dist/src/user/user.js"
files="dist/gatewaySdk/sources/*.js dist/gatewaySdk/src/*.js"

for file in $files; do
uglifyjs "$file" -o "$file"
Expand Down
3 changes: 3 additions & 0 deletions src/GatewayV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
clientTimingWrapper,
parameterChecker,
} from './utils/helper';
import { PDA } from './v3/pda/pda';

export class GatewayV3 {
private sdk: Sdk;
private pda: PDA;

constructor({
apiKey,
Expand All @@ -29,5 +31,6 @@ export class GatewayV3 {
});

this.sdk = getSdk(client, logging ? clientTimingWrapper : undefined);
this.pda = new PDA(this.sdk);
}
}
24 changes: 21 additions & 3 deletions src/v3/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ export class Auth {
signingCipher?: SignCipherEnum;
}) {
try {
isWalletAddressValid(signingKey, Chain.EVM);
let chain: Chain;
if (signingCipher === undefined) {
chain = Chain.EVM;
} else if (signingCipher === SignCipherEnum.ED25519) {
chain = Chain.SOL;
} else chain = Chain.EVM;
isWalletAddressValid(signingKey, chain);
isStringValid(signature);
return (
await this.sdk.createUser_mutation({
Expand All @@ -105,7 +111,13 @@ export class Auth {
*/
async generateNonce(wallet: string, cipher?: SignCipherEnum) {
try {
isWalletAddressValid(wallet, Chain.EVM);
let chain: Chain;
if (cipher === undefined) {
chain = Chain.EVM;
} else if (cipher === SignCipherEnum.ED25519) {
chain = Chain.SOL;
} else chain = Chain.EVM;
isWalletAddressValid(wallet, chain);
return await this.sdk.generateNonce_mutation({
input: { wallet, cipher },
});
Expand All @@ -121,7 +133,13 @@ export class Auth {
*/
async refreshToken(input: SignedWalletNonceInput) {
try {
isWalletAddressValid(input.signingKey, Chain.EVM);
let chain: Chain;
if (input.cipher === undefined) {
chain = Chain.EVM;
} else if (input.cipher === SignCipherEnum.ED25519) {
chain = Chain.SOL;
} else chain = Chain.EVM;
isWalletAddressValid(input.signingKey, chain);
isStringValid(input.signature);
return (await this.sdk.refreshToken_mutation({ input })).refreshToken;
} catch (error) {
Expand Down
23 changes: 17 additions & 6 deletions src/v3/pda/pda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ import {
Sdk,
UpdatePDAInput,
} from '../../../gatewaySdk/sources/GatewayV3';
import { Chain } from '../../types';
import { Chain, SignCipherEnum } from '../../types';
import { errorHandler } from '../../utils/errorHandler';
import {
isUUIDValid,
isWalletAddressValid,
validateObjectProperties,
} from '../../utils/validators';

// ask about helper functions like json encoder
// about validating singature
// about wallet type
// secp256k1=evm by default
// Ed25519=solana

export class PDA {
public sdk: Sdk;
Expand Down Expand Up @@ -111,8 +110,14 @@ export class PDA {
*/
async changePDAStatus(input: UpdatePDAStatusInput) {
try {
let chain: Chain;
if (input.signingCipher === undefined) {
chain = Chain.EVM;
} else if (input.signingCipher === SignCipherEnum.ED25519) {
chain = Chain.SOL;
} else chain = Chain.EVM;
validateObjectProperties(input.data);
isWalletAddressValid(input.signingKey, Chain.EVM);
isWalletAddressValid(input.signingKey, chain);
return await this.sdk.changePDAStatus_mutation({ input });
} catch (error) {
throw new Error(errorHandler(error));
Expand All @@ -127,8 +132,14 @@ export class PDA {
*/
async createPDA(pdaInput: CreatePDAInput) {
try {
let chain: Chain;
if (pdaInput.signingCipher === undefined) {
chain = Chain.EVM;
} else if (pdaInput.signingCipher === SignCipherEnum.ED25519) {
chain = Chain.SOL;
} else chain = Chain.EVM;
validateObjectProperties(pdaInput.data);
isWalletAddressValid(pdaInput.signingKey, Chain.EVM);
isWalletAddressValid(pdaInput.signingKey, chain);
return await this.sdk.createPDA_mutation({ input: pdaInput });
} catch (error) {
throw new Error(errorHandler(error));
Expand Down
117 changes: 117 additions & 0 deletions src/v3/user/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { UserIdentifierTypeV3 } from '../../types';
import { errorHandler } from '../../utils/errorHandler';
import { isStringValid } from '../../utils/validators';
import {
FilterDataModelInput,
myActivities_queryQueryVariables,
myActivitiesCount_queryQueryVariables,
myPDACount_queryQueryVariables,
myPDAs_queryQueryVariables,
Sdk,
} from '../../../gatewaySdk/sources/GatewayV3';

export class User {
public sdk: Sdk;

constructor(sdk: Sdk) {
this.sdk = sdk;
}

/**
* The function `me` makes an asynchronous call to `me_query` and returns the result, or throws an
* error if something goes wrong.
* @returns a Promise that resolves to me.
*/
async me() {
try {
return await this.sdk.me_query();
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function takes a user identifier type and value as input, queries the user using the SDK, and
* returns the result.
* @param - - `type`: The type of user identifier. It can be one of the following values:
* @returns The `user` function is returning the result of the `user_query` method call from the `sdk`
* object.
*/
async getSingleUser({
type,
value,
}: {
type: UserIdentifierTypeV3;
value: string;
}) {
try {
isStringValid(value);
return await this.sdk.user_query({ input: { type, value } });
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `myPDACount` is an asynchronous function that returns the count of a user's PDA
* based on an optional filter.
* @param {FilterPDAInput} [filter] - The `filter` parameter is an optional input that allows you to
* specify criteria for filtering the PDAs before counting them. It is
* of type `FilterPDAInput`.
* @returns a Promise that resolves to a number.
*/
async myPDACount(filter?: myPDACount_queryQueryVariables) {
try {
return (await this.sdk.myPDACount_query(filter)).myPDACount;
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `myPDAs` is an asynchronous function that takes in a `myPDAs_queryQueryVariables` object and returns a
* promise that resolves to a `myPDAs_queryQuery` object.
* @param {myPDAs_queryQueryVariables} - - `filter`: An object that contains filter criteria for the query.
* @returns a Promise that resolves to a value of type `myPDAs_queryQuery`.
*/
async myPDAs(variables?: myPDAs_queryQueryVariables) {
try {
return await this.sdk.myPDAs_query(variables);
} catch (error) {
throw new Error(errorHandler(error));
}
}

/**
* The function `myDataModelsCount` is an asynchronous function that retrieves the count of data
* models based on an optional filter and returns the count.
* @param {FilterDataModelInput} [filter] - The `filter` parameter is an optional input that allows
* you to specify conditions to filter the data models. It is of type `FilterDataModelInput`. You can
* use this parameter to define criteria such as filtering by a specific field value or applying
* logical operators like AND and OR to combine multiple conditions.
* @returns the count of data models that match the provided filter.
*/
async myDataModelsCount(filter?: FilterDataModelInput) {
try {
return (await this.sdk.dataModelsCount_query({ filter })).dataModelsCount;
} catch (error) {
throw new Error(errorHandler(error));
}
}

async myActivities(filter?: myActivities_queryQueryVariables) {
try {
return await this.sdk.myActivities_query(filter);
} catch (error) {
throw new Error(errorHandler(error));
}
}

async myActivitiesCount(filter?: myActivitiesCount_queryQueryVariables) {
try {
return (await this.sdk.myActivitiesCount_query(filter)).myActivitiesCount;
} catch (error) {
throw new Error(errorHandler(error));
}
}
}
43 changes: 43 additions & 0 deletions test/stubs/v3/user.stub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Activity, User } from '../../../gatewaySdk/sources/GatewayV3';

export const userStub = (overrideUser?: Partial<User>): User => ({
id: 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
createdAt: new Date('2021-01-01T12:00:00Z'),
updatedAt: new Date('2021-01-01T12:00:00Z'),
username: 'test01',
isCompleted: true,
issuedPDAs: [],
receivedPDAs: [],
receivedProofs: [],
recipientDataRequests: [],
verifierDataRequests: [],
roles: [],
did: 'did:gatewayid:abc123',
encryptionKey: '',
usernameLastUpdated: new Date('2021-01-01T12:00:00Z'),
...overrideUser,
});

export const activitiesStub = (): Activity[] => {
return [
{
action: 'PDA_ISSUANCE',
createdAt: '2024-01-09T13:09:29.020Z',
id: '659d4589abb2c024ff2ef8e5',
updatedAt: '2024-01-09T13:09:29.020Z',
metadata: {
creator: '',
dataModel: '',
issuer: '',
dataModels: [''],
organization: '',
owner: '',
pda: '',
proof: '',
requestTemplate: '',
signedBy: '',
status: '',
},
},
];
};
3 changes: 1 addition & 2 deletions test/v3/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { GraphQLClient } from 'graphql-request';

import { getSdk } from '../../gatewaySdk/sources/GatewayV3';
import { Auth } from '../../src/v3/auth/auth';
import { AuthMockService } from '../../__mocks__/v3/auth.mock';
import { authStub } from '../stubs/v3/auth.stub';
import { SignCipherEnum } from '../../src/types';
import { Auth } from '../../src/v3/auth/auth';

let auth: Auth;

Expand Down
6 changes: 3 additions & 3 deletions test/v3/pda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PDA } from '../../src/v3/pda/pda';
import { getSdk } from '../../gatewaySdk/sources/GatewayV3';
import { pdaBodyData, pdaCreateStub, pdaStub } from '../stubs/v3/pda.stub';
import { PDAMockService } from '../../__mocks__/v3/pda.mock';
import { authStub } from '../stubs/v3/auth.stub';

let pda: PDA;

Expand Down Expand Up @@ -134,9 +135,8 @@ describe('PDA SERVICE TESTING', () => {

const { updatePDA } = await pda.updatePDA({
data: pdaBodyData({ id: pdaStub().id }),
did: 'did:gatewayid:abc123',
signature:
'0x9ddccd4e4f97187334ec2ed980906c7adad096bd892a393243158c3acb6cf6d1',
did: authStub().did,
signature: authStub().signature,
});

expect(updatePDA.id).toBe(pdaStub().id);
Expand Down
Loading

0 comments on commit 549e539

Please sign in to comment.