Skip to content

Commit

Permalink
feat: added unit test for organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Nov 24, 2023
1 parent fdada9d commit 60bee1d
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 14 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build PR
name: Run Eslint & Test cases

on:
pull_request:
Expand All @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Project
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Install pnpm
run: npm install -g pnpm@7.33.5
- name: Install Project Dependencies
Expand All @@ -20,3 +20,7 @@ jobs:
run: pnpm mesh build
- name: Test project
run: pnpm test
env:
DATAMODEL_ID: ${{ secrets.DATAMODEL_ID }}
BEARER_TOKEN: ${{ secrets.BEARER_TOKEN }}
API_KEY: ${{ secrets.API_KEY }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
.mesh/
.mesh/
.env
.vscode
7 changes: 4 additions & 3 deletions .meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ sources:
endpoint: https://develop.protocol.mygateway.xyz/v1/graphql
transforms:
- filterSchema:
mode: bare
mode: bare
filters:
- Query.!transaction
- Query.!transactions
- Query.!transaction
- Query.!transactions
- Query.!myTransactions
customFetch: ./custom-fetch.ts

sdk:
Expand Down
3 changes: 3 additions & 0 deletions src/Gateway.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { getMeshSDK, Sdk } from '../.mesh';
import { Organization } from './organization/organization';

export class Gateway {
private sdk: Sdk;
public organization: Organization;

constructor({ apiKey, token }: { apiKey: string; token: string }) {
if (!apiKey && !token) throw new Error('No token found');
this.sdk = getMeshSDK({
apiKey,
token,
});
this.organization = new Organization(this.sdk);
}
}
83 changes: 75 additions & 8 deletions src/organization/organization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { CreateOrganizationInput, Sdk, TransferMemberInput } from '../../.mesh';
import {
CreateOrganizationInput,
FilterOrganizationInput,
MemberInput,
Sdk,
TransferMemberInput,
UpdateOrganizationInput,
} from '../../.mesh';
import { OrganizationIdentifierType } from '../types';
import { errorHandler } from '../utils/errorHandler';

export class Organization {
private sdk: Sdk;
Expand All @@ -8,16 +17,74 @@ export class Organization {
}

async createOrganization(organizationInput: CreateOrganizationInput) {
return await this.sdk.createOrganization_mutation({
input: organizationInput,
});
try {
return await this.sdk.createOrganization_mutation({
input: organizationInput,
});
} catch (error) {
throw new Error(errorHandler(error));
}
}

async addMemberToOrganization(memberInput: MemberInput) {
try {
return await this.sdk.addMemberToOrganization_mutation({
input: memberInput,
});
} catch (error) {
throw new Error(errorHandler(error));
}
}

async changeMemberRole(memberInput: MemberInput) {
try {
return await this.sdk.changeMemberRole_mutation({ input: memberInput });
} catch (error) {
throw new Error(errorHandler(error));
}
}

async removeMemberFromOrganization(memberInput: TransferMemberInput) {
return await this.sdk.removeMemberFromOrganization_mutation({
input: memberInput,
});
try {
return await this.sdk.removeMemberFromOrganization_mutation({
input: memberInput,
});
} catch (error) {
throw new Error(errorHandler(error));
}
}


async updateOrganization(updatedOrganization: UpdateOrganizationInput) {
try {
return await this.sdk.updateOrganization_mutation({
input: updatedOrganization,
});
} catch (error) {
throw new Error(errorHandler(error));
}
}

async getOrganization(type: OrganizationIdentifierType, value: string) {
try {
return await this.sdk.organization_query({ input: { type, value } });
} catch (error) {
throw new Error(errorHandler(error));
}
}

async getOrganizations({
filter,
skip,
take,
}: {
filter?: FilterOrganizationInput;
skip?: number;
take?: number;
}) {
try {
return await this.sdk.organizations_query({ filter, skip, take });
} catch (error) {
throw new Error(errorHandler(error));
}
}
}
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ export enum UserIdentifierType {
SOLANA = 'SOLANA',
USER_ID = 'USER_ID',
}

export enum OrganizationIdentifierType {
GATEWAY_ID = 'GATEWAY_ID',
ORG_ID = 'ORG_ID',
}

export enum OrganizationRole {
Admin = 'Admin',
Member = 'Member',
Owner = 'Owner',
}
7 changes: 7 additions & 0 deletions src/utils/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const errorHandler = (error: any): string => {
if (typeof error === 'object' && error !== null && 'message' in error) {
return error.message;
} else {
return 'Something went wrong!';
}
};
86 changes: 86 additions & 0 deletions test/organization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import dotenv from 'dotenv';
import { Gateway } from '../src/Gateway';
import {
OrganizationIdentifierType,
OrganizationRole,
UserIdentifierType,
} from '../src/types';
dotenv.config();
const DEFAULT_TIMEOUT = 10000;

let api: Gateway;

beforeAll(() => {
api = new Gateway({
apiKey: process.env.API_KEY!,
token: process.env.BEARER_TOKEN!,
});
});

describe('ORGANIZATION SERVICE TESTING', () => {
it(
'organization crud',
async () => {
let obj = {
username: 'test_for_sdk_2',
name: 'test org sdk 2',
description: 'test organization',
};
const { createOrganization } =
await api.organization.createOrganization(obj);
const { organization } = await api.organization.getOrganization(
OrganizationIdentifierType.ORG_ID,
createOrganization.id,
);
expect(organization?.id).toEqual(createOrganization.id);
let updatedOrgObj = {
description: 'changed description for organization',
id: createOrganization.id,
};
const { updateOrganization } =
await api.organization.updateOrganization(updatedOrgObj);
expect(updateOrganization.description).toEqual(updatedOrgObj.description);
},
DEFAULT_TIMEOUT,
);

it(
'member crud organization',
async () => {
let addMemberObj = {
organization: {
type: OrganizationIdentifierType.ORG_ID,
value: process.env.ORGAINZATION_ID!,
},
user: { type: UserIdentifierType.GATEWAY_ID, value: 'testing_sdk' },
};
let changeMemberRoleObj = {
...addMemberObj,
role: OrganizationRole.Admin,
};
const { addMemberToOrganization } =
await api.organization.addMemberToOrganization(addMemberObj);
expect(addMemberToOrganization).toBeDefined();
const { changeMemberRole } =
await api.organization.changeMemberRole(changeMemberRoleObj);
expect(changeMemberRole).toBeDefined();
const { removeMemberFromOrganization } =
await api.organization.removeMemberFromOrganization(addMemberObj);
expect(removeMemberFromOrganization).toBeDefined();
},
DEFAULT_TIMEOUT,
);

it(
'organizations',
async () => {
const { organizations } = await api.organization.getOrganizations({
filter: { verified: false },
skip: 0,
take: 10,
});
expect(organizations.length).toBeGreaterThanOrEqual(0);
},
DEFAULT_TIMEOUT,
);
});

0 comments on commit 60bee1d

Please sign in to comment.