diff --git a/app/src/components/Download.cy.js b/app/src/components/Download.cy.js index cf61eedc..4f33edf8 100644 --- a/app/src/components/Download.cy.js +++ b/app/src/components/Download.cy.js @@ -127,6 +127,15 @@ describe('', () => { shouldBeInvalid('#url_input'); }); + it('can be submitted with a frequency of Once', () => { + cy.get('#url_input').type('https://example.com/playlist').wait(500); + cy.get('#frequency_select').click(); + cy.contains('[role="option"]:visible', 'Once').should('be.visible').click({force: true}); + cy.get('#frequency_select').should('have.value', 'Once'); + + cy.get('#download_form_download_button').should('not.be.disabled'); + }); + it('can choose different options', () => { /* * `#download_frequency_selector` and `.item` named the old dropdown and its menu rows. diff --git a/app/src/hooks/useForm.js b/app/src/hooks/useForm.js index 0c03ec7c..8959a43d 100644 --- a/app/src/hooks/useForm.js +++ b/app/src/hooks/useForm.js @@ -67,7 +67,11 @@ export function useForm({ setReady(false); return } - const missingValues = Object.keys(memoizedRequires).filter(i => !_.get(formData, i)); + // 0 is a real value (a download frequency of "Once"), only empty values are missing. + const missingValues = Object.keys(memoizedRequires).filter(i => { + const value = _.get(formData, i); + return value === null || value === undefined || value === '' || value === false || Number.isNaN(value); + }); if (missingValues.length > 0) { console.debug(`Form invalid because it is missing required value: ${missingValues[0]}`); setReady(false); diff --git a/app/src/hooks/useForm.test.js b/app/src/hooks/useForm.test.js index 443f0411..9d37ea62 100644 --- a/app/src/hooks/useForm.test.js +++ b/app/src/hooks/useForm.test.js @@ -727,6 +727,29 @@ describe('useForm', () => { }); }); + it('zero satisfies a required field', async () => { + // A download frequency of 0 means "Once". + const {result} = renderHook(() => useForm({ + defaultFormData: {frequency: null}, + submitter: jest.fn(), + })); + + act(() => { + result.current.getCustomProps({name: 'frequency', required: true}); + }); + await waitFor(() => { + expect(result.current.ready).toBe(false); + }); + + act(() => { + result.current.setValue('frequency', 0); + }); + await waitFor(() => { + expect(result.current.formData.frequency).toBe(0); + expect(result.current.ready).toBe(true); + }); + }); + it('addRequires marks field as required', async () => { const {result} = renderHook(() => useForm({ defaultFormData: {field: ''}, diff --git a/wrolpi/downloader.py b/wrolpi/downloader.py index 84727acd..9eae4baa 100644 --- a/wrolpi/downloader.py +++ b/wrolpi/downloader.py @@ -1062,6 +1062,8 @@ class DownloadManager: sub_downloader: str | None = None, frequency: int = None, settings: Dict = None, collection_id: int = None) -> Download: download = Download.find_by_id(session, id_) + # A once-download has no frequency; a frequency of 0 ("Once") is never stored as an interval. + frequency = frequency or None if collection_id and not frequency: raise InvalidDownload(f'A once-download cannot be associated with a Collection') download.url = url diff --git a/wrolpi/test/test_root_api.py b/wrolpi/test/test_root_api.py index a8163666..4b8eff7a 100644 --- a/wrolpi/test/test_root_api.py +++ b/wrolpi/test/test_root_api.py @@ -584,6 +584,32 @@ async def test_update_download_with_settings_collection_id(test_session, async_c f'Expected NO_CONTENT but got {response.status_code}: {response.json}' +@pytest.mark.asyncio +async def test_update_download_frequency_once(test_session, async_client, test_download_manager_config, + test_download_manager): + """Editing a recurring Download to a frequency of 0 ("Once") stores a once-download, not a zero interval.""" + + async def dispatch_downloads(*a, **kw): + pass + + with mock.patch('wrolpi.downloader.DownloadManager.dispatch_downloads', dispatch_downloads): + body = dict(urls=['https://example.com/feed.xml'], downloader='rss', sub_downloader='archive', + frequency=86400) + request, response = await async_client.post('/api/download', json=body) + assert response.status_code == HTTPStatus.CREATED + download = test_session.query(Download).one() + assert download.frequency == 86400 + + body = dict(urls=['https://example.com/feed.xml'], downloader='rss', sub_downloader='archive', frequency=0) + request, response = await async_client.put(f'/api/download/{download.id}', json=body) + assert response.status_code == HTTPStatus.NO_CONTENT, response.json + + test_session.expire_all() + download = test_session.query(Download).one() + assert download.frequency is None + assert test_download_manager.get_recurring_downloads(test_session) == [] + + @pytest.mark.asyncio async def test_get_downloaders(async_client): """A list of Downloaders the user can use can be gotten."""