@@ -876,3 +876,56 @@ def test_delete_reaction_owned_by_the_current_user():
876876
877877 reaction .refresh_from_db ()
878878 assert reaction .users .exists ()
879+
880+
881+ def test_create_reaction_exceeds_maximum (settings ):
882+ """
883+ Users should not be able to add more than REACTIONS_MAX_PER_COMMENT
884+ (here we set it to 10) distinct emoji reactions to a comment.
885+ They should, however, be able to add themselves to an existing reaction.
886+ """
887+ user1 = factories .UserFactory ()
888+ user2 = factories .UserFactory ()
889+ document = factories .DocumentFactory (
890+ link_reach = "restricted" ,
891+ users = [(user1 , models .RoleChoices .ADMIN ), (user2 , models .RoleChoices .ADMIN )],
892+ )
893+ thread = factories .ThreadFactory (document = document )
894+ comment = factories .CommentFactory (thread = thread )
895+
896+ client = APIClient ()
897+ client .force_login (user1 )
898+
899+ # Add max distinct reactions
900+ max_reactions = settings .REACTIONS_MAX_PER_COMMENT
901+ emojis = factories .ReactionFactory .generate_emojis (max_reactions + 1 )
902+ for emoji in emojis [:max_reactions ]:
903+ response = client .post (
904+ f"/api/v1.0/documents/{ document .id !s} /threads/{ thread .id !s} /"
905+ f"comments/{ comment .id !s} /reactions/" ,
906+ {"emoji" : emoji },
907+ )
908+ assert response .status_code == 201
909+
910+ # Attempt to add another distinct reaction
911+ response = client .post (
912+ f"/api/v1.0/documents/{ document .id !s} /threads/{ thread .id !s} /"
913+ f"comments/{ comment .id !s} /reactions/" ,
914+ {"emoji" : emojis [max_reactions ]},
915+ )
916+ assert response .status_code == 400
917+ expected_message = (
918+ f"A comment can have a maximum of { max_reactions } distinct reactions."
919+ )
920+ assert response .json () == {"emoji" : [expected_message ]}
921+
922+ # Attempt to add user2 to one of the existing reactions (should succeed)
923+ client .force_login (user2 )
924+ response = client .post (
925+ f"/api/v1.0/documents/{ document .id !s} /threads/{ thread .id !s} /"
926+ f"comments/{ comment .id !s} /reactions/" ,
927+ {"emoji" : emojis [0 ]},
928+ )
929+ assert response .status_code == 201
930+ reaction = models .Reaction .objects .get (comment = comment , emoji = emojis [0 ])
931+ assert reaction .users .count () == 2
0 commit comments