Skip to content
This repository has been archived by the owner on Sep 10, 2020. It is now read-only.

Commit

Permalink
[utils] Add error handling in csvfile validation #251
Browse files Browse the repository at this point in the history
Fixes #251
1.Add try except blocks to catch the error
2.Add test for the same

Add test for checking invalid file format

Add dummy pdf file
  • Loading branch information
Aviral14 committed Nov 20, 2019
1 parent 3b745b5 commit 975d939
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
27 changes: 17 additions & 10 deletions django_freeradius/tests/base/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
class BaseTestUtils(object):
def test_find_available_username(self):
User = get_user_model()
User.objects.create(username='rohith', password='password')
self.assertEqual(find_available_username('rohith', []), 'rohith1')
User.objects.create(username='rohith1', password='password')
self.assertEqual(find_available_username('rohith', []), 'rohith2')
User.objects.create(username="rohith", password="password")
self.assertEqual(find_available_username("rohith", []), "rohith1")
User.objects.create(username="rohith1", password="password")
self.assertEqual(find_available_username("rohith", []), "rohith2")

def test_validate_csvfile(self):
invalid_csv_path = self._get_path('static/test_batch_invalid.csv')
improper_csv_path = self._get_path('static/test_batch_improper.csv')
invalid_csv_path = self._get_path("static/test_batch_invalid.csv")
improper_csv_path = self._get_path("static/test_batch_improper.csv")
invalid_format_path = self._get_path("static/test_batch_invalid_format.pdf")
with self.assertRaises(ValidationError) as error:
validate_csvfile(open(invalid_csv_path, 'rt'))
self.assertTrue('Enter a valid email address' in error.exception.message)
validate_csvfile(open(invalid_format_path, "rb"))
self.assertTrue(
"Incorrect file format has been uploaded. Please Try Again!"
in error.exception.message
)
with self.assertRaises(ValidationError) as error:
validate_csvfile(open(improper_csv_path, 'rt'))
self.assertTrue('Improper CSV format' in error.exception.message)
validate_csvfile(open(invalid_csv_path, "rt"))
self.assertTrue("Enter a valid email address" in error.exception.message)
with self.assertRaises(ValidationError) as error:
validate_csvfile(open(improper_csv_path, "rt"))
self.assertTrue("Improper CSV format" in error.exception.message)
Binary file not shown.
5 changes: 4 additions & 1 deletion django_freeradius/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def find_available_username(username, users_list, prefix=False):

def validate_csvfile(csvfile):
csv_data = csvfile.read()
csv_data = csv_data.decode('utf-8') if isinstance(csv_data, bytes) else csv_data
try:
csv_data = csv_data.decode('utf-8') if isinstance(csv_data, bytes) else csv_data
except UnicodeDecodeError:
raise ValidationError("Incorrect file format has been uploaded. Please Try Again!")
reader = csv.reader(StringIO(csv_data), delimiter=',')
error_message = "The CSV contains a line with invalid data,\
line number {} triggered the following error: {}"
Expand Down

0 comments on commit 975d939

Please sign in to comment.