2021-12-20 16:08:13 -07:00
|
|
|
import json
|
|
|
|
|
from http import HTTPStatus
|
|
|
|
|
from itertools import zip_longest
|
|
|
|
|
|
2022-02-16 21:57:27 -07:00
|
|
|
from mock import mock
|
|
|
|
|
|
2022-05-18 17:24:50 -06:00
|
|
|
from wrolpi.admin import HotspotStatus
|
2022-02-16 21:57:27 -07:00
|
|
|
from wrolpi.common import get_config
|
2021-12-20 16:08:13 -07:00
|
|
|
from wrolpi.db import get_db_session
|
2023-04-05 10:40:16 -06:00
|
|
|
from wrolpi.downloader import Download, get_download_manager_config
|
2021-12-20 16:08:13 -07:00
|
|
|
from wrolpi.root_api import api_app
|
2022-04-07 11:17:53 -06:00
|
|
|
from wrolpi.test.common import TestAPI, wrap_test_db, skip_circleci
|
2021-12-20 16:08:13 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRootAPI(TestAPI):
|
|
|
|
|
|
2022-07-09 18:51:45 -06:00
|
|
|
@wrap_test_db
|
2021-12-20 16:08:13 -07:00
|
|
|
def test_index(self):
|
|
|
|
|
"""
|
|
|
|
|
Index should have some details in an HTML response
|
|
|
|
|
"""
|
|
|
|
|
request, response = api_app.test_client.get('/')
|
|
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
assert b'html' in response.body
|
|
|
|
|
|
2023-06-09 16:16:43 -06:00
|
|
|
request, response = api_app.test_client.get('/api')
|
|
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
assert b'html' in response.body
|
|
|
|
|
|
2021-12-20 16:08:13 -07:00
|
|
|
def test_echo(self):
|
|
|
|
|
"""
|
|
|
|
|
Echo should send back a JSON object with our request details.
|
|
|
|
|
"""
|
|
|
|
|
request, response = api_app.test_client.get('/api/echo?hello=world')
|
|
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
self.assertDictContains(response.json['form'], {})
|
|
|
|
|
assert response.json['method'] == 'GET', 'Method was not GET'
|
|
|
|
|
assert response.json['json'] is None, 'JSON was not empty'
|
|
|
|
|
assert response.json['args'] == {'hello': ['world']}
|
|
|
|
|
|
|
|
|
|
data = {'foo': 'bar'}
|
2021-12-21 20:39:55 -07:00
|
|
|
request, response = api_app.test_client.post('/api/echo', content=json.dumps(data))
|
2021-12-20 16:08:13 -07:00
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
self.assertDictContains(response.json['form'], {})
|
|
|
|
|
assert response.json['method'] == 'POST', 'Method was not POST'
|
|
|
|
|
assert response.json['json'] == data, 'JSON was not data'
|
|
|
|
|
assert response.json['args'] == {}
|
|
|
|
|
|
|
|
|
|
def test_valid_regex(self):
|
|
|
|
|
"""
|
|
|
|
|
The endpoint should only return valid if the regex is valid.
|
|
|
|
|
"""
|
|
|
|
|
data = {'regex': 'foo'}
|
2021-12-21 20:39:55 -07:00
|
|
|
request, response = api_app.test_client.post('/api/valid_regex', content=json.dumps(data))
|
2021-12-20 16:08:13 -07:00
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
assert json.loads(response.body) == {'valid': True, 'regex': 'foo'}
|
|
|
|
|
|
|
|
|
|
data = {'regex': '.*(title match).*'}
|
2021-12-21 20:39:55 -07:00
|
|
|
request, response = api_app.test_client.post('/api/valid_regex', content=json.dumps(data))
|
2021-12-20 16:08:13 -07:00
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
assert json.loads(response.body) == {'valid': True, 'regex': '.*(title match).*'}
|
|
|
|
|
|
|
|
|
|
data = {'regex': '.*(missing parenthesis.*'}
|
2021-12-21 20:39:55 -07:00
|
|
|
request, response = api_app.test_client.post('/api/valid_regex', content=json.dumps(data))
|
2021-12-20 16:08:13 -07:00
|
|
|
assert response.status_code == HTTPStatus.BAD_REQUEST
|
|
|
|
|
assert json.loads(response.body) == {'valid': False, 'regex': '.*(missing parenthesis.*'}
|
|
|
|
|
|
2022-07-09 18:51:45 -06:00
|
|
|
@wrap_test_db
|
2021-12-20 16:08:13 -07:00
|
|
|
def test_get_settings(self):
|
2022-06-28 10:09:35 -06:00
|
|
|
with mock.patch('wrolpi.admin.CPUFREQ_INFO_BIN', '/usr/bin/cpufreq-info'), \
|
|
|
|
|
mock.patch('wrolpi.admin.NMCLI_BIN', '/usr/bin/nmcli'):
|
2022-03-19 15:18:40 -06:00
|
|
|
request, response = api_app.test_client.get('/api/settings')
|
|
|
|
|
self.assertOK(response)
|
2021-12-20 16:08:13 -07:00
|
|
|
|
|
|
|
|
@wrap_test_db
|
|
|
|
|
def test_get_downloads(self):
|
|
|
|
|
request, response = api_app.test_client.get('/api/download')
|
|
|
|
|
self.assertDictContains(response.json, {'once_downloads': [], 'recurring_downloads': []})
|
|
|
|
|
|
|
|
|
|
with get_db_session(commit=True) as session:
|
|
|
|
|
d1 = Download(url='https://example.com/1', status='complete')
|
|
|
|
|
d2 = Download(url='https://example.com/2', status='pending')
|
|
|
|
|
d3 = Download(url='https://example.com/3', status='deferred')
|
|
|
|
|
d4 = Download(url='https://example.com/4', status='deferred')
|
|
|
|
|
d5 = Download(url='https://example.com/5', status='new')
|
|
|
|
|
d6 = Download(url='https://example.com/6', status='deferred', frequency=60)
|
|
|
|
|
d7 = Download(url='https://example.com/7', status='pending', frequency=60)
|
|
|
|
|
session.add_all([d1, d2, d3, d4, d5, d6, d7])
|
|
|
|
|
|
|
|
|
|
request, response = api_app.test_client.get('/api/download')
|
|
|
|
|
expected = [
|
|
|
|
|
{'url': 'https://example.com/2', 'status': 'pending'},
|
|
|
|
|
{'url': 'https://example.com/5', 'status': 'new'},
|
|
|
|
|
{'url': 'https://example.com/3', 'status': 'deferred'},
|
|
|
|
|
{'url': 'https://example.com/4', 'status': 'deferred'},
|
|
|
|
|
{'url': 'https://example.com/1', 'status': 'complete'},
|
|
|
|
|
]
|
|
|
|
|
for download, expected in zip_longest(response.json['once_downloads'], expected):
|
|
|
|
|
self.assertDictContains(download, expected)
|
|
|
|
|
expected = [
|
|
|
|
|
{'url': 'https://example.com/7', 'status': 'pending'},
|
|
|
|
|
{'url': 'https://example.com/6', 'status': 'deferred'},
|
|
|
|
|
]
|
|
|
|
|
for download, expected in zip_longest(response.json['recurring_downloads'], expected):
|
|
|
|
|
self.assertDictContains(download, expected)
|
|
|
|
|
|
|
|
|
|
@wrap_test_db
|
|
|
|
|
def test_downloads_sorter(self):
|
|
|
|
|
with get_db_session(commit=True) as session:
|
|
|
|
|
downloads = [
|
2023-01-03 08:53:00 -07:00
|
|
|
dict(status='complete', last_successful_download='2020-01-01T00:00:01+00:00'),
|
|
|
|
|
dict(status='complete', last_successful_download='2020-01-01T00:00:03+00:00'),
|
|
|
|
|
dict(status='complete', last_successful_download='2020-01-01T00:00:02+00:00'),
|
2021-12-20 16:08:13 -07:00
|
|
|
# last_successful_download are equal, so id is used next.
|
2023-01-03 08:53:00 -07:00
|
|
|
dict(status='pending', last_successful_download='2020-01-01T00:00:01+00:00', url='1'),
|
|
|
|
|
dict(status='pending', last_successful_download='2020-01-01T00:00:01+00:00', url='2'),
|
2021-12-20 16:08:13 -07:00
|
|
|
|
2023-01-03 08:53:00 -07:00
|
|
|
dict(status='pending', last_successful_download='2020-01-01T00:00:04+00:00'),
|
2021-12-20 16:08:13 -07:00
|
|
|
dict(status='failed'),
|
2023-01-03 08:53:00 -07:00
|
|
|
dict(status='failed', last_successful_download='2020-01-01T00:00:01+00:00'),
|
2021-12-20 16:08:13 -07:00
|
|
|
]
|
|
|
|
|
for download in downloads:
|
|
|
|
|
session.add(Download(url=download.pop('url', 'https://example.com'), **download))
|
|
|
|
|
|
|
|
|
|
expected = [
|
2023-01-03 08:53:00 -07:00
|
|
|
dict(status='pending', last_successful_download='2020-01-01T00:00:04+00:00'),
|
|
|
|
|
dict(status='pending', last_successful_download='2020-01-01T00:00:01+00:00', url='1'),
|
|
|
|
|
dict(status='pending', last_successful_download='2020-01-01T00:00:01+00:00', url='2'),
|
2021-12-20 16:08:13 -07:00
|
|
|
dict(status='failed'),
|
2023-01-03 08:53:00 -07:00
|
|
|
dict(status='failed', last_successful_download='2020-01-01T00:00:01+00:00'),
|
|
|
|
|
dict(status='complete', last_successful_download='2020-01-01T00:00:03+00:00'),
|
|
|
|
|
dict(status='complete', last_successful_download='2020-01-01T00:00:02+00:00'),
|
|
|
|
|
dict(status='complete', last_successful_download='2020-01-01T00:00:01+00:00'),
|
2021-12-20 16:08:13 -07:00
|
|
|
]
|
|
|
|
|
request, response = api_app.test_client.get('/api/download')
|
|
|
|
|
once_downloads = response.json['once_downloads']
|
|
|
|
|
for d1, d2 in zip_longest(expected, once_downloads):
|
|
|
|
|
self.assertDictContains(d2, d1)
|
|
|
|
|
|
|
|
|
|
@wrap_test_db
|
|
|
|
|
def test_downloads_sorter_recurring(self):
|
|
|
|
|
with get_db_session(commit=True) as session:
|
|
|
|
|
downloads = [
|
|
|
|
|
dict(status='complete', frequency=1),
|
2023-01-03 08:53:00 -07:00
|
|
|
dict(status='complete', frequency=2, next_download='2020-01-01T00:00:01+00:00'),
|
|
|
|
|
dict(status='complete', frequency=2, next_download='2020-01-01T00:00:02+00:00'),
|
2021-12-20 16:08:13 -07:00
|
|
|
dict(status='pending', frequency=1),
|
|
|
|
|
dict(status='pending', frequency=4),
|
|
|
|
|
dict(status='failed', frequency=1),
|
|
|
|
|
dict(status='failed', frequency=1),
|
|
|
|
|
]
|
|
|
|
|
for download in downloads:
|
|
|
|
|
session.add(Download(url='https://example.com', **download))
|
|
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
|
dict(status='pending', frequency=1),
|
|
|
|
|
dict(status='pending', frequency=4),
|
|
|
|
|
dict(status='failed', frequency=1),
|
|
|
|
|
dict(status='failed'),
|
2023-01-03 08:53:00 -07:00
|
|
|
dict(status='complete', frequency=2, next_download='2020-01-01T00:00:01+00:00'),
|
|
|
|
|
dict(status='complete', frequency=2, next_download='2020-01-01T00:00:02+00:00'),
|
2021-12-20 16:08:13 -07:00
|
|
|
dict(status='complete', frequency=1),
|
|
|
|
|
]
|
|
|
|
|
request, response = api_app.test_client.get('/api/download')
|
|
|
|
|
recurring_downloads = response.json['recurring_downloads']
|
|
|
|
|
for d1, d2 in zip_longest(expected, recurring_downloads):
|
|
|
|
|
self.assertDictContains(d2, d1)
|
2022-02-16 21:57:27 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_hotspot_settings(test_session, test_client, test_config):
|
|
|
|
|
"""
|
|
|
|
|
The User can toggle the Hotspot via /settings. The Hotspot can be automatically started on startup.
|
|
|
|
|
"""
|
|
|
|
|
config = get_config()
|
|
|
|
|
assert config.hotspot_on_startup is True
|
|
|
|
|
|
|
|
|
|
with mock.patch('wrolpi.root_api.admin') as mock_admin:
|
|
|
|
|
# Turning on the hotspot succeeds.
|
|
|
|
|
mock_admin.enable_hotspot.return_value = True
|
|
|
|
|
request, response = test_client.patch('/api/settings', content=json.dumps({'hotspot_status': True}))
|
|
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT, response.json
|
|
|
|
|
mock_admin.enable_hotspot.assert_called_once()
|
|
|
|
|
mock_admin.reset_mock()
|
|
|
|
|
|
|
|
|
|
# Turning on the hotspot fails.
|
|
|
|
|
mock_admin.enable_hotspot.return_value = False
|
|
|
|
|
request, response = test_client.patch('/api/settings', content=json.dumps({'hotspot_status': True}))
|
|
|
|
|
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR, response.json
|
|
|
|
|
assert response.json['code'] == 35
|
|
|
|
|
mock_admin.enable_hotspot.assert_called_once()
|
|
|
|
|
|
|
|
|
|
# Turning off the hotspot succeeds.
|
|
|
|
|
mock_admin.disable_hotspot.return_value = True
|
|
|
|
|
request, response = test_client.patch('/api/settings', content=json.dumps({'hotspot_status': False}))
|
|
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT, response.json
|
|
|
|
|
mock_admin.disable_hotspot.assert_called_once()
|
|
|
|
|
mock_admin.reset_mock()
|
|
|
|
|
|
|
|
|
|
# Turning off the hotspot succeeds.
|
|
|
|
|
mock_admin.disable_hotspot.return_value = False
|
|
|
|
|
request, response = test_client.patch('/api/settings', content=json.dumps({'hotspot_status': False}))
|
|
|
|
|
assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR, response.json
|
|
|
|
|
assert response.json['code'] == 35
|
|
|
|
|
mock_admin.disable_hotspot.assert_called_once()
|
|
|
|
|
|
2022-05-18 17:24:50 -06:00
|
|
|
mock_admin.enable_hotspot.reset_mock()
|
|
|
|
|
mock_admin.enable_hotspot.return_value = True
|
|
|
|
|
mock_admin.hotspot_status.return_value = HotspotStatus.connected
|
2022-02-16 21:57:27 -07:00
|
|
|
|
2022-04-30 12:20:49 -06:00
|
|
|
# Hotspot password can be changed.
|
|
|
|
|
mock_admin.disable_hotspot.return_value = True
|
|
|
|
|
content = {'hotspot_password': 'new password', 'hotspot_ssid': 'new ssid'}
|
|
|
|
|
request, response = test_client.patch('/api/settings', content=json.dumps(content))
|
|
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
|
|
|
|
assert config.hotspot_password == 'new password'
|
|
|
|
|
assert config.hotspot_ssid == 'new ssid'
|
2022-05-18 17:24:50 -06:00
|
|
|
# Changing the password restarts the hotspot.
|
|
|
|
|
mock_admin.enable_hotspot.assert_called_once()
|
2022-04-30 12:20:49 -06:00
|
|
|
|
2022-02-16 21:57:27 -07:00
|
|
|
|
2022-04-07 11:17:53 -06:00
|
|
|
@skip_circleci
|
2022-02-16 21:57:27 -07:00
|
|
|
def test_throttle_toggle(test_session, test_client, test_config):
|
2022-04-01 16:45:59 -06:00
|
|
|
with mock.patch('wrolpi.admin.subprocess') as mock_subprocess, \
|
2022-06-28 10:09:35 -06:00
|
|
|
mock.patch('wrolpi.admin.CPUFREQ_INFO_BIN', "this value isn't even used"):
|
2022-02-16 21:57:27 -07:00
|
|
|
mock_subprocess.check_output.side_effect = [
|
|
|
|
|
b'wlan0: unavailable',
|
|
|
|
|
b'The governor "ondemand" may decide ',
|
|
|
|
|
]
|
|
|
|
|
request, response = test_client.get('/api/settings')
|
|
|
|
|
|
|
|
|
|
# Throttle is off by default.
|
|
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
assert response.json['throttle_on_startup'] is False
|
|
|
|
|
assert response.json['throttle_status'] == 'ondemand'
|
2022-04-01 16:31:49 -06:00
|
|
|
|
|
|
|
|
|
2022-05-20 15:15:31 -06:00
|
|
|
def test_clear_downloads(test_session, test_client, test_config, test_download_manager_config):
|
2022-04-01 16:31:49 -06:00
|
|
|
with get_db_session(commit=True) as session:
|
|
|
|
|
d1 = Download(url='https://example.com/1', status='complete')
|
|
|
|
|
d2 = Download(url='https://example.com/2', status='pending')
|
|
|
|
|
d3 = Download(url='https://example.com/3', status='deferred')
|
|
|
|
|
d4 = Download(url='https://example.com/4', status='new')
|
2022-04-01 16:45:59 -06:00
|
|
|
d5 = Download(url='https://example.com/5', status='failed')
|
2022-04-02 16:01:43 -06:00
|
|
|
d6 = Download(url='https://example.com/6', status='failed', frequency=60)
|
2022-04-07 11:17:53 -06:00
|
|
|
d7 = Download(url='https://example.com/7', status='complete', frequency=60)
|
|
|
|
|
session.add_all([d1, d2, d3, d4, d5, d6, d7])
|
2022-04-02 16:01:43 -06:00
|
|
|
|
|
|
|
|
def check_downloads(response_, once_downloads_, recurring_downloads_, status_code=None):
|
|
|
|
|
if status_code:
|
|
|
|
|
assert response_.status_code == status_code
|
|
|
|
|
|
|
|
|
|
for download, once_download in zip_longest(response_.json['once_downloads'], once_downloads_):
|
|
|
|
|
assert download
|
|
|
|
|
assert once_download
|
|
|
|
|
assert download['url'] == once_download['url']
|
|
|
|
|
assert download['status'] == once_download['status']
|
|
|
|
|
for download, recurring_download in zip_longest(response_.json['recurring_downloads'], recurring_downloads_):
|
|
|
|
|
assert download
|
|
|
|
|
assert recurring_download
|
|
|
|
|
assert download['url'] == recurring_download['url']
|
|
|
|
|
assert download['status'] == recurring_download['status']
|
|
|
|
|
|
|
|
|
|
# All created downloads are present.
|
2022-04-01 16:31:49 -06:00
|
|
|
request, response = api_app.test_client.get('/api/download')
|
2022-04-02 16:01:43 -06:00
|
|
|
once_downloads = [
|
2022-04-01 16:31:49 -06:00
|
|
|
{'url': 'https://example.com/2', 'status': 'pending'},
|
2022-04-01 16:45:59 -06:00
|
|
|
{'url': 'https://example.com/5', 'status': 'failed'},
|
2022-04-01 16:31:49 -06:00
|
|
|
{'url': 'https://example.com/4', 'status': 'new'},
|
|
|
|
|
{'url': 'https://example.com/3', 'status': 'deferred'},
|
|
|
|
|
{'url': 'https://example.com/1', 'status': 'complete'},
|
|
|
|
|
]
|
2022-04-07 11:17:53 -06:00
|
|
|
recurring_downloads = [
|
|
|
|
|
{'url': 'https://example.com/6', 'status': 'failed'},
|
|
|
|
|
{'url': 'https://example.com/7', 'status': 'complete'},
|
|
|
|
|
]
|
2022-04-02 16:01:43 -06:00
|
|
|
check_downloads(response, once_downloads, recurring_downloads, status_code=HTTPStatus.OK)
|
2022-04-01 16:31:49 -06:00
|
|
|
|
2022-04-07 11:17:53 -06:00
|
|
|
# Once "complete" download is removed.
|
2022-04-01 16:31:49 -06:00
|
|
|
request, response = api_app.test_client.post('/api/download/clear_completed')
|
|
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
2022-04-01 16:45:59 -06:00
|
|
|
request, response = api_app.test_client.get('/api/download')
|
2022-04-02 16:01:43 -06:00
|
|
|
once_downloads = [
|
2022-04-01 16:45:59 -06:00
|
|
|
{'url': 'https://example.com/2', 'status': 'pending'},
|
|
|
|
|
{'url': 'https://example.com/5', 'status': 'failed'},
|
|
|
|
|
{'url': 'https://example.com/4', 'status': 'new'},
|
|
|
|
|
{'url': 'https://example.com/3', 'status': 'deferred'},
|
|
|
|
|
]
|
2022-04-02 16:01:43 -06:00
|
|
|
check_downloads(response, once_downloads, recurring_downloads, status_code=HTTPStatus.OK)
|
2022-04-01 16:45:59 -06:00
|
|
|
|
2022-04-02 16:01:43 -06:00
|
|
|
# Failed "once" download is removed, recurring failed is not removed.
|
2022-04-01 16:45:59 -06:00
|
|
|
request, response = api_app.test_client.post('/api/download/clear_failed')
|
|
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
2022-04-01 16:31:49 -06:00
|
|
|
request, response = api_app.test_client.get('/api/download')
|
2022-04-02 16:01:43 -06:00
|
|
|
once_downloads = [
|
2022-04-01 16:31:49 -06:00
|
|
|
{'url': 'https://example.com/2', 'status': 'pending'},
|
|
|
|
|
{'url': 'https://example.com/4', 'status': 'new'},
|
|
|
|
|
{'url': 'https://example.com/3', 'status': 'deferred'},
|
|
|
|
|
]
|
2022-04-02 16:01:43 -06:00
|
|
|
check_downloads(response, once_downloads, recurring_downloads, status_code=HTTPStatus.OK)
|
|
|
|
|
|
|
|
|
|
# Failed once-downloads will not be downloaded again.
|
2023-04-05 10:40:16 -06:00
|
|
|
assert get_download_manager_config().skip_urls == ['https://example.com/5', ]
|
2022-06-30 16:22:57 -06:00
|
|
|
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
def test_get_status(test_client, test_session):
|
2022-06-30 16:22:57 -06:00
|
|
|
"""Get the server status information."""
|
|
|
|
|
request, response = test_client.get('/api/status')
|
|
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
assert 'cpu_info' in response.json and isinstance(response.json['cpu_info'], dict)
|
|
|
|
|
assert 'load' in response.json and isinstance(response.json['load'], dict)
|
|
|
|
|
assert 'drives' in response.json and isinstance(response.json['drives'], list)
|
2022-10-16 11:53:44 -06:00
|
|
|
assert 'downloads' in response.json and isinstance(response.json['downloads'], dict)
|
|
|
|
|
assert 'hotspot_status' in response.json and isinstance(response.json['hotspot_status'], str)
|
|
|
|
|
assert 'throttle_status' in response.json and isinstance(response.json['throttle_status'], str)
|
|
|
|
|
assert 'version' in response.json and isinstance(response.json['version'], str)
|
2022-10-19 09:18:53 -06:00
|
|
|
assert 'memory_stats' in response.json and isinstance(response.json['memory_stats'], dict)
|
2022-12-23 16:04:09 -07:00
|
|
|
assert 'flags' in response.json and isinstance(response.json['flags'], list)
|
2022-08-06 05:22:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_post_download(test_session, test_client, test_download_manager_config):
|
|
|
|
|
"""Test creating once-downlaods and recurring downloads."""
|
|
|
|
|
|
|
|
|
|
async def queue_downloads(*a, **kw):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
with mock.patch('wrolpi.downloader.DownloadManager.queue_downloads', queue_downloads):
|
|
|
|
|
# Create a single recurring Download.
|
|
|
|
|
content = dict(
|
|
|
|
|
urls='https://example.com/1\n',
|
|
|
|
|
frequency=1_000,
|
|
|
|
|
downloader='archive',
|
|
|
|
|
)
|
|
|
|
|
request, response = test_client.post('/api/download', content=json.dumps(content))
|
|
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
|
|
|
|
|
|
|
|
|
assert {i.url for i in test_session.query(Download)} == {'https://example.com/1', }
|
|
|
|
|
|
|
|
|
|
# Create to once-downloads.
|
|
|
|
|
content = dict(
|
|
|
|
|
urls='https://example.com/2\nhttps://example.com/3\n',
|
|
|
|
|
downloader='archive',
|
|
|
|
|
)
|
|
|
|
|
request, response = test_client.post('/api/download', content=json.dumps(content))
|
|
|
|
|
assert response.status_code == HTTPStatus.NO_CONTENT
|
|
|
|
|
|
|
|
|
|
assert {i.url for i in test_session.query(Download)} == {f'https://example.com/{i}' for i in range(1, 4)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_downloaders(test_client):
|
|
|
|
|
"""A list of Downloaders the user can use can be gotten."""
|
|
|
|
|
request, response = test_client.get('/api/downloaders')
|
|
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
assert 'downloaders' in response.json, 'Downloaders not returned'
|
|
|
|
|
assert isinstance(response.json['downloaders'], list) and len(response.json['downloaders']), \
|
|
|
|
|
'No downloaders returned'
|
2022-12-16 11:10:01 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_global_statistics(test_session, test_client):
|
|
|
|
|
request, response = test_client.get('/api/statistics')
|
|
|
|
|
assert response.json['global_statistics']['db_size'] > 1
|
2023-06-11 12:31:56 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_post_vin_number_decoder(test_client):
|
|
|
|
|
"""A VIN number can be decoded."""
|
|
|
|
|
# Invalid VIN number.
|
|
|
|
|
content = dict(vin_number='5N1NJ01CXST00000')
|
|
|
|
|
request, response = test_client.post('/api/vin_number_decoder', content=json.dumps(content))
|
|
|
|
|
assert response.status_code == HTTPStatus.BAD_REQUEST
|
|
|
|
|
assert response.json == dict()
|
|
|
|
|
|
|
|
|
|
# Test VIN from vininfo.
|
|
|
|
|
content = dict(vin_number='5N1NJ01CXST000001')
|
|
|
|
|
request, response = test_client.post('/api/vin_number_decoder', content=json.dumps(content))
|
|
|
|
|
assert response.status_code == HTTPStatus.OK
|
|
|
|
|
assert response.json['vin'] == {'body': 'Sedan, 4-Door, Standard Body Truck',
|
|
|
|
|
'country': 'United States',
|
|
|
|
|
'engine': 'VH45DE',
|
|
|
|
|
'manufacturer': 'Nissan',
|
|
|
|
|
'model': 'Maxima',
|
|
|
|
|
'plant': 'Tochigi, Oppama',
|
|
|
|
|
'region': 'North America',
|
|
|
|
|
'serial': None,
|
|
|
|
|
'transmission': None,
|
|
|
|
|
'years': '1995'}
|