generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from graasp/146/publicDownloadZip
refactor: add public export item
- Loading branch information
Showing
12 changed files
with
179 additions
and
88 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 was deleted.
Oops, something went wrong.
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,27 @@ | ||
import { QueryClientConfig, UUID } from '../types'; | ||
import configureAxios, { fallbackToPublic } from './axios'; | ||
import { buildExportItemRoute, buildExportPublicItemRoute } from './routes'; | ||
|
||
const axios = configureAxios(); | ||
|
||
/* eslint-disable import/prefer-default-export */ | ||
export const exportItem = async ( | ||
id: UUID, | ||
{ API_HOST }: QueryClientConfig, | ||
options?: { public: boolean }, | ||
) => | ||
fallbackToPublic( | ||
() => | ||
axios({ | ||
url: `${API_HOST}/${buildExportItemRoute(id)}`, | ||
method: 'GET', | ||
responseType: 'blob', | ||
}), | ||
() => | ||
axios({ | ||
url: `${API_HOST}/${buildExportPublicItemRoute(id)}`, | ||
method: 'GET', | ||
responseType: 'blob', | ||
}), | ||
options, | ||
); |
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 was deleted.
Oops, something went wrong.
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,114 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import nock from 'nock'; | ||
import Cookies from 'js-cookie'; | ||
import { act } from 'react-test-renderer'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { mockMutation, setUpTest, waitForMutation } from '../../test/utils'; | ||
import { REQUEST_METHODS } from '../api/utils'; | ||
import { MUTATION_KEYS } from '../config/keys'; | ||
import { | ||
buildExportItemRoute, | ||
buildExportPublicItemRoute, | ||
} from '../api/routes'; | ||
import { exportItemRoutine } from '../routines'; | ||
import { UNAUTHORIZED_RESPONSE } from '../../test/constants'; | ||
|
||
const mockedNotifier = jest.fn(); | ||
const { wrapper, queryClient, useMutation } = setUpTest({ | ||
notifier: mockedNotifier, | ||
}); | ||
|
||
jest.spyOn(Cookies, 'get').mockReturnValue({ session: 'somesession' }); | ||
|
||
describe('Export Zip', () => { | ||
afterEach(() => { | ||
queryClient.clear(); | ||
nock.cleanAll(); | ||
}); | ||
|
||
describe(MUTATION_KEYS.EXPORT_ZIP, () => { | ||
const itemId = 'item-id'; | ||
const route = `/${buildExportItemRoute(itemId)}`; | ||
const mutation = () => useMutation(MUTATION_KEYS.EXPORT_ZIP); | ||
|
||
it('Export zip', async () => { | ||
const endpoints = [ | ||
{ | ||
response: { id: 'id', content: 'content' }, | ||
method: REQUEST_METHODS.GET, | ||
route, | ||
}, | ||
]; | ||
|
||
const mockedMutation = await mockMutation({ | ||
endpoints, | ||
mutation, | ||
wrapper, | ||
}); | ||
|
||
await act(async () => { | ||
await mockedMutation.mutate({ id: itemId }); | ||
await waitForMutation(); | ||
}); | ||
|
||
expect(mockedNotifier).toHaveBeenCalledWith({ | ||
type: exportItemRoutine.SUCCESS, | ||
}); | ||
}); | ||
|
||
it(`Fallback to public`, async () => { | ||
const endpoints = [ | ||
{ | ||
response: UNAUTHORIZED_RESPONSE, | ||
statusCode: StatusCodes.UNAUTHORIZED, | ||
route, | ||
}, | ||
{ | ||
response: { id: 'id', content: 'content' }, | ||
method: REQUEST_METHODS.GET, | ||
route: `/${buildExportPublicItemRoute(itemId)}`, | ||
}, | ||
]; | ||
|
||
const mockedMutation = await mockMutation({ | ||
endpoints, | ||
mutation, | ||
wrapper, | ||
}); | ||
|
||
await act(async () => { | ||
await mockedMutation.mutate({ id: itemId }); | ||
await waitForMutation(); | ||
}); | ||
|
||
expect(mockedNotifier).toHaveBeenCalledWith({ | ||
type: exportItemRoutine.SUCCESS, | ||
}); | ||
}); | ||
|
||
it(`Fallback to public automatically`, async () => { | ||
const endpoints = [ | ||
{ | ||
response: { id: 'id', content: 'public content' }, | ||
method: REQUEST_METHODS.GET, | ||
route: `/${buildExportPublicItemRoute(itemId)}`, | ||
}, | ||
]; | ||
|
||
const mockedMutation = await mockMutation({ | ||
endpoints, | ||
mutation, | ||
wrapper, | ||
}); | ||
|
||
await act(async () => { | ||
await mockedMutation.mutate({ id: itemId, options: { public: true } }); | ||
await waitForMutation(); | ||
}); | ||
|
||
expect(mockedNotifier).toHaveBeenCalledWith({ | ||
type: exportItemRoutine.SUCCESS, | ||
}); | ||
}); | ||
}); | ||
}); |
11 changes: 7 additions & 4 deletions
11
src/mutations/itemDownload.ts → src/mutations/itemExport.ts
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,19 +1,22 @@ | ||
import { QueryClient } from 'react-query'; | ||
import * as Api from '../api'; | ||
import { MUTATION_KEYS } from '../config/keys'; | ||
import { downloadItemRoutine } from '../routines'; | ||
import { exportItemRoutine } from '../routines'; | ||
import { QueryClientConfig } from '../types'; | ||
|
||
export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => { | ||
const { notifier } = queryConfig; | ||
|
||
/** | ||
* @param options.public {boolean} force fallback to public endpoint | ||
*/ | ||
queryClient.setMutationDefaults(MUTATION_KEYS.EXPORT_ZIP, { | ||
mutationFn: (id) => Api.downloadItem(id, queryConfig).then((data) => data), | ||
mutationFn: ({ id, options }) => Api.exportItem(id, queryConfig, options), | ||
onSuccess: () => { | ||
notifier?.({ type: downloadItemRoutine.SUCCESS }); | ||
notifier?.({ type: exportItemRoutine.SUCCESS }); | ||
}, | ||
onError: (error) => { | ||
notifier?.({ type: downloadItemRoutine.FAILURE, payload: { error } }); | ||
notifier?.({ type: exportItemRoutine.FAILURE, payload: { error } }); | ||
}, | ||
}); | ||
}; |
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,4 +1,4 @@ | ||
import createRoutine from './utils'; | ||
|
||
/* eslint-disable import/prefer-default-export */ | ||
export const downloadItemRoutine = createRoutine('EXPORT_ZIP'); | ||
export const exportItemRoutine = createRoutine('EXPORT_ZIP'); |