# Path: via/requests_tools/headers.py
# def add_request_headers(headers):
#     """Add headers for 3rd party providers which we access data from."""
# 
#     # Pass our abuse policy in request headers for third-party site admins.
#     headers["X-Abuse-Policy"] = "https://web.hypothes.is/abuse-policy/"
#     headers["X-Complaints-To"] = "https://web.hypothes.is/report-abuse/"
# 
#     return headers
# 
# Path: via/services/jstor.py
# class JSTORAPI:
#     DEFAULT_DOI_PREFIX = "10.2307"
#     """Main DOI prefix. Used for real DOIs and non registered pseudo-DOIs with the same format"""
# 
#     def __init__(self, http_service: HTTPService, url):
#         self._jstor_pdf_url = url
#         self._http = http_service
# 
#     @property
#     def enabled(self):
#         return bool(self._jstor_pdf_url)
# 
#     def jstor_pdf_stream(self, jstor_url: str, jstor_ip: str):
#         doi = jstor_url.replace("jstor://", "")
#         if not "/" in doi:
#             doi = f"{self.DEFAULT_DOI_PREFIX}/{doi}"
# 
#         url = f"{self._jstor_pdf_url}/{doi}?ip={jstor_ip}"
#         return self._http.stream(
#             url, headers=add_request_headers({"Accept": "application/pdf"})
#         )
# 
# def factory(_context, request):
#     return JSTORAPI(
#         http_service=request.find_service(HTTPService),
#         url=request.registry.settings.get("jstor_pdf_url", None),
#     )


from unittest.mock import sentinel
from via.requests_tools.headers import add_request_headers
from via.services.jstor import JSTORAPI, factory
import pytest


class TestJSTORAPI:
    @pytest.mark.parametrize(
        "url,expected", [(None, False), ("http://jstor-api.com", True)]
    )
    def test_enabled(self, url, expected):
        assert JSTORAPI(sentinel.http, url).enabled == expected

    @pytest.mark.parametrize(
        "url,expected",
        [
            ("jstor://ARTICLE_ID", "http://jstor-api.com/10.2307/ARTICLE_ID?ip=IP"),
            ("jstor://PREFIX/SUFFIX", "http://jstor-api.com/PREFIX/SUFFIX?ip=IP"),
        ],
    )
    def test_pdf_stream(self, http_service, url, expected):
        stream = JSTORAPI(http_service, "http://jstor-api.com").jstor_pdf_stream(
            url, "IP"
        )

        http_service.stream.assert_called_once_with(
            expected, headers=add_request_headers({"Accept": "application/pdf"})
        )
        assert stream == http_service.stream.return_value


class TestFactory:
    @pytest.mark.usefixtures("http_service")
    def test_it(self, pyramid_request):

# Path: via/requests_tools/headers.py
# def add_request_headers(headers):
#     """Add headers for 3rd party providers which we access data from."""
# 
#     # Pass our abuse policy in request headers for third-party site admins.
#     headers["X-Abuse-Policy"] = "https://web.hypothes.is/abuse-policy/"
#     headers["X-Complaints-To"] = "https://web.hypothes.is/report-abuse/"
# 
#     return headers
# 
# Path: via/services/jstor.py
# class JSTORAPI:
#     DEFAULT_DOI_PREFIX = "10.2307"
#     """Main DOI prefix. Used for real DOIs and non registered pseudo-DOIs with the same format"""
# 
#     def __init__(self, http_service: HTTPService, url):
#         self._jstor_pdf_url = url
#         self._http = http_service
# 
#     @property
#     def enabled(self):
#         return bool(self._jstor_pdf_url)
# 
#     def jstor_pdf_stream(self, jstor_url: str, jstor_ip: str):
#         doi = jstor_url.replace("jstor://", "")
#         if not "/" in doi:
#             doi = f"{self.DEFAULT_DOI_PREFIX}/{doi}"
# 
#         url = f"{self._jstor_pdf_url}/{doi}?ip={jstor_ip}"
#         return self._http.stream(
#             url, headers=add_request_headers({"Accept": "application/pdf"})
#         )
# 
# def factory(_context, request):
#     return JSTORAPI(
#         http_service=request.find_service(HTTPService),
#         url=request.registry.settings.get("jstor_pdf_url", None),
#     )


# Path: tests/unit/via/services/jstor_test.py
from unittest.mock import sentinel
from via.requests_tools.headers import add_request_headers
from via.services.jstor import JSTORAPI, factory
import pytest



class TestJSTORAPI:
    @pytest.mark.parametrize(
        "url,expected", [(None, False), ("http://jstor-api.com", True)]
    )
    def test_enabled(self, url, expected):
        assert JSTORAPI(sentinel.http, url).enabled == expected

    @pytest.mark.parametrize(
        "url,expected",
        [
            ("jstor://ARTICLE_ID", "http://jstor-api.com/10.2307/ARTICLE_ID?ip=IP"),
            ("jstor://PREFIX/SUFFIX", "http://jstor-api.com/PREFIX/SUFFIX?ip=IP"),
        ],
    )
    def test_pdf_stream(self, http_service, url, expected):
        stream = JSTORAPI(http_service, "http://jstor-api.com").jstor_pdf_stream(
            url, "IP"
        )

        http_service.stream.assert_called_once_with(
            expected, headers=add_request_headers({"Accept": "application/pdf"})
        )
        assert stream == http_service.stream.return_value


class TestFactory:
    @pytest.mark.usefixtures("http_service")
    def test_it(self, pyramid_request):

        assert isinstance(factory(sentinel.context, pyramid_request), JSTORAPI)