cap-js / attachments

The @cap-js/attachments package is a CDS plugin that provides out-of-the box asset storage and handling by using an aspect Attachments. It also provides a CAP-level, easy to use integration of the SAP Object Store.
Apache License 2.0
12 stars 4 forks source link

Without malware scanner, setTimeout to "mock" the scan results in "no such table" exception when running e.g. in jest test suite #71

Closed Zahlii closed 1 month ago

Zahlii commented 3 months ago

https://github.com/cap-js/attachments/blob/main/lib/malwareScanner.js#L26

Due to the (unawaited) setTimeout call, it can happen that the UPDATE() statement is attempted in the background after test teardown, resulting in an error as follows:

SqliteError: no such table: sapit_cdh_unintegrated_UnintegratedDataRequests_attachments in:
    UPDATE sapit_cdh_unintegrated_UnintegratedDataRequests_attachments AS attachments SET status=?,ID=?,modifiedAt=coalesce(ISO(NULL), session_context('$now')),modifiedBy=coalesce(NULL, session_context('$user.id')) WHERE attachments.ID = ?

Our test setup basically looks as follows:

beforeEach() => {
await data.delete();
await POST('.../attachments', {mimeType: 'image/png', filename: 'test.png'});
}

it('put content', async () => {
await PUT('.../attachments(ID=...)/content', 'mock content');
// this will trigger setTimeout() call internally

await new Promise<void>((res, rej) => setTimeout((_) => res(), 5100));
// this line MUST be contained to make sure the test can only tear down once the setTimeout has triggered, otherwise we risk the above race condition/error
}
muskansethi1 commented 1 month ago

Hi @Zahlii,

We don't anymore mock the Malware scanning except when in development profile from version 1.1.5. The status will be set to Unscanned in such cases.

Let me know if the issue still persists.

Best Regards, Muskan