Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix: all unit test cases for backend modules (HBE-171) (#51)

Mir Arif Hasanffc08227dd4a
diff --git a/packages/hoppscotch-backend/src/admin/admin.service.spec.ts b/packages/hoppscotch-backend/src/admin/admin.service.spec.ts
index e3b3af022..7242997bd 100644
--- a/packages/hoppscotch-backend/src/admin/admin.service.spec.ts
+++ b/packages/hoppscotch-backend/src/admin/admin.service.spec.ts
@@ -62,7 +62,7 @@ describe('AdminService', () => {
const results = await adminService.fetchInvitedUsers();
expect(results).toEqual(invitedUsers);
});
- test('should resolve left and return an error if invited users not found', async () => {
+ test('should resolve left and return an empty array if invited users not found', async () => {
mockPrisma.invitedUsers.findMany.mockResolvedValue([]);
const results = await adminService.fetchInvitedUsers();
diff --git a/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts b/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts
index 7fbff36e3..bd5bb208f 100644
--- a/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts
+++ b/packages/hoppscotch-backend/src/team-collection/team-collection.service.ts
@@ -973,62 +973,4 @@ export class TeamCollectionService {
const teamCollectionsCount = this.prisma.teamCollection.count();
return teamCollectionsCount;
}
-
- // async importCollectionFromFirestore(
- // userUid: string,
- // fbCollectionPath: string,
- // teamID: string,
- // parentCollectionID?: string,
- // ): Promise<TeamCollection> {
- // const syncDoc = await this.fb.firestore
- // .doc(`users/${userUid}/collections/sync`)
- // .get();
-
- // if (!syncDoc.exists) throw new Error(TEAM_USER_NO_FB_SYNCDATA);
-
- // const doc = syncDoc.data();
-
- // if (!doc) throw new Error(TEAM_USER_NO_FB_SYNCDATA);
-
- // // The 'target' variable will have the intended path to reach
- // let target: FBCollectionFolder | null | undefined;
- // try {
- // const indexPaths = fbCollectionPath.split('/').map((x) => parseInt(x));
- // target = doc.collection[indexPaths.shift() as number];
- // while (indexPaths.length > 0)
- // {
- // const index = indexPaths.shift() as number;
- // target = target?.folders[index];
- // }
- // } catch (e) {
- // target = null;
- // }
-
- // if (!target) throw new Error(TEAM_FB_COLL_PATH_RESOLVE_FAIL);
-
- // const queryGen = this.generatePrismaQueryObjForFBCollFolder(target, teamID);
-
- // let result: TeamCollection;
-
- // if (parentCollectionID) {
- // result = await this.prisma.teamCollection.create({
- // data: {
- // ...queryGen,
- // parent: {
- // connect: {
- // id: parentCollectionID,
- // },
- // },
- // },
- // });
- // } else {
- // result = await this.prisma.teamCollection.create({
- // data: queryGen,
- // });
- // }
-
- // this.pubsub.publish(`team_coll/${teamID}/coll_added`, result);
-
- // return result;
- // }
}
diff --git a/packages/hoppscotch-backend/src/team-request/team-request.service.spec.ts b/packages/hoppscotch-backend/src/team-request/team-request.service.spec.ts
index 377f1beb3..31c826efc 100644
--- a/packages/hoppscotch-backend/src/team-request/team-request.service.spec.ts
+++ b/packages/hoppscotch-backend/src/team-request/team-request.service.spec.ts
@@ -8,6 +8,7 @@ import {
TEAM_INVALID_ID,
TEAM_REQ_NOT_FOUND,
TEAM_REQ_REORDERING_FAILED,
+ TEAM_COLL_NOT_FOUND,
} from 'src/errors';
import * as E from 'fp-ts/Either';
import { mockDeep, mockReset } from 'jest-mock-extended';
@@ -42,6 +43,9 @@ const teamCollection: DbTeamCollection = {
parentID: null,
teamID: team.id,
title: 'Team Collection 1',
+ orderIndex: 1,
+ createdOn: new Date(),
+ updatedOn: new Date(),
};
const dbTeamRequests: DbTeamRequest[] = [];
for (let i = 1; i <= 10; i++) {
@@ -236,7 +240,7 @@ describe('deleteTeamRequest', () => {
describe('createTeamRequest', () => {
test('rejects for invalid collection id', async () => {
- mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(null);
+ mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(E.left(TEAM_INVALID_COLL_ID));
const response = await teamRequestService.createTeamRequest(
'invalidcollid',
@@ -253,7 +257,9 @@ describe('createTeamRequest', () => {
const dbRequest = dbTeamRequests[0];
const teamRequest = teamRequests[0];
- mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(team);
+ mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(
+ E.right(team),
+ );
mockPrisma.teamRequest.create.mockResolvedValue(dbRequest);
const response = teamRequestService.createTeamRequest(
@@ -270,7 +276,9 @@ describe('createTeamRequest', () => {
const dbRequest = dbTeamRequests[0];
const teamRequest = teamRequests[0];
- mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(team);
+ mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(
+ E.right(team),
+ );
mockPrisma.teamRequest.create.mockResolvedValue(dbRequest);
await teamRequestService.createTeamRequest(
@@ -359,7 +367,9 @@ describe('getTeamOfRequest', () => {
describe('getCollectionOfRequest', () => {
test('rejects for invalid collection id', async () => {
- mockTeamCollectionService.getCollection.mockResolvedValue(null as any);
+ mockTeamCollectionService.getCollection.mockResolvedValue(
+ E.left(TEAM_COLL_NOT_FOUND),
+ );
expect(
teamRequestService.getCollectionOfRequest(teamRequests[0]),
@@ -367,7 +377,9 @@ describe('getCollectionOfRequest', () => {
});
test('resolves for valid collection id', async () => {
- mockTeamCollectionService.getCollection.mockResolvedValue(teamCollection);
+ mockTeamCollectionService.getCollection.mockResolvedValue(
+ E.right(teamCollection),
+ );
expect(
teamRequestService.getCollectionOfRequest(teamRequests[0]),
diff --git a/packages/hoppscotch-backend/src/team-request/team-request.service.ts b/packages/hoppscotch-backend/src/team-request/team-request.service.ts
index 3e48ea589..0d89999e0 100644
--- a/packages/hoppscotch-backend/src/team-request/team-request.service.ts
+++ b/packages/hoppscotch-backend/src/team-request/team-request.service.ts
@@ -231,8 +231,8 @@ export class TeamRequestService {
const teamCollection = await this.teamCollectionService.getCollection(
req.collectionID,
);
- if (!teamCollection) return E.left(TEAM_INVALID_COLL_ID);
- return E.right(teamCollection);
+ if (E.isLeft(teamCollection)) return E.left(TEAM_INVALID_COLL_ID);
+ return E.right(teamCollection.right);
}
/**
diff --git a/packages/hoppscotch-backend/src/team/team.service.spec.ts b/packages/hoppscotch-backend/src/team/team.service.spec.ts
index 6da64f0ca..60ee683e3 100644
--- a/packages/hoppscotch-backend/src/team/team.service.spec.ts
+++ b/packages/hoppscotch-backend/src/team/team.service.spec.ts
@@ -10,11 +10,12 @@ import {
TEAM_INVALID_ID_OR_USER,
} from '../errors';
import { mockDeep, mockReset } from 'jest-mock-extended';
+import * as O from 'fp-ts/Option';
const mockPrisma = mockDeep<PrismaService>();
const mockUserService = {
- getUserWithEmail: jest.fn(),
+ findUserByEmail: jest.fn(),
getUserForUID: jest.fn(),
authenticateWithIDToken: jest.fn(),
};
@@ -171,15 +172,17 @@ describe('addMemberToTeam', () => {
describe('addMemberToTeamWithEmail', () => {
afterEach(() => {
- mockUserService.getUserWithEmail.mockClear();
+ mockUserService.findUserByEmail.mockClear();
mockUserService.authenticateWithIDToken.mockClear();
mockUserService.authenticateWithIDToken.mockClear();
});
test('resolves when user with email exists', () => {
- mockUserService.getUserWithEmail.mockResolvedValueOnce({
- uid: dbTeamMember.userUid,
- });
+ mockUserService.findUserByEmail.mockResolvedValueOnce(
+ O.some({
+ uid: dbTeamMember.userUid,
+ }),
+ );
mockPrisma.teamMember.create.mockResolvedValue(dbTeamMember);
const result = teamService.addMemberToTeamWithEmail(
@@ -191,7 +194,7 @@ describe('addMemberToTeamWithEmail', () => {
});
test("rejects with user with email doesn't exist with USER_NOT_FOUND", () => {
- mockUserService.getUserWithEmail.mockResolvedValue(null);
+ mockUserService.findUserByEmail.mockResolvedValue(O.none);
const result = teamService.addMemberToTeamWithEmail(
dbTeamMember.teamID,
@@ -202,9 +205,11 @@ describe('addMemberToTeamWithEmail', () => {
});
test('makes update in the database', async () => {
- mockUserService.getUserWithEmail.mockResolvedValueOnce({
- uid: dbTeamMember.userUid,
- });
+ mockUserService.findUserByEmail.mockResolvedValueOnce(
+ O.some({
+ uid: dbTeamMember.userUid,
+ }),
+ );
mockPrisma.teamMember.create.mockResolvedValue(dbTeamMember);
await teamService.addMemberToTeamWithEmail(
@@ -227,9 +232,11 @@ describe('addMemberToTeamWithEmail', () => {
});
test('fires "team/<team_id>/member_added" pubsub message with correct payload', async () => {
- mockUserService.getUserWithEmail.mockResolvedValueOnce({
- uid: dbTeamMember.userUid,
- });
+ mockUserService.findUserByEmail.mockResolvedValueOnce(
+ O.some({
+ uid: dbTeamMember.userUid,
+ }),
+ );
mockPrisma.teamMember.create.mockResolvedValue(dbTeamMember);
await teamService.addMemberToTeamWithEmail(
@@ -947,7 +954,7 @@ describe('fetchAllTeams', () => {
const result = await teamService.fetchAllTeams('teamID', 20);
expect(result).toEqual(teams);
});
- test('should resolve left and return an error when users not found', async () => {
+ test('should resolve left and return an empty array when users not found', async () => {
mockPrisma.team.findMany.mockResolvedValueOnce([]);
const result = await teamService.fetchAllTeams(null, 20);
diff --git a/packages/hoppscotch-backend/src/user-collection/user-collection.service.spec.ts b/packages/hoppscotch-backend/src/user-collection/user-collection.service.spec.ts
index 97eb03489..2b45b3602 100644
--- a/packages/hoppscotch-backend/src/user-collection/user-collection.service.spec.ts
+++ b/packages/hoppscotch-backend/src/user-collection/user-collection.service.spec.ts
@@ -506,9 +506,10 @@ describe('createUserCollection', () => {
});
test('should throw USER_NOT_OWNER when user is not the owner of the collection', async () => {
// isOwnerCheck
- mockPrisma.userCollection.findFirstOrThrow.mockRejectedValueOnce(
- 'NotFoundError',
- );
+ mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce({
+ ...rootRESTUserCollection,
+ userUid: 'othser-user-uid',
+ });
const result = await userCollectionService.createUserCollection(
user,
@@ -520,7 +521,7 @@ describe('createUserCollection', () => {
});
test('should successfully create a new root REST user-collection with valid inputs', async () => {
// isOwnerCheck
- mockPrisma.userCollection.findFirstOrThrow.mockResolvedValueOnce(
+ mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce(
rootRESTUserCollection,
);
@@ -540,7 +541,7 @@ describe('createUserCollection', () => {
});
test('should successfully create a new root GQL user-collection with valid inputs', async () => {
// isOwnerCheck
- mockPrisma.userCollection.findFirstOrThrow.mockResolvedValueOnce(
+ mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce(
rootGQLUserCollection,
);
@@ -560,7 +561,7 @@ describe('createUserCollection', () => {
});
test('should successfully create a new child REST user-collection with valid inputs', async () => {
// isOwnerCheck
- mockPrisma.userCollection.findFirstOrThrow.mockResolvedValueOnce(
+ mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce(
childRESTUserCollection,
);
@@ -580,7 +581,7 @@ describe('createUserCollection', () => {
});
test('should successfully create a new child GQL user-collection with valid inputs', async () => {
// isOwnerCheck
- mockPrisma.userCollection.findFirstOrThrow.mockResolvedValueOnce(
+ mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce(
childGQLUserCollection,
);
@@ -594,13 +595,13 @@ describe('createUserCollection', () => {
user,
childGQLUserCollection.title,
childGQLUserCollection.id,
- ReqType.REST,
+ ReqType.GQL,
);
expect(result).toEqualRight(childGQLUserCollection);
});
test('should send pubsub message to "user_coll/<userID>/created" if child REST user-collection is created successfully', async () => {
// isOwnerCheck
- mockPrisma.userCollection.findFirstOrThrow.mockResolvedValueOnce(
+ mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce(
childRESTUserCollection,
);
@@ -623,7 +624,7 @@ describe('createUserCollection', () => {
});
test('should send pubsub message to "user_coll/<userID>/created" if child GQL user-collection is created successfully', async () => {
// isOwnerCheck
- mockPrisma.userCollection.findFirstOrThrow.mockResolvedValueOnce(
+ mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce(
childGQLUserCollection,
);
@@ -637,7 +638,7 @@ describe('createUserCollection', () => {
user,
childGQLUserCollection.title,
childGQLUserCollection.id,
- ReqType.REST,
+ ReqType.GQL,
);
expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_coll/${user.uid}/created`,
@@ -646,7 +647,7 @@ describe('createUserCollection', () => {
});
test('should send pubsub message to "user_coll/<userID>/created" if REST root user-collection is created successfully', async () => {
// isOwnerCheck
- mockPrisma.userCollection.findFirstOrThrow.mockResolvedValueOnce(
+ mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce(
rootRESTUserCollection,
);
@@ -669,7 +670,7 @@ describe('createUserCollection', () => {
});
test('should send pubsub message to "user_coll/<userID>/created" if GQL root user-collection is created successfully', async () => {
// isOwnerCheck
- mockPrisma.userCollection.findFirstOrThrow.mockResolvedValueOnce(
+ mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce(
rootGQLUserCollection,
);
@@ -683,7 +684,7 @@ describe('createUserCollection', () => {
user,
rootGQLUserCollection.title,
rootGQLUserCollection.id,
- ReqType.REST,
+ ReqType.GQL,
);
expect(mockPubSub.publish).toHaveBeenCalledWith(
`user_coll/${user.uid}/created`,
diff --git a/packages/hoppscotch-backend/src/user/user.service.spec.ts b/packages/hoppscotch-backend/src/user/user.service.spec.ts
index 2545c4611..01a764dd9 100644
--- a/packages/hoppscotch-backend/src/user/user.service.spec.ts
+++ b/packages/hoppscotch-backend/src/user/user.service.spec.ts
@@ -427,7 +427,7 @@ describe('UserService', () => {
const result = await userService.fetchAllUsers('123344', 20);
expect(result).toEqual(users);
});
- test('should resolve left and return an error when users not found', async () => {
+ test('should resolve left and return an empty array when users not found', async () => {
mockPrisma.user.findMany.mockResolvedValueOnce([]);
const result = await userService.fetchAllUsers(null, 20);