Skip to content

Commit

Permalink
#993 ADD celery task 1: every day check that extracts ever… (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefrado authored Dec 10, 2024
1 parent 1b41492 commit f742b05
Show file tree
Hide file tree
Showing 11 changed files with 863 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/sdg/core/tests/test_management_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from datetime import date, datetime
from io import StringIO
from unittest.mock import MagicMock, patch

from django.conf import settings
from django.core import mail
Expand All @@ -23,6 +24,9 @@
from sdg.core.tests.factories.logius import ThemaFactory, UniformeProductnaamFactory
from sdg.organisaties.models import LokaleOverheid
from sdg.organisaties.tests.factories.overheid import BevoegdeOrganisatieFactory
from sdg.producten.management.commands.check_broken_links import (
Command as CheckBrokenLinksCommand,
)
from sdg.producten.tests.factories.product import (
GeneriekProductFactory,
ReferentieProductFactory,
Expand Down Expand Up @@ -503,3 +507,54 @@ def test_user_flag_with_eligable_user(self):

self.assertIn("No eligable users found.", out)
self.assertEqual(len(mail.outbox), 0)


class TestCommandCheckBrokenLinks(CommandTestCase):
def setUp(self):
super().setUp()
self.command = CheckBrokenLinksCommand

def test_handle_response_code_successful(self):
out = self.call_command("check_broken_links")
self.assertIn("Deleted 0 old BrokenLinks.", out)

def test_request_head_redirect_handling(self):
def test_case(url, equals_status_code):
response = self.command().request_head(url=url)
self.assertEqual(response.status_code, equals_status_code)

# Test case 1 - Successful request to Example
test_case("https://google.com/", 200)
# Test case 2 - Successful request to Google
test_case("https://example.com/", 200)
# Test case 3 - Multiple redirects, final status 200
test_case("https://digid.nl/inloggen", 200)
# Test case 4 - 301 Redirect
test_case("https://httpbin.org/status/301", 200)
# Test case 5 - 302 Redirect
test_case("https://httpbin.org/status/302", 200)
# Test case 6 - 303 Redirect
test_case("https://httpbin.org/status/303", 200)
# Test case 7 - 308 Redirect
test_case(
"https://www.zoetermeer.nl/verhuizen-naar-het-buitenland-emigreren/", 200
)
# Test case 8 - 403 Client error
test_case("https://httpbin.org/status/403", 403)
# Test case 9 - 404 Not Found
test_case("https://google.com/404", 404)
# Test case 10 - URL without protocol (should raise an error or handle gracefully)
test_case("www.google.com", 200)
# Test case 11 - 500 Server error
test_case("https://httpbin.org/status/500", 500)

def test_clean_up_removed_urls(self):
removed_links_count = 0
out = self.call_command("check_broken_links")
self.assertIn(f"Deleted {removed_links_count} old BrokenLinks.", str(out))

def test_reset_all_broken_links(self):
out = self.call_command("check_broken_links", "--reset")
self.assertIn(
"Successfully cleared the error_count of every BrokenLink.", str(out)
)
68 changes: 68 additions & 0 deletions src/sdg/fixtures/django_celery_beat.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@
"timezone": "UTC"
}
},
{
"model": "django_celery_beat.crontabschedule",
"pk": 7,
"fields": {
"minute": "0",
"hour": "9",
"day_of_week": "*",
"day_of_month": "1",
"month_of_year": "*",
"timezone": "UTC"
}
},
{
"model": "django_celery_beat.periodictasks",
"pk": 1,
Expand Down Expand Up @@ -281,5 +293,61 @@
"date_changed": "2023-04-06T12:22:36.304",
"description": "Send notification emails about recent reference product updates."
}
},
{
"model": "django_celery_beat.periodictask",
"pk": 8,
"fields": {
"name": "Check Broken Links",
"task": "sdg.producten.tasks.check_broken_links",
"interval": null,
"crontab": 1,
"solar": null,
"clocked": null,
"args": "[]",
"kwargs": "{}",
"queue": null,
"exchange": null,
"routing_key": null,
"headers": "{}",
"priority": null,
"expires": null,
"expire_seconds": null,
"one_off": false,
"start_time": null,
"enabled": false,
"last_run_at": null,
"total_run_count": 0,
"date_changed": "2024-11-04T12:00:00.000",
"description": "Send notification emails about broken links detected in product fields."
}
},
{
"model": "django_celery_beat.periodictask",
"pk": 9,
"fields": {
"name": "Send monthly broken links report",
"task": "sdg.producten.tasks.send_monthly_broken_links_report",
"interval": null,
"crontab": 7,
"solar": null,
"clocked": null,
"args": "[]",
"kwargs": "{}",
"queue": null,
"exchange": null,
"routing_key": null,
"headers": "{}",
"priority": null,
"expires": null,
"expire_seconds": null,
"one_off": false,
"start_time": null,
"enabled": false,
"last_run_at": null,
"total_run_count": 0,
"date_changed": "2024-11-04T14:00:00.000",
"description": "Send monthly broken links report to the all redactors of the content."
}
}
]
Loading

0 comments on commit f742b05

Please sign in to comment.