Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N21-1336 change tool status determination method #4534

Merged
merged 14 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import { ContextExternalTool } from '../../context-external-tool/domain';
import { ToolConfigurationStatus } from '../enum';
import { ToolVersion } from '../interface';

// TODO N21-1337 remove class when tool versioning is removed
@Injectable()
export class CommonToolService {
/**
* @deprecated use ToolVersionService
*/
determineToolConfigurationStatus(
externalTool: ExternalTool,
schoolExternalTool: SchoolExternalTool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import { LoggerModule } from '@src/core/logger';
import { CommonToolModule } from '../common';
import { ExternalToolModule } from '../external-tool';
import { SchoolExternalToolModule } from '../school-external-tool';
import {
ContextExternalToolAuthorizableService,
ContextExternalToolService,
ContextExternalToolValidationService,
ToolReferenceService,
} from './service';
import { ContextExternalToolAuthorizableService, ContextExternalToolService, ToolReferenceService } from './service';
import { ContextExternalToolValidationService } from './service/context-external-tool-validation.service';
import { ToolConfigModule } from '../tool-config.module';
import { ToolVersionService } from './service/tool-version-service';

@Module({
imports: [CommonToolModule, ExternalToolModule, SchoolExternalToolModule, LoggerModule],
imports: [CommonToolModule, ExternalToolModule, SchoolExternalToolModule, LoggerModule, ToolConfigModule],
providers: [
ContextExternalToolService,
ContextExternalToolValidationService,
ContextExternalToolAuthorizableService,
ToolReferenceService,
ToolVersionService,
],
exports: [
ContextExternalToolService,
ContextExternalToolValidationService,
ContextExternalToolAuthorizableService,
ToolReferenceService,
ToolVersionService,
],
})
export class ContextExternalToolModule {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './context-external-tool.service';
export * from './context-external-tool-validation.service';
export * from './context-external-tool-authorizable.service';
export * from './tool-reference.service';
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { ObjectId } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { contextExternalToolFactory, externalToolFactory, schoolExternalToolFactory } from '@shared/testing';
import { ToolConfigurationStatus } from '../../common/enum';
import { CommonToolService } from '../../common/service';
import { ExternalToolLogoService, ExternalToolService } from '../../external-tool/service';
import { SchoolExternalToolService } from '../../school-external-tool/service';
import { ToolReference } from '../domain';
import { ContextExternalToolService } from './context-external-tool.service';
import { ToolReferenceService } from './tool-reference.service';
import { ToolVersionService } from './tool-version-service';

describe('ToolReferenceService', () => {
let module: TestingModule;
Expand All @@ -17,7 +17,7 @@ describe('ToolReferenceService', () => {
let externalToolService: DeepMocked<ExternalToolService>;
let schoolExternalToolService: DeepMocked<SchoolExternalToolService>;
let contextExternalToolService: DeepMocked<ContextExternalToolService>;
let commonToolService: DeepMocked<CommonToolService>;
let toolVersionService: DeepMocked<ToolVersionService>;
let externalToolLogoService: DeepMocked<ExternalToolLogoService>;

beforeAll(async () => {
Expand All @@ -37,8 +37,8 @@ describe('ToolReferenceService', () => {
useValue: createMock<ContextExternalToolService>(),
},
{
provide: CommonToolService,
useValue: createMock<CommonToolService>(),
provide: ToolVersionService,
useValue: createMock<ToolVersionService>(),
},
{
provide: ExternalToolLogoService,
Expand All @@ -51,7 +51,7 @@ describe('ToolReferenceService', () => {
externalToolService = module.get(ExternalToolService);
schoolExternalToolService = module.get(SchoolExternalToolService);
contextExternalToolService = module.get(ContextExternalToolService);
commonToolService = module.get(CommonToolService);
toolVersionService = module.get(ToolVersionService);
externalToolLogoService = module.get(ExternalToolLogoService);
});

Expand Down Expand Up @@ -79,7 +79,7 @@ describe('ToolReferenceService', () => {
contextExternalToolService.findById.mockResolvedValueOnce(contextExternalTool);
schoolExternalToolService.findById.mockResolvedValueOnce(schoolExternalTool);
externalToolService.findById.mockResolvedValueOnce(externalTool);
commonToolService.determineToolConfigurationStatus.mockReturnValue(ToolConfigurationStatus.OUTDATED);
toolVersionService.determineToolConfigurationStatus.mockResolvedValue(ToolConfigurationStatus.OUTDATED);
externalToolLogoService.buildLogoUrl.mockReturnValue(logoUrl);

return {
Expand All @@ -96,7 +96,7 @@ describe('ToolReferenceService', () => {

await service.getToolReference(contextExternalToolId);

expect(commonToolService.determineToolConfigurationStatus).toHaveBeenCalledWith(
expect(toolVersionService.determineToolConfigurationStatus).toHaveBeenCalledWith(
externalTool,
schoolExternalTool,
contextExternalTool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { Injectable } from '@nestjs/common';
import { EntityId } from '@shared/domain';
import { ToolConfigurationStatus } from '../../common/enum';
import { CommonToolService } from '../../common/service';
import { ExternalTool } from '../../external-tool/domain';
import { ExternalToolLogoService, ExternalToolService } from '../../external-tool/service';
import { SchoolExternalTool } from '../../school-external-tool/domain';
import { SchoolExternalToolService } from '../../school-external-tool/service';
import { ContextExternalTool, ToolReference } from '../domain';
import { ToolReferenceMapper } from '../mapper';
import { ContextExternalToolService } from './context-external-tool.service';
import { ToolVersionService } from './tool-version-service';

@Injectable()
export class ToolReferenceService {
constructor(
private readonly externalToolService: ExternalToolService,
private readonly schoolExternalToolService: SchoolExternalToolService,
private readonly contextExternalToolService: ContextExternalToolService,
private readonly commonToolService: CommonToolService,
private readonly externalToolLogoService: ExternalToolLogoService
private readonly externalToolLogoService: ExternalToolLogoService,
private readonly toolVersionService: ToolVersionService
) {}

async getToolReference(contextExternalToolId: EntityId): Promise<ToolReference> {
Expand All @@ -29,7 +28,7 @@ export class ToolReferenceService {
);
const externalTool: ExternalTool = await this.externalToolService.findById(schoolExternalTool.toolId);

const status: ToolConfigurationStatus = this.commonToolService.determineToolConfigurationStatus(
const status = await this.toolVersionService.determineToolConfigurationStatus(
externalTool,
schoolExternalTool,
contextExternalTool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { contextExternalToolFactory, externalToolFactory, schoolExternalToolFactory } from '@shared/testing';
import { ApiValidationError } from '@shared/common';
import { SchoolExternalToolValidationService } from '../../school-external-tool/service';
import { ToolVersionService } from './tool-version-service';
import { ContextExternalToolValidationService } from './context-external-tool-validation.service';
import { CommonToolService } from '../../common/service';
import { IToolFeatures, ToolFeatures } from '../../tool-config';
import { ToolConfigurationStatus } from '../../common/enum';

describe('ToolVersionService', () => {
let module: TestingModule;
let service: ToolVersionService;

let contextExternalToolValidationService: DeepMocked<ContextExternalToolValidationService>;
let schoolExternalToolValidationService: DeepMocked<SchoolExternalToolValidationService>;
let commonToolService: DeepMocked<CommonToolService>;
let toolFeatures: DeepMocked<IToolFeatures>;

beforeAll(async () => {
module = await Test.createTestingModule({
providers: [
ToolVersionService,
{
provide: ContextExternalToolValidationService,
useValue: createMock<ContextExternalToolValidationService>(),
},
{
provide: SchoolExternalToolValidationService,
useValue: createMock<SchoolExternalToolValidationService>(),
},
{
provide: CommonToolService,
useValue: createMock<CommonToolService>(),
},
{
provide: ToolFeatures,
useValue: {
toolStatusWithoutVersions: false,
},
},
],
}).compile();

service = module.get(ToolVersionService);
contextExternalToolValidationService = module.get(ContextExternalToolValidationService);
schoolExternalToolValidationService = module.get(SchoolExternalToolValidationService);
commonToolService = module.get(CommonToolService);
toolFeatures = module.get(ToolFeatures);
});

afterAll(async () => {
await module.close();
});

afterEach(() => {
jest.resetAllMocks();
});

describe('determineToolConfigurationStatus', () => {
describe('when FEATURE_COMPUTE_TOOL_STATUS_WITHOUT_VERSIONS_ENABLED is false', () => {
const setup = () => {
const externalTool = externalToolFactory.buildWithId();
const schoolExternalTool = schoolExternalToolFactory.buildWithId({
toolId: externalTool.id as string,
});
const contextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(schoolExternalTool.id as string)
.buildWithId();

toolFeatures.toolStatusWithoutVersions = false;

return {
externalTool,
schoolExternalTool,
contextExternalTool,
};
};

it('should call CommonToolService', async () => {
const { externalTool, schoolExternalTool, contextExternalTool } = setup();

await service.determineToolConfigurationStatus(externalTool, schoolExternalTool, contextExternalTool);

expect(commonToolService.determineToolConfigurationStatus).toHaveBeenCalledWith(
externalTool,
schoolExternalTool,
contextExternalTool
);
});
});

describe('when FEATURE_COMPUTE_TOOL_STATUS_WITHOUT_VERSIONS_ENABLED is true and validation runs through', () => {
const setup = () => {
const externalTool = externalToolFactory.buildWithId();
const schoolExternalTool = schoolExternalToolFactory.buildWithId({
toolId: externalTool.id as string,
});
const contextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(schoolExternalTool.id as string)
.buildWithId();

toolFeatures.toolStatusWithoutVersions = true;

schoolExternalToolValidationService.validate.mockResolvedValue();
contextExternalToolValidationService.validate.mockResolvedValueOnce();

return {
externalTool,
schoolExternalTool,
contextExternalTool,
};
};

it('should return latest tool status', async () => {
const { externalTool, schoolExternalTool, contextExternalTool } = setup();

const status: ToolConfigurationStatus = await service.determineToolConfigurationStatus(
externalTool,
schoolExternalTool,
contextExternalTool
);

expect(status).toEqual(ToolConfigurationStatus.LATEST);
});

it('should call schoolExternalToolValidationService', async () => {
const { externalTool, schoolExternalTool, contextExternalTool } = setup();

await service.determineToolConfigurationStatus(externalTool, schoolExternalTool, contextExternalTool);

expect(schoolExternalToolValidationService.validate).toHaveBeenCalledWith(schoolExternalTool);
});

it('should call contextExternalToolValidationService', async () => {
const { externalTool, schoolExternalTool, contextExternalTool } = setup();

await service.determineToolConfigurationStatus(externalTool, schoolExternalTool, contextExternalTool);

expect(schoolExternalToolValidationService.validate).toHaveBeenCalledWith(schoolExternalTool);
});
});

describe('when FEATURE_COMPUTE_TOOL_STATUS_WITHOUT_VERSIONS_ENABLED is true and validation throws an error', () => {
const setup = () => {
const externalTool = externalToolFactory.buildWithId();
const schoolExternalTool = schoolExternalToolFactory.buildWithId({
toolId: externalTool.id as string,
});
const contextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(schoolExternalTool.id as string)
.buildWithId();

toolFeatures.toolStatusWithoutVersions = true;

schoolExternalToolValidationService.validate.mockResolvedValue();
contextExternalToolValidationService.validate.mockRejectedValueOnce(ApiValidationError);

return {
externalTool,
schoolExternalTool,
contextExternalTool,
};
};

it('should return outdated tool status', async () => {
IgorCapCoder marked this conversation as resolved.
Show resolved Hide resolved
const { externalTool, schoolExternalTool, contextExternalTool } = setup();

const status: ToolConfigurationStatus = await service.determineToolConfigurationStatus(
externalTool,
schoolExternalTool,
contextExternalTool
);

expect(status).toEqual(ToolConfigurationStatus.OUTDATED);
});

it('should call schoolExternalToolValidationService', async () => {
const { externalTool, schoolExternalTool, contextExternalTool } = setup();

await service.determineToolConfigurationStatus(externalTool, schoolExternalTool, contextExternalTool);

expect(schoolExternalToolValidationService.validate).toHaveBeenCalledWith(schoolExternalTool);
});

it('should call contextExternalToolValidationService', async () => {
const { externalTool, schoolExternalTool, contextExternalTool } = setup();

await service.determineToolConfigurationStatus(externalTool, schoolExternalTool, contextExternalTool);

expect(contextExternalToolValidationService.validate).toHaveBeenCalledWith(contextExternalTool);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Injectable } from '@nestjs/common/decorators/core/injectable.decorator';
import { Inject } from '@nestjs/common';
import { ContextExternalToolValidationService } from './context-external-tool-validation.service';
import { SchoolExternalToolValidationService } from '../../school-external-tool/service';
import { IToolFeatures, ToolFeatures } from '../../tool-config';
import { ExternalTool } from '../../external-tool/domain';
import { SchoolExternalTool } from '../../school-external-tool/domain';
import { ContextExternalTool } from '../domain';
import { ToolConfigurationStatus } from '../../common/enum';
import { CommonToolService } from '../../common/service';

@Injectable()
export class ToolVersionService {
constructor(
private readonly contextExternalToolValidationService: ContextExternalToolValidationService,
private readonly schoolExternalToolValidationService: SchoolExternalToolValidationService,
private readonly commonToolService: CommonToolService,
@Inject(ToolFeatures) private readonly toolFeatures: IToolFeatures
) {}

async determineToolConfigurationStatus(
externalTool: ExternalTool,
schoolExternalTool: SchoolExternalTool,
contextExternalTool: ContextExternalTool
): Promise<ToolConfigurationStatus> {
// TODO N21-1337 remove if statement, when feature flag is removed
if (this.toolFeatures.toolStatusWithoutVersions) {
try {
await this.schoolExternalToolValidationService.validate(schoolExternalTool);
await this.contextExternalToolValidationService.validate(contextExternalTool);
return ToolConfigurationStatus.LATEST;
} catch (err) {
arnegns marked this conversation as resolved.
Show resolved Hide resolved
return ToolConfigurationStatus.OUTDATED;
}
}
const status: ToolConfigurationStatus = this.commonToolService.determineToolConfigurationStatus(
IgorCapCoder marked this conversation as resolved.
Show resolved Hide resolved
externalTool,
schoolExternalTool,
contextExternalTool
);

return status;
}
}
Loading
Loading