def test_fetch_or_create_disk_media_item_with_attributes(db): """ Attributes can be passed in, which propagate to the media item object. """ data = datadir.join('1200x6566.png').read(mode='rb') item = media.fetch_or_create_media_item(data, file_type='png', attributes={'spam': 'eggs'}) assert item.attributes == {'spam': 'eggs'}
def test_created_submission_attributes(db, monkeypatch): """ ``Submission.create`` adds a Submission to the database with attributes taken from its arguments and datestamped with the current time. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') monkeypatch.setattr(content.Submission, 'now', staticmethod(lambda: arrow.get(0))) content.Submission.create(owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.visual, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data) db.flush() [sub] = content.Submission.query.all() assert sub.userid == user.userid assert sub.title == 'Title' assert sub.rating == ratings.GENERAL assert sub.content == 'Description.' assert sub.subtype == 1010 assert sub.folderid is None assert sorted(sub.tags) == ['eggs', 'spam'] assert sub.unixtime == arrow.get(0) assert sub.sorttime == arrow.get(0)
def test_file_type_for_category_invalid_file_format(): """ ``InvalidFileFormat`` is raised if the image data isn't a GIF, JPG, or PNG. """ data = datadir.join('1x70.bmp').read(mode='rb') with pytest.raises(exceptions.InvalidFileFormat): files.file_type_for_category(data, Category.visual)
def test_fetch_or_create_disk_media_item_with_attributes(db): """ Attributes can be passed in, which propagate to the media item object. """ data = datadir.join('1200x6566.png').read(mode='rb') item = media.MediaItem.fetch_or_create(data, file_type='png', attributes={'spam': 'eggs'}) assert item.attributes == {'spam': 'eggs'}
def test_file_type_for_category_invalid_file_format(): """ ``InvalidFileFormat`` is raised if the image data isn't a GIF, JPG, or PNG. """ data = datadir.join('1x70.bmp').read(mode='rb') with pytest.raises(exceptions.InvalidFileFormat): files.file_type_for_category(data, Category.visual)
def test_fetch_or_create_disk_media_item_with_image_and_attributes(db): """ Passing an image and attributes merges the two sets of attributes. """ data = datadir.join('1200x6566.png').read(mode='rb') im = images.from_buffer(data) item = media.fetch_or_create_media_item(data, file_type='png', im=im, attributes={'spam': 'eggs'}) assert item.attributes == {'spam': 'eggs', 'width': 1200, 'height': 6566}
def test_disk_media_item_display_url(db): """ Disk media items have a display_url that's fanned out from /static/media. """ data = datadir.join('1200x6566.png').read(mode='rb') item = media.fetch_or_create_media_item(data, file_type='png') assert item.display_url == ( '/static/media/a5/de/ef/a5deef985bde4438969b5f74a1864f7a5b1d127df3197b4fadf3f855201278b4.png')
def test_fetch_or_create_disk_media_item_with_image_and_attributes(db): """ Passing an image and attributes merges the two sets of attributes. """ data = datadir.join('1200x6566.png').read(mode='rb') im = images.from_buffer(data) item = media.MediaItem.fetch_or_create(data, file_type='png', im=im, attributes={'spam': 'eggs'}) assert item.attributes == {'spam': 'eggs', 'width': 1200, 'height': 6566}
def test_disk_media_item_display_url(db): """ Disk media items have a display_url that's fanned out from /static/media. """ data = datadir.join('1200x6566.png').read(mode='rb') item = media.MediaItem.fetch_or_create(data, file_type='png') assert item.display_url == ( '/static/media/a5/de/ef/a5deef985bde4438969b5f74a1864f7a5b1d127df3197b4fadf3f855201278b4.png')
def test_disk_media_item_display_url_ax_rule(db): """ The display_url replaces ``media/ad`` with ``media/ax`` because adblock sucks. """ data = datadir.join('1x70.gif').read(mode='rb') item = media.fetch_or_create_media_item(data, file_type='gif') assert item.display_url == ( '/static/media/ax/b2/06/adb20677ffcfda9605812f7f47aaa94a9c9b3e1a0b365e43872dc55199f5f224.gif')
def test_file_type_for_category_visual_results(filename, expected_size, expected_fmt): """ ``file_type_for_category`` will, for visual submissions, return the decoded image and its file format. """ data = datadir.join(filename).read(mode='rb') decoded, fmt = files.file_type_for_category(data, Category.visual) assert fmt == expected_fmt assert tuple(decoded.size) == expected_size
def test_disk_media_item_display_url_ax_rule(db): """ The display_url replaces ``media/ad`` with ``media/ax`` because adblock sucks. """ data = datadir.join('1x70.gif').read(mode='rb') item = media.MediaItem.fetch_or_create(data, file_type='gif') assert item.display_url == ( '/static/media/ax/b2/06/adb20677ffcfda9605812f7f47aaa94a9c9b3e1a0b365e43872dc55199f5f224.gif')
def staticdir(tmpdir): tmpdir = tmpdir.join('libweasyl-staticdir') configure_libweasyl( dbsession=sessionmaker, not_found_exception=NotFound, base_file_path=tmpdir.strpath, staff_config_path=str(datadir.join("weasyl-staff.yaml")), media_link_formatter_callback=media_link_formatter.format_media_link, ) return tmpdir
def test_fetch_or_create_disk_media_item_with_image(db): """ An image can be passed in, which pulls out width/height attributes and autodetects the file type. """ data = datadir.join('1200x6566.png').read(mode='rb') im = images.from_buffer(data) item = media.fetch_or_create_media_item(data, im=im) assert item.file_type == 'png' assert item.attributes == {'width': 1200, 'height': 6566}
def test_fetch_or_create_disk_media_item_with_image(db): """ An image can be passed in, which pulls out width/height attributes and autodetects the file type. """ data = datadir.join('1200x6566.png').read(mode='rb') im = images.from_buffer(data) item = media.MediaItem.fetch_or_create(data, im=im) assert item.file_type == 'png' assert item.attributes == {'width': 1200, 'height': 6566}
def test_fetch_or_create_disk_media_item_fetches_extant_items(db): """ Calling ``MediaItem.fetch_or_create`` with data that's already in the database gives back the extant media item. """ data = datadir.join('1200x6566.png').read(mode='rb') item1 = media.MediaItem.fetch_or_create(data, file_type='png') db.flush() item2 = media.MediaItem.fetch_or_create(data, file_type='png') assert item1.mediaid == item2.mediaid
def test_file_type_for_category_visual_results(filename, expected_size, expected_fmt): """ ``file_type_for_category`` will, for visual submissions, return the decoded image and its file format. """ data = datadir.join(filename).read(mode='rb') decoded, fmt = files.file_type_for_category(data, Category.visual) assert fmt == expected_fmt assert tuple(decoded.size) == expected_size
def test_fetch_or_create_disk_media_item_fetches_extant_items(db): """ Calling ``fetch_or_create_media_item`` with data that's already in the database gives back the extant media item. """ data = datadir.join('1200x6566.png').read(mode='rb') item1 = media.fetch_or_create_media_item(data, file_type='png') db.flush() item2 = media.fetch_or_create_media_item(data, file_type='png') assert item1.mediaid == item2.mediaid
def staticdir(tmpdir): tmpdir = tmpdir.join('libweasyl-staticdir') configure_libweasyl( dbsession=sessionmaker, not_found_exception=NotFound, base_file_path=tmpdir.strpath, staff_config_path=str(datadir.join("weasyl-staff.yaml")), media_link_formatter_callback=media_link_formatter.format_media_link, ) return tmpdir
def test_invalid_category_submission_data(db): """ Unrecognized categories are treated as errors when a submission is created with submission data. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') with pytest.raises(ValueError): content.Submission.create( owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category='spam', subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data)
def test_submission_data_gets_limited_to_default(db, monkeypatch): """ If no submission size limit is provided, the default is used per-file type. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') monkeypatch.setitem(constants.DEFAULT_LIMITS, 'gif', 1) with pytest.raises(exceptions.SubmissionFileTooLarge): content.Submission.create( owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.visual, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data)
def test_submission_data_gets_limited(db): """ Submission data must be smaller than the size limit. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') with pytest.raises(exceptions.SubmissionFileTooLarge): content.Submission.create( owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.visual, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data, submission_size_limit=1)
def test_submission_data_and_embed_link_fails(db): """ Conversely, a submission with both submission data and an embed link is invalid. """ user = make_user(db) data = datadir.join('fake.swf').read(mode='rb') with pytest.raises(exceptions.InvalidData): content.Submission.create( owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.multimedia, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data, embed_link='https://example.com/')
def test_fetch_or_create_disk_media_item(staticdir, db): """ ``fetch_or_create_media_item`` by default creates a disk media item, populates its attributes, and stores the file on disk. """ data = datadir.join('1200x6566.png').read(mode='rb') item = media.fetch_or_create_media_item(data, file_type='png') assert item.sha256 == 'a5deef985bde4438969b5f74a1864f7a5b1d127df3197b4fadf3f855201278b4' assert item.file_type == 'png' assert staticdir.join( 'static', 'media', 'a5', 'de', 'ef', 'a5deef985bde4438969b5f74a1864f7a5b1d127df3197b4fadf3f855201278b4.png' ).read(mode='rb') == data
def test_fetch_or_create_disk_media_item(staticdir, db): """ ``MediaItem.fetch_or_create`` by default creates a disk media item, populates its attributes, and stores the file on disk. """ data = datadir.join('1200x6566.png').read(mode='rb') item = media.MediaItem.fetch_or_create(data, file_type='png') assert item.sha256 == 'a5deef985bde4438969b5f74a1864f7a5b1d127df3197b4fadf3f855201278b4' assert item.file_type == 'png' assert staticdir.join( 'static', 'media', 'a5', 'de', 'ef', 'a5deef985bde4438969b5f74a1864f7a5b1d127df3197b4fadf3f855201278b4.png' ).read(mode='rb') == data
def test_read_uncompressed_flash_header(): """ parse_flash_header can parse the header of uncompressed flash files. """ infile = datadir.join('test.swf').open(mode='rb') header = flash.parse_flash_header(infile) assert header == { 'compression': None, 'size': 153, 'version': 5, 'width': 550, 'height': 400, }
def test_read_lzma_compressed_flash_header(): """ parse_flash_header can parse the header of lzma compressed flash files. """ infile = datadir.join('lzma.swf').open(mode='rb') header = flash.parse_flash_header(infile) assert header == { 'compression': 'lzma', 'size': 3294, 'version': 25, 'width': 550, 'height': 400, }
def test_read_zlib_compressed_flash_header(): """ parse_flash_header can parse the header of zlib compressed flash files. """ infile = datadir.join('flash_eyes.swf').open(mode='rb') header = flash.parse_flash_header(infile) assert header == { 'compression': 'zlib', 'size': 15900, 'version': 6, 'width': 300, 'height': 300, }
def test_created_submission_critique_setting(db): """ Creating a critique-requested submission sets the critique setting. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') content.Submission.create( owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.visual, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data, critique_requested=True) db.flush() [sub] = content.Submission.query.all() assert 'critique' in sub.settings
def test_read_zlib_compressed_flash_header(): """ parse_flash_header can parse the header of zlib compressed flash files. """ infile = datadir.join('flash_eyes.swf').open(mode='rb') header = flash.parse_flash_header(infile) assert header == { 'compression': 'zlib', 'size': 15900, 'version': 6, 'width': 300, 'height': 300, }
def test_read_lzma_compressed_flash_header(): """ parse_flash_header can parse the header of lzma compressed flash files. """ infile = datadir.join('lzma.swf').open(mode='rb') header = flash.parse_flash_header(infile) assert header == { 'compression': 'lzma', 'size': 3294, 'version': 25, 'width': 550, 'height': 400, }
def test_read_uncompressed_flash_header(): """ parse_flash_header can parse the header of uncompressed flash files. """ infile = datadir.join('test.swf').open(mode='rb') header = flash.parse_flash_header(infile) assert header == { 'compression': None, 'size': 153, 'version': 5, 'width': 550, 'height': 400, }
def test_submission_data_gets_limited_to_default(db, monkeypatch): """ If no submission size limit is provided, the default is used per-file type. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') monkeypatch.setitem(constants.DEFAULT_LIMITS, 'gif', 1) with pytest.raises(exceptions.SubmissionFileTooLarge): content.Submission.create(owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.visual, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data)
def test_invalid_category_submission_data(db): """ Unrecognized categories are treated as errors when a submission is created with submission data. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') with pytest.raises(ValueError): content.Submission.create(owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category='spam', subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data)
def test_submission_data_gets_limited(db): """ Submission data must be smaller than the size limit. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') with pytest.raises(exceptions.SubmissionFileTooLarge): content.Submission.create(owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.visual, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data, submission_size_limit=1)
def test_submission_data_and_embed_link_fails(db): """ Conversely, a submission with both submission data and an embed link is invalid. """ user = make_user(db) data = datadir.join('fake.swf').read(mode='rb') with pytest.raises(exceptions.InvalidData): content.Submission.create(owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.multimedia, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data, embed_link='https://example.com/')
def test_created_submission_critique_setting(db): """ Creating a critique-requested submission sets the critique setting. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') content.Submission.create(owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.visual, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data, critique_requested=True) db.flush() [sub] = content.Submission.query.all() assert 'critique' in sub.settings
def test_created_submission_attributes(db, monkeypatch): """ ``Submission.create`` adds a Submission to the database with attributes taken from its arguments and datestamped with the current time. """ user = make_user(db) data = datadir.join('1x70.gif').read(mode='rb') monkeypatch.setattr(content.Submission, 'now', staticmethod(lambda: arrow.get(0))) content.Submission.create( owner=user, title='Title', rating=ratings.GENERAL, description='Description.', category=Category.visual, subtype=1010, folder=None, tags=['spam', 'eggs'], submission_data=data) db.flush() [sub] = content.Submission.query.all() assert sub.userid == user.userid assert sub.title == 'Title' assert sub.rating == ratings.GENERAL assert sub.content == 'Description.' assert sub.subtype == 1010 assert sub.folderid is None assert sorted(sub.tags) == ['eggs', 'spam'] assert sub.unixtime == arrow.get(0) assert sub.sorttime == arrow.get(0)