Skip to content

Commit

Permalink
tests: add tests for errors in xml upload
Browse files Browse the repository at this point in the history
  • Loading branch information
MyPyDavid committed Feb 8, 2024
1 parent 1ea58fa commit 8995ac2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
5 changes: 3 additions & 2 deletions rdmo/core/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
}


def read_xml_file(file_name):
def read_xml_file(file_name, raise_exception=False):
try:
return ET.parse(file_name).getroot()
except Exception as e:
log.error('Xml parsing error: ' + str(e))
raise e from e
if raise_exception:
raise e from e


def parse_xml_string(string):
Expand Down
21 changes: 16 additions & 5 deletions rdmo/management/tests/test_viewset_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
'list': 'v1-management:upload-list'
}

xml_error_files = [
('file-does-not-exist.xml', 'may not be blank'),
('xml/error.xml', 'syntax error'),
('xml/error-version.xml', 'RDMO XML Version: 99'),
]

@pytest.mark.parametrize('username,password', users)
def test_list(db, client, username, password):
Expand Down Expand Up @@ -110,13 +115,19 @@ def test_create_empty(db, client, username, password):


@pytest.mark.parametrize('username,password', users)
def test_create_error(db, client, username, password):
@pytest.mark.parametrize('xml_file_path, error_message', xml_error_files)
def test_create_error(db, client, username, password, xml_file_path, error_message):
client.login(username=username, password=password)

xml_file = Path(settings.BASE_DIR) / 'xml' / 'error.xml'

xml_file = Path(settings.BASE_DIR).joinpath(xml_file_path)
url = reverse(urlnames['list'])
with open(xml_file, encoding='utf8') as f:
response = client.post(url, {'file': f})
try:
with open(xml_file, encoding='utf8') as f:
response = client.post(url, {'file': f})
except FileNotFoundError:
response = client.post(url)

assert response.status_code == status_map['create_error'][username], response.json()
if response.status_code == 400:
response_msg = ",".join(response.json()['file'])
assert error_message in response_msg
2 changes: 1 addition & 1 deletion rdmo/management/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create(self, request, *args, **kwargs):

# step 2: parse xml
try:
root = read_xml_file(import_tmpfile_name)
root = read_xml_file(import_tmpfile_name, raise_exception=True)
except Exception as e:
logger.info('XML parsing error. Import failed.')
raise ValidationError({'file': [
Expand Down
3 changes: 3 additions & 0 deletions testing/xml/error-version.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<rdmo xmlns:dc="http://purl.org/dc/elements/1.1/" created="2023-04-20T09:39:10.351808+02:00" version="99.9.9">
</rdmo>

0 comments on commit 8995ac2

Please sign in to comment.