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

This commit is contained in:
ankitsridhar16
2023-01-19 12:56:41 +05:30
parent 4aad8d36a9
commit d10ed664bf

View File

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