Skip to content

Commit ad8879c

Browse files
Akshat SinghAkshat Singh
authored andcommitted
Fix
1 parent 1581b86 commit ad8879c

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

tests/unit/challenges/test_views.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5961,8 +5961,11 @@ def test_request_challenge_approval_with_successful_subscription_email(
59615961
)
59625962
response = self.client.get(url)
59635963

5964+
# Verify email function was called with correct challenge
59645965
mock_send_email.assert_called_once_with(self.challenge)
59655966

5967+
# Verify success logging
5968+
59665969
mock_logger.info.assert_any_call(
59675970
"Subscription plans email sent successfully for challenge {}".format(
59685971
self.challenge.pk
@@ -6001,14 +6004,19 @@ def test_request_challenge_approval_with_email_failure_continues_approval(
60016004
)
60026005
response = self.client.get(url)
60036006

6007+
# Verify email function was called
60046008
mock_send_email.assert_called_once_with(self.challenge)
60056009

6010+
# Verify error logging
6011+
60066012
mock_logger.error.assert_any_call(
60076013
"Failed to send subscription plans email for challenge {}: {}".format(
60086014
self.challenge.pk, "Email service unavailable"
60096015
)
60106016
)
60116017

6018+
# Verify approval process continues despite email failure
6019+
60126020
self.assertEqual(response.status_code, status.HTTP_200_OK)
60136021
self.assertEqual(
60146022
response.data,
@@ -6025,10 +6033,12 @@ def test_request_challenge_approval_challenge_not_found(
60256033
"""Test that email is not sent when challenge doesn't exist"""
60266034
url = reverse_lazy(
60276035
"challenges:request_challenge_approval_by_pk",
6028-
kwargs={"challenge_pk": 99999},
6036+
kwargs={"challenge_pk": 99999}, # Non-existent challenge
60296037
)
60306038
response = self.client.get(url)
60316039

6040+
# Verify email function was not called for non-existent challenge
6041+
60326042
mock_send_email.assert_not_called()
60336043

60346044
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
@@ -6038,6 +6048,8 @@ def test_request_challenge_approval_challenge_not_found(
60386048
def test_request_challenge_approval_user_not_host(self, mock_send_email):
60396049
"""Test that email is not sent when user is not challenge host"""
60406050

6051+
# Create a different user who is not a challenge host
6052+
60416053
other_user = User.objects.create(
60426054
username="otheruser",
60436055
password="other_password",
@@ -6050,6 +6062,8 @@ def test_request_challenge_approval_user_not_host(self, mock_send_email):
60506062
verified=True,
60516063
)
60526064

6065+
# Authenticate as the other user
6066+
60536067
self.client.force_authenticate(user=other_user)
60546068

60556069
url = reverse_lazy(
@@ -6058,6 +6072,8 @@ def test_request_challenge_approval_user_not_host(self, mock_send_email):
60586072
)
60596073
response = self.client.get(url)
60606074

6075+
# Verify email function was not called for unauthorized user
6076+
60616077
mock_send_email.assert_not_called()
60626078

60636079
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@@ -6071,7 +6087,7 @@ def test_request_challenge_approval_webhook_failure_after_email_success(
60716087
responses.add(
60726088
responses.POST,
60736089
settings.APPROVAL_WEBHOOK_URL,
6074-
body=b"error",
6090+
body=b"error", # Simulate webhook failure
60756091
status=200,
60766092
content_type="text/plain",
60776093
)
@@ -6082,8 +6098,11 @@ def test_request_challenge_approval_webhook_failure_after_email_success(
60826098
)
60836099
response = self.client.get(url)
60846100

6101+
# Verify email function was called despite webhook failure
60856102
mock_send_email.assert_called_once_with(self.challenge)
60866103

6104+
# Webhook failure should result in error response
6105+
60876106
self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE)
60886107
self.assertIn("error", response.data)
60896108

@@ -6099,6 +6118,8 @@ def test_request_challenge_approval_with_smtp_error(self, mock_send_email):
60996118
content_type="text/plain",
61006119
)
61016120

6121+
# Simulate SMTP error
6122+
61026123
from smtplib import SMTPException
61036124

61046125
mock_send_email.side_effect = SMTPException(
@@ -6111,8 +6132,11 @@ def test_request_challenge_approval_with_smtp_error(self, mock_send_email):
61116132
)
61126133
response = self.client.get(url)
61136134

6135+
# Verify email function was called
61146136
mock_send_email.assert_called_once_with(self.challenge)
61156137

6138+
# Approval should continue despite SMTP error
6139+
61166140
self.assertEqual(response.status_code, status.HTTP_200_OK)
61176141

61186142
@responses.activate
@@ -6122,6 +6146,8 @@ def test_request_challenge_approval_email_integration_with_challenge_phases(
61226146
):
61236147
"""Test email integration with challenge that has multiple phases"""
61246148

6149+
# Create additional challenge phase
6150+
61256151
with self.settings(MEDIA_ROOT="/tmp/evalai"):
61266152
additional_phase = ChallengePhase.objects.create(
61276153
name="Additional Phase",
@@ -6138,9 +6164,12 @@ def test_request_challenge_approval_email_integration_with_challenge_phases(
61386164
),
61396165
)
61406166

6167+
# Create a finished submission for the additional phase to satisfy the submission check
61416168
from jobs.models import Submission
61426169
from participants.models import Participant
61436170

6171+
# Ensure participant team is associated with the challenge and user is a participant
6172+
61446173
self.challenge.participant_teams.add(self.participant_team)
61456174
Participant.objects.get_or_create(
61466175
user=self.user,
@@ -6152,7 +6181,7 @@ def test_request_challenge_approval_email_integration_with_challenge_phases(
61526181
participant_team=self.participant_team,
61536182
challenge_phase=additional_phase,
61546183
created_by=self.user,
6155-
status="submitted",
6184+
status="submitted", # Start with submitted status
61566185
input_file=SimpleUploadedFile(
61576186
"test_input.txt", b"test input", content_type="text/plain"
61586187
),
@@ -6163,6 +6192,8 @@ def test_request_challenge_approval_email_integration_with_challenge_phases(
61636192
is_public=True,
61646193
)
61656194

6195+
# Manually update the status to finished after creation to bypass any automatic processing
6196+
61666197
submission.status = "finished"
61676198
submission.save()
61686199

@@ -6180,6 +6211,8 @@ def test_request_challenge_approval_email_integration_with_challenge_phases(
61806211
)
61816212
response = self.client.get(url)
61826213

6214+
# Verify email function was called with the challenge
6215+
61836216
mock_send_email.assert_called_once_with(self.challenge)
61846217

61856218
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -6191,6 +6224,8 @@ def test_request_challenge_approval_email_not_sent_when_submissions_incomplete(
61916224
):
61926225
"""Test that email is not sent when submission check fails"""
61936226

6227+
# Create a challenge phase without finished submissions
6228+
61946229
with self.settings(MEDIA_ROOT="/tmp/evalai"):
61956230
ChallengePhase.objects.create(
61966231
name="Unfinished Phase",
@@ -6213,8 +6248,11 @@ def test_request_challenge_approval_email_not_sent_when_submissions_incomplete(
62136248
)
62146249
response = self.client.get(url)
62156250

6251+
# Email should NOT be sent when submission check fails
62166252
mock_send_email.assert_not_called()
62176253

6254+
# The request should fail due to unfinished submissions
6255+
62186256
self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE)
62196257
self.assertIn(
62206258
"do not have finished submissions", response.data["error"]
@@ -6325,6 +6363,8 @@ def test_create_challenge_using_github_success(self):
63256363
self.assertEqual(Leaderboard.objects.count(), 1)
63266364
self.assertEqual(ChallengePhaseSplit.objects.count(), 1)
63276365

6366+
# Verify github_repository is properly stored
6367+
63286368
challenge = Challenge.objects.first()
63296369
self.assertEqual(
63306370
challenge.github_repository,

0 commit comments

Comments
 (0)