-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIEXPMGR-64 Cannot re-run 1 day+ old holdings exports (#110)
- Loading branch information
1 parent
a91e175
commit 2d7908d
Showing
11 changed files
with
193 additions
and
56 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
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
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 |
---|---|---|
@@ -1,31 +1,75 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import user from '@testing-library/user-event'; | ||
|
||
import '@folio/stripes-acq-components/test/jest/__mock__'; | ||
|
||
import { useSecureDownload } from '../../hooks'; | ||
import { ExportJobId } from './ExportJobId'; | ||
|
||
const renderExportJobId = (jobId, files = []) => render( | ||
<ExportJobId | ||
jobId={jobId} | ||
files={files} | ||
/>, | ||
const renderExportJobId = (job) => render( | ||
<ExportJobId job={job} />, | ||
); | ||
|
||
describe('BursarExportsManualRunner', () => { | ||
jest.mock('../../hooks', () => ({ | ||
...jest.requireActual('../../hooks'), | ||
useSecureDownload: jest.fn(), | ||
})); | ||
|
||
describe('ExportJobId', () => { | ||
const downloadSecurelyMock = jest.fn(); | ||
|
||
beforeEach(() => { | ||
downloadSecurelyMock.mockClear(); | ||
|
||
useSecureDownload.mockClear().mockReturnValue({ | ||
download: downloadSecurelyMock, | ||
}); | ||
}); | ||
|
||
it('should render job id as a text when no files provided', () => { | ||
const jobId = '1001'; | ||
const { getByText, queryByRole } = renderExportJobId(jobId); | ||
const jobName = '1001'; | ||
const { getByText, queryByRole } = renderExportJobId({ name: jobName }); | ||
|
||
expect(getByText(jobId)).toBeDefined(); | ||
expect(getByText(jobName)).toBeDefined(); | ||
expect(queryByRole('button')).toBeNull(); | ||
}); | ||
|
||
it('should render job id as a button when files are provided', () => { | ||
const jobId = '1001'; | ||
const { getByText, getByTestId } = renderExportJobId(jobId, ['/test.png']); | ||
const jobName = '1001'; | ||
const { getByText, getByTestId } = renderExportJobId({ | ||
name: jobName, | ||
files: ['/test.png'], | ||
}); | ||
|
||
expect(getByText(jobId)).toBeDefined(); | ||
expect(getByText(jobName)).toBeDefined(); | ||
expect(getByTestId('text-link')).toBeDefined(); | ||
}); | ||
|
||
it('should use secure download for eholdings exports', () => { | ||
const jobName = '1001'; | ||
const { getByTestId } = renderExportJobId({ | ||
name: jobName, | ||
files: ['/test.png'], | ||
fileNames: ['/test.png'], | ||
type: 'E_HOLDINGS', | ||
}); | ||
|
||
user.click(getByTestId('text-link')); | ||
|
||
expect(downloadSecurelyMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not use secure download for eholdings exports', () => { | ||
const jobName = '1001'; | ||
const { getByTestId } = renderExportJobId({ | ||
name: jobName, | ||
files: ['/test.png'], | ||
type: 'CIRCULATION_LOG', | ||
}); | ||
|
||
user.click(getByTestId('text-link')); | ||
|
||
expect(downloadSecurelyMock).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './useExportConfig'; | ||
export * from './useExportJobScheduler'; | ||
export * from './useSecureDownload'; |
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 @@ | ||
export * from './useSecureDownload'; |
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,33 @@ | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
|
||
import { | ||
downloadBase64, | ||
useShowCallout, | ||
} from '@folio/stripes-acq-components'; | ||
|
||
import { | ||
EXPORT_JOBS_API, | ||
} from '../../constants'; | ||
|
||
export const useSecureDownload = (jobId) => { | ||
const ky = useOkapiKy(); | ||
const showCallout = useShowCallout(); | ||
|
||
const download = async (fileName) => { | ||
return ky.get(`${EXPORT_JOBS_API}/${jobId}/download`, { | ||
headers: { accept: 'application/octet-stream' }, | ||
}) | ||
.blob() | ||
.then(data => { | ||
downloadBase64(fileName, URL.createObjectURL(data)); | ||
}) | ||
.catch(() => { | ||
showCallout({ | ||
messageId: 'ui-export-manager.exportJob.details.action.download.error', | ||
type: 'error', | ||
}); | ||
}); | ||
}; | ||
|
||
return { download }; | ||
}; |
63 changes: 63 additions & 0 deletions
63
src/common/hooks/useSecureDownload/useSecureDownload.test.js
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,63 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { useOkapiKy } from '@folio/stripes/core'; | ||
import { | ||
downloadBase64, | ||
useShowCallout, | ||
} from '@folio/stripes-acq-components'; | ||
|
||
import { useSecureDownload } from './useSecureDownload'; | ||
|
||
jest.mock('@folio/stripes-acq-components', () => { | ||
return { | ||
...jest.requireActual('@folio/stripes-acq-components'), | ||
downloadBase64: jest.fn(), | ||
useShowCallout: jest.fn(() => jest.fn()), | ||
}; | ||
}); | ||
|
||
const jobId = 'uid'; | ||
const toastMessage = type => `ui-export-manager.exportJob.details.action.${type}`; | ||
|
||
describe('useSecureDownload', () => { | ||
const kyGetMock = jest.fn(() => ({ | ||
blob: () => Promise.resolve(), | ||
})); | ||
const showCallout = jest.fn(); | ||
|
||
beforeEach(() => { | ||
downloadBase64.mockClear(); | ||
kyGetMock.mockClear(); | ||
showCallout.mockClear(); | ||
|
||
useShowCallout.mockClear().mockReturnValue(showCallout); | ||
|
||
useOkapiKy.mockClear().mockReturnValue({ | ||
get: kyGetMock, | ||
}); | ||
}); | ||
|
||
it('should make get request to download file', async () => { | ||
const { result } = renderHook( | ||
() => useSecureDownload(jobId), | ||
); | ||
|
||
await result.current.download('fileName.csv'); | ||
|
||
expect(kyGetMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle export job download error', async () => { | ||
kyGetMock.mockReturnValue({ blob: () => Promise.reject() }); | ||
|
||
const { result } = renderHook( | ||
() => useSecureDownload(jobId), | ||
); | ||
|
||
await result.current.download('fileName.csv'); | ||
|
||
expect(showCallout).toHaveBeenCalledWith(expect.objectContaining({ | ||
messageId: toastMessage`download.error`, | ||
})); | ||
}); | ||
}); |