From 4edd0d5f54a18cff6780e16dee1946574f98be4f Mon Sep 17 00:00:00 2001 From: Matheus Felipe <50463866+matheusfelipeog@users.noreply.github.com> Date: Wed, 12 Jan 2022 20:00:56 -0300 Subject: [PATCH] Create basic tests to has_cloudflare_protection --- scripts/tests/test_validate_links.py | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/scripts/tests/test_validate_links.py b/scripts/tests/test_validate_links.py index a559f712..73103661 100644 --- a/scripts/tests/test_validate_links.py +++ b/scripts/tests/test_validate_links.py @@ -4,10 +4,29 @@ import unittest from validate.links import find_links_in_text from validate.links import get_host_from_link +from validate.links import has_cloudflare_protection + + +class FakeResponse(): + def __init__(self, code: int, headers: dict, text: str) -> None: + self.status_code = code + self.headers = headers + self.text = text class TestValidateLinks(unittest.TestCase): + def setUp(self): + self.code_200 = 200 + self.code_403 = 403 + self.code_503 = 503 + + self.cloudflare_headers = {'Server': 'cloudflare'} + self.no_cloudflare_headers = {'Server': 'google'} + + self.text_with_cloudflare_flags = '403 Forbidden Cloudflare We are checking your browser...' + self.text_without_cloudflare_flags = 'Lorem Ipsum' + def test_find_link_in_text(self): text = """ # this is valid @@ -68,3 +87,49 @@ class TestValidateLinks(unittest.TestCase): with self.assertRaises(TypeError): get_host_from_link() + + def test_has_cloudflare_protection_with_code_403_and_503_in_response(self): + resp_with_cloudflare_protection_code_403 = FakeResponse( + code=self.code_403, + headers=self.cloudflare_headers, + text=self.text_with_cloudflare_flags + ) + + resp_with_cloudflare_protection_code_503 = FakeResponse( + code=self.code_503, + headers=self.cloudflare_headers, + text=self.text_with_cloudflare_flags + ) + + result1 = has_cloudflare_protection(resp_with_cloudflare_protection_code_403) + result2 = has_cloudflare_protection(resp_with_cloudflare_protection_code_503) + + self.assertTrue(result1) + self.assertTrue(result2) + + def test_has_cloudflare_protection_when_there_is_no_protection(self): + resp_without_cloudflare_protection1 = FakeResponse( + code=self.code_200, + headers=self.no_cloudflare_headers, + text=self.text_without_cloudflare_flags + ) + + resp_without_cloudflare_protection2 = FakeResponse( + code=self.code_403, + headers=self.no_cloudflare_headers, + text=self.text_without_cloudflare_flags + ) + + resp_without_cloudflare_protection3 = FakeResponse( + code=self.code_503, + headers=self.no_cloudflare_headers, + text=self.text_without_cloudflare_flags + ) + + result1 = has_cloudflare_protection(resp_without_cloudflare_protection1) + result2 = has_cloudflare_protection(resp_without_cloudflare_protection2) + result3 = has_cloudflare_protection(resp_without_cloudflare_protection3) + + self.assertFalse(result1) + self.assertFalse(result2) + self.assertFalse(result3)