4
4
from abc import ABC , abstractmethod
5
5
from io import BytesIO
6
6
from typing import Literal
7
- from unittest .mock import Mock , PropertyMock , patch
7
+ from unittest .mock import MagicMock , Mock , PropertyMock , patch
8
8
9
9
import openpyxl
10
10
import xlrd
@@ -423,11 +423,11 @@ class TestUserBulkUpdateView(WebTestStaffMode):
423
423
filename = os .path .join (settings .BASE_DIR , "staff/fixtures/test_user_bulk_update_file.txt" )
424
424
425
425
@classmethod
426
- def setUpTestData (cls ):
426
+ def setUpTestData (cls ) -> None :
427
427
cls .random_excel_file_content = excel_data .random_file_content
428
428
cls .manager = make_manager ()
429
429
430
- def test_testrun_deletes_no_users (self ):
430
+ def test_testrun_deletes_no_users (self ) -> None :
431
431
page = self .app .get (self .url , user = self .manager )
432
432
form = page .forms ["user-bulk-update-form" ]
433
433
@@ -443,7 +443,7 @@ def test_testrun_deletes_no_users(self):
443
443
helper_delete_all_import_files (self .manager .id )
444
444
445
445
@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 :
447
447
baker .
make (
UserProfile ,
email = "[email protected] " )
448
448
baker .make (
449
449
UserProfile ,
first_name_given = "Elisabeth" ,
last_name = "Fröhlich" ,
email = "[email protected] "
@@ -483,24 +483,32 @@ def test_multiple_email_matches_trigger_error(self):
483
483
484
484
@override_settings (INSTITUTION_EMAIL_DOMAINS = ["institution.example.com" , "internal.example.com" ])
485
485
@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 :
487
487
mock_remove .return_value = ["This text is supposed to be visible on the website." ]
488
488
testuser1 = baker .
make (
UserProfile ,
email = "[email protected] " )
489
489
testuser2 = baker .
make (
UserProfile ,
email = "[email protected] " )
490
490
testuser1 .delegates .set ([testuser2 ])
491
491
baker .
make (
UserProfile ,
email = "[email protected] " )
492
- contribution1 = baker .make (Contribution )
493
492
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
+ )
494
497
evaluation = baker .make (
495
498
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 ,
499
506
)
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 )])
502
508
contributor2 = baker .make (
503
- UserProfile ,
email = "[email protected] " ,
contributions = [
contribution2 ]
509
+ UserProfile ,
510
+
511
+ contributions = [baker .make (Contribution , evaluation = evaluation )],
504
512
)
505
513
testuser1 .cc_users .set ([contributor2 ])
506
514
@@ -512,13 +520,15 @@ def test_handles_users(self, mock_remove):
512
520
response = form .submit (name = "operation" , value = "test" )
513
521
514
522
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
516
524
)
525
+ self .assertIn ("1 participation of responsible would be removed due to inactivity." , response )
526
+
517
527
518
528
self .assertIn (mock_remove .return_value [0 ], response )
519
- self .assertEqual (mock_remove .call_count , 2 )
529
+ self .assertEqual (mock_remove .call_count , 3 )
520
530
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 ] ])
522
532
mock_remove .reset_mock ()
523
533
524
534
form = response .forms ["user-bulk-update-form" ]
@@ -542,19 +552,18 @@ def test_handles_users(self, mock_remove):
542
552
# contributor1 should still be active, contributor2 should have been set to inactive
543
553
self .
assertTrue (
UserProfile .
objects .
get (
email = "[email protected] " ).
is_active )
544
554
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 )
547
557
548
558
self .assertEqual (set (UserProfile .objects .all ()), expected_users )
549
559
550
- # mock gets called for every user to be deleted (once for the test run and once for the real run)
551
560
self .assertIn (mock_remove .return_value [0 ], response )
552
- self .assertEqual (mock_remove .call_count , 2 )
561
+ self .assertEqual (mock_remove .call_count , 3 )
553
562
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 ] ])
555
564
556
565
@override_settings (DEBUG = False )
557
- def test_wrong_files_dont_crash (self ):
566
+ def test_wrong_files_dont_crash (self ) -> None :
558
567
page = self .app .get (self .url , user = self .manager )
559
568
form = page .forms ["user-bulk-update-form" ]
560
569
form ["user_file" ] = ("import.xls" , self .random_excel_file_content )
0 commit comments