Skip to content

Commit 9d581e9

Browse files
mocking http calls
1 parent 6282e20 commit 9d581e9

4 files changed

Lines changed: 93 additions & 65 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ user:
2626

2727
tests: ## Run tests
2828
tests:
29-
uv run python manage.py test courses.tests
29+
uv run python manage.py test courses.tests --timing --durations 30
3030

3131

3232
data: ## Add data to database

courses/tests/test_data.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def test_homework_data_view(self):
6666
homework_link="https://github.com/DataTalksClub",
6767
)
6868

69-
self.submission.full_clean()
7069
self.submission.save()
7170

7271
self.question = Question.objects.create(
@@ -235,7 +234,6 @@ def test_project_data_view(self):
235234
commit_id="abcd1234",
236235
)
237236

238-
self.project_submission.full_clean()
239237
self.project_submission.save()
240238

241239
url = reverse(
@@ -442,47 +440,6 @@ def test_project_data_view(self):
442440
submission["passed"], expected_submission["passed"]
443441
)
444442

445-
def test_homework_submission_with_404_url(self):
446-
self.homework = Homework.objects.create(
447-
course=self.course,
448-
title="Test Homework",
449-
description="Test Homework Description",
450-
due_date=timezone.now() + timezone.timedelta(days=7),
451-
state=HomeworkState.OPEN.value,
452-
slug="test-homework",
453-
)
454-
455-
self.submission = Submission(
456-
homework=self.homework,
457-
student=self.user,
458-
enrollment=self.enrollment,
459-
homework_link="https://httpbin.org/status/404",
460-
)
461-
with self.assertRaises(ValidationError):
462-
self.submission.full_clean()
463-
464-
def test_project_submission_with_404_url(self):
465-
self.project = Project.objects.create(
466-
course=self.course,
467-
slug="test-project",
468-
title="Test Project",
469-
description="Description",
470-
submission_due_date=timezone.now()
471-
+ timezone.timedelta(days=7),
472-
peer_review_due_date=timezone.now()
473-
+ timezone.timedelta(days=14),
474-
)
475-
476-
self.project_submission = ProjectSubmission(
477-
project=self.project,
478-
student=self.user,
479-
enrollment=self.enrollment,
480-
github_link="https://httpbin.org/status/404",
481-
commit_id="abcd1234",
482-
)
483-
with self.assertRaises(ValidationError):
484-
self.project_submission.full_clean()
485-
486443
def test_graduate_data_view(self):
487444
"""Test that only students who passed enough projects are returned."""
488445

courses/tests/test_homework.py

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,12 @@ def test_homework_detail_submission_post_with_submissions(
815815
answer6 = answers.get(question=self.question6)
816816
self.assertEqual(answer6.answer_text, "1,2")
817817

818-
def test_submit_homework_with_all_fields(self):
818+
@mock.patch("requests.get")
819+
def test_submit_homework_with_all_fields(self, mock_get):
820+
mock_response = mock.Mock()
821+
mock_response.status_code = 200
822+
mock_get.return_value = mock_response
823+
819824
self.course.homework_problems_comments_field = True
820825
self.course.save()
821826

@@ -896,7 +901,14 @@ def test_submit_homework_with_all_fields(self):
896901
post_data["faq_contribution"],
897902
)
898903

899-
def test_submit_homework_with_all_fields_optional_empty(self):
904+
@mock.patch("requests.get")
905+
def test_submit_homework_with_all_fields_optional_empty(
906+
self, mock_get
907+
):
908+
mock_response = mock.Mock()
909+
mock_response.status_code = 200
910+
mock_get.return_value = mock_response
911+
900912
self.homework.homework_url_field = True
901913
self.homework.learning_in_public_cap = 7
902914
self.homework.time_spent_lectures_field = True
@@ -916,7 +928,7 @@ def test_submit_homework_with_all_fields_optional_empty(self):
916928
f"answer_{self.question4.id}": ["3"],
917929
f"answer_{self.question5.id}": ["3.141516"],
918930
f"answer_{self.question6.id}": ["1", "2"],
919-
"homework_url": "https://httpbin.org/status/200",
931+
"homework_url": "https://github.com/existing/repo",
920932
"learning_in_public_links[]": [""],
921933
"time_spent_lectures": "",
922934
"time_spent_homework": "",
@@ -953,6 +965,42 @@ def test_submit_homework_with_all_fields_optional_empty(self):
953965
self.assertEqual(submission.problems_comments, "")
954966
self.assertEqual(submission.faq_contribution, "")
955967

968+
@mock.patch("requests.get")
969+
def test_submit_homework_url_validation_404_error(self, mock_get):
970+
mock_response = mock.Mock()
971+
mock_response.status_code = 404
972+
mock_get.return_value = mock_response
973+
974+
self.homework.homework_url_field = True
975+
self.homework.save()
976+
977+
self.client.login(**credentials)
978+
979+
post_data = {
980+
f"answer_{self.question1.id}": ["1"],
981+
f"answer_{self.question2.id}": ["Some other text"],
982+
f"answer_{self.question3.id}": ["1", "2", "4"],
983+
f"answer_{self.question4.id}": ["3"],
984+
f"answer_{self.question5.id}": ["3.141516"],
985+
f"answer_{self.question6.id}": ["1", "2"],
986+
"homework_url": "https://github.com/nonexistent/repo",
987+
}
988+
989+
url = reverse(
990+
"homework",
991+
kwargs={
992+
"course_slug": self.course.slug,
993+
"homework_slug": self.homework.slug,
994+
},
995+
)
996+
997+
response = self.client.post(url, post_data)
998+
999+
# Should return form with errors due to 404
1000+
self.assertEqual(response.status_code, 200)
1001+
self.assertContains(response, "The submitted GitHub link")
1002+
self.assertContains(response, "does not exist")
1003+
9561004
def test_submit_homework_learning_in_public_empty_and_duplicates(
9571005
self,
9581006
):
@@ -970,9 +1018,10 @@ def test_submit_homework_learning_in_public_empty_and_duplicates(
9701018
f"answer_{self.question5.id}": ["3.141516"],
9711019
f"answer_{self.question6.id}": ["1", "2"],
9721020
"learning_in_public_links[]": [
973-
"https://httpbin.org/status/200",
974-
"https://httpbin.org/status/200",
975-
"https://github.com/DataTalksClub",
1021+
"https://test.org/totally-existing-url/1",
1022+
"https://test.org/totally-existing-url/1",
1023+
"https://test.org/totally-existing-url/2",
1024+
"https://test.org/totally-existing-url/3",
9761025
],
9771026
}
9781027

@@ -994,8 +1043,9 @@ def test_submit_homework_learning_in_public_empty_and_duplicates(
9941043
)
9951044

9961045
expected_learning_in_public_links = [
997-
"https://httpbin.org/status/200",
998-
"https://github.com/DataTalksClub",
1046+
"https://test.org/totally-existing-url/1",
1047+
"https://test.org/totally-existing-url/2",
1048+
"https://test.org/totally-existing-url/3",
9991049
]
10001050
self.assertEqual(
10011051
submission.learning_in_public_links,

courses/tests/test_project_view.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from unittest import mock
34
from django.urls import reverse
45
from django.test import TestCase, Client
56
from django.utils import timezone
@@ -167,10 +168,15 @@ def test_project_detail_with_scored_project(self):
167168
# Check if the context has 'disabled' as True
168169
self.assertTrue(response.context["disabled"])
169170

170-
def test_project_submission_post_no_submissions(self):
171+
@mock.patch("requests.get")
172+
def test_project_submission_post_no_submissions(self, mock_get):
171173
"""
172174
Test posting a project submission when there are no existing submissions.
173175
"""
176+
mock_response = mock.Mock()
177+
mock_response.status_code = 200
178+
mock_get.return_value = mock_response
179+
174180
self.client.login(**credentials)
175181
url = reverse(
176182
"project", args=[self.course.slug, self.project.slug]
@@ -207,7 +213,12 @@ def test_project_submission_post_no_submissions(self):
207213
submission.faq_contribution, data["faq_contribution"]
208214
)
209215

210-
def test_project_submission_post_creates_enrollment(self):
216+
@mock.patch("requests.get")
217+
def test_project_submission_post_creates_enrollment(self, mock_get):
218+
mock_response = mock.Mock()
219+
mock_response.status_code = 200
220+
mock_get.return_value = mock_response
221+
211222
self.enrollment.delete()
212223

213224
enrollments = Enrollment.objects.filter(
@@ -224,7 +235,7 @@ def test_project_submission_post_creates_enrollment(self):
224235
)
225236

226237
data = {
227-
"github_link": "https://httpbin.org/status/200",
238+
"github_link": "https://github.com/existing/repo",
228239
"commit_id": "1234567",
229240
"time_spent": "2",
230241
"problems_comments": "Encountered an issue with...",
@@ -235,18 +246,23 @@ def test_project_submission_post_creates_enrollment(self):
235246

236247
self.assertEqual(enrollments.count(), 1)
237248

238-
def test_project_submission_post_with_submissions(self):
249+
@mock.patch("requests.get")
250+
def test_project_submission_post_with_submissions(self, mock_get):
239251
"""
240252
Test posting a project submission when there are existing submissions.
241253
"""
254+
mock_response = mock.Mock()
255+
mock_response.status_code = 200
256+
mock_get.return_value = mock_response
257+
242258
self.client.login(**credentials)
243259

244260
# Create an initial submission
245261
submission = ProjectSubmission.objects.create(
246262
project=self.project,
247263
student=self.user,
248264
enrollment=self.enrollment,
249-
github_link="https://httpbin.org/status/200",
265+
github_link="https://github.com/existing/repo",
250266
commit_id="123456a",
251267
)
252268

@@ -255,7 +271,7 @@ def test_project_submission_post_with_submissions(self):
255271
)
256272

257273
data = {
258-
"github_link": "https://httpbin.org/status/200",
274+
"github_link": "https://github.com/existing/repo",
259275
"commit_id": "123456e",
260276
"time_spent": "3",
261277
"problems_comments": "No issues encountered.",
@@ -293,7 +309,7 @@ def test_remove_project_submission(self):
293309
project=self.project,
294310
student=self.user,
295311
enrollment=self.enrollment,
296-
github_link="https://httpbin.org/status/200",
312+
github_link="https://github.com/existing/repo",
297313
commit_id="123456a",
298314
)
299315

@@ -305,18 +321,17 @@ def test_remove_project_submission(self):
305321

306322
self.assertEqual(count_sumissions, 1)
307323

308-
309324
url = reverse(
310325
"project", args=[self.course.slug, self.project.slug]
311326
)
312327

313328
data = {
314-
"github_link": "https://httpbin.org/status/200",
329+
"github_link": "https://github.com/existing/repo",
315330
"commit_id": "123456e",
316331
"time_spent": "3",
317332
"problems_comments": "No issues encountered.",
318333
"faq_contribution": "Helped a peer with their problem.",
319-
"action": "delete"
334+
"action": "delete",
320335
}
321336

322337
response = self.client.post(url, data)
@@ -330,7 +345,6 @@ def test_remove_project_submission(self):
330345

331346
self.assertEqual(count_sumissions, 0)
332347

333-
334348
# this test requires a redesing of the project view
335349
# skipping for now
336350
# def test_submission_exist_post_with_error(self):
@@ -399,7 +413,7 @@ def test_project_submission_not_accepting_responses(self):
399413
)
400414

401415
data = {
402-
"github_link": "https://httpbin.org/status/200",
416+
"github_link": "https://github.com/existing/repo",
403417
"commit_id": "1234567",
404418
"time_spent": "2",
405419
"problems_comments": "Encountered an issue with...",
@@ -417,11 +431,18 @@ def test_project_submission_not_accepting_responses(self):
417431

418432
self.assertEqual(submissions.count(), 0)
419433

420-
def test_project_submission_post_invalid_link_no_submission(self):
434+
@mock.patch("requests.get")
435+
def test_project_submission_post_invalid_link_no_submission(
436+
self, mock_get
437+
):
421438
"""
422439
When the link is invalid and there's no submission yet,
423440
no submission is created
424441
"""
442+
mock_response = mock.Mock()
443+
mock_response.status_code = 404
444+
mock_get.return_value = mock_response
445+
425446
self.client.login(**credentials)
426447
url = reverse(
427448
"project", args=[self.course.slug, self.project.slug]

0 commit comments

Comments
 (0)