def test_unknown_place_within_country(self):
        # Test an authentication document that names an unknown
        # place within a country.
        sf = MockPlace()
        us = MockPlace(inside={"San Francisco": sf})
        MockPlace.by_name["US"] = us

        self.parse_places({"US": "Nowheresville"},
                          expected_unknown={"US": ["Nowheresville"]})
 def test_name_treated_as_title(self):
     """Some invalid documents put the library name in 'name' instead of title.
     We can handle these documents.
     """
     document = dict(name="My library")
     auth = AuthDoc.from_dict(None, document, MockPlace())
     eq_("My library", auth.title)
    def test_empty_document(self):
        """Provide an empty Authentication For OPDS document to test
        default values.
        """
        place = MockPlace()
        everywhere = place.everywhere(None)
        parsed = AuthDoc.from_string(None, "{}", place)

        # In the absence of specific information, it's assumed the
        # OPDS server is open to everyone.
        eq_(([everywhere], {}, {}), parsed.service_area)
        eq_(([everywhere], {}, {}), parsed.focus_area)
        eq_([parsed.PUBLIC_AUDIENCE], parsed.audiences)

        eq_(None, parsed.id)
        eq_(None, parsed.title)
        eq_(None, parsed.service_description)
        eq_(None, parsed.color_scheme)
        eq_(None, parsed.collection_size)
        eq_(None, parsed.public_key)
        eq_(None, parsed.website)
        eq_(False, parsed.online_registration)
        eq_(None, parsed.root)
        eq_([], parsed.links)
        eq_(None, parsed.logo)
        eq_(None, parsed.logo_link)
        eq_(False, parsed.anonymous_access)
    def test_ambiguous_place_within_country(self):
        # Test an authentication document that names an ambiguous
        # place within a country.
        us = MockPlace(inside={"Springfield": MockPlace.AMBIGUOUS})
        MockPlace.by_name["US"] = us

        self.parse_places({"US": ["Springfield"]},
                          expected_ambiguous={"US": ["Springfield"]})
 def test_anonymous_access(self):
     """You can signal that your OPDS server allows anonymous access by
     including it as an authentication type.
     """
     document = dict(authentication=[
         dict(type="http://opds-spec.org/auth/basic"),
         dict(type="https://librarysimplified.org/rel/auth/anonymous")
     ])
     auth = AuthDoc.from_dict(None, document, MockPlace())
     eq_(True, auth.anonymous_access)
    def test_places_within_country(self):
        # Test an authentication document that says a library
        # covers one or more places within a country.

        # This authentication document covers two places called
        # "San Francisco" (one in the US and one in Mexico) as well as a
        # place called "Mexico City" in Mexico.
        #
        # Note that it's invalid to map a country name to a single
        # place name (it's supposed to always be a list), but our
        # parser can handle it.
        doc = {"US": "San Francisco", "MX": ["San Francisco", "Mexico City"]}

        place1 = MockPlace()
        place2 = MockPlace()
        place3 = MockPlace()
        place4 = MockPlace()
        us = MockPlace(inside={"San Francisco": place1, "San Jose": place2})
        mx = MockPlace(inside={"San Francisco": place3, "Mexico City": place4})
        MockPlace.by_name["US"] = us
        MockPlace.by_name["MX"] = mx

        # AuthenticationDocument.parse_coverage is able to turn those
        # three place names into place objects.
        self.parse_places(doc, expected_places=[place1, place3, place4])
 def test_logo_link(self):
     """You can link to your logo, instead of including it in the
     document.
     """
     document = {
         "links": [dict(rel="logo", href="http://logo.com/logo.jpg")]
     }
     auth = AuthDoc.from_dict(None, document, MockPlace())
     eq_(None, auth.logo)
     eq_({
         "href": "http://logo.com/logo.jpg",
         "rel": "logo"
     }, auth.logo_link)
    def test_unknown_country(self):
        # Test an authentication document that says a library covers an
        # entire country, but the library registry doesn't know anything about
        # that country's geography.

        canada = MockPlace()
        MockPlace.by_name["CA"] = canada
        self.parse_places(
            {
                "Memory Alpha": self.EVERYWHERE,
                "CA": self.EVERYWHERE
            },
            expected_places=[canada],
            expected_unknown={"Memory Alpha": self.EVERYWHERE})
    def test_ambiguous_country(self):
        # Test the unlikely scenario where an authentication document says a
        # library covers an entire country, but it's ambiguous which
        # country is being referred to.

        canada = MockPlace()
        MockPlace.by_name["CA"] = canada
        MockPlace.by_name["Europe I think?"] = MockPlace.AMBIGUOUS
        self.parse_places(
            {
                "Europe I think?": self.EVERYWHERE,
                "CA": self.EVERYWHERE
            },
            expected_places=[canada],
            expected_ambiguous={"Europe I think?": self.EVERYWHERE})
    def test_unscoped_place_is_in_default_nation(self):
        # Test an authentication document that names places without
        # saying which nation they're in.
        ca = MockPlace()
        ut = MockPlace()

        # Without a default nation on the server side, we can't make
        # sense of these place names.
        self.parse_places("CA", expected_unknown={"??": "CA"})

        self.parse_places(["CA", "UT"], expected_unknown={"??": ["CA", "UT"]})

        us = MockPlace(inside={"CA": ca, "UT": ut})
        us.abbreviated_name = "US"
        MockPlace.by_name["US"] = us

        # With a default nation in place, a bare string like "CA"
        # is treated the same as a correctly formatted dictionary
        # like {"US": ["CA"]}
        MockPlace._default_nation = us
        self.parse_places("CA", expected_places=[ca])
        self.parse_places(["CA", "UT"], expected_places=[ca, ut])

        MockPlace._default_nation = None
 def test_entire_country(self):
     # Test an authentication document that says a library covers an
     # entire country.
     us = MockPlace()
     MockPlace.by_name["US"] = us
     self.parse_places({"US": self.EVERYWHERE}, expected_places=[us])
 def test_audiences(self):
     """You can specify the target audiences for your OPDS server."""
     document = {"audience": ["educational-secondary", "research"]}
     auth = AuthDoc.from_dict(None, document, MockPlace())
     eq_(["educational-secondary", "research"], auth.audiences)
    def test_real_document(self):
        """Test an Authentication For OPDS document that demonstrates
        most of the features we're looking for.
        """
        document = {
            "id":
            "http://library/authentication-for-opds-file",
            "title":
            "Ansonia Public Library",
            "links": [{
                "rel": "logo",
                "href": "data:image/png;base64,some-image-data",
                "type": "image/png"
            }, {
                "rel": "alternate",
                "href": "http://ansonialibrary.org",
                "type": "text/html"
            }, {
                "rel": "register",
                "href": "http://example.com/get-a-card/",
                "type": "text/html"
            }, {
                "rel": "start",
                "href": "http://catalog.example.com/",
                "type": "text/html/"
            }, {
                "rel": "start",
                "href": "http://opds.example.com/",
                "type": "application/atom+xml;profile=opds-catalog"
            }],
            "service_description":
            "Serving Ansonia, CT",
            "color_scheme":
            "gold",
            "collection_size": {
                "eng": 100,
                "spa": 20
            },
            "public_key":
            "a public key",
            "features": {
                "disabled": [],
                "enabled":
                ["https://librarysimplified.org/rel/policy/reservations"]
            },
            "authentication": [{
                "type": "http://opds-spec.org/auth/basic",
                "description": "Log in with your library barcode",
                "inputs": {
                    "login": {
                        "keyboard": "Default"
                    },
                    "password": {
                        "keyboard": "Default"
                    }
                },
                "labels": {
                    "login": "******",
                    "password": "******"
                }
            }]
        }

        place = MockPlace()
        everywhere = place.everywhere(None)
        parsed = AuthDoc.from_dict(None, document, place)

        # Information about the OPDS server has been extracted from
        # JSON and put into the AuthenticationDocument object.
        eq_("http://library/authentication-for-opds-file", parsed.id)
        eq_("Ansonia Public Library", parsed.title)
        eq_("Serving Ansonia, CT", parsed.service_description)
        eq_("gold", parsed.color_scheme)
        eq_({"eng": 100, "spa": 20}, parsed.collection_size)
        eq_("a public key", parsed.public_key)
        eq_(
            {
                u'rel': 'alternate',
                u'href': u'http://ansonialibrary.org',
                u'type': u'text/html'
            }, parsed.website)
        eq_(True, parsed.online_registration)
        eq_(
            {
                "rel": "start",
                "href": "http://opds.example.com/",
                "type": "application/atom+xml;profile=opds-catalog"
            }, parsed.root)
        eq_("data:image/png;base64,some-image-data", parsed.logo)
        eq_(None, parsed.logo_link)
        eq_(False, parsed.anonymous_access)