# -*- coding: utf-8 -*-
#
# Copyright (C) 2026 XiVO Solutions
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

from hamcrest import assert_that
from hamcrest import equal_to
from hamcrest import is_not
from hamcrest import contains_string

from .base_dird_integration_test import BaseDirdIntegrationTest
from .base_dird_integration_test import VALID_TOKEN
from .base_dird_integration_test import VALID_UUID


class TestPhoneLookupByLabelDatabaseDown(BaseDirdIntegrationTest):
    asset = 'lookup_by_labels_db_down'

    # This asset configures a `db_uri` that points at an unreachable database.
    # The request must fail closed and not fallback.
    #
    # Contrast with TestPhoneLookupByLabel (asset `lookup_by_labels`), where no
    # `db_uri` is configured at all: degradation to `default_sources`.

    def test_that_an_unreadable_label_database_fails_closed(self):
        response = self.get_lookup_by_label_cisco_result(VALID_UUID,
                                                         term='smith',
                                                         token=VALID_TOKEN)

        assert_that(response.status_code, equal_to(503))

    def test_that_no_directory_result_is_leaked_when_labels_cannot_be_read(self):
        response = self.get_lookup_by_label_cisco_result(VALID_UUID,
                                                         term='smith',
                                                         token=VALID_TOKEN)

        assert_that(response.text, is_not(contains_string('Default')))
        assert_that(response.text, is_not(contains_string('1000')))

    def test_that_authorization_is_still_enforced(self):
        response = self.get_lookup_by_label_cisco_result(VALID_UUID,
                                                         term='smith',
                                                         token='unknown-token')

        assert_that(response.status_code, equal_to(401))
