Exemple #1
0
    def subject_propagator(self) -> "URLAvailabilityChecker":
        """
        Propagate the currently set subject.

        .. warning::
            You are not invited to run this method directly.
        """

        self.http_status_code_query_tool.set_subject(self.idna_subject)

        self.domain_syntax_checker.subject = self.idna_subject
        self.ip_syntax_checker.subject = self.idna_subject
        self.url_syntax_checker.subject = self.idna_subject

        self.status = AvailabilityCheckerStatus()
        self.status.params = self.params
        self.status.dns_lookup_record = None
        self.status.whois_lookup_record = None

        self.status.subject = self.subject
        self.status.idna_subject = self.idna_subject
        self.status.status = None

        self.query_syntax_checker()

        return self
    def test_push_with_whois_no_json_response(self, request_mock) -> None:
        """
        Tests the method which let us push some dataset into the collection.
        """

        response_dict = self.response_dataset
        response_dict["subject"] = "example.net"
        self.availability_status_dataset["expiration_date"] = "23-nov-2090"

        def mocking(*args, **kwargs):  # pylint: disable=unused-argument
            response_content = "I'm a teapot."

            response = requests.models.Response()
            response.url = "https://example.org/v1/status/availability"
            response.status_code = 418

            # pylint: disable=protected-access
            response._content = str.encode(response_content)

            response.history = [response]

            return response

        self.query_tool.url_base = "https://example.org"
        self.query_tool.token = secrets.token_urlsafe(6)
        request_mock.side_effect = mocking

        expected = None
        actual = self.query_tool.push(
            AvailabilityCheckerStatus(**self.availability_status_dataset))

        self.assertEqual(expected, actual)
    def test_push_subject_empty_str(self) -> None:
        """
        Tests the method which let us push some dataset into the collection.

        In this test, we test the case that the given subject is an empty string.
        """

        self.availability_status_dataset["subject"] = ""

        self.assertRaises(
            ValueError,
            lambda: self.query_tool.push(
                AvailabilityCheckerStatus(**self.availability_status_dataset)),
        )
    def test_push_checker_type_not_str(self) -> None:
        """
        Tests the method which let us push some dataset into the collection.

        In this test, we test the case that the given subject is not a string.
        """

        self.availability_status_dataset["checker_type"] = 987

        self.assertRaises(
            TypeError,
            lambda: self.query_tool.push(
                AvailabilityCheckerStatus(**self.availability_status_dataset)),
        )
    def test_push_checker_type_not_supported(self) -> None:
        """
        Tests the method which let us push some dataset into the collection.

        In this test, we test the case that the given subject is not a string.
        """

        self.availability_status_dataset["checker_type"] = "GIT"
        self.query_tool.token = secrets.token_urlsafe(6)

        self.assertRaises(
            ValueError,
            lambda: self.query_tool.push(
                AvailabilityCheckerStatus(**self.availability_status_dataset)),
        )
    def test_push_token_not_given(self) -> None:
        """
        Tests the method which let us push some dataset into the collection.

        In this test, we test the case that no token is given.
        """

        if "PYFUNCEBLE_COLLECTION_API_TOKEN" in os.environ:
            del os.environ["PYFUNCEBLE_COLLECTION_API_TOKEN"]

        self.query_tool.token = ""

        expected = None
        actual = self.query_tool.push(
            AvailabilityCheckerStatus(**self.availability_status_dataset))

        self.assertEqual(expected, actual)
    def test_push_with_whois_token_not_given(self) -> None:
        """
        Tests the method which let us push some dataset into the collection.

        In this test, we test the case that no token is given.
        """

        response_dict = self.response_dataset
        response_dict["subject"] = "example.net"
        self.availability_status_dataset["expiration_date"] = "23-nov-2090"

        if "PYFUNCEBLE_COLLECTION_API_TOKEN" in os.environ:
            del os.environ["PYFUNCEBLE_COLLECTION_API_TOKEN"]

        self.query_tool.token = ""

        expected = None
        actual = self.query_tool.push(
            AvailabilityCheckerStatus(**self.availability_status_dataset))

        self.assertEqual(expected, actual)
Exemple #8
0
    def setUp(self) -> None:
        """
        Setups everything we need.
        """

        self.status = AvailabilityCheckerStatus(subject="example.org")
Exemple #9
0
class TestAvailabilityCheckerStatus(unittest.TestCase):
    """
    Tests of our availability status handler.
    """
    def setUp(self) -> None:
        """
        Setups everything we need.
        """

        self.status = AvailabilityCheckerStatus(subject="example.org")

    def tearDown(self) -> None:
        """
        Destroys everything we need.
        """

        del self.status

    def test_is_special(self) -> None:
        """
        Tests the method which let us check if the current status is a special
        one.
        """

        self.status.status_after_extra_rules = "ACTIVE"
        self.status.status_before_extra_rules = "INACTIVE"

        self.status.status = "ACTIVE"
        self.status.status_source = "SPECIAL"

        expected = True
        actual = self.status.is_special()

        self.assertEqual(expected, actual)

    def test_is_not_special(self) -> None:
        """
        Tests the method which let us check if the current status is a not a
        special one.
        """

        self.status.status = "ACTIVE"
        self.status.status_source = "DNSLOOKUP"

        expected = False
        actual = self.status.is_special()

        self.assertEqual(expected, actual)

    def test_is_available(self) -> None:
        """
        Tests the method which let us check if the current status represent
        an available status.
        """

        self.status.status = "ACTIVE"
        self.status.status_source = "HTTP CODE"

        expected = True
        actual = self.status.is_available()

        self.assertEqual(expected, actual)

    def test_is_not_available(self) -> None:
        """
        Tests the method which let us check if the current represent a non-
        available status.
        """

        self.status.status = "INACTIVE"
        self.status.status_source = "STDLOOKUP"

        expected = False
        actual = self.status.is_available()

        self.assertEqual(expected, actual)

    def test_is_active(self) -> None:
        """
        Tests the method which let us check if the current status represent
        an active status.
        """

        self.status.status = "ACTIVE"
        self.status.status_source = "DNSLOOKUP"

        expected = True
        actual = self.status.is_active()

        self.assertEqual(expected, actual)

    def test_is_not_active(self) -> None:
        """
        Tests the method which let us check if the current represent a non-
        active status.
        """

        self.status.status = "INACTIVE"
        self.status.status_source = "DNSLOOKUP"

        expected = False
        actual = self.status.is_active()

        self.assertEqual(expected, actual)

    def test_is_inactive(self) -> None:
        """
        Tests the method which let us check if the current status represent
        an inactive status.
        """

        self.status.status = "INACTIVE"
        self.status.status_source = "DNSLOOKUP"

        expected = True
        actual = self.status.is_inactive()

        self.assertEqual(expected, actual)

    def test_is_not_inactive(self) -> None:
        """
        Tests the method which let us check if the current represent a non-
        inactive status.
        """

        self.status.status = "ACTIVE"
        self.status.status_source = "NETINFO"

        expected = False
        actual = self.status.is_inactive()

        self.assertEqual(expected, actual)

    def test_is_invalid(self) -> None:
        """
        Tests the method which let us check if the current status represent
        an invalid status.
        """

        self.status.status = "INVALID"
        self.status.status_source = "SYNTAX"

        expected = True
        actual = self.status.is_invalid()

        self.assertEqual(expected, actual)

    def test_is_not_invalid(self) -> None:
        """
        Tests the method which let us check if the current represent a non-
        invalid status.
        """

        self.status.status = "ACTIVE"
        self.status.status_source = "DNSLOOKUP"

        expected = False
        actual = self.status.is_invalid()

        self.assertEqual(expected, actual)
Exemple #10
0
    def __init__(
        self,
        subject: Optional[str] = None,
        *,
        use_extra_rules: Optional[bool] = None,
        use_whois_lookup: Optional[bool] = None,
        use_dns_lookup: Optional[bool] = None,
        use_netinfo_lookup: Optional[bool] = None,
        use_http_code_lookup: Optional[bool] = None,
        use_reputation_lookup: Optional[bool] = None,
        do_syntax_check_first: Optional[bool] = None,
        db_session: Optional[Session] = None,
        use_whois_db: Optional[bool] = None,
        use_collection: Optional[bool] = None,
    ) -> None:
        self.dns_query_tool = DNSQueryTool()
        self.whois_query_tool = WhoisQueryTool()
        self.addressinfo_query_tool = AddressInfo()
        self.hostbyaddr_query_tool = HostByAddrInfo()
        self.http_status_code_query_tool = HTTPStatusCode()
        self.domain_syntax_checker = DomainSyntaxChecker()
        self.ip_syntax_checker = IPSyntaxChecker()
        self.url_syntax_checker = URLSyntaxChecker()
        self.extra_rules_handler = ExtraRulesHandler()
        self.db_session = db_session

        self.params = AvailabilityCheckerParams()

        self.status = AvailabilityCheckerStatus()
        self.status.params = self.params
        self.status.dns_lookup_record = self.dns_query_tool.lookup_record
        self.status.whois_lookup_record = self.whois_query_tool.lookup_record

        if use_extra_rules is not None:
            self.use_extra_rules = use_extra_rules
        else:
            self.guess_and_set_use_extra_rules()

        if use_whois_lookup is not None:
            self.use_whois_lookup = use_whois_lookup
        else:
            self.guess_and_set_use_whois_lookup()

        if use_dns_lookup is not None:
            self.use_dns_lookup = use_dns_lookup
        else:
            self.guess_and_set_dns_lookup()

        if use_netinfo_lookup is not None:
            self.use_netinfo_lookup = use_netinfo_lookup
        else:
            self.guess_and_set_use_netinfo_lookup()

        if use_http_code_lookup is not None:
            self.use_http_code_lookup = use_http_code_lookup
        else:
            self.guess_and_set_use_http_code_lookup()

        if use_reputation_lookup is not None:
            self.use_reputation_lookup = use_reputation_lookup
        else:
            self.guess_and_set_use_reputation_lookup()

        if use_whois_db is not None:
            self.use_whois_db = use_whois_db
        else:
            self.guess_and_set_use_whois_db()

        super().__init__(
            subject,
            do_syntax_check_first=do_syntax_check_first,
            db_session=db_session,
            use_collection=use_collection,
        )