def test_open_access_content_mirrored(self): # Make sure that open access material links are translated to our S3 buckets, and that # commercial material links are left as is. # Note: Mirroring tests passing does not guarantee that all code now # correctly calls on CirculationData, as well as Metadata. This is a risk. mirror = MockS3Uploader() # Here's a book. edition, pool = self._edition(with_license_pool=True) # Here's a link to the content of the book, which will be mirrored. link_mirrored = LinkData(rel=Hyperlink.OPEN_ACCESS_DOWNLOAD, href="http://example.com/", media_type=Representation.EPUB_MEDIA_TYPE, content="i am a tiny book") # This link will not be mirrored. link_unmirrored = LinkData(rel=Hyperlink.DRM_ENCRYPTED_DOWNLOAD, href="http://example.com/2", media_type=Representation.EPUB_MEDIA_TYPE, content="i am a pricy book") # Apply the metadata. policy = ReplacementPolicy(mirror=mirror) metadata = Metadata( data_source=edition.data_source, links=[link_mirrored, link_unmirrored], ) metadata.apply(edition, pool.collection, replace=policy) # make sure the refactor is done right, and metadata does not upload eq_(0, len(mirror.uploaded)) circulation_data = CirculationData( data_source=edition.data_source, primary_identifier=edition.primary_identifier, links=[link_mirrored, link_unmirrored], ) circulation_data.apply(self._db, pool.collection, replace=policy) # make sure the refactor is done right, and circulation does upload eq_(1, len(mirror.uploaded)) # Only the open-access link has been 'mirrored'. [book] = mirror.uploaded # It's remained an open-access link. eq_([Hyperlink.OPEN_ACCESS_DOWNLOAD], [x.rel for x in book.resource.links]) # It's been 'mirrored' to the appropriate S3 bucket. assert book.mirror_url.startswith( 'https://s3.amazonaws.com/test.content.bucket/') expect = '/%s/%s.epub' % (edition.primary_identifier.identifier, edition.title) assert book.mirror_url.endswith(expect) # make sure the mirrored link is safely on edition sorted_edition_links = sorted(pool.identifier.links, key=lambda x: x.rel) unmirrored_representation, mirrored_representation = [ edlink.resource.representation for edlink in sorted_edition_links ] assert mirrored_representation.mirror_url.startswith( 'https://s3.amazonaws.com/test.content.bucket/') # make sure the unmirrored link is safely on edition eq_('http://example.com/2', unmirrored_representation.url) # make sure the unmirrored link has not been translated to an S3 URL eq_(None, unmirrored_representation.mirror_url)
def test_image_scale_and_mirror(self): # Make sure that open access material links are translated to our S3 buckets, and that # commercial material links are left as is. # Note: mirroring links is now also CirculationData's job. So the unit tests # that test for that have been changed to call to mirror cover images. # However, updated tests passing does not guarantee that all code now # correctly calls on CirculationData, too. This is a risk. mirror = DummyS3Uploader() edition, pool = self._edition(with_license_pool=True) content = open(self.sample_cover_path("test-book-cover.png")).read() l1 = LinkData(rel=Hyperlink.IMAGE, href="http://example.com/", media_type=Representation.JPEG_MEDIA_TYPE, content=content) thumbnail_content = open( self.sample_cover_path("tiny-image-cover.png")).read() l2 = LinkData(rel=Hyperlink.THUMBNAIL_IMAGE, href="http://example.com/thumb.jpg", media_type=Representation.JPEG_MEDIA_TYPE, content=content) # When we call metadata.apply, all image links will be scaled and # 'mirrored'. policy = ReplacementPolicy(mirror=mirror) metadata = Metadata(links=[l1, l2], data_source=edition.data_source) metadata.apply(edition, pool.collection, replace=policy) # Two Representations were 'mirrored'. image, thumbnail = mirror.uploaded # The image... [image_link] = image.resource.links eq_(Hyperlink.IMAGE, image_link.rel) # And its thumbnail. eq_(image, thumbnail.thumbnail_of) # The original image is too big to be a thumbnail. eq_(600, image.image_height) eq_(400, image.image_width) # The thumbnail is the right height. eq_(Edition.MAX_THUMBNAIL_HEIGHT, thumbnail.image_height) eq_(Edition.MAX_THUMBNAIL_WIDTH, thumbnail.image_width) # The thumbnail is newly generated from the full-size # image--the thumbnail that came in from the OPDS feed was # ignored. assert thumbnail.url != l2.href assert thumbnail.content != l2.content # Both images have been 'mirrored' to Amazon S3. assert image.mirror_url.startswith( 'http://s3.amazonaws.com/test.cover.bucket/') assert image.mirror_url.endswith('cover.jpg') # The thumbnail image has been converted to PNG. assert thumbnail.mirror_url.startswith( 'http://s3.amazonaws.com/test.cover.bucket/scaled/300/') assert thumbnail.mirror_url.endswith('cover.png')