Esempio n. 1
0
    def test_ignored_duplicate_warning(self):
        __, __, warnings_test, __ = UserImporter.process(self.duplicate_excel_content, test_run=True)
        __, __, warnings_no_test, __ = UserImporter.process(self.duplicate_excel_content, test_run=False)

        self.assertEqual(warnings_test, warnings_no_test)
        self.assertEqual(warnings_test[ImporterWarning.IGNORED], [
            "The duplicated row 4 in sheet 'Users' was ignored. It was first found in sheet 'Users' on row 3."])
Esempio n. 2
0
    def test_import_makes_inactive_user_active(self):
        user = baker.make(UserProfile,
                          email="*****@*****.**",
                          is_active=False)

        __, __, warnings_test, __ = UserImporter.process(
            self.valid_excel_content, test_run=True)
        self.assertEqual(
            warnings_test[ImporterWarning.INACTIVE],
            [
                "The following user is currently marked inactive and will be marked active upon importing: "
                f" None None, [email protected] [{user_edit_link(user.pk)}]",
            ],
        )

        __, __, warnings_no_test, __ = UserImporter.process(
            self.valid_excel_content, test_run=False)
        self.assertEqual(
            warnings_no_test[ImporterWarning.INACTIVE],
            [
                "The following user was previously marked inactive and is now marked active upon importing: "
                f" None None, [email protected] [{user_edit_link(user.pk)}]"
            ],
        )

        self.assertEqual(UserProfile.objects.count(), 2)
Esempio n. 3
0
    def test_email_mismatch_warning(self):
        mommy.make(UserProfile, email="*****@*****.**", username="******")

        __, __, warnings_test, __ = UserImporter.process(self.valid_excel_content, test_run=True)
        __, __, warnings_no_test, __ = UserImporter.process(self.valid_excel_content, test_run=False)
        self.assertEqual(warnings_test, warnings_no_test)
        self.assertIn("The existing user would be overwritten with the following data:<br>"
                " - lucilia.manilium ( None None, [email protected]) (existing)<br>"
                " - lucilia.manilium ( Lucilia Manilium, [email protected]) (new)",
                warnings_test[ExcelImporter.W_EMAIL])
Esempio n. 4
0
    def test_invalid_file_error(self):
        original_user_count = UserProfile.objects.count()

        __, __, __, errors_test = UserImporter.process(self.invalid_excel_content, test_run=True)
        __, __, __, errors_no_test = UserImporter.process(self.invalid_excel_content, test_run=False)

        self.assertEqual(errors_test, errors_no_test)
        self.assertIn('Sheet "Sheet1", row 2: Email address is missing.', errors_test)
        self.assertIn('Errors occurred while parsing the input data. No data was imported.', errors_test)
        self.assertEqual(UserProfile.objects.count(), original_user_count)
Esempio n. 5
0
    def test_random_file_error(self):
        original_user_count = UserProfile.objects.count()

        __, __, __, errors_test = UserImporter.process(self.random_excel_content, test_run=True)
        __, __, __, errors_no_test = UserImporter.process(self.random_excel_content, test_run=False)

        self.assertEqual(errors_test, errors_no_test)
        self.assertIn("Couldn't read the file. Error: Unsupported format, or corrupt file:"
                " Expected BOF record; found b'42\\n'", errors_test)
        self.assertEqual(UserProfile.objects.count(), original_user_count)
Esempio n. 6
0
    def test_invalid_file_error(self):
        original_user_count = UserProfile.objects.count()

        __, __, __, errors_test = UserImporter.process(self.invalid_excel_content, test_run=True)
        __, __, __, errors_no_test = UserImporter.process(self.invalid_excel_content, test_run=False)

        self.assertEqual(errors_test, errors_no_test)
        self.assertIn('Sheet "Sheet1", row 2: Email address is missing.', errors_test)
        self.assertIn('Errors occurred while parsing the input data. No data was imported.', errors_test)
        self.assertEqual(UserProfile.objects.count(), original_user_count)
Esempio n. 7
0
    def test_random_file_error(self):
        original_user_count = UserProfile.objects.count()

        __, __, __, errors_test = UserImporter.process(self.random_excel_content, test_run=True)
        __, __, __, errors_no_test = UserImporter.process(self.random_excel_content, test_run=False)

        self.assertEqual(errors_test, errors_no_test)
        self.assertEqual(errors_test[ImporterError.SCHEMA], [
            "Couldn't read the file. Error: Unsupported format, or corrupt file: Expected BOF record; found b'42\\n'"])
        self.assertEqual(UserProfile.objects.count(), original_user_count)
Esempio n. 8
0
    def test_duplicate_warning(self):
        baker.make(UserProfile, first_name='Lucilia', last_name="Manilium", email="*****@*****.**")

        __, __, warnings_test, __ = UserImporter.process(self.valid_excel_content, test_run=True)
        __, __, warnings_no_test, __ = UserImporter.process(self.valid_excel_content, test_run=False)

        self.assertEqual(warnings_test, warnings_no_test)
        self.assertEqual(warnings_test[ImporterWarning.DUPL], [
            "An existing user has the same first and last name as a new user:<br />"
            " -  Lucilia Manilium, [email protected] (existing)<br />"
            " -  Lucilia Manilium, [email protected] (new)"])
Esempio n. 9
0
    def test_test_and_notest_equality(self):
        # success messages are supposed to be different in a test and import run
        list_test, __, warnings_test, errors_test = UserImporter.process(self.valid_excel_content, test_run=True)
        list_notest, __, warnings_notest, errors_notest = UserImporter.process(self.valid_excel_content, test_run=False)

        notest_string_list = ["{} {}".format(user.full_name, user.email) for user in list_notest]
        test_string_list = ["{} {}".format(user.full_name, user.email) for user in list_test]

        self.assertEqual(notest_string_list, test_string_list)
        self.assertEqual(warnings_test, warnings_notest)
        self.assertEqual(errors_test, errors_notest)
Esempio n. 10
0
    def test_duplicate_warning(self):
        mommy.make(UserProfile, first_name='Lucilia', last_name="Manilium", username="******")

        __, __, warnings_test, __ = UserImporter.process(self.valid_excel_content, test_run=True)
        __, __, warnings_no_test, __ = UserImporter.process(self.valid_excel_content, test_run=False)

        self.assertEqual(warnings_test, warnings_no_test)
        self.assertIn("An existing user has the same first and last name as a new user:<br>"
                " - lucilia.manilium2 ( Lucilia Manilium, ) (existing)<br>"
                " - lucilia.manilium ( Lucilia Manilium, [email protected]) (new)",
                warnings_test[ExcelImporter.W_DUPL])
Esempio n. 11
0
    def test_test_and_notest_equality(self):
        # success messages are supposed to be different in a test and import run
        list_test, __, warnings_test, errors_test = UserImporter.process(self.valid_excel_content, test_run=True)
        list_notest, __, warnings_notest, errors_notest = UserImporter.process(self.valid_excel_content, test_run=False)

        notest_string_list = ["{} {}".format(user.full_name, user.email) for user in list_notest]
        test_string_list = ["{} {}".format(user.full_name, user.email) for user in list_test]

        self.assertEqual(notest_string_list, test_string_list)
        self.assertEqual(warnings_test, warnings_notest)
        self.assertEqual(errors_test, errors_notest)
Esempio n. 12
0
    def test_import_makes_inactive_user_active(self):
        mommy.make(UserProfile, username="******", is_active=False)

        __, __, warnings_test, __ = UserImporter.process(self.valid_excel_content, test_run=True)
        __, __, warnings_notest, __ = UserImporter.process(self.valid_excel_content, test_run=False)
        self.assertEqual(warnings_test, warnings_notest)

        self.assertIn("The following user is currently marked inactive and will be marked active upon importing: "
            "lucilia.manilium ( None None, )",
            warnings_test[ExcelImporter.W_INACTIVE])

        self.assertEqual(UserProfile.objects.count(), 2)
Esempio n. 13
0
    def test_random_file_error(self):
        original_user_count = UserProfile.objects.count()

        __, __, __, errors_test = UserImporter.process(self.random_excel_content, test_run=True)
        __, __, __, errors_no_test = UserImporter.process(self.random_excel_content, test_run=False)

        self.assertEqual(errors_test, errors_no_test)
        self.assertEqual(
            errors_test[ImporterError.SCHEMA],
            ["Couldn't read the file. Error: File is not a zip file"],
        )
        self.assertEqual(UserProfile.objects.count(), original_user_count)
Esempio n. 14
0
    def test_email_mismatch_warning(self):
        mommy.make(UserProfile, email="*****@*****.**", username="******")

        __, __, warnings_test, __ = UserImporter.process(
            self.valid_excel_content, test_run=True)
        __, __, warnings_no_test, __ = UserImporter.process(
            self.valid_excel_content, test_run=False)
        self.assertEqual(warnings_test, warnings_no_test)
        self.assertIn(
            "The existing user would be overwritten with the following data:<br>"
            " - lucilia.manilium ( None None, [email protected]) (existing)<br>"
            " - lucilia.manilium ( Lucilia Manilium, [email protected]) (new)",
            warnings_test[ExcelImporter.W_EMAIL])
Esempio n. 15
0
    def test_import_makes_inactive_user_active(self):
        mommy.make(UserProfile, email="*****@*****.**", is_active=False)

        __, __, warnings_test, __ = UserImporter.process(self.valid_excel_content, test_run=True)
        self.assertIn("The following user is currently marked inactive and will be marked active upon importing: "
                      " None None, [email protected]",
                      warnings_test[ExcelImporter.W_INACTIVE])

        __, __, warnings_no_test, __ = UserImporter.process(self.valid_excel_content, test_run=False)
        self.assertIn("The following user was previously marked inactive and is now marked active upon importing: "
            " None None, [email protected]",
            warnings_no_test[ExcelImporter.W_INACTIVE])

        self.assertEqual(UserProfile.objects.count(), 2)
Esempio n. 16
0
    def test_import_makes_inactive_user_active(self):
        mommy.make(UserProfile, username="******", is_active=False)

        __, __, warnings_test, __ = UserImporter.process(
            self.valid_excel_content, test_run=True)
        __, __, warnings_notest, __ = UserImporter.process(
            self.valid_excel_content, test_run=False)
        self.assertEqual(warnings_test, warnings_notest)

        self.assertIn(
            "The following user is currently marked inactive and will be marked active upon importing: "
            "lucilia.manilium ( None None, )",
            warnings_test[ExcelImporter.W_INACTIVE])

        self.assertEqual(UserProfile.objects.count(), 2)
Esempio n. 17
0
def course_import_participants(request, semester_id, course_id):
    course = get_object_or_404(Course, id=course_id)
    semester = get_object_or_404(Semester, id=semester_id)
    raise_permission_denied_if_archived(course)

    form = UserImportForm(request.POST or None, request.FILES or None)

    if form.is_valid():
        operation = request.POST.get('operation')
        if operation not in ('test', 'import'):
            raise SuspiciousOperation("Invalid POST operation")

        # Extract data from form.
        excel_file = form.cleaned_data['excel_file']

        test_run = operation == 'test'

        # Parse table.
        imported_users = UserImporter.process(request, excel_file, test_run)

        # Test run, or an error occurred while parsing -> stay and display error.
        if test_run or not imported_users:
            return render(request, "staff_import_participants.html",
                          dict(course=course, form=form))
        else:
            # Add users to course participants. * converts list into parameters.
            course.participants.add(*imported_users)
            messages.success(
                request, "%d Participants added to course %s" %
                (len(imported_users), course.name))
            return redirect('staff:semester_view', semester_id)
    else:
        return render(request, "staff_import_participants.html",
                      dict(course=course, form=form, semester=semester))
Esempio n. 18
0
def user_import(request):
    form = UserImportForm(request.POST or None, request.FILES or None)
    operation = request.POST.get('operation')

    if form.is_valid():
        if operation not in ('test', 'import'):
            raise SuspiciousOperation("Invalid POST operation")

        test_run = operation == 'test'
        excel_file = form.cleaned_data['excel_file']
        UserImporter.process(request, excel_file, test_run)
        if test_run:
            return render(request, "staff_user_import.html", dict(form=form))
        return redirect('staff:user_index')
    else:
        return render(request, "staff_user_import.html", dict(form=form))
Esempio n. 19
0
def user_import(request):
    form = UserImportForm(request.POST or None, request.FILES or None)
    operation = request.POST.get('operation')

    if form.is_valid():
        if operation not in ('test', 'import'):
            raise PermissionDenied

        test_run = operation == 'test'
        excel_file = form.cleaned_data['excel_file']
        UserImporter.process(request, excel_file, test_run)
        if test_run:
            return render(request, "staff_user_import.html", dict(form=form))
        return redirect('evap.staff.views.user_index')       
    else:
        return render(request, "staff_user_import.html", dict(form=form))
Esempio n. 20
0
    def test_random_file_error(self):
        original_user_count = UserProfile.objects.count()

        with open(self.filename_random, "rb") as excel_file:
            excel_content = excel_file.read()

        __, __, __, errors_test = UserImporter.process(excel_content,
                                                       test_run=True)
        __, __, __, errors_no_test = UserImporter.process(excel_content,
                                                          test_run=False)

        self.assertEqual(errors_test, errors_no_test)
        self.assertIn(
            "Couldn't read the file. Error: Unsupported format, or corrupt file:"
            " Expected BOF record; found b'42\\n'", errors_test)
        self.assertEqual(UserProfile.objects.count(), original_user_count)
Esempio n. 21
0
def user_import(request):
    form = UserImportForm(request.POST or None, request.FILES or None)
    operation = request.POST.get("operation")

    if form.is_valid():
        if operation not in ("test", "import"):
            raise PermissionDenied

        test_run = operation == "test"
        excel_file = form.cleaned_data["excel_file"]
        UserImporter.process(request, excel_file, test_run)
        if test_run:
            return render(request, "staff_user_import.html", dict(form=form))
        return redirect("staff:user_index")
    else:
        return render(request, "staff_user_import.html", dict(form=form))
Esempio n. 22
0
def user_import(request):
    form = UserImportForm(request.POST or None, request.FILES or None)
    operation = request.POST.get('operation')

    if form.is_valid():
        if operation not in ('test', 'import'):
            raise SuspiciousOperation("Invalid POST operation")

        test_run = operation == 'test'
        excel_file = form.cleaned_data['excel_file']
        UserImporter.process(request, excel_file, test_run)
        if test_run:
            return render(request, "staff_user_import.html", dict(form=form))
        return redirect('staff:user_index')
    else:
        return render(request, "staff_user_import.html", dict(form=form))
Esempio n. 23
0
    def test_test_run_does_not_change_database(self):
        original_users = list(UserProfile.objects.all())

        __, __, __, __ = UserImporter.process(self.valid_excel_content,
                                              test_run=True)

        self.assertEqual(original_users, list(UserProfile.objects.all()))
Esempio n. 24
0
def course_participant_import(request, semester_id, course_id):
    course = get_object_or_404(Course, id=course_id)
    semester = get_object_or_404(Semester, id=semester_id)
    raise_permission_denied_if_archived(course)

    form = UserImportForm(request.POST or None, request.FILES or None)

    if form.is_valid():
        operation = request.POST.get('operation')
        if operation not in ('test', 'import'):
            raise SuspiciousOperation("Invalid POST operation")

        # Extract data from form.
        excel_file = form.cleaned_data['excel_file']

        test_run = operation == 'test'

        # Parse table.
        imported_users = UserImporter.process(request, excel_file, test_run)

        # Test run, or an error occurred while parsing -> stay and display error.
        if test_run or not imported_users:
            return render(request, "staff_course_participant_import.html", dict(course=course, form=form))
        else:
            # Add users to course participants. * converts list into parameters.
            course.participants.add(*imported_users)
            messages.success(request, "%d Participants added to course %s" % (len(imported_users), course.name))
            return redirect('staff:semester_view', semester_id)
    else:
        return render(request, "staff_course_participant_import.html", dict(course=course, form=form, semester=semester))
Esempio n. 25
0
    def test_duplicate_warning(self):
        mommy.make(UserProfile,
                   first_name='Lucilia',
                   last_name="Manilium",
                   username="******")

        __, __, warnings_test, __ = UserImporter.process(
            self.valid_excel_content, test_run=True)
        __, __, warnings_no_test, __ = UserImporter.process(
            self.valid_excel_content, test_run=False)

        self.assertEqual(warnings_test, warnings_no_test)
        self.assertIn(
            "An existing user has the same first and last name as a new user:<br>"
            " - lucilia.manilium2 ( Lucilia Manilium, ) (existing)<br>"
            " - lucilia.manilium ( Lucilia Manilium, [email protected]) (new)",
            warnings_test[ExcelImporter.W_DUPL])
Esempio n. 26
0
 def test_unhandled_exception(self, mocked_db_access):
     mocked_db_access.side_effect = Exception("Contact your database admin right now!")
     result, __, __, errors = UserImporter.process(self.valid_excel_content, test_run=False)
     self.assertEqual(result, [])
     self.assertIn(
         "Import finally aborted after exception: 'Contact your database admin right now!'",
         errors[ImporterError.GENERAL],
     )
Esempio n. 27
0
 def test_disallow_non_string_types(self):
     imported_users, __, __, errors = UserImporter.process(self.numerical_excel_content, test_run=False)
     self.assertEqual(len(imported_users), 0)
     self.assertIn("The input data is malformed. No data was imported.", errors[ImporterError.GENERAL])
     # The sheet has a float in row 3 and an int row 4. All others rows only contain strings.
     self.assertSetEqual(
         {
             "Wrong data type in sheet 'Users' in row 3. Please make sure all cells are string types, not numerical.",
             "Wrong data type in sheet 'Users' in row 4. Please make sure all cells are string types, not numerical.",
         },
         set(errors[ImporterError.SCHEMA]),
     )
Esempio n. 28
0
    def test_created_users(self):
        original_user_count = UserProfile.objects.count()

        user_list, success_messages, warnings, errors = UserImporter.process(self.valid_excel_content, test_run=False)

        self.assertIn("Successfully read sheet 'Users'.", success_messages)
        self.assertIn('Successfully created 2 users:<br />Lucilia Manilium ([email protected])<br />Bastius Quid ([email protected])', success_messages)
        self.assertIn('Successfully read Excel file.', success_messages)
        self.assertEqual(warnings, {})
        self.assertEqual(errors, {})

        self.assertEqual(len(user_list), 2)
        self.assertEqual(UserProfile.objects.count(), 2 + original_user_count)
        self.assertTrue(isinstance(user_list[0], UserProfile))
        self.assertTrue(UserProfile.objects.filter(email="*****@*****.**").exists())
        self.assertTrue(UserProfile.objects.filter(email="*****@*****.**").exists())
Esempio n. 29
0
    def test_created_users(self):
        original_user_count = UserProfile.objects.count()

        user_list, v1, v2, v3 = UserImporter.process(self.valid_excel_content, test_run=False)

        self.assertIn("Successfully read sheet 'Users'.", v1)
        self.assertIn('Successfully created 2 user(s):<br>Lucilia Manilium (lucilia.manilium)<br>Bastius Quid (bastius.quid.ext)', v1)
        self.assertIn('Successfully read Excel file.', v1)
        self.assertEqual(v2, {})
        self.assertEqual(v3, [])

        self.assertEqual(len(user_list), 2)
        self.assertEqual(UserProfile.objects.count(), 2 + original_user_count)
        self.assertTrue(isinstance(user_list[0], UserProfile))
        self.assertTrue(UserProfile.objects.filter(email="*****@*****.**").exists())
        self.assertTrue(UserProfile.objects.filter(email="*****@*****.**").exists())
Esempio n. 30
0
    def test_created_users(self):
        original_user_count = UserProfile.objects.count()

        user_list, v1, v2, v3 = UserImporter.process(self.valid_excel_content,
                                                     test_run=False)

        self.assertIn("Successfully read sheet 'Users'.", v1)
        self.assertIn(
            'Successfully created 2 user(s):<br>Lucilia Manilium (lucilia.manilium)<br>Bastius Quid (bastius.quid.ext)',
            v1)
        self.assertIn('Successfully read Excel file.', v1)
        self.assertEqual(v2, {})
        self.assertEqual(v3, [])

        self.assertEqual(len(user_list), 2)
        self.assertEqual(UserProfile.objects.count(), 2 + original_user_count)
        self.assertTrue(isinstance(user_list[0], UserProfile))
        self.assertTrue(
            UserProfile.objects.filter(
                email="*****@*****.**").exists())
        self.assertTrue(
            UserProfile.objects.filter(
                email="*****@*****.**").exists())
Esempio n. 31
0
    def test_test_run_does_not_change_database(self):
        original_users = list(UserProfile.objects.all())

        __, __, __, __ = UserImporter.process(self.valid_excel_content, test_run=True)

        self.assertEqual(original_users, list(UserProfile.objects.all()))