* feat: introducing admin module, resolvers and service files as a module * feat: adding admin module in the app module * feat: introducing admin guard and decorator for allowing admin operations * feat: invited user model * chore: added user invitation mail description to mailer service * chore: added admin and user related error * feat: added invited users as a new model in prisma * chore: added admin related topics to pubsub * chore: added service method to fetch all users from user table * chore: added user deletion base implementation * Revert "chore: added user deletion base implementation" This reverts commit d1615ad83db2bae946e2d366a903d2f95051dabb. * feat: adding team related operations to admin * chore: adding admin related service methods to teams module service * chore: adding admin related service methods to team coll invitations requests envs * chore: added more module error messages * chore: added admin check service method * chore: added find individual user by UID in admin * HBE-106 feat: introduced code to handle first time admin login setup (#23) * test: wrote test cases for verifyAdmin route service method * chore: added comments to verifyAdmin service method * chore: deleted the prisma migration file * chore: added find admin users * feat: added user deletion into admin module * chore: admin user related errors * chore: fixed registry pattern in the shortcodes and teams to handle user deletion * chore: add subscription topic for user deletion * chore: updated user type in data handler * feat: implement and fix user deletion * feat: added make user admin mutation * chore: added unit tests for admin specific service methods in admin module * chore: added invitation not found error * chore: added admin specific operation test cases in specific modules * chore: added tests related to user deletion and admin related operation in user module * chore: updated to error constant when invitations not found * chore: fix rebase overwritten methods * feat: implement remove user as admin * chore: add new line * feat: introducing basic metrics into the self-hosted admin module (HBE-104) (#43) * feat: introducing admin module, resolvers and service files as a module * feat: adding admin module in the app module * feat: introducing admin guard and decorator for allowing admin operations * feat: invited user model * chore: added user invitation mail description to mailer service * chore: added admin and user related error * feat: added invited users as a new model in prisma * chore: added admin related topics to pubsub * chore: added service method to fetch all users from user table * chore: added user deletion base implementation * Revert "chore: added user deletion base implementation" This reverts commit d1615ad83db2bae946e2d366a903d2f95051dabb. * feat: adding team related operations to admin * chore: adding admin related service methods to teams module service * chore: adding admin related service methods to team coll invitations requests envs * chore: added more module error messages * chore: added admin check service method * chore: added find individual user by UID in admin * HBE-106 feat: introduced code to handle first time admin login setup (#23) * test: wrote test cases for verifyAdmin route service method * chore: added comments to verifyAdmin service method * chore: deleted the prisma migration file * chore: added find admin users * feat: added user deletion into admin module * chore: admin user related errors * chore: fixed registry pattern in the shortcodes and teams to handle user deletion * chore: add subscription topic for user deletion * chore: updated user type in data handler * feat: implement and fix user deletion * feat: added make user admin mutation * chore: added unit tests for admin specific service methods in admin module * chore: added invitation not found error * chore: added admin specific operation test cases in specific modules * chore: added tests related to user deletion and admin related operation in user module * chore: updated to error constant when invitations not found * chore: fix rebase overwritten methods * feat: implement remove user as admin * chore: add new line * chore: created new GQL return type for admin module * chore: created resolver and service method for method to fetch org metrics * chore: removed all entities relevant to seperate query for fetching admin metrics * chore: created all resolvers for metrics * feat: completed adding field resolves to query org metrics * test: wrote tests for all metrics related methods in admin module * test: added test cases for get count functions in multiple modules * chore: removed prisma migration folder * Delete backend-schema.gql * chore: resolved merge conflicts in team test file --------- Co-authored-by: ankitsridhar16 <ankit.sridhar16@gmail.com> * refactor: update mailer service to stop using postmark (#38) * refactor: update mailer service to stop using postmark * chore: remove postmark as a dep and move out postmark code * chore: remove postmark variables from .env.example * chore: add formal errors for mailer initialization errors * chore: add and update jsdoc comments in mailer service methods * chore: added user invitation mail description to mailer service * chore: updated with review changes requested for admin module * feat: adding admin resolver to gql schema * feat: adding input args for admin resolvers * chore: invited user renamed * chore: updated mailer service to be compatible with new mailer * chore: updated team service with review changes * chore: updated team collection service with review changes * chore: updated team environments service with review changes * chore: updated team requests service with review changes * chore: updated user service with review changes * refactor: invited user model * chore: review changes implemented * chore: implemented the review changes for admin, user and teams module * chore: removed error handling and implemented review changes * refactor: naming change for IsAdmin --------- Co-authored-by: Balu Babu <balub997@gmail.com> Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
427 lines
14 KiB
TypeScript
427 lines
14 KiB
TypeScript
import { mockDeep, mockReset } from 'jest-mock-extended';
|
|
import { PrismaService } from 'src/prisma/prisma.service';
|
|
import { TeamEnvironment } from './team-environments.model';
|
|
import { TeamEnvironmentsService } from './team-environments.service';
|
|
import { TEAM_ENVIRONMENT_NOT_FOUND } from 'src/errors';
|
|
|
|
const mockPrisma = mockDeep<PrismaService>();
|
|
|
|
const mockPubSub = {
|
|
publish: jest.fn().mockResolvedValue(null),
|
|
};
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore
|
|
const teamEnvironmentsService = new TeamEnvironmentsService(
|
|
mockPrisma,
|
|
mockPubSub as any,
|
|
);
|
|
|
|
const teamEnvironment = {
|
|
id: '123',
|
|
name: 'test',
|
|
teamID: 'abc123',
|
|
variables: [{}],
|
|
};
|
|
|
|
beforeEach(() => {
|
|
mockReset(mockPrisma);
|
|
mockPubSub.publish.mockClear();
|
|
});
|
|
|
|
describe('TeamEnvironmentsService', () => {
|
|
describe('getTeamEnvironment', () => {
|
|
test('queries the db with the id', async () => {
|
|
mockPrisma.teamEnvironment.findFirst.mockResolvedValue(teamEnvironment);
|
|
|
|
await teamEnvironmentsService.getTeamEnvironment('123')();
|
|
|
|
expect(mockPrisma.teamEnvironment.findFirst).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
where: {
|
|
id: '123',
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
|
|
test('requests prisma to reject the query promise if not found', async () => {
|
|
mockPrisma.teamEnvironment.findFirst.mockResolvedValue(teamEnvironment);
|
|
|
|
await teamEnvironmentsService.getTeamEnvironment('123')();
|
|
|
|
expect(mockPrisma.teamEnvironment.findFirst).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
rejectOnNotFound: true,
|
|
}),
|
|
);
|
|
});
|
|
|
|
test('should return a Some of the correct environment if exists', async () => {
|
|
mockPrisma.teamEnvironment.findFirst.mockResolvedValue(teamEnvironment);
|
|
|
|
const result = await teamEnvironmentsService.getTeamEnvironment('123')();
|
|
|
|
expect(result).toEqualSome(teamEnvironment);
|
|
});
|
|
|
|
test('should return a None if the environment does not exist', async () => {
|
|
mockPrisma.teamEnvironment.findFirst.mockRejectedValue('NotFoundError');
|
|
|
|
const result = await teamEnvironmentsService.getTeamEnvironment('123')();
|
|
|
|
expect(result).toBeNone();
|
|
});
|
|
});
|
|
describe('createTeamEnvironment', () => {
|
|
test('should create and return a new team environment given a valid name,variable and team ID', async () => {
|
|
mockPrisma.teamEnvironment.create.mockResolvedValue(teamEnvironment);
|
|
|
|
const result = await teamEnvironmentsService.createTeamEnvironment(
|
|
teamEnvironment.name,
|
|
teamEnvironment.teamID,
|
|
JSON.stringify(teamEnvironment.variables),
|
|
)();
|
|
|
|
expect(result).toEqual(<TeamEnvironment>{
|
|
id: teamEnvironment.id,
|
|
name: teamEnvironment.name,
|
|
teamID: teamEnvironment.teamID,
|
|
variables: JSON.stringify(teamEnvironment.variables),
|
|
});
|
|
});
|
|
|
|
test('should reject if given team ID is invalid', async () => {
|
|
mockPrisma.teamEnvironment.create.mockRejectedValue(null as any);
|
|
|
|
await expect(
|
|
teamEnvironmentsService.createTeamEnvironment(
|
|
teamEnvironment.name,
|
|
'invalidteamid',
|
|
JSON.stringify(teamEnvironment.variables),
|
|
),
|
|
).rejects.toBeDefined();
|
|
});
|
|
|
|
test('should reject if provided team environment name is not a string', async () => {
|
|
mockPrisma.teamEnvironment.create.mockRejectedValue(null as any);
|
|
|
|
await expect(
|
|
teamEnvironmentsService.createTeamEnvironment(
|
|
null as any,
|
|
teamEnvironment.teamID,
|
|
JSON.stringify(teamEnvironment.variables),
|
|
),
|
|
).rejects.toBeDefined();
|
|
});
|
|
|
|
test('should reject if provided variable is not a string', async () => {
|
|
mockPrisma.teamEnvironment.create.mockRejectedValue(null as any);
|
|
|
|
await expect(
|
|
teamEnvironmentsService.createTeamEnvironment(
|
|
teamEnvironment.name,
|
|
teamEnvironment.teamID,
|
|
null as any,
|
|
),
|
|
).rejects.toBeDefined();
|
|
});
|
|
|
|
test('should send pubsub message to "team_environment/<teamID>/created" if team environment is created successfully', async () => {
|
|
mockPrisma.teamEnvironment.create.mockResolvedValueOnce(teamEnvironment);
|
|
|
|
const result = await teamEnvironmentsService.createTeamEnvironment(
|
|
teamEnvironment.name,
|
|
teamEnvironment.teamID,
|
|
JSON.stringify(teamEnvironment.variables),
|
|
)();
|
|
|
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
|
`team_environment/${teamEnvironment.teamID}/created`,
|
|
result,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('deleteTeamEnvironment', () => {
|
|
test('should resolve to true given a valid team environment ID', async () => {
|
|
mockPrisma.teamEnvironment.delete.mockResolvedValueOnce(teamEnvironment);
|
|
|
|
const result = await teamEnvironmentsService.deleteTeamEnvironment(
|
|
teamEnvironment.id,
|
|
)();
|
|
|
|
expect(result).toEqualRight(true);
|
|
});
|
|
|
|
test('should throw TEAM_ENVIRONMMENT_NOT_FOUND if given id is invalid', async () => {
|
|
mockPrisma.teamEnvironment.delete.mockRejectedValue('RecordNotFound');
|
|
|
|
const result = await teamEnvironmentsService.deleteTeamEnvironment(
|
|
'invalidid',
|
|
)();
|
|
|
|
expect(result).toEqualLeft(TEAM_ENVIRONMENT_NOT_FOUND);
|
|
});
|
|
|
|
test('should send pubsub message to "team_environment/<teamID>/deleted" if team environment is deleted successfully', async () => {
|
|
mockPrisma.teamEnvironment.delete.mockResolvedValueOnce(teamEnvironment);
|
|
|
|
const result = await teamEnvironmentsService.deleteTeamEnvironment(
|
|
teamEnvironment.id,
|
|
)();
|
|
|
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
|
`team_environment/${teamEnvironment.teamID}/deleted`,
|
|
{
|
|
...teamEnvironment,
|
|
variables: JSON.stringify(teamEnvironment.variables),
|
|
},
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('updateVariablesInTeamEnvironment', () => {
|
|
test('should add new variable to a team environment', async () => {
|
|
mockPrisma.teamEnvironment.update.mockResolvedValueOnce({
|
|
...teamEnvironment,
|
|
variables: [{ key: 'value' }],
|
|
});
|
|
|
|
const result = await teamEnvironmentsService.updateTeamEnvironment(
|
|
teamEnvironment.id,
|
|
teamEnvironment.name,
|
|
JSON.stringify([{ key: 'value' }]),
|
|
)();
|
|
|
|
expect(result).toEqualRight(<TeamEnvironment>{
|
|
...teamEnvironment,
|
|
variables: JSON.stringify([{ key: 'value' }]),
|
|
});
|
|
});
|
|
|
|
test('should add new variable to already existing list of variables in a team environment', async () => {
|
|
mockPrisma.teamEnvironment.update.mockResolvedValueOnce({
|
|
...teamEnvironment,
|
|
variables: [{ key: 'value' }, { key_2: 'value_2' }],
|
|
});
|
|
|
|
const result = await teamEnvironmentsService.updateTeamEnvironment(
|
|
teamEnvironment.id,
|
|
teamEnvironment.name,
|
|
JSON.stringify([{ key: 'value' }, { key_2: 'value_2' }]),
|
|
)();
|
|
|
|
expect(result).toEqualRight(<TeamEnvironment>{
|
|
...teamEnvironment,
|
|
variables: JSON.stringify([{ key: 'value' }, { key_2: 'value_2' }]),
|
|
});
|
|
});
|
|
|
|
test('should edit existing variables in a team environment', async () => {
|
|
mockPrisma.teamEnvironment.update.mockResolvedValueOnce({
|
|
...teamEnvironment,
|
|
variables: [{ key: '1234' }],
|
|
});
|
|
|
|
const result = await teamEnvironmentsService.updateTeamEnvironment(
|
|
teamEnvironment.id,
|
|
teamEnvironment.name,
|
|
JSON.stringify([{ key: '1234' }]),
|
|
)();
|
|
|
|
expect(result).toEqualRight(<TeamEnvironment>{
|
|
...teamEnvironment,
|
|
variables: JSON.stringify([{ key: '1234' }]),
|
|
});
|
|
});
|
|
|
|
test('should delete existing variable in a team environment', async () => {
|
|
mockPrisma.teamEnvironment.update.mockResolvedValueOnce(teamEnvironment);
|
|
|
|
const result = await teamEnvironmentsService.updateTeamEnvironment(
|
|
teamEnvironment.id,
|
|
teamEnvironment.name,
|
|
JSON.stringify([{}]),
|
|
)();
|
|
|
|
expect(result).toEqualRight(<TeamEnvironment>{
|
|
...teamEnvironment,
|
|
variables: JSON.stringify([{}]),
|
|
});
|
|
});
|
|
|
|
test('should edit name of an existing team environment', async () => {
|
|
mockPrisma.teamEnvironment.update.mockResolvedValueOnce({
|
|
...teamEnvironment,
|
|
variables: [{ key: '123' }],
|
|
});
|
|
|
|
const result = await teamEnvironmentsService.updateTeamEnvironment(
|
|
teamEnvironment.id,
|
|
teamEnvironment.name,
|
|
JSON.stringify([{ key: '123' }]),
|
|
)();
|
|
|
|
expect(result).toEqualRight(<TeamEnvironment>{
|
|
...teamEnvironment,
|
|
variables: JSON.stringify([{ key: '123' }]),
|
|
});
|
|
});
|
|
|
|
test('should reject to TEAM_ENVIRONMMENT_NOT_FOUND if provided id is invalid', async () => {
|
|
mockPrisma.teamEnvironment.update.mockRejectedValue('RecordNotFound');
|
|
|
|
const result = await teamEnvironmentsService.updateTeamEnvironment(
|
|
'invalidid',
|
|
teamEnvironment.name,
|
|
JSON.stringify(teamEnvironment.variables),
|
|
)();
|
|
|
|
expect(result).toEqualLeft(TEAM_ENVIRONMENT_NOT_FOUND);
|
|
});
|
|
|
|
test('should send pubsub message to "team_environment/<teamID>/updated" if team environment is updated successfully', async () => {
|
|
mockPrisma.teamEnvironment.update.mockResolvedValueOnce(teamEnvironment);
|
|
|
|
const result = await teamEnvironmentsService.updateTeamEnvironment(
|
|
teamEnvironment.id,
|
|
teamEnvironment.name,
|
|
JSON.stringify([{ key: 'value' }]),
|
|
)();
|
|
|
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
|
`team_environment/${teamEnvironment.teamID}/updated`,
|
|
{
|
|
...teamEnvironment,
|
|
variables: JSON.stringify(teamEnvironment.variables),
|
|
},
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('deleteAllVariablesFromTeamEnvironment', () => {
|
|
test('should delete all variables in a team environment', async () => {
|
|
mockPrisma.teamEnvironment.update.mockResolvedValueOnce(teamEnvironment);
|
|
|
|
const result =
|
|
await teamEnvironmentsService.deleteAllVariablesFromTeamEnvironment(
|
|
teamEnvironment.id,
|
|
)();
|
|
|
|
expect(result).toEqualRight(<TeamEnvironment>{
|
|
...teamEnvironment,
|
|
variables: JSON.stringify([{}]),
|
|
});
|
|
});
|
|
|
|
test('should reject to TEAM_ENVIRONMMENT_NOT_FOUND if provided id is invalid', async () => {
|
|
mockPrisma.teamEnvironment.update.mockRejectedValue('RecordNotFound');
|
|
|
|
const result =
|
|
await teamEnvironmentsService.deleteAllVariablesFromTeamEnvironment(
|
|
'invalidid',
|
|
)();
|
|
|
|
expect(result).toEqualLeft(TEAM_ENVIRONMENT_NOT_FOUND);
|
|
});
|
|
|
|
test('should send pubsub message to "team_environment/<teamID>/updated" if team environment is updated successfully', async () => {
|
|
mockPrisma.teamEnvironment.update.mockResolvedValueOnce(teamEnvironment);
|
|
|
|
const result =
|
|
await teamEnvironmentsService.deleteAllVariablesFromTeamEnvironment(
|
|
teamEnvironment.id,
|
|
)();
|
|
|
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
|
`team_environment/${teamEnvironment.teamID}/updated`,
|
|
{
|
|
...teamEnvironment,
|
|
variables: JSON.stringify([{}]),
|
|
},
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('createDuplicateEnvironment', () => {
|
|
test('should duplicate an existing team environment', async () => {
|
|
mockPrisma.teamEnvironment.findFirst.mockResolvedValueOnce(
|
|
teamEnvironment,
|
|
);
|
|
|
|
mockPrisma.teamEnvironment.create.mockResolvedValueOnce({
|
|
...teamEnvironment,
|
|
id: 'newid',
|
|
});
|
|
|
|
const result = await teamEnvironmentsService.createDuplicateEnvironment(
|
|
teamEnvironment.id,
|
|
)();
|
|
|
|
expect(result).toEqualRight(<TeamEnvironment>{
|
|
...teamEnvironment,
|
|
id: 'newid',
|
|
variables: JSON.stringify(teamEnvironment.variables),
|
|
});
|
|
});
|
|
|
|
test('should reject to TEAM_ENVIRONMMENT_NOT_FOUND if provided id is invalid', async () => {
|
|
mockPrisma.teamEnvironment.findFirst.mockRejectedValue('NotFoundError');
|
|
|
|
const result = await teamEnvironmentsService.createDuplicateEnvironment(
|
|
teamEnvironment.id,
|
|
)();
|
|
|
|
expect(result).toEqualLeft(TEAM_ENVIRONMENT_NOT_FOUND);
|
|
});
|
|
|
|
test('should send pubsub message to "team_environment/<teamID>/created" if team environment is updated successfully', async () => {
|
|
mockPrisma.teamEnvironment.findFirst.mockResolvedValueOnce(
|
|
teamEnvironment,
|
|
);
|
|
|
|
mockPrisma.teamEnvironment.create.mockResolvedValueOnce({
|
|
...teamEnvironment,
|
|
id: 'newid',
|
|
});
|
|
|
|
const result = await teamEnvironmentsService.createDuplicateEnvironment(
|
|
teamEnvironment.id,
|
|
)();
|
|
|
|
expect(mockPubSub.publish).toHaveBeenCalledWith(
|
|
`team_environment/${teamEnvironment.teamID}/created`,
|
|
{
|
|
...teamEnvironment,
|
|
id: 'newid',
|
|
variables: JSON.stringify([{}]),
|
|
},
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('totalEnvsInTeam', () => {
|
|
test('should resolve right and return a total team envs count ', async () => {
|
|
mockPrisma.teamEnvironment.count.mockResolvedValueOnce(2);
|
|
const result = await teamEnvironmentsService.totalEnvsInTeam('id1');
|
|
expect(mockPrisma.teamEnvironment.count).toHaveBeenCalledWith({
|
|
where: {
|
|
teamID: 'id1',
|
|
},
|
|
});
|
|
expect(result).toEqual(2);
|
|
});
|
|
test('should resolve left and return an error when no team envs found', async () => {
|
|
mockPrisma.teamEnvironment.count.mockResolvedValueOnce(0);
|
|
const result = await teamEnvironmentsService.totalEnvsInTeam('id1');
|
|
expect(mockPrisma.teamEnvironment.count).toHaveBeenCalledWith({
|
|
where: {
|
|
teamID: 'id1',
|
|
},
|
|
});
|
|
expect(result).toEqual(0);
|
|
});
|
|
});
|
|
});
|