From 8326e25207b0d2bfe0575d5e5d6e1c588eb29213 Mon Sep 17 00:00:00 2001 From: Julien BERNARD Date: Mon, 2 Oct 2023 09:25:42 -0400 Subject: [PATCH] Add test for label forms with invalid labels --- src/lgr_web/tests/test_label_forms.py | 21 ++++++++++++++++++--- src/lgr_web/views.py | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/lgr_web/tests/test_label_forms.py b/src/lgr_web/tests/test_label_forms.py index 1416482f..3e4ecaf8 100644 --- a/src/lgr_web/tests/test_label_forms.py +++ b/src/lgr_web/tests/test_label_forms.py @@ -32,6 +32,21 @@ def test_label_file_forms(self): response = self.client.post(url, {'labels-form-labels': labels_file}) self.assertEqual(response['Content-Type'], 'text/csv') - self.assertEqual(response.content.decode('utf-8-sig'), 'Code point sequence,U-label,A-label,Note\r\n' - 'U+0645 U+0643 U+062A U+0628,مكتب,xn--ngbd8eh,-\r\n' - 'U+00E9 U+0074 U+00E9,été,xn--t-9fab,-\r\n') + self.assertEqual(response.content.decode('utf-8-sig'), 'Input,Code point sequence,U-label,A-label,Note\r\n' + 'مكتب,U+0645 U+0643 U+062A U+0628,مكتب,xn--ngbd8eh,-\r\n' + 'été,U+00E9 U+0074 U+00E9,été,xn--t-9fab,-\r\n') + + def test_label_file_forms_invalid_label(self): + self.login_user() + + labels_file = SimpleUploadedFile('labels.txt', f'-invalid\nxn--abcd\nét é'.encode('utf-8'), + content_type='text/plain') + + url = reverse('lgr_label_file_forms') + response = self.client.post(url, {'labels-form-labels': labels_file}) + + self.assertEqual(response['Content-Type'], 'text/csv') + self.assertEqual(response.content.decode('utf-8-sig'), 'Input,Code point sequence,U-label,A-label,Note\r\n' + '-invalid,U+002D U+0069 U+006E U+0076 U+0061 U+006C U+0069 U+0064,-,-,-invalid is invalid due to hypen restrictions in the RFC5891 as it starts with a hyphen-minus.\r\n' + 'xn--abcd,U+0078 U+006E U+002D U+002D U+0061 U+0062 U+0063 U+0064,-,-,xn--abcd is invalid due to invalid Punycode.\r\n' + 'ét é,U+00E9 U+0074 U+0020 U+00E9,-,-,Label \'ét é\' contains spaces that are not PVALID for IDNA2008\r\n') diff --git a/src/lgr_web/views.py b/src/lgr_web/views.py index eb9f34b5..4062a307 100644 --- a/src/lgr_web/views.py +++ b/src/lgr_web/views.py @@ -117,7 +117,7 @@ def form_valid(self, form): error = e if not valid: - writer.writerow([label, format_cp(parsed_label), '-', '-', lgr_exception_to_text(error)]) + writer.writerow([label, format_cp(parsed_label) or '-', '-', '-', lgr_exception_to_text(error)]) return response