# Path: via/services/secure_link.py
# class SecureLinkService:
#     """A service for signing and checking URLs have been signed."""
# 
#     def __init__(self, secret, signed_urls_required):
#         """Initialise the service.
# 
#         :param secret: Shared secret to sign and verify URLs with
#         :param signed_urls_required: Enable or disable URL signing
#         """
#         self._via_secure_url = ViaSecureURL(secret)
#         self._signed_urls_required = signed_urls_required
# 
#     def request_is_valid(self, request) -> bool:
#         """Check whether a request has been signed.
# 
#         This will also pass if signing is disabled.
#         """
#         if not self._signed_urls_required:
#             return True
# 
#         try:
#             self._via_secure_url.verify(request.url)
#         except TokenException:
#             return False
# 
#         return True
# 
#     def sign_url(self, url):
#         """Get a signed URL (if URL signing is enabled)."""
# 
#         if self._signed_urls_required:
#             return self._via_secure_url.create(url, max_age=timedelta(hours=25))
# 
#         return url
# 
# def factory(_context, request):
#     return SecureLinkService(
#         secret=request.registry.settings["via_secret"],
#         signed_urls_required=request.registry.settings["signed_urls_required"],
#     )
# 
# def has_secure_url_token(view):
#     """Require the request to have a valid signature."""
# 
#     def view_wrapper(context, request):
#         if not request.find_service(SecureLinkService).request_is_valid(request):
#             return HTTPUnauthorized()
# 
#         return view(context, request)
# 
#     return view_wrapper


from datetime import timedelta
from unittest.mock import create_autospec, sentinel
from h_vialib.exceptions import TokenException
from pyramid.httpexceptions import HTTPUnauthorized
from via.services.secure_link import SecureLinkService, factory, has_secure_url_token
import pytest
        service._via_secure_url.verify.assert_not_called()

    def test_sign_url(self, service):
        result = service.sign_url(sentinel.url)

        service._via_secure_url.create.assert_called_once_with(
            sentinel.url, max_age=timedelta(hours=25)
        )
        assert result == service._via_secure_url.create.return_value

    @pytest.mark.usefixtures("with_signed_urls_not_required")
    def test_sign_url_if_signatures_not_required(self, service):
        result = service.sign_url(sentinel.url)

        assert result == sentinel.url

    @pytest.fixture
    def service(self):
        return SecureLinkService(secret="not_a_secret", signed_urls_required=True)

    @pytest.fixture
    def with_signed_urls_not_required(self, service):
        service._signed_urls_required = False

    @pytest.fixture(autouse=True)
    def ViaSecureURL(self, patch):
        return patch("via.services.secure_link.ViaSecureURL")


class TestFactory:

# Path: via/services/secure_link.py
# class SecureLinkService:
#     """A service for signing and checking URLs have been signed."""
# 
#     def __init__(self, secret, signed_urls_required):
#         """Initialise the service.
# 
#         :param secret: Shared secret to sign and verify URLs with
#         :param signed_urls_required: Enable or disable URL signing
#         """
#         self._via_secure_url = ViaSecureURL(secret)
#         self._signed_urls_required = signed_urls_required
# 
#     def request_is_valid(self, request) -> bool:
#         """Check whether a request has been signed.
# 
#         This will also pass if signing is disabled.
#         """
#         if not self._signed_urls_required:
#             return True
# 
#         try:
#             self._via_secure_url.verify(request.url)
#         except TokenException:
#             return False
# 
#         return True
# 
#     def sign_url(self, url):
#         """Get a signed URL (if URL signing is enabled)."""
# 
#         if self._signed_urls_required:
#             return self._via_secure_url.create(url, max_age=timedelta(hours=25))
# 
#         return url
# 
# def factory(_context, request):
#     return SecureLinkService(
#         secret=request.registry.settings["via_secret"],
#         signed_urls_required=request.registry.settings["signed_urls_required"],
#     )
# 
# def has_secure_url_token(view):
#     """Require the request to have a valid signature."""
# 
#     def view_wrapper(context, request):
#         if not request.find_service(SecureLinkService).request_is_valid(request):
#             return HTTPUnauthorized()
# 
#         return view(context, request)
# 
#     return view_wrapper


# Path: tests/unit/via/services/secure_link_test.py
from datetime import timedelta
from unittest.mock import create_autospec, sentinel
from h_vialib.exceptions import TokenException
from pyramid.httpexceptions import HTTPUnauthorized
from via.services.secure_link import SecureLinkService, factory, has_secure_url_token
import pytest

        service._via_secure_url.verify.assert_not_called()

    def test_sign_url(self, service):
        result = service.sign_url(sentinel.url)

        service._via_secure_url.create.assert_called_once_with(
            sentinel.url, max_age=timedelta(hours=25)
        )
        assert result == service._via_secure_url.create.return_value

    @pytest.mark.usefixtures("with_signed_urls_not_required")
    def test_sign_url_if_signatures_not_required(self, service):
        result = service.sign_url(sentinel.url)

        assert result == sentinel.url

    @pytest.fixture
    def service(self):
        return SecureLinkService(secret="not_a_secret", signed_urls_required=True)

    @pytest.fixture
    def with_signed_urls_not_required(self, service):
        service._signed_urls_required = False

    @pytest.fixture(autouse=True)
    def ViaSecureURL(self, patch):
        return patch("via.services.secure_link.ViaSecureURL")


class TestFactory:

    def test_it(self, pyramid_request, SecureLinkService):