10
10
from django .conf import settings
11
11
from django .contrib .auth .models import Group
12
12
from django .core import mail
13
+ from django .db import transaction
13
14
from django .db .models import Model
14
15
from django .http import HttpResponse
15
16
from django .test import override_settings
50
51
from evap .grades .models import GradeDocument
51
52
from evap .results .tools import TextResult , cache_results , get_results
52
53
from evap .rewards .models import RewardPointGranting , SemesterActivation
54
+ from evap .rewards .tools import reward_points_of_user
53
55
from evap .staff .forms import ContributionCopyForm , ContributionCopyFormset , CourseCopyForm , EvaluationCopyForm
54
56
from evap .staff .tests .utils import (
55
57
WebTestStaffMode ,
@@ -302,7 +304,7 @@ def test_inactive_edit(self, mock_remove):
302
304
mock_remove .assert_called_with (self .testuser )
303
305
self .assertIn (mock_remove .return_value [0 ], response )
304
306
305
- def test_reward_points_granting_message (self ):
307
+ def test_reward_point_granting (self ):
306
308
evaluation = baker .make (Evaluation , course__semester__is_active = True )
307
309
already_evaluated = baker .make (Evaluation , course = baker .make (Course , semester = evaluation .course .semester ))
308
310
SemesterActivation .objects .create (semester = evaluation .course .semester , is_active = True )
@@ -317,6 +319,8 @@ def test_reward_points_granting_message(self):
317
319
form = page .forms ["user-form" ]
318
320
form ["evaluations_participating_in" ] = [already_evaluated .pk ]
319
321
322
+ self .assertEqual (reward_points_of_user (student ), 0 )
323
+
320
324
page = form .submit ().follow ()
321
325
# fetch the user name, which became lowercased
322
326
student .refresh_from_db ()
@@ -328,6 +332,8 @@ def test_reward_points_granting_message(self):
328
332
page ,
329
333
)
330
334
335
+ self .assertEqual (reward_points_of_user (student ), 3 )
336
+
331
337
332
338
class TestUserDeleteView (DeleteViewTestMixin , WebTestStaffMode ):
333
339
url = reverse ("staff:user_delete" )
@@ -895,6 +901,36 @@ def test_success_with_data(self):
895
901
896
902
self .assertFalse (Semester .objects .filter (pk = self .instance .pk ).exists ())
897
903
904
+ @override_settings (REWARD_POINTS = [(1 , 3 )])
905
+ def test_does_not_grant_redemption_points (self ):
906
+ student = baker .
make (
UserProfile ,
email = "[email protected] " )
907
+ evaluation = baker .make (
908
+ Evaluation , participants = [student ], state = Evaluation .State .PUBLISHED , course__semester = self .instance
909
+ )
910
+ SemesterActivation .objects .update_or_create (semester = self .instance , defaults = {"is_active" : True })
911
+
912
+ # student must be participant in at least one other evaluation
913
+ baker .make (
914
+ Evaluation ,
915
+ participants = [student ],
916
+ voters = [student ],
917
+ state = Evaluation .State .PUBLISHED ,
918
+ course__semester = self .instance ,
919
+ )
920
+
921
+ self .instance .archive ()
922
+ self .instance .delete_grade_documents ()
923
+ self .instance .archive_results ()
924
+
925
+ # just deleting the evaluation would grant redemption point -- ensures setup above is sufficient
926
+ with transaction .atomic ():
927
+ Evaluation .objects .get (pk = evaluation .pk ).delete ()
928
+ assert reward_points_of_user (student ) == 3
929
+ transaction .set_rollback (True )
930
+
931
+ self .app .post (self .url , params = self .post_params , user = self .user )
932
+ self .assertEqual (reward_points_of_user (student ), 0 )
933
+
898
934
899
935
class TestSemesterAssignView (WebTestStaffMode ):
900
936
@classmethod
@@ -2370,6 +2406,36 @@ def test_invalid_deletion(self):
2370
2406
self .app .post (self .url , user = self .manager , params = self .post_params , status = 400 )
2371
2407
self .assertTrue (Evaluation .objects .filter (pk = self .evaluation .pk ).exists ())
2372
2408
2409
+ @patch .object (Evaluation , "can_be_deleted_by_manager" , True )
2410
+ def test_reward_point_granting (self ):
2411
+ already_evaluated = baker .make (Evaluation , course__semester = self .evaluation .course .semester )
2412
+ SemesterActivation .objects .create (semester = self .evaluation .course .semester , is_active = True )
2413
+
2414
+ # does not get reward points as it was the only evaluation in this semester
2415
+ student = baker .make (
2416
+ UserProfile ,
email = "[email protected] " ,
evaluations_participating_in = [
self .
evaluation ]
2417
+ )
2418
+
2419
+ # get reward points
2420
+ baker .make (
2421
+ UserProfile ,
2422
+ email = iter (f"{ name } @institution.example.com" for name in ["a" , "b" , "c" , "d" , "e" ]),
2423
+ evaluations_participating_in = [self .evaluation , already_evaluated ],
2424
+ evaluations_voted_for = [already_evaluated ],
2425
+ _quantity = 5 ,
2426
+ )
2427
+
2428
+ self .assertEqual (reward_points_of_user (student ), 0 )
2429
+
2430
+ with patch ("evap.staff.views.logger" ) as mock :
2431
+ self .app .post (self .url , user = self .manager , params = self .post_params , status = 200 )
2432
+
2433
+ self .assertEqual (mock .info .call_args_list [0 ][1 ]["extra" ]["num_grantings" ], 5 )
2434
+ self .assertEqual (reward_points_of_user (student ), 0 )
2435
+
2436
+ user_a = UserProfile .
objects .
get (
email = "[email protected] " )
2437
+ self .assertEqual (reward_points_of_user (user_a ), 3 )
2438
+
2373
2439
2374
2440
class TestSingleResultEditView (WebTestStaffModeWith200Check ):
2375
2441
@classmethod
0 commit comments