summaryrefslogtreecommitdiff
path: root/yaksh/test_views.py
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/test_views.py')
-rw-r--r--yaksh/test_views.py137
1 files changed, 137 insertions, 0 deletions
diff --git a/yaksh/test_views.py b/yaksh/test_views.py
index dc06126..e27d5ef 100644
--- a/yaksh/test_views.py
+++ b/yaksh/test_views.py
@@ -1507,6 +1507,8 @@ class TestCourses(TestCase):
self.user1_course.delete()
self.user2_course.delete()
+
+
def test_courses_denies_anonymous(self):
"""
If not logged in redirect to login page
@@ -1758,6 +1760,141 @@ class TestCourseDetail(TestCase):
self.student.delete()
self.user1_course.delete()
+
+ def test_upload_users_with_correct_csv(self):
+ # Given
+ self.client.login(
+ username=self.user1.username,
+ password=self.user1_plaintext_pass
+ )
+ csv_file_path = os.path.join(settings.FIXTURE_DIRS, "users_correct.csv")
+ csv_file = open(csv_file_path, 'rb')
+ upload_file = SimpleUploadedFile(csv_file_path, csv_file.read())
+
+ # When
+ response = self.client.post(reverse('yaksh:upload_users',
+ kwargs={'course_id': self.user1_course.id}),
+ data={'csv_file': upload_file})
+ csv_file.close()
+
+ # Then
+ uploaded_user = User.objects.filter(email="abc@xyz.com")
+ self.assertEqual(uploaded_user.count(), 1)
+ self.assertEqual(response.status_code, 200)
+ self.assertIn('upload_details', response.context)
+ self.assertTemplateUsed(response, 'yaksh/course_detail.html')
+
+
+ def test_upload_users_with_wrong_csv(self):
+ # Given
+ self.client.login(
+ username=self.user1.username,
+ password=self.user1_plaintext_pass
+ )
+ csv_file_path = os.path.join(settings.FIXTURE_DIRS, "demo_questions.zip")
+ csv_file = open(csv_file_path, 'rb')
+ upload_file = SimpleUploadedFile(csv_file_path, csv_file.read())
+ message = "The file uploaded is not a CSV file."
+
+ # When
+ response = self.client.post(reverse('yaksh:upload_users',
+ kwargs={'course_id': self.user1_course.id}),
+ data={'csv_file': upload_file})
+ csv_file.close()
+
+ # Then
+ self.assertEqual(response.status_code, 200)
+ self.assertNotIn('upload_details', response.context)
+ self.assertIn('message', response.context)
+ self.assertEqual(response.context['message'], message)
+ self.assertTemplateUsed(response, 'yaksh/course_detail.html')
+
+
+ def test_upload_users_csv_with_missing_headers(self):
+ # Given
+ self.client.login(
+ username=self.user1.username,
+ password=self.user1_plaintext_pass
+ )
+ csv_file_path = os.path.join(settings.FIXTURE_DIRS, "users_some_headers_missing.csv")
+ csv_file = open(csv_file_path, 'rb')
+ upload_file = SimpleUploadedFile(csv_file_path, csv_file.read())
+ message = "The CSV file does not contain the required headers"
+
+ # When
+ response = self.client.post(reverse('yaksh:upload_users',
+ kwargs={'course_id': self.user1_course.id}),
+ data={'csv_file': upload_file})
+ csv_file.close()
+
+ # Then
+ self.assertEqual(response.status_code, 200)
+ self.assertNotIn('upload_details', response.context)
+ self.assertIn('message', response.context)
+ self.assertEqual(response.context['message'], message)
+ self.assertTemplateUsed(response, 'yaksh/course_detail.html')
+
+ def test_upload_users_csv_with_no_values(self):
+ # Given
+ self.client.login(
+ username=self.user1.username,
+ password=self.user1_plaintext_pass
+ )
+ csv_file_path = os.path.join(settings.FIXTURE_DIRS, "users_with_no_values.csv")
+ csv_file = open(csv_file_path, 'rb')
+ upload_file = SimpleUploadedFile(csv_file_path, csv_file.read())
+
+ # When
+ response = self.client.post(reverse('yaksh:upload_users',
+ kwargs={'course_id': self.user1_course.id}),
+ data={'csv_file': upload_file})
+ csv_file.close()
+
+ # Then
+ self.assertEqual(response.status_code, 200)
+ self.assertIn('upload_details', response.context)
+ self.assertNotIn('message', response.context)
+ self.assertIn("No rows in the CSV file", response.context['upload_details'])
+ self.assertTemplateUsed(response, 'yaksh/course_detail.html')
+
+
+ def test_upload_users_csv_with_missing_values(self):
+ '''
+ This test takes csv with 3 row values.
+ 1st row has a missing row.
+ 2nd has a proper row.
+ 3rd has a same row has 2nd
+
+ Only 2nd user will be added.
+
+ This test proves that:
+ - Row with missing values is ignored and continued with next row.
+ - Duplicate user is not created.
+ '''
+ # Given
+ self.client.login(
+ username=self.user1.username,
+ password=self.user1_plaintext_pass
+ )
+ csv_file_path = os.path.join(settings.FIXTURE_DIRS, "users_some_values_missing.csv")
+ csv_file = open(csv_file_path, 'rb')
+ upload_file = SimpleUploadedFile(csv_file_path, csv_file.read())
+
+ # When
+ response = self.client.post(reverse('yaksh:upload_users',
+ kwargs={'course_id': self.user1_course.id}),
+ data={'csv_file': upload_file})
+ csv_file.close()
+
+ # Then
+ uploaded_user = User.objects.filter(email="dummy@xyz.com")
+ self.assertEqual(uploaded_user.count(), 1)
+ self.assertEqual(response.status_code, 200)
+ self.assertIn('upload_details', response.context)
+ self.assertNotIn('message', response.context)
+ self.assertTemplateUsed(response, 'yaksh/course_detail.html')
+
+
def test_course_detail_denies_anonymous(self):
"""
If not logged in redirect to login page