-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(DOSDelete): remove isTruncated and use eachPage instead, add tests
- Loading branch information
1 parent
94acce2
commit e2644f1
Showing
4 changed files
with
176 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { Delete } from '@DOSDelete/utils/Delete.ts' | ||
const AWS = require('aws-sdk') | ||
|
||
const spyLog = jest.spyOn(console, 'log') | ||
const spyError = jest.spyOn(console, 'error') | ||
|
||
describe('DOSDelete', () => { | ||
const baseParameters = { | ||
digitalGlobExpressions: ['**'], | ||
digitalEndpoint: { | ||
parameters: { username: 'test', password: 'test' }, | ||
scheme: 'test', | ||
}, | ||
digitalRegion: 'test', | ||
digitalBucket: 'test', | ||
digitalCredentials: 'test', | ||
} | ||
|
||
afterEach(() => { | ||
spyLog.mockClear() | ||
spyError.mockClear() | ||
AWS.clearAllMocks() | ||
}) | ||
test('should delete files successfully', async () => { | ||
const listFiles = AWS.spyOnEachPage('S3', 'listObjectsV2', [ | ||
{ Contents: [{ Key: 'test/path/1.txt' }] }, | ||
{ Contents: [{ Key: 'text/path/2.txt' }] }, | ||
]) | ||
|
||
const deleteObjectsResponse = AWS.spyOnPromise('S3', 'deleteObjects', true) | ||
|
||
const deleteFiles = new Delete({ | ||
...baseParameters, | ||
digitalEnableSemver: false, | ||
digitalSemverKeepOnly: 0, | ||
}) | ||
|
||
await deleteFiles.init() | ||
|
||
expect(listFiles.mock.calls).toEqual([ | ||
[{ Bucket: 'test', Prefix: undefined }], | ||
]) | ||
expect(deleteObjectsResponse).toHaveBeenCalledWith({ | ||
Bucket: 'test', | ||
Delete: { | ||
Objects: [{ Key: 'test/path/1.txt' }, { Key: 'text/path/2.txt' }], | ||
}, | ||
}) | ||
}) | ||
test('should show message when files not found', async () => { | ||
const listFiles = AWS.spyOnEachPage('S3', 'listObjectsV2', []) | ||
|
||
const deleteFiles = new Delete({ | ||
...baseParameters, | ||
digitalEnableSemver: false, | ||
digitalSemverKeepOnly: 0, | ||
}) | ||
|
||
await deleteFiles.init() | ||
|
||
expect(listFiles.mock.calls).toEqual([ | ||
[{ Bucket: 'test', Prefix: undefined }], | ||
]) | ||
expect(spyLog.mock.calls).toEqual([ | ||
["Deleting files from 'root' in bucket test"], | ||
["Searching 'root' prefix for files to delete"], | ||
["No files found at 'root'"], | ||
]) | ||
}) | ||
|
||
test('should throw an error when delete fails', async () => { | ||
const listFiles = AWS.spyOnEachPage('S3', 'listObjectsV2', [ | ||
{ Contents: [{ Key: 'test/path/1.txt' }] }, | ||
new Error('Not able to find files'), | ||
]) | ||
|
||
const deleteFiles = new Delete({ | ||
...baseParameters, | ||
digitalEnableSemver: false, | ||
digitalSemverKeepOnly: 0, | ||
}) | ||
|
||
try { | ||
await deleteFiles.init() | ||
} catch (error) { | ||
expect(error).toEqual(new Error('Not able to find files')) | ||
expect(spyError.mock.calls[0]).toEqual([ | ||
'Deleting files failed', | ||
new Error('Not able to find files'), | ||
]) | ||
} | ||
}) | ||
|
||
test('should delete files successfully using semantic version', async () => { | ||
const listFiles = AWS.spyOnEachPage('S3', 'listObjectsV2', [ | ||
{ Contents: [{ Key: 'test/path/v1.0.0.txt' }] }, | ||
{ Contents: [{ Key: 'text/path/v2.0.0.txt' }] }, | ||
]) | ||
|
||
const deleteObjectsResponse = AWS.spyOnPromise('S3', 'deleteObjects', true) | ||
|
||
const deleteFiles = new Delete({ | ||
...baseParameters, | ||
digitalTargetFolder: 'test/', | ||
digitalEnableSemver: true, | ||
digitalSemverKeepOnly: 1, | ||
}) | ||
|
||
await deleteFiles.init() | ||
|
||
expect(listFiles.mock.calls).toEqual([ | ||
[{ Bucket: 'test', Prefix: 'test/' }], | ||
]) | ||
expect(deleteObjectsResponse).toHaveBeenCalledWith({ | ||
Bucket: 'test', | ||
Delete: { | ||
Objects: [{ Key: 'test/path/v1.0.0.txt' }], | ||
}, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters