def test_links_filtered(self): # test that filter links to only metadata-relevant ones link1 = LinkData(Hyperlink.OPEN_ACCESS_DOWNLOAD, "example.epub") link2 = LinkData(rel=Hyperlink.IMAGE, href="http://example.com/") link3 = LinkData(rel=Hyperlink.DESCRIPTION, content="foo") link4 = LinkData( rel=Hyperlink.THUMBNAIL_IMAGE, href="http://thumbnail.com/", media_type=Representation.JPEG_MEDIA_TYPE, ) link5 = LinkData( rel=Hyperlink.IMAGE, href="http://example.com/", thumbnail=link4, media_type=Representation.JPEG_MEDIA_TYPE, ) links = [link1, link2, link3, link4, link5] identifier = IdentifierData(Identifier.GUTENBERG_ID, "1") metadata = Metadata( data_source=DataSource.GUTENBERG, primary_identifier=identifier, links=links, ) filtered_links = sorted(metadata.links, key=lambda x: x.rel) eq_([link2, link5, link4, link3], filtered_links)
def test_rights_status_open_access_link_no_rights_uses_data_source_default( self): identifier = IdentifierData( Identifier.GUTENBERG_ID, "abcd", ) # Here's a CirculationData that will create an open-access # LicensePoolDeliveryMechanism. link = LinkData(rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url) circulation_data = CirculationData( data_source=DataSource.GUTENBERG, primary_identifier=identifier, links=[link], ) replace_formats = ReplacementPolicy(formats=True, ) pool, ignore = circulation_data.license_pool(self._db, self._default_collection) pool.open_access = False # Applying this CirculationData to a LicensePool makes it # open-access. circulation_data.apply(self._db, pool.collection, replace_formats) eq_(True, pool.open_access) eq_(1, pool.delivery_mechanisms.count()) # The delivery mechanism's rights status is the default for # the data source. eq_(RightsStatus.PUBLIC_DOMAIN_USA, pool.delivery_mechanisms[0].rights_status.uri) # Even if a commercial source like Overdrive should offer a # link with rel="open access", unless we know it's an # open-access link we will give it a RightsStatus of # IN_COPYRIGHT. identifier = IdentifierData( Identifier.OVERDRIVE_ID, "abcd", ) link = LinkData(rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url) circulation_data = CirculationData( data_source=DataSource.OVERDRIVE, primary_identifier=identifier, links=[link], ) pool, ignore = circulation_data.license_pool(self._db, self._default_collection) pool.open_access = False circulation_data.apply(self._db, pool.collection, replace_formats) eq_(RightsStatus.IN_COPYRIGHT, pool.delivery_mechanisms[0].rights_status.uri) eq_(False, pool.open_access)
def test_links_filtered(self): # Tests that passed-in links filter down to only the relevant ones. link1 = LinkData(Hyperlink.OPEN_ACCESS_DOWNLOAD, "example.epub") link2 = LinkData(rel=Hyperlink.IMAGE, href="http://example.com/") link3 = LinkData(rel=Hyperlink.DESCRIPTION, content="foo") link4 = LinkData( rel=Hyperlink.THUMBNAIL_IMAGE, href="http://thumbnail.com/", media_type=Representation.JPEG_MEDIA_TYPE, ) link5 = LinkData( rel=Hyperlink.IMAGE, href="http://example.com/", thumbnail=link4, media_type=Representation.JPEG_MEDIA_TYPE, ) links = [link1, link2, link3, link4, link5] identifier = IdentifierData(Identifier.GUTENBERG_ID, "1") circulation_data = CirculationData( DataSource.GUTENBERG, primary_identifier=identifier, links=links, ) filtered_links = sorted(circulation_data.links, key=lambda x:x.rel) eq_([link1], filtered_links)
def test_rights_status_open_access_link_no_rights_uses_data_source_default(self): identifier = IdentifierData( Identifier.GUTENBERG_ID, "abcd", ) link = LinkData( rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url ) circulation_data = CirculationData( data_source=DataSource.GUTENBERG, primary_identifier=identifier, links=[link], ) replace = ReplacementPolicy( formats=True, ) pool, ignore = circulation_data.license_pool(self._db) circulation_data.apply(pool, replace) eq_(True, pool.open_access) eq_(1, len(pool.delivery_mechanisms)) # The delivery mechanism's rights status is the default for # the data source. eq_(RightsStatus.PUBLIC_DOMAIN_USA, pool.delivery_mechanisms[0].rights_status.uri) # Even if a commercial source like Overdrive should offer a # link with rel="open access", unless we know it's an # open-access link we will give it a RightsStatus of # IN_COPYRIGHT. identifier = IdentifierData( Identifier.OVERDRIVE_ID, "abcd", ) link = LinkData( rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url ) circulation_data = CirculationData( data_source=DataSource.OVERDRIVE, primary_identifier=identifier, links=[link], ) pool, ignore = circulation_data.license_pool(self._db) circulation_data.apply(pool, replace) eq_(RightsStatus.IN_COPYRIGHT, pool.delivery_mechanisms[0].rights_status.uri) # This will cause the work to be treated as a non-open-access # work. eq_(False, pool.open_access)
def test_links(self): edition = self._edition() l1 = LinkData(rel=Hyperlink.IMAGE, href="http://example.com/") l2 = LinkData(rel=Hyperlink.DESCRIPTION, content="foo") metadata = Metadata(links=[l1, l2], data_source=edition.data_source) metadata.apply(edition) [image, description] = sorted(edition.primary_identifier.links, key=lambda x: x.rel) eq_(Hyperlink.IMAGE, image.rel) eq_("http://example.com/", image.resource.url) eq_(Hyperlink.DESCRIPTION, description.rel) eq_("foo", description.resource.representation.content)
def test_rights_status_default_rights_passed_in(self): identifier = IdentifierData( Identifier.GUTENBERG_ID, "abcd", ) link = LinkData(rel=Hyperlink.DRM_ENCRYPTED_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url) circulation_data = CirculationData( data_source=DataSource.OA_CONTENT_SERVER, primary_identifier=identifier, default_rights_uri=RightsStatus.CC_BY, links=[link], ) replace = ReplacementPolicy(formats=True, ) pool, ignore = circulation_data.license_pool(self._db, self._default_collection) circulation_data.apply(self._db, pool.collection, replace) eq_(True, pool.open_access) eq_(1, pool.delivery_mechanisms.count()) # The rights status is the one that was passed in to CirculationData. eq_(RightsStatus.CC_BY, pool.delivery_mechanisms[0].rights_status.uri)
def image_link_to_linkdata(cls, link, rel): if not link: return None return LinkData(rel=rel, href=link, media_type=Representation.JPEG_MEDIA_TYPE)
def test_non_open_access_book_not_mirrored(self): data_source = DataSource.lookup(self._db, DataSource.GUTENBERG) m = Metadata(data_source=data_source) mirror = DummyS3Uploader(fail=True) h = DummyHTTPClient() policy = ReplacementPolicy(mirror=mirror, http_get=h.do_get) content = "foo" link = LinkData(rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href="http://example.com/", content=content, rights_uri=RightsStatus.IN_COPYRIGHT) identifier = self._identifier() link_obj, is_new = identifier.add_link( rel=link.rel, href=link.href, data_source=data_source, media_type=link.media_type, content=link.content, ) # The Hyperlink object makes it look like an open-access book, # but the context we have from the OPDS feed says that it's # not. m.mirror_link(None, data_source, link, link_obj, policy) # No HTTP requests were made. eq_([], h.requests) # Nothing was uploaded. eq_([], mirror.uploaded)
def test_make_thumbnail_assigns_pool(self): identifier = IdentifierData(Identifier.GUTENBERG_ID, "1") #identifier = self._identifier() #identifier = IdentifierData(type=Identifier.GUTENBERG_ID, identifier=edition.primary_identifier) edition = self._edition(identifier_id=identifier.identifier) link = LinkData( rel=Hyperlink.THUMBNAIL_IMAGE, href="http://thumbnail.com/", media_type=Representation.JPEG_MEDIA_TYPE, ) metadata = Metadata( data_source=edition.data_source, primary_identifier=identifier, links=[link], ) circulation = CirculationData(data_source=edition.data_source, primary_identifier=identifier) metadata.circulation = circulation metadata.apply(edition) thumbnail_link = edition.primary_identifier.links[0] circulation_pool, is_new = circulation.license_pool(self._db) eq_(thumbnail_link.license_pool, circulation_pool)
def test_rights_status_default_rights_from_data_source(self): identifier = IdentifierData( Identifier.GUTENBERG_ID, "abcd", ) link = LinkData(rel=Hyperlink.DRM_ENCRYPTED_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url) circulation_data = CirculationData( data_source=DataSource.OA_CONTENT_SERVER, primary_identifier=identifier, links=[link], ) replace = ReplacementPolicy(formats=True, ) # This pool starts off as not being open-access. pool, ignore = circulation_data.license_pool(self._db, self._default_collection) eq_(False, pool.open_access) circulation_data.apply(self._db, pool.collection, replace) # The pool became open-access because it was given a # link that came from the OS content server. eq_(True, pool.open_access) eq_(1, pool.delivery_mechanisms.count()) # The rights status is the default for the OA content server. eq_(RightsStatus.GENERIC_OPEN_ACCESS, pool.delivery_mechanisms[0].rights_status.uri)
def test_circulationdata_can_be_deepcopied(self): # Check that we didn't put something in the CirculationData that # will prevent it from being copied. (e.g., self.log) subject = SubjectData(Subject.TAG, "subject") contributor = ContributorData() identifier = IdentifierData(Identifier.GUTENBERG_ID, "1") link = LinkData(Hyperlink.OPEN_ACCESS_DOWNLOAD, "example.epub") format = FormatData(Representation.EPUB_MEDIA_TYPE, DeliveryMechanism.NO_DRM) rights_uri = RightsStatus.GENERIC_OPEN_ACCESS circulation_data = CirculationData( DataSource.GUTENBERG, primary_identifier=identifier, links=[link], licenses_owned=5, licenses_available=5, licenses_reserved=None, patrons_in_hold_queue=None, formats=[format], default_rights_uri=rights_uri, ) circulation_data_copy = deepcopy(circulation_data) # If deepcopy didn't throw an exception we're ok. assert circulation_data_copy is not None
def test_rights_status_open_access_link_with_rights(self): identifier = IdentifierData( Identifier.OVERDRIVE_ID, "abcd", ) link = LinkData( rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url, rights_uri=RightsStatus.CC_BY_ND, ) circulation_data = CirculationData( data_source=DataSource.OVERDRIVE, primary_identifier=identifier, links=[link], ) replace = ReplacementPolicy( formats=True, ) pool, ignore = circulation_data.license_pool(self._db) circulation_data.apply(pool, replace) eq_(True, pool.open_access) eq_(1, len(pool.delivery_mechanisms)) eq_(RightsStatus.CC_BY_ND, pool.delivery_mechanisms[0].rights_status.uri)
def test_rights_status_open_access_link_no_rights(self): identifier = IdentifierData( Identifier.OVERDRIVE_ID, "abcd", ) link = LinkData( rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url ) circulation_data = CirculationData( data_source=DataSource.OVERDRIVE, primary_identifier=identifier, links=[link], ) replace = ReplacementPolicy( formats=True, ) pool, ignore = circulation_data.license_pool(self._db) circulation_data.apply(pool, replace) eq_(True, pool.open_access) eq_(1, len(pool.delivery_mechanisms)) # Rights status is generic open access because there's an open access # link but no other rights info. eq_(RightsStatus.GENERIC_OPEN_ACCESS, pool.delivery_mechanisms[0].rights_status.uri)
def test_rights_status_commercial_link_with_rights(self): identifier = IdentifierData( Identifier.OVERDRIVE_ID, "abcd", ) link = LinkData( rel=Hyperlink.DRM_ENCRYPTED_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url, rights_uri=RightsStatus.IN_COPYRIGHT, ) format = FormatData( content_type=link.media_type, drm_scheme=DeliveryMechanism.ADOBE_DRM, link=link, rights_uri=RightsStatus.IN_COPYRIGHT, ) circulation_data = CirculationData( data_source=DataSource.OVERDRIVE, primary_identifier=identifier, links=[link], formats=[format], ) replace = ReplacementPolicy( formats=True, ) pool, ignore = circulation_data.license_pool(self._db) circulation_data.apply(pool, replace) eq_(False, pool.open_access) eq_(1, len(pool.delivery_mechanisms)) eq_(RightsStatus.IN_COPYRIGHT, pool.delivery_mechanisms[0].rights_status.uri)
def test_rights_status_default_rights_from_data_source(self): identifier = IdentifierData( Identifier.GUTENBERG_ID, "abcd", ) link = LinkData( rel=Hyperlink.DRM_ENCRYPTED_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url ) circulation_data = CirculationData( data_source=DataSource.OA_CONTENT_SERVER, primary_identifier=identifier, links=[link], ) replace = ReplacementPolicy( formats=True, ) pool, ignore = circulation_data.license_pool(self._db) circulation_data.apply(pool, replace) eq_(True, pool.open_access) eq_(1, len(pool.delivery_mechanisms)) # The rights status is the default for the OA content server. eq_(RightsStatus.GENERIC_OPEN_ACCESS, pool.delivery_mechanisms[0].rights_status.uri)
def test_mirror_404_error(self): mirror = DummyS3Uploader() h = DummyHTTPClient() h.queue_response(404) policy = ReplacementPolicy(mirror=mirror, http_get=h.do_get) edition, pool = self._edition(with_license_pool=True) data_source = DataSource.lookup(self._db, DataSource.GUTENBERG) link = LinkData( rel=Hyperlink.IMAGE, media_type=Representation.JPEG_MEDIA_TYPE, href="http://example.com/", ) link_obj, ignore = edition.primary_identifier.add_link( rel=link.rel, href=link.href, data_source=data_source, license_pool=pool, media_type=link.media_type, content=link.content, ) m = Metadata(data_source=data_source) m.mirror_link(edition, data_source, link, link_obj, policy) # Since we got a 404 error, the cover image was not mirrored. eq_(404, link_obj.resource.representation.status_code) eq_(None, link_obj.resource.representation.mirror_url) eq_([], mirror.uploaded)
def test_has_open_access_link(self): identifier = IdentifierData(Identifier.GUTENBERG_ID, "1") circulationdata = CirculationData( DataSource.GUTENBERG, identifier, ) # No links eq_(False, circulationdata.has_open_access_link) linkdata = LinkData( rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, href=self._url, ) circulationdata.links = [linkdata] # Open-access link with no explicit rights URI. eq_(True, circulationdata.has_open_access_link) # Open-access link with contradictory rights URI. linkdata.rights_uri = RightsStatus.IN_COPYRIGHT eq_(False, circulationdata.has_open_access_link) # Open-access link with consistent rights URI. linkdata.rights_uri = RightsStatus.GENERIC_OPEN_ACCESS eq_(True, circulationdata.has_open_access_link)
def test_consolidate_links(self): # If a link turns out to be a dud, consolidate_links() # gets rid of it. links = [None, None] eq_([], OPDSImporter.consolidate_links(links)) links = [ LinkData(href=self._url, rel=rel, media_type="image/jpeg") for rel in [ Hyperlink.OPEN_ACCESS_DOWNLOAD, Hyperlink.IMAGE, Hyperlink.THUMBNAIL_IMAGE, Hyperlink.OPEN_ACCESS_DOWNLOAD ] ] old_link = links[2] links = OPDSImporter.consolidate_links(links) eq_([ Hyperlink.OPEN_ACCESS_DOWNLOAD, Hyperlink.IMAGE, Hyperlink.OPEN_ACCESS_DOWNLOAD ], [x.rel for x in links]) link = links[1] eq_(old_link, link.thumbnail) links = [ LinkData(href=self._url, rel=rel, media_type="image/jpeg") for rel in [ Hyperlink.THUMBNAIL_IMAGE, Hyperlink.IMAGE, Hyperlink.THUMBNAIL_IMAGE, Hyperlink.IMAGE ] ] t1, i1, t2, i2 = links links = OPDSImporter.consolidate_links(links) eq_([Hyperlink.IMAGE, Hyperlink.IMAGE], [x.rel for x in links]) eq_(t1, i1.thumbnail) eq_(t2, i2.thumbnail) links = [ LinkData(href=self._url, rel=rel, media_type="image/jpeg") for rel in [Hyperlink.THUMBNAIL_IMAGE, Hyperlink.IMAGE, Hyperlink.IMAGE] ] t1, i1, i2 = links links = OPDSImporter.consolidate_links(links) eq_([Hyperlink.IMAGE, Hyperlink.IMAGE], [x.rel for x in links]) eq_(t1, i1.thumbnail) eq_(None, i2.thumbnail)
def make_link_data(cls, rel, href=None, media_type=None, rights_uri=None, content=None): """Hook method for creating a LinkData object. Intended to be overridden in subclasses. """ return LinkData(rel=rel, href=href, media_type=media_type, rights_uri=rights_uri, content=content )
def image_link_to_linkdata(cls, link_url, rel): if not link_url or (link_url.find("http") < 0): return None media_type = None if link_url.endswith(".jpg"): media_type = "image/jpeg" return LinkData(rel=rel, href=link_url, media_type=media_type)
def test_mirror_open_access_link_mirror_failure(self): edition, pool = self._edition(with_license_pool=True) data_source = DataSource.lookup(self._db, DataSource.GUTENBERG) m = Metadata(data_source=data_source) mirror = DummyS3Uploader(fail=True) h = DummyHTTPClient() policy = ReplacementPolicy(mirror=mirror, http_get=h.do_get) content = open(self.sample_cover_path("test-book-cover.png")).read() link = LinkData(rel=Hyperlink.IMAGE, media_type=Representation.JPEG_MEDIA_TYPE, href="http://example.com/", content=content) link_obj, ignore = edition.primary_identifier.add_link( rel=link.rel, href=link.href, data_source=data_source, license_pool=pool, media_type=link.media_type, content=link.content, ) h.queue_response(200, media_type=Representation.JPEG_MEDIA_TYPE) m.mirror_link(edition, data_source, link, link_obj, policy) representation = link_obj.resource.representation # The representation was fetched successfully. eq_(None, representation.fetch_exception) assert representation.fetched_at != None # But mirroing failed. assert representation.mirror_exception != None eq_(None, representation.mirrored_at) eq_(link.media_type, representation.media_type) eq_(link.href, representation.url) # The mirror url should still be set. assert "Gutenberg" in representation.mirror_url assert representation.mirror_url.endswith( "%s/cover.jpg" % edition.primary_identifier.identifier) # Book content is still there since it wasn't mirrored. assert representation.content != None # the edition's identifier-associated license pool should not be # suppressed just because fetch failed on getting image. eq_(False, pool.suppressed) # the license pool only gets its license_exception column filled in # if fetch failed on getting an Hyperlink.OPEN_ACCESS_DOWNLOAD-type epub. eq_(None, pool.license_exception)
def test_mirror_open_access_link_mirror_failure(self): mirror = DummyS3Uploader(fail=True) h = DummyHTTPClient() edition, pool = self._edition(with_license_pool=True) data_source = DataSource.lookup(self._db, DataSource.GUTENBERG) policy = ReplacementPolicy(mirror=mirror, http_get=h.do_get) circulation_data = CirculationData( data_source=edition.data_source, primary_identifier=edition.primary_identifier, ) link = LinkData( rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url, ) link_obj, ignore = edition.primary_identifier.add_link( rel=link.rel, href=link.href, data_source=data_source, license_pool=pool, media_type=link.media_type, content=link.content, ) h.queue_response(200, media_type=Representation.EPUB_MEDIA_TYPE) circulation_data.mirror_link(pool, data_source, link, link_obj, policy) representation = link_obj.resource.representation # The representation was fetched successfully. eq_(None, representation.fetch_exception) assert representation.fetched_at != None # But mirroing failed. assert representation.mirror_exception != None eq_(None, representation.mirrored_at) eq_(link.media_type, representation.media_type) eq_(link.href, representation.url) # The mirror url should still be set. assert "Gutenberg" in representation.mirror_url assert representation.mirror_url.endswith("%s.epub" % edition.title) # Book content is still there since it wasn't mirrored. assert representation.content != None # The license pool is suppressed when mirroring fails. eq_(True, pool.suppressed) assert representation.mirror_exception in pool.license_exception
def test_mirror_with_content_modifier(self): edition, pool = self._edition(with_license_pool=True) data_source = DataSource.lookup(self._db, DataSource.GUTENBERG) m = Metadata(data_source=data_source) mirror = DummyS3Uploader() def dummy_content_modifier(representation): representation.content = "Replaced Content" h = DummyHTTPClient() policy = ReplacementPolicy(mirror=mirror, content_modifier=dummy_content_modifier, http_get=h.do_get) link = LinkData( rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href="http://example.com/test.epub", content="I'm an epub", ) link_obj, ignore = edition.primary_identifier.add_link( rel=link.rel, href=link.href, data_source=data_source, license_pool=pool, media_type=link.media_type, content=link.content, ) h.queue_response(200, media_type=Representation.EPUB_MEDIA_TYPE) m.mirror_link(edition, data_source, link, link_obj, policy) representation = link_obj.resource.representation # The representation was fetched successfully. eq_(None, representation.fetch_exception) assert representation.fetched_at != None # The mirror url is set. assert "Gutenberg" in representation.mirror_url assert representation.mirror_url.endswith( "%s/%s.epub" % (edition.primary_identifier.identifier, edition.title)) # Content isn't there since it was mirrored. eq_(None, representation.content) # The representation was mirrored, with the modified content. eq_([representation], mirror.uploaded) eq_(["Replaced Content"], mirror.content)
def summary_to_linkdata(detail): if not detail: return None if not 'value' in detail or not detail['value']: return None content = detail['value'] media_type = detail.get('type', 'text/plain') return LinkData(rel=Hyperlink.DESCRIPTION, media_type=media_type, content=content)
def test_image_and_thumbnail(self): edition = self._edition() l2 = LinkData( rel=Hyperlink.THUMBNAIL_IMAGE, href="http://thumbnail.com/", media_type=Representation.JPEG_MEDIA_TYPE, ) l1 = LinkData( rel=Hyperlink.IMAGE, href="http://example.com/", thumbnail=l2, media_type=Representation.JPEG_MEDIA_TYPE, ) metadata = Metadata(links=[l1, l2], data_source=edition.data_source) metadata.apply(edition) [image, thumbnail] = sorted(edition.primary_identifier.links, key=lambda x: x.rel) eq_(Hyperlink.IMAGE, image.rel) eq_([thumbnail.resource.representation], image.resource.representation.thumbnails)
def image_link_to_linkdata(cls, link, rel): if not link or not 'href' in link: return None href = link['href'] if '00000000-0000-0000-0000' in href: # This is a stand-in cover for preorders. It's better not # to have a cover at all -- we might be able to get one # later, or from another source. return None href = OverdriveAPI.make_link_safe(href) media_type = link.get('type', None) return LinkData(rel=rel, href=href, media_type=media_type)
def test_implicit_format_for_open_access_link(self): # A format is a delivery mechanism. We handle delivery on open access # pools from our mirrored content in S3. # Tests that when a link is open access, a pool can be delivered. edition, pool = self._edition(with_license_pool=True) # This is the delivery mechanism created by default when you # create a book with _edition(). [epub] = pool.delivery_mechanisms eq_(Representation.EPUB_MEDIA_TYPE, epub.delivery_mechanism.content_type) eq_(DeliveryMechanism.ADOBE_DRM, epub.delivery_mechanism.drm_scheme) link = LinkData( rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.PDF_MEDIA_TYPE, href=self._url ) circulation_data = CirculationData( data_source=DataSource.GUTENBERG, primary_identifier=edition.primary_identifier, links=[link], ) replace = ReplacementPolicy( formats=True, ) circulation_data.apply(pool, replace) # We destroyed the default delivery format and added a new, # open access delivery format. [pdf] = pool.delivery_mechanisms eq_(Representation.PDF_MEDIA_TYPE, pdf.delivery_mechanism.content_type) eq_(DeliveryMechanism.NO_DRM, pdf.delivery_mechanism.drm_scheme) circulation_data = CirculationData( data_source=DataSource.GUTENBERG, primary_identifier=edition.primary_identifier, links=[] ) replace = ReplacementPolicy( formats=True, links=True, ) circulation_data.apply(pool, replace) # Now we have no formats at all. eq_([], pool.delivery_mechanisms)
def test_metadata_can_be_deepcopied(self): # Check that we didn't put something in the metadata that # will prevent it from being copied. (e.g., self.log) subject = SubjectData(Subject.TAG, "subject") contributor = ContributorData() identifier = IdentifierData(Identifier.GUTENBERG_ID, "1") link = LinkData(Hyperlink.OPEN_ACCESS_DOWNLOAD, "example.epub") measurement = MeasurementData(Measurement.RATING, 5) circulation = CirculationData(data_source=DataSource.GUTENBERG, primary_identifier=identifier, licenses_owned=0, licenses_available=0, licenses_reserved=0, patrons_in_hold_queue=0) primary_as_data = IdentifierData(type=identifier.type, identifier=identifier.identifier) other_data = IdentifierData(type=u"abc", identifier=u"def") m = Metadata( DataSource.GUTENBERG, subjects=[subject], contributors=[contributor], primary_identifier=identifier, links=[link], measurements=[measurement], circulation=circulation, title="Hello Title", subtitle="Subtle Hello", sort_title="Sorting Howdy", language="US English", medium=Edition.BOOK_MEDIUM, series="1", series_position=1, publisher="Hello World Publishing House", imprint=u"Follywood", issued=datetime.datetime.utcnow(), published=datetime.datetime.utcnow(), identifiers=[primary_as_data, other_data], data_source_last_updated=datetime.datetime.utcnow(), ) m_copy = deepcopy(m) # If deepcopy didn't throw an exception we're ok. assert m_copy is not None
def test_mirror_open_access_link_fetch_failure(self): edition, pool = self._edition(with_license_pool=True) data_source = DataSource.lookup(self._db, DataSource.GUTENBERG) m = Metadata(data_source=data_source) mirror = DummyS3Uploader() h = DummyHTTPClient() policy = ReplacementPolicy(mirror=mirror, http_get=h.do_get) link = LinkData( rel=Hyperlink.IMAGE, media_type=Representation.JPEG_MEDIA_TYPE, href="http://example.com/", ) link_obj, ignore = edition.primary_identifier.add_link( rel=link.rel, href=link.href, data_source=data_source, license_pool=pool, media_type=link.media_type, content=link.content, ) h.queue_response(403) m.mirror_link(edition, data_source, link, link_obj, policy) representation = link_obj.resource.representation # Fetch failed, so we should have a fetch exception but no mirror url. assert representation.fetch_exception != None eq_(None, representation.mirror_exception) eq_(None, representation.mirror_url) eq_(link.href, representation.url) assert representation.fetched_at != None eq_(None, representation.mirrored_at) # the edition's identifier-associated license pool should not be # suppressed just because fetch failed on getting image. eq_(False, pool.suppressed) # the license pool only gets its license_exception column filled in # if fetch failed on getting an Hyperlink.OPEN_ACCESS_DOWNLOAD-type epub. eq_(None, pool.license_exception)
def test_mirror_open_access_link_fetch_failure(self): mirror = DummyS3Uploader() h = DummyHTTPClient() edition, pool = self._edition(with_license_pool=True) data_source = DataSource.lookup(self._db, DataSource.GUTENBERG) policy = ReplacementPolicy(mirror=mirror, http_get=h.do_get) circulation_data = CirculationData( data_source=edition.data_source, primary_identifier=edition.primary_identifier, ) link = LinkData( rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, media_type=Representation.EPUB_MEDIA_TYPE, href=self._url, ) link_obj, ignore = edition.primary_identifier.add_link( rel=link.rel, href=link.href, data_source=data_source, license_pool=pool, media_type=link.media_type, content=link.content, ) h.queue_response(403) circulation_data.mirror_link(pool, data_source, link, link_obj, policy) representation = link_obj.resource.representation # Fetch failed, so we should have a fetch exception but no mirror url. assert representation.fetch_exception != None eq_(None, representation.mirror_exception) eq_(None, representation.mirror_url) eq_(link.href, representation.url) assert representation.fetched_at != None eq_(None, representation.mirrored_at) # The license pool is suppressed when fetch fails. eq_(True, pool.suppressed) assert representation.fetch_exception in pool.license_exception