Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
chore: added service files for user history and unit tests
ankitsridhar16f7dadda52aba
diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.tsnew file mode 100644index 000000000..e387330c7--- /dev/null+++ b/packages/hoppscotch-backend/src/user-history/user-history.service.spec.ts@@ -0,0 +1,442 @@+import { UserHistoryService } from './user-history.service';+import { PrismaService } from '../prisma/prisma.service';+import { PubSubService } from '../pubsub/pubsub.service';+import { mockDeep, mockReset } from 'jest-mock-extended';+import { ReqType, UserHistory } from './user-history.model';+import {+ USER_HISTORY_INVALID_REQ_TYPE,+ USER_HISTORY_NOT_FOUND,+} from '../errors';++const mockPrisma = mockDeep<PrismaService>();+const mockPubSub = mockDeep<PubSubService>();++// eslint-disable-next-line @typescript-eslint/ban-ts-comment+// @ts-ignore+const userHistoryService = new UserHistoryService(+ mockPrisma,+ mockPubSub as any,+);++enum SubscriptionType {+ Created = 'created',+ Updated = 'updated',+ Deleted = 'deleted',+}++beforeEach(() => {+ mockReset(mockPrisma);+ mockPubSub.publish.mockClear();+});++describe('UserHistoryService', () => {+ describe('fetchUserHistory', () => {+ test('Should return a list of users REST history if exists', async () => {+ const executedOn = new Date();+ mockPrisma.userHistory.findMany.mockResolvedValueOnce([+ {+ userUid: 'abc',+ id: '1',+ request: [{}],+ responseMetadata: [{}],+ type: ReqType.REST,+ executedOn: executedOn,+ isStarred: false,+ },+ {+ userUid: 'abc',+ id: '2',+ request: [{}],+ responseMetadata: [{}],+ type: ReqType.REST,+ executedOn: executedOn,+ isStarred: true,+ },+ ]);++ const userHistory: UserHistory[] = [+ {+ userUid: 'abc',+ id: '1',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.REST,+ executedOn: executedOn,+ isStarred: false,+ },+ {+ userUid: 'abc',+ id: '2',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.REST,+ executedOn: executedOn,+ isStarred: true,+ },+ ];+ return expect(+ await userHistoryService.fetchUserHistory('abc', ReqType.REST),+ ).toEqual(userHistory);+ });+ test('Should return a list of users GQL history if exists', async () => {+ const executedOn = new Date();+ mockPrisma.userHistory.findMany.mockResolvedValueOnce([+ {+ userUid: 'abc',+ id: '1',+ request: [{}],+ responseMetadata: [{}],+ type: ReqType.GQL,+ executedOn: executedOn,+ isStarred: false,+ },+ {+ userUid: 'abc',+ id: '2',+ request: [{}],+ responseMetadata: [{}],+ type: ReqType.GQL,+ executedOn: executedOn,+ isStarred: true,+ },+ ]);++ const userHistory: UserHistory[] = [+ {+ userUid: 'abc',+ id: '1',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.GQL,+ executedOn: executedOn,+ isStarred: false,+ },+ {+ userUid: 'abc',+ id: '2',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.GQL,+ executedOn: executedOn,+ isStarred: true,+ },+ ];+ return expect(+ await userHistoryService.fetchUserHistory('abc', ReqType.GQL),+ ).toEqual(userHistory);+ });+ test('Should return an empty list of users REST history if doesnt exists', async () => {+ mockPrisma.userHistory.findMany.mockResolvedValueOnce([]);++ const userHistory: UserHistory[] = [];+ return expect(+ await userHistoryService.fetchUserHistory('abc', ReqType.REST),+ ).toEqual(userHistory);+ });+ test('Should return an empty list of users GQL history if doesnt exists', async () => {+ mockPrisma.userHistory.findMany.mockResolvedValueOnce([]);++ const userHistory: UserHistory[] = [];+ return expect(+ await userHistoryService.fetchUserHistory('abc', ReqType.GQL),+ ).toEqual(userHistory);+ });+ });+ describe('addRequestToHistory', () => {+ test('Should resolve right and add a REST request to users history, publish a subscription and return a `UserHistory` object', async () => {+ userHistoryService.validateReqType('REST');+ mockPrisma.userHistory.create.mockResolvedValueOnce({+ userUid: 'abc',+ id: '1',+ request: [{}],+ responseMetadata: [{}],+ type: ReqType.REST,+ executedOn: new Date(),+ isStarred: false,+ });++ const userHistory: UserHistory = <UserHistory>{+ userUid: 'abc',+ id: '1',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.REST,+ executedOn: new Date(),+ isStarred: false,+ };++ await userHistoryService.publishUserHistorySubscription(+ userHistory,+ SubscriptionType.Created,+ );++ return expect(+ await userHistoryService.addRequestToHistory(+ 'abc',+ JSON.stringify([{}]),+ JSON.stringify([{}]),+ 'REST',+ ),+ ).toEqualRight(userHistory);+ });+ test('Should resolve right and add a GQL request to users history, publish a subscription and return a `UserHistory` object', async () => {+ userHistoryService.validateReqType('GQL');+ mockPrisma.userHistory.create.mockResolvedValueOnce({+ userUid: 'abc',+ id: '1',+ request: [{}],+ responseMetadata: [{}],+ type: ReqType.GQL,+ executedOn: new Date(),+ isStarred: false,+ });++ const userHistory: UserHistory = <UserHistory>{+ userUid: 'abc',+ id: '1',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.GQL,+ executedOn: new Date(),+ isStarred: false,+ };++ await userHistoryService.publishUserHistorySubscription(+ userHistory,+ SubscriptionType.Created,+ );++ return expect(+ await userHistoryService.addRequestToHistory(+ 'abc',+ JSON.stringify([{}]),+ JSON.stringify([{}]),+ 'GQL',+ ),+ ).toEqualRight(userHistory);+ });+ test('Should resolve left when invalid ReqType is passed', async () => {+ userHistoryService.validateReqType('INVALID');+ return expect(+ await userHistoryService.addRequestToHistory(+ 'abc',+ JSON.stringify([{}]),+ JSON.stringify([{}]),+ 'INVALID',+ ),+ ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE);+ });+ });+ describe('starUnstarRequestInHistory', () => {+ test('Should resolve right and star/unstar a request in the history', async () => {+ mockPrisma.userHistory.findFirst.mockResolvedValueOnce({+ userUid: 'abc',+ id: '1',+ request: [{}],+ responseMetadata: [{}],+ type: ReqType.REST,+ executedOn: new Date(),+ isStarred: false,+ });++ mockPrisma.userHistory.update.mockResolvedValueOnce({+ userUid: 'abc',+ id: '1',+ request: [{}],+ responseMetadata: [{}],+ type: ReqType.REST,+ executedOn: new Date(),+ isStarred: true,+ });++ const userHistory: UserHistory = <UserHistory>{+ userUid: 'abc',+ id: '1',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.REST,+ executedOn: new Date(),+ isStarred: true,+ };++ await userHistoryService.publishUserHistorySubscription(+ userHistory,+ SubscriptionType.Updated,+ );++ return expect(+ await userHistoryService.starUnstarRequestInHistory('abc', '1'),+ ).toEqualRight(userHistory);+ });+ test('Should resolve left and error out due to invalid request ID', async () => {+ mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null);++ return expect(+ await userHistoryService.starUnstarRequestInHistory('abc', '1'),+ ).toEqualLeft(USER_HISTORY_NOT_FOUND);+ });+ test('Should resolve left and error out due to invalid request ID', async () => {+ mockPrisma.userHistory.findFirst.mockResolvedValueOnce(null);++ return expect(+ await userHistoryService.starUnstarRequestInHistory('abc', '1'),+ ).toEqualLeft(USER_HISTORY_NOT_FOUND);+ });+ });+ describe('removeRequestFromHistory', () => {+ test('Should resolve right and delete request from users history, publish a subscription', async () => {+ mockPrisma.userHistory.delete.mockResolvedValueOnce({+ userUid: 'abc',+ id: '1',+ request: [{}],+ responseMetadata: [{}],+ type: ReqType.REST,+ executedOn: new Date(),+ isStarred: false,+ });++ const userHistory: UserHistory = <UserHistory>{+ userUid: 'abc',+ id: '1',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.REST,+ executedOn: new Date(),+ isStarred: false,+ };++ await userHistoryService.publishUserHistorySubscription(+ userHistory,+ SubscriptionType.Deleted,+ );++ return expect(+ await userHistoryService.removeRequestFromHistory('abc', '1'),+ ).toEqualRight(userHistory);+ });+ test('Should resolve left and error out when req id is invalid ', async () => {+ mockPrisma.userHistory.delete.mockResolvedValueOnce(null);++ return expect(+ await userHistoryService.removeRequestFromHistory('abc', '1'),+ ).toEqualLeft(USER_HISTORY_NOT_FOUND);+ });+ });+ describe('deleteAllUserHistory', () => {+ test('Should resolve right and delete all user REST history for a request type', async () => {+ userHistoryService.validateReqType('REST');+ mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({+ count: 2,+ });++ return expect(+ await userHistoryService.deleteAllUserHistory('abc', 'REST'),+ ).toEqualRight(2);+ });+ test('Should resolve right and delete all user GQL history for a request type', async () => {+ userHistoryService.validateReqType('GQL');+ mockPrisma.userHistory.deleteMany.mockResolvedValueOnce({+ count: 2,+ });++ return expect(+ await userHistoryService.deleteAllUserHistory('abc', 'GQL'),+ ).toEqualRight(2);+ });+ test('Should resolve left and error when ReqType is invalid', async () => {+ userHistoryService.validateReqType('INVALID');++ return expect(+ await userHistoryService.deleteAllUserHistory('abc', 'INVALID'),+ ).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE);+ });+ });+ describe('validateReqType', () => {+ test('Should resolve right when a valid REST ReqType is provided', async () => {+ return expect(userHistoryService.validateReqType('REST')).toEqualRight(+ ReqType.REST,+ );+ });+ test('Should resolve right when a valid GQL ReqType is provided', async () => {+ return expect(userHistoryService.validateReqType('GQL')).toEqualRight(+ ReqType.GQL,+ );+ });+ test('Should resolve left and error out when a invalid ReqType is provided', async () => {+ return expect(userHistoryService.validateReqType('INVALID')).toEqualLeft(+ USER_HISTORY_INVALID_REQ_TYPE,+ );+ });+ });+ describe('publishUserHistorySubscription', () => {+ test('Should publish a created subscription', async () => {+ const result: UserHistory = {+ userUid: 'abc',+ id: '1',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.REST,+ executedOn: new Date(),+ isStarred: false,+ };++ await mockPubSub.publish(+ `user_history/${result.userUid}/created`,+ result,+ );++ return expect(+ await userHistoryService.publishUserHistorySubscription(+ result,+ SubscriptionType.Created,+ ),+ ).toBeUndefined();+ });+ test('Should publish a updated subscription', async () => {+ const result: UserHistory = {+ userUid: 'abc',+ id: '1',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.REST,+ executedOn: new Date(),+ isStarred: false,+ };++ await mockPubSub.publish(+ `user_history/${result.userUid}/updated`,+ result,+ );++ return expect(+ await userHistoryService.publishUserHistorySubscription(+ result,+ SubscriptionType.Updated,+ ),+ ).toBeUndefined();+ });+ test('Should publish a deleted subscription', async () => {+ const result: UserHistory = {+ userUid: 'abc',+ id: '1',+ request: JSON.stringify([{}]),+ responseMetadata: JSON.stringify([{}]),+ reqType: ReqType.REST,+ executedOn: new Date(),+ isStarred: false,+ };++ await mockPubSub.publish(+ `user_history/${result.userUid}/deleted`,+ result,+ );++ return expect(+ await userHistoryService.publishUserHistorySubscription(+ result,+ SubscriptionType.Deleted,+ ),+ ).toBeUndefined();+ });+ });+});diff --git a/packages/hoppscotch-backend/src/user-history/user-history.service.ts b/packages/hoppscotch-backend/src/user-history/user-history.service.tsindex 8fcdfbc11..6ff279279 100644--- a/packages/hoppscotch-backend/src/user-history/user-history.service.ts+++ b/packages/hoppscotch-backend/src/user-history/user-history.service.ts@@ -3,6 +3,10 @@ import { PrismaService } from '../prisma/prisma.service';import { PubSubService } from '../pubsub/pubsub.service';import { ReqType, UserHistory } from './user-history.model';import * as E from 'fp-ts/Either';+import {+ USER_HISTORY_INVALID_REQ_TYPE,+ USER_HISTORY_NOT_FOUND,+} from '../errors';// Contains constants for the subscription types we send to pubsub serviceenum SubscriptionType {@@ -19,7 +23,7 @@ export class UserHistoryService {) {}/**- * Fetch users REST or GraphQL history based on ReqType argument.+ * Fetch users REST or GraphQL history based on ReqType param.* @param uid Users uid* @param reqType request Type to fetch i.e. GraphQL or REST* @returns an array of user history@@ -41,6 +45,7 @@ export class UserHistoryService {request: JSON.stringify(history.request),responseMetadata: JSON.stringify(history.responseMetadata),isStarred: history.isStarred,+ executedOn: history.executedOn,});});@@ -53,7 +58,7 @@ export class UserHistoryService {* @param reqData the request data* @param resMetadata the response metadata* @param reqType request Type to fetch i.e. GraphQL or REST- * @returns an array of user history+ * @returns a `UserHistory` object*/async addRequestToHistory(uid: string,@@ -62,12 +67,14 @@ export class UserHistoryService {reqType: string,) {const requestType = this.validateReqType(reqType);+ if (E.isLeft(requestType)) return E.left(requestType.left);+const history = await this.prisma.userHistory.create({data: {userUid: uid,request: JSON.parse(reqData),responseMetadata: JSON.parse(resMetadata),- type: requestType,+ type: requestType.right,isStarred: false,},});@@ -87,7 +94,7 @@ export class UserHistoryService {SubscriptionType.Created,);- return userHistory;+ return E.right(userHistory);}/**@@ -104,7 +111,7 @@ export class UserHistoryService {});if (userHistory == null) {- return E.left('history doesnt exist');+ return E.left(USER_HISTORY_NOT_FOUND);}try {const updatedHistory = await this.prisma.userHistory.update({@@ -130,10 +137,9 @@ export class UserHistoryService {updatedUserHistory,SubscriptionType.Updated,);-return E.right(updatedUserHistory);} catch (e) {- E.left('error updating');+ E.left(USER_HISTORY_NOT_FOUND);}}@@ -167,7 +173,7 @@ export class UserHistoryService {);return E.right(deletedUserHistory);} catch (e) {- return E.left('error deleting history not found');+ return E.left(USER_HISTORY_NOT_FOUND);}}@@ -179,20 +185,22 @@ export class UserHistoryService {*/async deleteAllUserHistory(uid: string, reqType: string) {const requestType = this.validateReqType(reqType);- return await this.prisma.userHistory.deleteMany({+ if (E.isLeft(requestType)) return E.left(requestType.left);++ const deletedCount = await this.prisma.userHistory.deleteMany({where: {userUid: uid,- type: requestType,+ type: requestType.right,},});+ return E.right(deletedCount.count);}// Method that takes a request type argument as string and validates against `ReqType`validateReqType(reqType: string) {- let requestType: ReqType;- return reqType == ReqType.REST- ? (requestType = ReqType.REST)- : (requestType = ReqType.GQL);+ if (reqType == ReqType.REST) return E.right(ReqType.REST);+ else if (reqType == ReqType.GQL) return E.right(ReqType.GQL);+ return E.left(USER_HISTORY_INVALID_REQ_TYPE);}// Method to publish subscriptions based on the subscription type of the history@@ -203,19 +211,19 @@ export class UserHistoryService {switch (subscriptionType) {case SubscriptionType.Created:await this.pubsub.publish(- `user_history/${userHistory.id}/created`,+ `user_history/${userHistory.userUid}/created`,userHistory,);break;case SubscriptionType.Updated:await this.pubsub.publish(- `user_history/${userHistory.id}/updated`,+ `user_history/${userHistory.userUid}/updated`,userHistory,);break;case SubscriptionType.Deleted:await this.pubsub.publish(- `user_history/${userHistory.id}/deleted`,+ `user_history/${userHistory.userUid}/deleted`,userHistory,);break;