Skip to content

Commit 078ccd7

Browse files
committed
test: add coverage for inactive user login and duplicate collaboration validation
1 parent f13b8d6 commit 078ccd7

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

Mapapi/tests/test_additional_views_coverage_8.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ def test_login_missing_credentials(self):
6565
response = self.client.post(self.url, data, format='json')
6666

6767
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
68+
69+
def test_login_inactive_user(self):
70+
"""Test login with an inactive user"""
71+
# Create an inactive user
72+
inactive_user = User.objects.create_user(
73+
email='inactive@example.com',
74+
password='testpass123',
75+
first_name='Inactive',
76+
last_name='User',
77+
is_active=False
78+
)
79+
80+
data = {
81+
'email': 'inactive@example.com',
82+
'password': 'testpass123'
83+
}
84+
85+
response = self.client.post(self.url, data, format='json')
86+
87+
# Should return 401 Unauthorized for inactive users
88+
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
6889

6990

7091
@patch.object(EmailMultiAlternatives, 'send')

Mapapi/tests/test_serializer_missing_coverage.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,30 @@ def test_save(self):
116116

117117
class 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

187228
class ColaborationSerializerTests(TestCase):

0 commit comments

Comments
 (0)