55from rest_framework import status
66from rest_framework .test import APIClient
77
8- from core .models import Event , Job , SpamModeration
9- from core .tests .base import BaseModelTestCase , EventFactory , JobFactory
8+ from core .models import Event , Job , MemberProfile , SpamModeration
9+ from core .tests .base import BaseModelTestCase , EventFactory , JobFactory , \
10+ UserFactory
1011from library .models import Codebase
1112from library .tests .base import CodebaseFactory
1213
@@ -42,14 +43,17 @@ def setUp(self):
4243 title = "Test Codebase" , description = "Codebase Description"
4344 )
4445
45- # Create SpamModeration objects
46- self .job_spam = SpamModeration .objects .create (
46+ self .user_factory = UserFactory ()
47+ self .spammy_user = self .user_factory .create (username = "scamlikely" )
48+
49+ # Create SpamModeration objects (for MemberProfile the SpamModeration will be created automatically when user is created)
50+ self .job_spam_moderation = SpamModeration .objects .create (
4751 content_object = self .job , status = SpamModeration .Status .SCHEDULED_FOR_CHECK
4852 )
49- self .event_spam = SpamModeration .objects .create (
53+ self .event_spam_moderation = SpamModeration .objects .create (
5054 content_object = self .event , status = SpamModeration .Status .SCHEDULED_FOR_CHECK
5155 )
52- self .codebase_spam = SpamModeration .objects .create (
56+ self .codebase_spam_moderation = SpamModeration .objects .create (
5357 content_object = self .codebase ,
5458 status = SpamModeration .Status .SCHEDULED_FOR_CHECK ,
5559 )
@@ -101,13 +105,16 @@ def test_get_latest_spam_batch(self):
101105 self .assertEqual (response .status_code , status .HTTP_200_OK )
102106
103107 data = response .json ()
104- self .assertEqual (len (data ), 3 ) # We expect 3 items in the batch
108+ self .assertEqual (
109+ len (data ), 5
110+ ) # We expect 5 items in the batch (Event, Job, Codebase, MemberProfile) + MemberProfile of the test_user from super().setUp()
105111
106112 # Check if all content types are present
107113 content_types = [item ["contentType" ] for item in data ]
108114 self .assertIn ("job" , content_types )
109115 self .assertIn ("event" , content_types )
110116 self .assertIn ("codebase" , content_types )
117+ self .assertIn ("memberprofile" , content_types )
111118
112119 # Check structure of a job item
113120 job_item = next (item for item in data if item ["contentType" ] == "job" )
@@ -163,6 +170,40 @@ def test_update_spam_moderation_success(self):
163170 # Check if related content object was updated
164171 self .assertTrue (job .is_marked_spam )
165172
173+ def test_update_spam_moderation_success_memberprofile (self ):
174+ self .client .credentials (HTTP_X_API_KEY = self .api_key )
175+
176+ mp = MemberProfile .objects .get (id = self .spammy_user .member_profile .id )
177+
178+ data = {
179+ "id" : mp .spam_moderation .id ,
180+ "is_spam" : True ,
181+ "spam_indicators" : ["indicator1" , "indicator2" ],
182+ "reasoning" : "Test reasoning" ,
183+ "confidence" : 0.9 ,
184+ }
185+
186+ response = self .client .post ("/api/spam/update/" , data , format = "json" )
187+ self .assertEqual (response .status_code , status .HTTP_200_OK )
188+
189+ # Check if SpamModeration object was updated
190+ mp .refresh_from_db ()
191+ self .assertIsNotNone (mp .spam_moderation )
192+ self .assertEqual (mp .spam_moderation .status , SpamModeration .Status .SPAM_LIKELY )
193+ self .assertTrue (mp .is_marked_spam )
194+ self .assertEqual (mp .spam_moderation .detection_method , "LLM" )
195+ self .assertEqual (
196+ mp .spam_moderation .detection_details ["spam_indicators" ],
197+ ["indicator1" , "indicator2" ],
198+ )
199+ self .assertEqual (
200+ mp .spam_moderation .detection_details ["reasoning" ], "Test reasoning"
201+ )
202+ self .assertEqual (mp .spam_moderation .detection_details ["confidence" ], 0.9 )
203+
204+ # Check if related content object was updated
205+ self .assertTrue (mp .is_marked_spam )
206+
166207 def test_update_spam_moderation_not_spam (self ):
167208 self .client .credentials (HTTP_X_API_KEY = self .api_key )
168209
@@ -194,7 +235,7 @@ def test_update_spam_moderation_invalid_data(self):
194235 self .client .credentials (HTTP_X_API_KEY = self .api_key )
195236
196237 data = {
197- "id" : self .codebase_spam .id ,
238+ "id" : self .codebase_spam_moderation .id ,
198239 # Missing required 'is_spam' field
199240 }
200241
@@ -205,7 +246,7 @@ def test_update_spam_moderation_partial_update(self):
205246 self .client .credentials (HTTP_X_API_KEY = self .api_key )
206247
207248 data = {
208- "id" : self .codebase_spam .id ,
249+ "id" : self .codebase_spam_moderation .id ,
209250 "is_spam" : True ,
210251 # Only providing partial data
211252 }
0 commit comments