Skip to content

Commit 48762c0

Browse files
committed
add test for user bulk update
1 parent 876f619 commit 48762c0

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

evap/staff/tests/test_views.py

+30-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from abc import ABC, abstractmethod
55
from io import BytesIO
66
from typing import Literal
7-
from unittest.mock import Mock, PropertyMock, patch
7+
from unittest.mock import MagicMock, Mock, PropertyMock, patch
88

99
import openpyxl
1010
import xlrd
@@ -423,11 +423,11 @@ class TestUserBulkUpdateView(WebTestStaffMode):
423423
filename = os.path.join(settings.BASE_DIR, "staff/fixtures/test_user_bulk_update_file.txt")
424424

425425
@classmethod
426-
def setUpTestData(cls):
426+
def setUpTestData(cls) -> None:
427427
cls.random_excel_file_content = excel_data.random_file_content
428428
cls.manager = make_manager()
429429

430-
def test_testrun_deletes_no_users(self):
430+
def test_testrun_deletes_no_users(self) -> None:
431431
page = self.app.get(self.url, user=self.manager)
432432
form = page.forms["user-bulk-update-form"]
433433

@@ -443,7 +443,7 @@ def test_testrun_deletes_no_users(self):
443443
helper_delete_all_import_files(self.manager.id)
444444

445445
@override_settings(INSTITUTION_EMAIL_DOMAINS=["institution.example.com", "internal.example.com"])
446-
def test_multiple_email_matches_trigger_error(self):
446+
def test_multiple_email_matches_trigger_error(self) -> None:
447447
baker.make(UserProfile, email="[email protected]")
448448
baker.make(
449449
UserProfile, first_name_given="Elisabeth", last_name="Fröhlich", email="[email protected]"
@@ -483,24 +483,32 @@ def test_multiple_email_matches_trigger_error(self):
483483

484484
@override_settings(INSTITUTION_EMAIL_DOMAINS=["institution.example.com", "internal.example.com"])
485485
@patch("evap.staff.tools.remove_user_from_represented_and_ccing_users")
486-
def test_handles_users(self, mock_remove):
486+
def test_handles_users(self, mock_remove: MagicMock) -> None:
487487
mock_remove.return_value = ["This text is supposed to be visible on the website."]
488488
testuser1 = baker.make(UserProfile, email="[email protected]")
489489
testuser2 = baker.make(UserProfile, email="[email protected]")
490490
testuser1.delegates.set([testuser2])
491491
baker.make(UserProfile, email="[email protected]")
492-
contribution1 = baker.make(Contribution)
493492
semester = baker.make(Semester, participations_are_archived=True)
493+
course = baker.make(Course, semester=semester)
494+
responsible = baker.make(
495+
UserProfile, email="[email protected]", courses_responsible_for=[course]
496+
)
494497
evaluation = baker.make(
495498
Evaluation,
496-
course=baker.make(Course, semester=semester),
497-
_participant_count=0,
498-
_voter_count=0,
499+
course=course,
500+
participants=[responsible],
501+
voters=[responsible],
502+
vote_start_datetime=datetime.date(1900, 12, 1),
503+
vote_end_date=datetime.date(1900, 12, 1),
504+
_participant_count=1,
505+
_voter_count=1,
499506
)
500-
contribution2 = baker.make(Contribution, evaluation=evaluation)
501-
baker.make(UserProfile, email="[email protected]", contributions=[contribution1])
507+
baker.make(UserProfile, email="[email protected]", contributions=[baker.make(Contribution)])
502508
contributor2 = baker.make(
503-
UserProfile, email="[email protected]", contributions=[contribution2]
509+
UserProfile,
510+
511+
contributions=[baker.make(Contribution, evaluation=evaluation)],
504512
)
505513
testuser1.cc_users.set([contributor2])
506514

@@ -512,13 +520,15 @@ def test_handles_users(self, mock_remove):
512520
response = form.submit(name="operation", value="test")
513521

514522
self.assertIn(
515-
"1 will be updated, 1 will be deleted and 1 will be marked inactive. 1 new users will be created.", response
523+
"1 will be updated, 1 will be deleted and 2 will be marked inactive. 1 new users will be created.", response
516524
)
525+
self.assertIn("1 participation of responsible would be removed due to inactivity.", response)
526+
517527
self.assertIn("[email protected] > [email protected]", response)
518528
self.assertIn(mock_remove.return_value[0], response)
519-
self.assertEqual(mock_remove.call_count, 2)
529+
self.assertEqual(mock_remove.call_count, 3)
520530
calls = [[call[0][0].email, call[0][2]] for call in mock_remove.call_args_list]
521-
self.assertEqual(calls, [[testuser2.email, True], [contributor2.email, True]])
531+
self.assertEqual(calls, [[testuser2.email, True], [contributor2.email, True], [responsible.email, True]])
522532
mock_remove.reset_mock()
523533

524534
form = response.forms["user-bulk-update-form"]
@@ -542,19 +552,18 @@ def test_handles_users(self, mock_remove):
542552
# contributor1 should still be active, contributor2 should have been set to inactive
543553
self.assertTrue(UserProfile.objects.get(email="[email protected]").is_active)
544554
self.assertFalse(UserProfile.objects.get(email="[email protected]").is_active)
545-
# all should be active except for contributor2
546-
self.assertEqual(UserProfile.objects.filter(is_active=True).count(), len(expected_users) - 1)
555+
# all should be active except for contributor2, responsible
556+
self.assertEqual(UserProfile.objects.filter(is_active=True).count(), len(expected_users) - 2)
547557

548558
self.assertEqual(set(UserProfile.objects.all()), expected_users)
549559

550-
# mock gets called for every user to be deleted (once for the test run and once for the real run)
551560
self.assertIn(mock_remove.return_value[0], response)
552-
self.assertEqual(mock_remove.call_count, 2)
561+
self.assertEqual(mock_remove.call_count, 3)
553562
calls = [[call[0][0].email, call[0][2]] for call in mock_remove.call_args_list]
554-
self.assertEqual(calls, [[testuser2.email, False], [contributor2.email, False]])
563+
self.assertEqual(calls, [[testuser2.email, False], [contributor2.email, False], [responsible.email, False]])
555564

556565
@override_settings(DEBUG=False)
557-
def test_wrong_files_dont_crash(self):
566+
def test_wrong_files_dont_crash(self) -> None:
558567
page = self.app.get(self.url, user=self.manager)
559568
form = page.forms["user-bulk-update-form"]
560569
form["user_file"] = ("import.xls", self.random_excel_file_content)

0 commit comments

Comments
 (0)