diff options
author | Prabhu Ramachandran | 2017-11-14 19:03:36 +0530 |
---|---|---|
committer | GitHub | 2017-11-14 19:03:36 +0530 |
commit | 1996f5d6409a5f936700a906f7da1afacb664ce6 (patch) | |
tree | 4a747aac76728a342dc9440b06eff3fb20c87a8f /yaksh/test_views.py | |
parent | a7afd20e83bff405baf643647afb16a4569481aa (diff) | |
parent | 29e358a6fcd438fa7ddf3d3bccb2af1839158ba6 (diff) | |
download | online_test-1996f5d6409a5f936700a906f7da1afacb664ce6.tar.gz online_test-1996f5d6409a5f936700a906f7da1afacb664ce6.tar.bz2 online_test-1996f5d6409a5f936700a906f7da1afacb664ce6.zip |
Merge pull request #378 from prathamesh920/upload_users_csv
Upload users csv
Diffstat (limited to 'yaksh/test_views.py')
-rw-r--r-- | yaksh/test_views.py | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 652f44c..2dddcef 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -1539,6 +1539,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 @@ -1790,6 +1792,163 @@ 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(FIXTURES_DIR_PATH, "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_add_update_reject(self): + # Given + self.client.login( + username=self.user1.username, + password=self.user1_plaintext_pass + ) + csv_file_path = os.path.join(FIXTURES_DIR_PATH, + "users_add_update_reject.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(username="test") + user = uploaded_user[0] + self.assertEqual(uploaded_user.count(), 1) + self.assertEqual(user.first_name, "test2") + self.assertIn(user, self.user1_course.get_rejected()) + 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(FIXTURES_DIR_PATH, "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(FIXTURES_DIR_PATH, "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(FIXTURES_DIR_PATH, "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(FIXTURES_DIR_PATH, "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 |