Skip to content

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

chore: updated test cases with requested changes to handle publishing seperately

ankitsridhar16d10ed664bfb7
diff --git a/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts
index ca5078005..f41287f1d 100644
--- a/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts
+++ b/packages/hoppscotch-backend/src/user-environment/user-environments.service.spec.ts
@@ -3,26 +3,31 @@ import { mockDeep, mockReset } from 'jest-mock-extended';
import { PrismaService } from '../prisma/prisma.service';
import { UserEnvironmentsService } from './user-environments.service';
import {
- USER_ENVIRONMENT_ENV_DOESNT_EXISTS,
+ USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS,
USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED,
USER_ENVIRONMENT_GLOBAL_ENV_EXISTS,
+ USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME,
} from '../errors';
import { PubSubService } from '../pubsub/pubsub.service';
+import { SubscriptionHandler } from '../subscription-handler';
const mockPrisma = mockDeep<PrismaService>();
const mockPubSub = mockDeep<PubSubService>();
+const mockSubscriptionHandler = mockDeep<SubscriptionHandler>();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const userEnvironmentsService = new UserEnvironmentsService(
mockPrisma,
mockPubSub as any,
+ mockSubscriptionHandler,
);
enum SubscriptionType {
Created = 'created',
Updated = 'updated',
Deleted = 'deleted',
+ DeleteMany = 'delete_many',
}
const userPersonalEnvironments = [
@@ -131,228 +136,303 @@ describe('UserEnvironmentsService', () => {
expect(
await userEnvironmentsService.fetchUserGlobalEnvironment('abc'),
- ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS);
+ ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
});
});
describe('createUserEnvironment', () => {
- test(
- 'Should resolve right and create a users personal environment and return a `UserEnvironment` object ' +
- 'and publish a subscription',
- async () => {
- mockPrisma.userEnvironment.create.mockResolvedValueOnce({
- userUid: 'abc123',
- id: '123',
- name: 'test',
- variables: [{}],
- isGlobal: false,
- });
+ test('Should resolve right and create a users personal environment and return a `UserEnvironment` object ', async () => {
+ mockPrisma.userEnvironment.create.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: '123',
+ name: 'test',
+ variables: [{}],
+ isGlobal: false,
+ });
- const result: UserEnvironment = {
- userUid: 'abc123',
- id: '123',
- name: 'test',
- variables: JSON.stringify([{}]),
- isGlobal: false,
- };
-
- await userEnvironmentsService.publishUserEnvironmentSubscription(
- result,
- SubscriptionType.Created,
- );
-
- return expect(
- await userEnvironmentsService.createUserEnvironment(
- 'abc123',
- 'test',
- '[{}]',
- false,
- ),
- ).toEqualRight(result);
- },
- );
-
- test(
- 'Should resolve right and create a new users global environment and return a `UserEnvironment` object ' +
- 'and publish a subscription',
- async () => {
- mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null);
-
- mockPrisma.userEnvironment.create.mockResolvedValueOnce({
- userUid: 'abc123',
- id: '123',
- name: 'testgenv',
- variables: [{}],
- isGlobal: true,
- });
+ const result: UserEnvironment = {
+ userUid: 'abc123',
+ id: '123',
+ name: 'test',
+ variables: JSON.stringify([{}]),
+ isGlobal: false,
+ };
- const result: UserEnvironment = {
- userUid: 'abc123',
- id: '123',
- name: 'testgenv',
- variables: JSON.stringify([{}]),
- isGlobal: true,
- };
-
- await userEnvironmentsService.publishUserEnvironmentSubscription(
- result,
- SubscriptionType.Created,
- );
- return expect(
- await userEnvironmentsService.createUserEnvironment(
- 'abc123',
- 'test',
- '[{}]',
- true,
- ),
- ).toEqualRight(result);
- },
- );
-
- test(
- 'Should resolve left and not create a new users global environment if existing global env exists ' +
- 'and not publish a subscription',
- async () => {
- mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
- userUid: 'abc123',
- id: '123',
- name: 'testgenv',
- variables: [{}],
- isGlobal: true,
- });
-
- return expect(
- await userEnvironmentsService.createUserEnvironment(
- 'abc123',
- 'test',
- '[{}]',
- true,
- ),
- ).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS);
- },
- );
+ return expect(
+ await userEnvironmentsService.createUserEnvironment(
+ 'abc123',
+ 'test',
+ '[{}]',
+ false,
+ ),
+ ).toEqualRight(result);
+ });
+
+ test('Should resolve right and create a new users global environment and return a `UserEnvironment` object ', async () => {
+ mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null);
+
+ mockPrisma.userEnvironment.create.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: '123',
+ name: null,
+ variables: [{}],
+ isGlobal: true,
+ });
+
+ const result: UserEnvironment = {
+ userUid: 'abc123',
+ id: '123',
+ name: null,
+ variables: JSON.stringify([{}]),
+ isGlobal: true,
+ };
+
+ return expect(
+ await userEnvironmentsService.createUserEnvironment(
+ 'abc123',
+ null,
+ '[{}]',
+ true,
+ ),
+ ).toEqualRight(result);
+ });
+
+ test('Should resolve left and not create a new users global environment if existing global env exists ', async () => {
+ mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: '123',
+ name: null,
+ variables: [{}],
+ isGlobal: true,
+ });
+
+ return expect(
+ await userEnvironmentsService.createUserEnvironment(
+ 'abc123',
+ null,
+ '[{}]',
+ true,
+ ),
+ ).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_EXISTS);
+ });
+
+ test('Should resolve left when an invalid personal environment name has been passed', async () => {
+ return expect(
+ await userEnvironmentsService.createUserEnvironment(
+ 'abc123',
+ null,
+ '[{}]',
+ false,
+ ),
+ ).toEqualLeft(USER_ENVIRONMENT_INVALID_ENVIRONMENT_NAME);
+ });
+
+ test('Should create a users personal environment and publish a created subscription', async () => {
+ mockPrisma.userEnvironment.create.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: '123',
+ name: 'test',
+ variables: [{}],
+ isGlobal: false,
+ });
+
+ const result: UserEnvironment = {
+ userUid: 'abc123',
+ id: '123',
+ name: 'test',
+ variables: JSON.stringify([{}]),
+ isGlobal: false,
+ };
+
+ await userEnvironmentsService.createUserEnvironment(
+ 'abc123',
+ 'test',
+ '[{}]',
+ false,
+ );
+
+ return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith(
+ `user_environment/${result.userUid}`,
+ SubscriptionType.Created,
+ result,
+ );
+ });
+
+ test('Should create a users global environment and publish a created subscription', async () => {
+ mockPrisma.userEnvironment.create.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: '123',
+ name: '',
+ variables: [{}],
+ isGlobal: true,
+ });
+
+ const result: UserEnvironment = {
+ userUid: 'abc123',
+ id: '123',
+ name: '',
+ variables: JSON.stringify([{}]),
+ isGlobal: true,
+ };
+
+ await userEnvironmentsService.createUserEnvironment(
+ 'abc123',
+ '',
+ '[{}]',
+ true,
+ );
+
+ return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith(
+ `user_environment/${result.userUid}`,
+ SubscriptionType.Created,
+ result,
+ );
+ });
});
describe('UpdateUserEnvironment', () => {
- test(
- 'should resolve right and update a users personal or environment and return a `UserEnvironment` object ' +
- 'and publish a subscription',
- async () => {
- mockPrisma.userEnvironment.update.mockResolvedValueOnce({
- userUid: 'abc123',
- id: '123',
- name: 'test',
- variables: [{}],
- isGlobal: false,
- });
+ test('Should resolve right and update a users personal or environment and return a `UserEnvironment` object ', async () => {
+ mockPrisma.userEnvironment.update.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: '123',
+ name: 'test',
+ variables: [{}],
+ isGlobal: false,
+ });
- const result: UserEnvironment = {
- userUid: 'abc123',
- id: '123',
- name: 'test',
- variables: JSON.stringify([{}]),
- isGlobal: false,
- };
-
- await userEnvironmentsService.publishUserEnvironmentSubscription(
- result,
- SubscriptionType.Updated,
- );
- return expect(
- await userEnvironmentsService.updateUserEnvironment(
- 'abc123',
- 'test',
- '[{}]',
- ),
- ).toEqualRight(result);
- },
- );
-
- test(
- 'should resolve right and update a users global environment and return a `UserEnvironment` object ' +
- 'and publish a subscription',
- async () => {
- mockPrisma.userEnvironment.update.mockResolvedValueOnce({
- userUid: 'abc123',
- id: '123',
- name: '',
- variables: [{}],
- isGlobal: true,
- });
+ const result: UserEnvironment = {
+ userUid: 'abc123',
+ id: '123',
+ name: 'test',
+ variables: JSON.stringify([{}]),
+ isGlobal: false,
+ };
- const result: UserEnvironment = {
- userUid: 'abc123',
- id: '123',
- name: '',
- variables: JSON.stringify([{}]),
- isGlobal: true,
- };
-
- await userEnvironmentsService.publishUserEnvironmentSubscription(
- result,
- SubscriptionType.Updated,
- );
- return expect(
- await userEnvironmentsService.updateUserEnvironment(
- 'abc123',
- '',
- '[{}]',
- ),
- ).toEqualRight(result);
- },
- );
-
- test(
- 'should resolve left and not update a users environment if env doesnt exist ' +
- 'and publish a subscription',
- async () => {
- mockPrisma.userEnvironment.update.mockRejectedValueOnce({});
-
- return expect(
- await userEnvironmentsService.updateUserEnvironment(
- 'abc123',
- 'test',
- '[{}]',
- ),
- ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS);
- },
- );
- });
+ return expect(
+ await userEnvironmentsService.updateUserEnvironment(
+ 'abc123',
+ 'test',
+ '[{}]',
+ ),
+ ).toEqualRight(result);
+ });
- describe('deleteUserEnvironment', () => {
- test(
- 'Should resolve right and delete a users personal environment and return a `UserEnvironment` object ' +
- 'and publish a subscription',
- async () => {
- mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null);
- mockPrisma.userEnvironment.delete.mockResolvedValueOnce({
- userUid: 'abc123',
- id: 'env1',
- name: 'en1',
- variables: [{}],
- isGlobal: false,
- });
+ test('Should resolve right and update a users global environment and return a `UserEnvironment` object ', async () => {
+ mockPrisma.userEnvironment.update.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: '123',
+ name: null,
+ variables: [{}],
+ isGlobal: true,
+ });
- const result: UserEnvironment = {
- userUid: 'abc123',
- id: 'env1',
- name: 'en1',
- variables: JSON.stringify([{}]),
- isGlobal: false,
- };
+ const result: UserEnvironment = {
+ userUid: 'abc123',
+ id: '123',
+ name: null,
+ variables: JSON.stringify([{}]),
+ isGlobal: true,
+ };
+
+ return expect(
+ await userEnvironmentsService.updateUserEnvironment(
+ 'abc123',
+ null,
+ '[{}]',
+ ),
+ ).toEqualRight(result);
+ });
+
+ test('Should resolve left and not update a users environment if env doesnt exist ', async () => {
+ mockPrisma.userEnvironment.update.mockRejectedValueOnce({});
+
+ return expect(
+ await userEnvironmentsService.updateUserEnvironment(
+ 'abc123',
+ 'test',
+ '[{}]',
+ ),
+ ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
+ });
+
+ test('Should resolve right, update a users personal environment and publish an updated subscription ', async () => {
+ mockPrisma.userEnvironment.update.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: '123',
+ name: 'test',
+ variables: [{}],
+ isGlobal: false,
+ });
+
+ const result: UserEnvironment = {
+ userUid: 'abc123',
+ id: '123',
+ name: 'test',
+ variables: JSON.stringify([{}]),
+ isGlobal: false,
+ };
+
+ await userEnvironmentsService.updateUserEnvironment(
+ 'abc123',
+ 'test',
+ '[{}]',
+ );
+
+ return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith(
+ `user_environment/${result.id}`,
+ SubscriptionType.Updated,
+ result,
+ );
+ });
+
+ test('Should resolve right, update a users global environment and publish an updated subscription ', async () => {
+ mockPrisma.userEnvironment.update.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: '123',
+ name: null,
+ variables: [{}],
+ isGlobal: true,
+ });
+
+ const result: UserEnvironment = {
+ userUid: 'abc123',
+ id: '123',
+ name: null,
+ variables: JSON.stringify([{}]),
+ isGlobal: true,
+ };
- await userEnvironmentsService.publishUserEnvironmentSubscription(
- result,
- SubscriptionType.Deleted,
- );
+ await userEnvironmentsService.updateUserEnvironment(
+ 'abc123',
+ null,
+ '[{}]',
+ );
- return expect(
- await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'),
- ).toEqualRight(result);
- },
- );
+ return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith(
+ `user_environment/${result.id}`,
+ SubscriptionType.Updated,
+ result,
+ );
+ });
+ });
- test('Should resolve left and return an error when deleting a global user environment ', async () => {
+ describe('deleteUserEnvironment', () => {
+ test('Should resolve right and delete a users personal environment and return a `UserEnvironment` object ', async () => {
+ mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null);
+ mockPrisma.userEnvironment.delete.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: 'env1',
+ name: 'en1',
+ variables: [{}],
+ isGlobal: false,
+ });
+
+ return expect(
+ await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'),
+ ).toEqualRight(true);
+ });
+
+ test('Should resolve left and return an error when deleting a global user environment', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
userUid: 'abc123',
id: 'genv1',
@@ -366,150 +446,135 @@ describe('UserEnvironmentsService', () => {
).toEqualLeft(USER_ENVIRONMENT_GLOBAL_ENV_DELETION_FAILED);
});
- test('Should resolve left and return an error when deleting an invalid user environment ', async () => {
+ test('Should resolve left and return an error when deleting an invalid user environment', async () => {
mockPrisma.userEnvironment.delete.mockResolvedValueOnce(null);
return expect(
await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1'),
- ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS);
+ ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
+ });
+
+ test('Should resolve right, delete a users personal environment and publish a deleted subscription', async () => {
+ mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce(null);
+ mockPrisma.userEnvironment.delete.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: 'env1',
+ name: 'en1',
+ variables: [{}],
+ isGlobal: false,
+ });
+
+ const result: UserEnvironment = {
+ userUid: 'abc123',
+ id: 'env1',
+ name: 'en1',
+ variables: JSON.stringify([{}]),
+ isGlobal: false,
+ };
+
+ await userEnvironmentsService.deleteUserEnvironment('abc123', 'env1');
+
+ return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith(
+ `user_environment/${result.id}`,
+ SubscriptionType.Deleted,
+ result,
+ );
});
});
describe('deleteUserEnvironments', () => {
- test('Should return a count of users personal environment deleted', async () => {
+ test('Should publish a subscription with a count of deleted environments', async () => {
mockPrisma.userEnvironment.deleteMany.mockResolvedValueOnce({
count: 1,
});
- return expect(
- await userEnvironmentsService.deleteUserEnvironments('abc123'),
- ).toEqual(1);
+ await userEnvironmentsService.deleteUserEnvironments('abc123');
+
+ return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith(
+ `user_environment/abc123`,
+ SubscriptionType.DeleteMany,
+ 1,
+ );
});
});
- describe('deleteAllVariablesFromUsersGlobalEnvironment', () => {
- test(
- 'Should resolve right and delete all variables inside users global environment and return a `UserEnvironment` object ' +
- 'and publish a subscription',
- async () => {
- mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
- userUid: 'abc123',
- id: 'env1',
- name: 'en1',
- variables: [{}],
- isGlobal: true,
- });
-
- mockPrisma.userEnvironment.update.mockResolvedValueOnce({
- userUid: 'abc123',
- id: 'env1',
- name: 'en1',
- variables: [],
- isGlobal: true,
- });
-
- const result: UserEnvironment = {
- userUid: 'abc123',
- id: 'env1',
- name: 'en1',
- variables: JSON.stringify([]),
- isGlobal: true,
- };
-
- await userEnvironmentsService.publishUserEnvironmentSubscription(
- result,
- SubscriptionType.Updated,
- );
-
- return expect(
- await userEnvironmentsService.deleteAllVariablesFromUsersGlobalEnvironment(
- 'abc123',
- 'env1',
- ),
- ).toEqualRight(result);
- },
- );
-
- test('Should resolve left and return an error if global environment id and passed id dont match ', async () => {
+ describe('clearGlobalEnvironments', () => {
+ test('Should resolve right and delete all variables inside users global environment and return a `UserEnvironment` object', async () => {
mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
userUid: 'abc123',
- id: 'genv2',
+ id: 'env1',
name: 'en1',
variables: [{}],
isGlobal: true,
});
- return expect(
- await userEnvironmentsService.deleteUserEnvironment('abc123', 'genv1'),
- ).toEqualLeft(USER_ENVIRONMENT_ENV_DOESNT_EXISTS);
- });
- });
+ mockPrisma.userEnvironment.update.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: 'env1',
+ name: 'en1',
+ variables: [],
+ isGlobal: true,
+ });
- describe('publishUserEnvironmentSubscription', () => {
- test('Should publish a created subscription', async () => {
const result: UserEnvironment = {
userUid: 'abc123',
- id: '123',
- name: '',
- variables: JSON.stringify([{}]),
+ id: 'env1',
+ name: 'en1',
+ variables: JSON.stringify([]),
isGlobal: true,
};
- await mockPubSub.publish(
- `user_environment/${result.userUid}/created`,
- result,
- );
-
return expect(
- await userEnvironmentsService.publishUserEnvironmentSubscription(
- result,
- SubscriptionType.Created,
- ),
- ).toBeUndefined();
+ await userEnvironmentsService.clearGlobalEnvironments('abc123', 'env1'),
+ ).toEqualRight(result);
});
- test('Should publish a updated subscription', async () => {
- const result: UserEnvironment = {
+ test('Should resolve left and return an error if global environment id and passed id dont match', async () => {
+ mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
userUid: 'abc123',
- id: '123',
- name: '',
- variables: JSON.stringify([{}]),
+ id: 'genv2',
+ name: 'en1',
+ variables: [{}],
isGlobal: true,
- };
-
- await mockPubSub.publish(
- `user_environment/${result.userUid}/updated`,
- result,
- );
+ });
return expect(
- await userEnvironmentsService.publishUserEnvironmentSubscription(
- result,
- SubscriptionType.Updated,
- ),
- ).toBeUndefined();
+ await userEnvironmentsService.deleteUserEnvironment('abc123', 'genv1'),
+ ).toEqualLeft(USER_ENVIRONMENT_ENV_DOES_NOT_EXISTS);
});
- test('Should publish a deleted subscription', async () => {
+ test('Should resolve right,delete all variables inside users global environment and publish an updated subscription', async () => {
+ mockPrisma.userEnvironment.findFirst.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: 'env1',
+ name: 'en1',
+ variables: [{}],
+ isGlobal: true,
+ });
+
+ mockPrisma.userEnvironment.update.mockResolvedValueOnce({
+ userUid: 'abc123',
+ id: 'env1',
+ name: 'en1',
+ variables: [],
+ isGlobal: true,
+ });
+
const result: UserEnvironment = {
userUid: 'abc123',
- id: '123',
- name: '',
- variables: JSON.stringify([{}]),
+ id: 'env1',
+ name: 'en1',
+ variables: JSON.stringify([]),
isGlobal: true,
};
- await mockPubSub.publish(
- `user_environment/${result.userUid}/deleted`,
+ await userEnvironmentsService.clearGlobalEnvironments('abc123', 'env1');
+
+ return expect(mockSubscriptionHandler.publish).toHaveBeenCalledWith(
+ `user_environment/${result.id}`,
+ SubscriptionType.Updated,
result,
);
-
- return expect(
- await userEnvironmentsService.publishUserEnvironmentSubscription(
- result,
- SubscriptionType.Deleted,
- ),
- ).toBeUndefined();
});
});
});