diff --git a/packages/hoppscotch-backend/src/infra-config/infra-config.service.spec.ts b/packages/hoppscotch-backend/src/infra-config/infra-config.service.spec.ts index 3c5507902..707294231 100644 --- a/packages/hoppscotch-backend/src/infra-config/infra-config.service.spec.ts +++ b/packages/hoppscotch-backend/src/infra-config/infra-config.service.spec.ts @@ -2,9 +2,15 @@ import { mockDeep, mockReset } from 'jest-mock-extended'; import { PrismaService } from 'src/prisma/prisma.service'; import { InfraConfigService } from './infra-config.service'; import { InfraConfigEnum } from 'src/types/InfraConfig'; -import { INFRA_CONFIG_NOT_FOUND, INFRA_CONFIG_UPDATE_FAILED } from 'src/errors'; +import { + INFRA_CONFIG_NOT_FOUND, + INFRA_CONFIG_OPERATION_NOT_ALLOWED, + INFRA_CONFIG_UPDATE_FAILED, +} from 'src/errors'; import { ConfigService } from '@nestjs/config'; import * as helper from './helper'; +import { InfraConfig as dbInfraConfig } from '@prisma/client'; +import { InfraConfig } from './infra-config.model'; const mockPrisma = mockDeep(); const mockConfigService = mockDeep(); @@ -16,12 +22,82 @@ const infraConfigService = new InfraConfigService( mockConfigService, ); +const INITIALIZED_DATE_CONST = new Date(); +const dbInfraConfigs: dbInfraConfig[] = [ + { + id: '3', + name: InfraConfigEnum.GOOGLE_CLIENT_ID, + value: 'abcdefghijkl', + active: true, + createdOn: INITIALIZED_DATE_CONST, + updatedOn: INITIALIZED_DATE_CONST, + }, + { + id: '4', + name: InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS, + value: 'google', + active: true, + createdOn: INITIALIZED_DATE_CONST, + updatedOn: INITIALIZED_DATE_CONST, + }, +]; +const infraConfigs: InfraConfig[] = [ + { + name: dbInfraConfigs[0].name as InfraConfigEnum, + value: dbInfraConfigs[0].value, + }, + { + name: dbInfraConfigs[1].name as InfraConfigEnum, + value: dbInfraConfigs[1].value, + }, +]; + beforeEach(() => { mockReset(mockPrisma); }); describe('InfraConfigService', () => { describe('update', () => { + it('should update the infra config without backend server restart', async () => { + const name = InfraConfigEnum.GOOGLE_CLIENT_ID; + const value = 'true'; + + mockPrisma.infraConfig.update.mockResolvedValueOnce({ + id: '', + name, + value, + active: true, + createdOn: new Date(), + updatedOn: new Date(), + }); + + jest.spyOn(helper, 'stopApp').mockReturnValueOnce(); + const result = await infraConfigService.update(name, value); + + expect(helper.stopApp).not.toHaveBeenCalled(); + expect(result).toEqualRight({ name, value }); + }); + + it('should update the infra config with backend server restart', async () => { + const name = InfraConfigEnum.GOOGLE_CLIENT_ID; + const value = 'true'; + + mockPrisma.infraConfig.update.mockResolvedValueOnce({ + id: '', + name, + value, + active: true, + createdOn: new Date(), + updatedOn: new Date(), + }); + jest.spyOn(helper, 'stopApp').mockReturnValueOnce(); + + const result = await infraConfigService.update(name, value, true); + + expect(helper.stopApp).toHaveBeenCalledTimes(1); + expect(result).toEqualRight({ name, value }); + }); + it('should update the infra config', async () => { const name = InfraConfigEnum.GOOGLE_CLIENT_ID; const value = 'true'; @@ -103,4 +179,45 @@ describe('InfraConfigService', () => { expect(result).toEqualLeft(INFRA_CONFIG_NOT_FOUND); }); }); + + describe('getMany', () => { + it('should throw error if any disallowed names are provided', async () => { + const disallowedNames = [InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS]; + const result = await infraConfigService.getMany(disallowedNames); + + expect(result).toEqualLeft(INFRA_CONFIG_OPERATION_NOT_ALLOWED); + }); + it('should resolve right with disallowed names if `checkDisallowed` parameter passed', async () => { + const disallowedNames = [InfraConfigEnum.VITE_ALLOWED_AUTH_PROVIDERS]; + + const dbInfraConfigResponses = dbInfraConfigs.filter((dbConfig) => + disallowedNames.includes(dbConfig.name as InfraConfigEnum), + ); + mockPrisma.infraConfig.findMany.mockResolvedValueOnce( + dbInfraConfigResponses, + ); + + const result = await infraConfigService.getMany(disallowedNames, false); + + expect(result).toEqualRight( + infraConfigs.filter((i) => disallowedNames.includes(i.name)), + ); + }); + + it('should return right with infraConfigs if Prisma query succeeds', async () => { + const allowedNames = [InfraConfigEnum.GOOGLE_CLIENT_ID]; + + const dbInfraConfigResponses = dbInfraConfigs.filter((dbConfig) => + allowedNames.includes(dbConfig.name as InfraConfigEnum), + ); + mockPrisma.infraConfig.findMany.mockResolvedValueOnce( + dbInfraConfigResponses, + ); + + const result = await infraConfigService.getMany(allowedNames); + expect(result).toEqualRight( + infraConfigs.filter((i) => allowedNames.includes(i.name)), + ); + }); + }); });