Skip to content

Commit

Permalink
N21-1677 logic + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrikallab committed Apr 30, 2024
1 parent ca0b50b commit 4deb72b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ describe(ToolConfigurationStatusService.name, () => {
commonToolValidationService.validateParameters.mockReturnValueOnce([
new ToolParameterMandatoryValueMissingLoggableException(undefined, customParameter),
new ToolParameterDuplicateLoggableException(undefined, customParameter.name),
new ToolParameterOptionalValueMissingLoggableException(undefined, customParameter),
]);

return {
Expand Down Expand Up @@ -351,6 +352,50 @@ describe(ToolConfigurationStatusService.name, () => {
});
});

describe('when validation of ContextExternalTool throws only missing value on optional parameter errors', () => {
const setup = () => {
const customParameter = customParameterFactory.build();
const externalTool = externalToolFactory.buildWithId({ parameters: [customParameter] });
const schoolExternalTool = schoolExternalToolFactory.buildWithId({
toolId: externalTool.id as string,
});
const contextExternalTool = contextExternalToolFactory
.withSchoolExternalToolRef(schoolExternalTool.id as string)
.buildWithId();

commonToolValidationService.validateParameters.mockReturnValueOnce([]);
commonToolValidationService.validateParameters.mockReturnValueOnce([
new ToolParameterOptionalValueMissingLoggableException(undefined, customParameter),
new ToolParameterOptionalValueMissingLoggableException(undefined, customParameter),
new ToolParameterOptionalValueMissingLoggableException(undefined, customParameter),
]);

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

it('should return incomplete operational as tool status', () => {
const { externalTool, schoolExternalTool, contextExternalTool } = setup();

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

expect(status).toEqual<ContextExternalToolConfigurationStatus>({
isOutdatedOnScopeSchool: false,
isOutdatedOnScopeContext: false,
isIncompleteOnScopeContext: false,
isIncompleteOperationalOnScopeContext: true,
isDeactivated: false,
});
});
});

describe('when SchoolExternalTool is deactivated', () => {
const setup = () => {
const externalTool = externalToolFactory.buildWithId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ export class ToolConfigurationStatusService {
)
) {
configurationStatus.isIncompleteOnScopeContext = true;
} else if (
contextParameterErrors.some(
(error: ValidationError) => error instanceof ToolParameterOptionalValueMissingLoggableException
)
) {
} else if (this.isIncompleteOperational(contextParameterErrors) && !this.isOutdated(contextParameterErrors)) {
configurationStatus.isIncompleteOperationalOnScopeContext = true;
configurationStatus.isOutdatedOnScopeContext = false;
} else if (this.isIncompleteOperational(contextParameterErrors) && this.isOutdated(contextParameterErrors)) {
configurationStatus.isIncompleteOperationalOnScopeContext = true;
}
}
Expand All @@ -63,10 +62,22 @@ export class ToolConfigurationStatusService {
}

private isToolDeactivated(externalTool: ExternalTool, schoolExternalTool: SchoolExternalTool) {
if (externalTool.isDeactivated || (schoolExternalTool.status && schoolExternalTool.status.isDeactivated)) {
return true;
}
return !!(externalTool.isDeactivated || (schoolExternalTool.status && schoolExternalTool.status.isDeactivated));
}

private isIncompleteOperational(errors: ValidationError[]) {
return errors.some((error: ValidationError) => error instanceof ToolParameterOptionalValueMissingLoggableException);
}

private isOutdated(contextParameterErrors: ValidationError[]): boolean {
const parameterWithoutOptional: ValidationError[] = contextParameterErrors.filter(
(error: ValidationError) => !this.isOptional(error)
);

return parameterWithoutOptional.length > 0;
}

return false;
isOptional(error: ValidationError): boolean {
return error instanceof ToolParameterOptionalValueMissingLoggableException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe('ToolStatusOutdatedLoggableException', () => {
toolId,
toolConfigStatus.isOutdatedOnScopeSchool,
toolConfigStatus.isOutdatedOnScopeContext,
toolConfigStatus.isIncompleteOnScopeContext,
toolConfigStatus.isIncompleteOperationalOnScopeContext,
toolConfigStatus.isDeactivated
);

Expand All @@ -35,6 +37,8 @@ describe('ToolStatusOutdatedLoggableException', () => {
toolId: 'toolId',
isOutdatedOnScopeSchool: false,
isOutdatedOnScopeContext: false,
isIncompleteOnScopeContext: false,
isIncompleteOperationalOnScopeContext: false,
isDeactivated: false,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ export class ToolStatusOutdatedLoggableException extends BadRequestException imp
private readonly toolId: EntityId,
private readonly isOutdatedOnScopeSchool: boolean,
private readonly isOutdatedOnScopeContext: boolean,
private readonly isIncompleteOnScopeContext: boolean,
private readonly isIncompleteOperationalOnScopeContext: boolean,
private readonly isDeactivated: boolean
) {
super();
}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
// TODO refactor exception to status not launchable
type: 'TOOL_STATUS_OUTDATED',
message: 'The status of the tool is outdated and cannot be launched by the user.',
stack: this.stack,
Expand All @@ -23,6 +26,8 @@ export class ToolStatusOutdatedLoggableException extends BadRequestException imp
toolId: this.toolId,
isOutdatedOnScopeSchool: this.isOutdatedOnScopeSchool,
isOutdatedOnScopeContext: this.isOutdatedOnScopeContext,
isIncompleteOnScopeContext: this.isIncompleteOnScopeContext,
isIncompleteOperationalOnScopeContext: this.isIncompleteOperationalOnScopeContext,
isDeactivated: this.isDeactivated,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ describe('ToolLaunchService', () => {
isOutdatedOnScopeContext: true,
isOutdatedOnScopeSchool: true,
isIncompleteOnScopeContext: false,
isIncompleteOperationalOnScopeContext: false,
isDeactivated: true,
})
);
Expand All @@ -253,7 +254,7 @@ describe('ToolLaunchService', () => {
const func = () => service.getLaunchData(userId, launchParams.contextExternalTool);

await expect(func).rejects.toThrow(
new ToolStatusOutdatedLoggableException(userId, contextExternalToolId, true, true, true)
new ToolStatusOutdatedLoggableException(userId, contextExternalToolId, true, true, false, false, true)
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,19 @@ export class ToolLaunchService {
contextExternalTool
);

if (status.isOutdatedOnScopeSchool || status.isOutdatedOnScopeContext || status.isDeactivated) {
if (
status.isOutdatedOnScopeSchool ||
status.isOutdatedOnScopeContext ||
status.isDeactivated ||
status.isIncompleteOnScopeContext
) {
throw new ToolStatusOutdatedLoggableException(
userId,
contextExternalTool.id ?? '',
status.isOutdatedOnScopeSchool,
status.isOutdatedOnScopeContext,
status.isIncompleteOnScopeContext,
status.isIncompleteOperationalOnScopeContext,
status.isDeactivated
);
}
Expand Down

0 comments on commit 4deb72b

Please sign in to comment.