@@ -116,16 +116,30 @@ def test_save(self):
116116
117117class CollaborationSerializerTests (TestCase ):
118118 def setUp (self ):
119+ # Create the user who will be the incident taker
120+ self .incident_taker = User .objects .create_user (
121+ email = 'taker@example.com' ,
122+ password = 'testpass123' ,
123+ first_name = 'Incident' ,
124+ last_name = 'Taker' ,
125+ organisation = 'Test Org'
126+ )
127+
128+ # Create the test user who will create the collaboration
119129 self .user = User .objects .create_user (
120130 email = 'user@example.com' ,
121131 password = 'testpass123' ,
122132 first_name = 'Test' ,
123- last_name = 'User'
133+ last_name = 'User' ,
134+ organisation = 'User Org'
124135 )
136+
137+ # Create an incident with the taker
125138 self .incident = Incident .objects .create (
126139 title = 'Test Incident' ,
127140 zone = 'Test Zone' ,
128- user_id = self .user
141+ user_id = self .incident_taker ,
142+ taken_by = self .incident_taker
129143 )
130144
131145 def test_validate_end_date_in_past (self ):
@@ -182,6 +196,33 @@ def test_validate_duplicate_collaboration(self):
182196
183197 # Check that the error message is correct
184198 self .assertIn ('La date de fin doit être dans le futur' , str (context .exception ))
199+
200+ def test_validate_duplicate_collaboration (self ):
201+ """Test CollaborationSerializer.validate() with duplicate collaboration"""
202+ # First create a collaboration
203+ future_date = timezone .now ().date () + timedelta (days = 7 )
204+ data = {
205+ 'incident' : self .incident .id ,
206+ 'user' : self .user .id ,
207+ 'end_date' : future_date ,
208+ 'status' : 'pending'
209+ }
210+ serializer = CollaborationSerializer (data = data )
211+ self .assertTrue (serializer .is_valid ())
212+ collaboration = serializer .save ()
213+
214+ # Try to create another collaboration with the same user and incident
215+ duplicate_serializer = CollaborationSerializer (data = {
216+ 'incident' : self .incident .id ,
217+ 'user' : self .user .id ,
218+ 'end_date' : future_date + timedelta (days = 1 ),
219+ 'status' : 'pending'
220+ })
221+
222+ self .assertFalse (duplicate_serializer .is_valid ())
223+ self .assertIn ('non_field_errors' , duplicate_serializer .errors )
224+ self .assertIn ('Une collaboration existe déjà pour cet utilisateur sur cet incident' ,
225+ str (duplicate_serializer .errors ['non_field_errors' ]))
185226
186227
187228class ColaborationSerializerTests (TestCase ):
0 commit comments