Skip to content

Commit

Permalink
Refactor HTTP status codes for external resources (#2199)
Browse files Browse the repository at this point in the history
* Refactor HTTP status codes for external resources

* Refactor hard-coded values in unit test

* Refactor hard-coded status codes in api test
  • Loading branch information
pt2302 authored Jun 5, 2024
1 parent 231ad95 commit 40d2aeb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
5 changes: 3 additions & 2 deletions external_resources/api_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for External Resources API"""

import pytest
from rest_framework.status import HTTP_200_OK

from external_resources.api import is_url_broken
from external_resources.constants import RESOURCE_UNCHECKED_STATUSES
Expand All @@ -9,12 +10,12 @@

def test_is_url_broken_valid(mocker):
"""Test for working url"""
mock_response = mocker.Mock(status_code=200)
mock_response = mocker.Mock(status_code=HTTP_200_OK)
mocker.patch("external_resources.api.requests.head", return_value=mock_response)

result, status_code = is_url_broken("http://google.com")
assert not result
assert status_code == 200
assert status_code == HTTP_200_OK


@pytest.mark.parametrize("status_code", RESOURCE_UNCHECKED_STATUSES)
Expand Down
35 changes: 18 additions & 17 deletions external_resources/constants.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
"""Constants for External Resources module"""

# HTTP Status Codes
HTTP_BAD_REQUEST = 400
HTTP_UNAUTHORIZED = 401
HTTP_PAYMENT_REQUIRED = 402
HTTP_FORBIDDEN = 403
HTTP_TOO_MANY_REQUESTS = 429
HTTP_REQUEST_TIMEOUT = 408
HTTP_SERVICE_UNAVAILABLE = 503
from rest_framework.status import (
HTTP_400_BAD_REQUEST,
HTTP_401_UNAUTHORIZED,
HTTP_402_PAYMENT_REQUIRED,
HTTP_403_FORBIDDEN,
HTTP_408_REQUEST_TIMEOUT,
HTTP_429_TOO_MANY_REQUESTS,
HTTP_503_SERVICE_UNAVAILABLE,
)

# External Resource
RESOURCE_BROKEN_STATUS_START = HTTP_BAD_REQUEST
# constants for external resources
RESOURCE_BROKEN_STATUS_START = HTTP_400_BAD_REQUEST
RESOURCE_BROKEN_STATUS_END = 600
RESOURCE_UNCHECKED_STATUSES = [
HTTP_UNAUTHORIZED,
HTTP_PAYMENT_REQUIRED,
HTTP_FORBIDDEN,
HTTP_TOO_MANY_REQUESTS,
HTTP_REQUEST_TIMEOUT,
HTTP_SERVICE_UNAVAILABLE,
HTTP_401_UNAUTHORIZED,
HTTP_402_PAYMENT_REQUIRED,
HTTP_403_FORBIDDEN,
HTTP_408_REQUEST_TIMEOUT,
HTTP_429_TOO_MANY_REQUESTS,
HTTP_503_SERVICE_UNAVAILABLE,
]

# Celery Task
# constants for Celery task
EXTERNAL_RESOURCE_TASK_RATE_LIMIT = "100/s"
EXTERNAL_RESOURCE_TASK_PRIORITY = 4 # Lowest priority from range (0 - 4)
55 changes: 48 additions & 7 deletions external_resources/tasks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from typing import Literal

import pytest
from rest_framework.status import (
HTTP_200_OK,
HTTP_400_BAD_REQUEST,
HTTP_401_UNAUTHORIZED,
)

from external_resources.exceptions import CheckFailedError
from external_resources.factories import ExternalResourceStateFactory
Expand Down Expand Up @@ -72,13 +77,49 @@ def test_check_external_resources_for_breakages_zero_websites(
"resource_status",
),
[
(False, 200, False, 200, ExternalResourceState.Status.VALID),
(False, 200, True, 400, ExternalResourceState.Status.VALID),
(True, 400, False, 200, ExternalResourceState.Status.VALID),
(True, 400, True, 400, ExternalResourceState.Status.BROKEN),
(False, 200, True, 401, ExternalResourceState.Status.VALID),
(True, 401, False, 200, ExternalResourceState.Status.VALID),
(True, 401, True, 401, ExternalResourceState.Status.UNCHECKED),
(False, HTTP_200_OK, False, HTTP_200_OK, ExternalResourceState.Status.VALID),
(
False,
HTTP_200_OK,
True,
HTTP_400_BAD_REQUEST,
ExternalResourceState.Status.VALID,
),
(
True,
HTTP_400_BAD_REQUEST,
False,
HTTP_200_OK,
ExternalResourceState.Status.VALID,
),
(
True,
HTTP_400_BAD_REQUEST,
True,
HTTP_400_BAD_REQUEST,
ExternalResourceState.Status.BROKEN,
),
(
False,
HTTP_200_OK,
True,
HTTP_401_UNAUTHORIZED,
ExternalResourceState.Status.VALID,
),
(
True,
HTTP_401_UNAUTHORIZED,
False,
HTTP_200_OK,
ExternalResourceState.Status.VALID,
),
(
True,
HTTP_401_UNAUTHORIZED,
True,
HTTP_401_UNAUTHORIZED,
ExternalResourceState.Status.UNCHECKED,
),
],
)
def test_check_external_resources( # noqa: PLR0913
Expand Down

0 comments on commit 40d2aeb

Please sign in to comment.