def test_gallery(settings, tmpdir): "Test the Gallery class." settings['destination'] = str(tmpdir) settings['webm_options'] = ['-missing-option', 'foobar'] gal = Gallery(settings, ncpu=1) gal.build() out_html = os.path.join(settings['destination'], 'index.html') assert os.path.isfile(out_html) with open(out_html, 'r') as f: html = f.read() assert '<title>Sigal test gallery</title>' in html logger = logging.getLogger('sigal') logger.setLevel(logging.DEBUG) try: gal = Gallery(settings, ncpu=1) with pytest.raises(SubprocessException): gal.build() finally: logger.setLevel(logging.INFO)
def test_gallery_max_img_pixels(settings, tmpdir, monkeypatch): "Test the Gallery class with the max_img_pixels setting." # monkeypatch is used here to reset the value to the PIL default. # This value does not matter, other than it is "large" # to show that settings['max_img_pixels'] works. monkeypatch.setattr('PIL.Image.MAX_IMAGE_PIXELS', 100_000_000) with open(str(tmpdir.join('my.css')), mode='w') as f: f.write('color: red') settings['destination'] = str(tmpdir) settings['user_css'] = str(tmpdir.join('my.css')) settings['max_img_pixels'] = 5000 logger = logging.getLogger('sigal') logger.setLevel(logging.DEBUG) try: with pytest.raises(PILImage.DecompressionBombError): gal = Gallery(settings, ncpu=1) gal.build() settings['max_img_pixels'] = 100_000_000 gal = Gallery(settings, ncpu=1) gal.build() finally: logger.setLevel(logging.INFO)
def test_gallery(settings, tmpdir): "Test the Gallery class." with open(str(tmpdir.join('my.css')), mode='w') as f: f.write('color: red') settings['destination'] = str(tmpdir) settings['user_css'] = str(tmpdir.join('my.css')) settings['webm_options'] = ['-missing-option', 'foobar'] gal = Gallery(settings, ncpu=1) gal.build() mycss = os.path.join(settings['destination'], 'static', 'my.css') assert os.path.isfile(mycss) out_html = os.path.join(settings['destination'], 'index.html') assert os.path.isfile(out_html) with open(out_html) as f: html = f.read() assert '<title>Sigal test gallery</title>' in html assert '<link rel="stylesheet" href="static/my.css">' in html logger = logging.getLogger('sigal') logger.setLevel(logging.DEBUG) try: gal = Gallery(settings, ncpu=1) with pytest.raises(SubprocessException): gal.build() finally: logger.setLevel(logging.INFO)
def test_encrypt(settings, tmpdir, disconnect_signals): settings['destination'] = str(tmpdir) if "sigal.plugins.encrypt" not in settings["plugins"]: settings['plugins'] += ["sigal.plugins.encrypt"] settings['encrypt_options'] = { 'password': '******', 'ask_password': True, 'gcm_tag': 'AuTheNTiCatIoNtAG', 'kdf_salt': 'saltysaltsweetysweet', 'kdf_iters': 10000, 'encrypt_symlinked_originals': False, } init_plugins(settings) gal = Gallery(settings) gal.build() # check the encrypt cache exists cachePath = os.path.join(settings["destination"], ".encryptCache") assert os.path.isfile(cachePath) encryptCache = None with open(cachePath, "rb") as cacheFile: encryptCache = pickle.load(cacheFile) assert isinstance(encryptCache, dict) testAlbum = gal.albums["encryptTest"] key, tag = get_key_tag(settings) for media in testAlbum: # check if sizes are stored in cache assert cache_key(media) in encryptCache assert "size" in encryptCache[cache_key(media)] assert "thumb_size" in encryptCache[cache_key(media)] assert "encrypted" in encryptCache[cache_key(media)] encryptedImages = [media.dst_path, media.thumb_path] if settings["keep_orig"]: encryptedImages.append( os.path.join(settings["destination"], media.path, media.big)) # check if images are encrypted by trying to decrypt for image in encryptedImages: with open(image, "rb") as infile: with BytesIO() as outfile: endec.decrypt(key, infile, outfile, tag) # check static files have been copied static = os.path.join(settings["destination"], 'static') assert os.path.isfile(os.path.join(static, "decrypt.js")) assert os.path.isfile(os.path.join(static, "keycheck.txt")) assert os.path.isfile(os.path.join(settings["destination"], "sw.js")) # check keycheck file with open(os.path.join(settings["destination"], 'static', "keycheck.txt"), "rb") as infile: with BytesIO() as outfile: endec.decrypt(key, infile, outfile, tag)
def test_titleregexp(settings, tmpdir, disconnect_signals): if "sigal.plugins.titleregexp" not in settings["plugins"]: settings['plugins'] += ["sigal.plugins.titleregexp"] init_plugins(settings) gal = Gallery(settings) gal.build() assert gal.albums.get('dir1').albums[1].title == "titleregexp 02"
def test_ignores(settings, tmpdir): tmp = str(tmpdir) settings['destination'] = tmp settings['ignore_directories'] = ['*test2'] settings['ignore_files'] = ['dir2/Hubble*', '*.png'] gal = Gallery(settings, ncpu=1) gal.build() assert 'test2' not in os.listdir(join(tmp, 'dir1')) assert 'archlinux-kiss-1024x640.png' not in os.listdir( join(tmp, 'dir1', 'test1')) assert 'Hubble Interacting Galaxy NGC 5257.jpg' not in os.listdir( join(tmp, 'dir2'))
def test_gallery(settings, tmpdir): "Test the Gallery class." settings['destination'] = str(tmpdir) gal = Gallery(settings, ncpu=1) gal.build() out_html = os.path.join(settings['destination'], 'index.html') assert os.path.isfile(out_html) with open(out_html, 'r') as f: html = f.read() assert '<title>Sigal test gallery</title>' in html
def test_ignores(settings, tmpdir): tmp = str(tmpdir) settings['destination'] = tmp settings['ignore_directories'] = ['*test2', 'accentué'] settings['ignore_files'] = ['dir2/Hubble*', '*.png', '*CMB_*'] gal = Gallery(settings, ncpu=1) gal.build() assert 'test2' not in os.listdir(join(tmp, 'dir1')) assert 'accentué' not in os.listdir(tmp) assert 'CMB_Timeline300_no_WMAP.jpg' not in os.listdir( join(tmp, 'dir1', 'test1')) assert 'Hubble Interacting Galaxy NGC 5257.jpg' not in os.listdir( join(tmp, 'dir2'))
def test_gallery(tmpdir): "Test the Gallery class." default_conf = os.path.join(SAMPLE_DIR, 'sigal.conf.py') settings = read_settings(default_conf) settings['destination'] = str(tmpdir) gal = Gallery(settings, ncpu=1) gal.build() out_html = os.path.join(settings['destination'], 'index.html') assert os.path.isfile(out_html) with open(out_html, 'r') as f: html = f.read() assert '<title>Sigal test gallery</title>' in html
def test_nonmedia_files(settings, tmpdir, disconnect_signals): settings['destination'] = str(tmpdir) settings['plugins'] += ['sigal.plugins.nonmedia_files'] init_plugins(settings) gal = Gallery(settings) gal.build() outfile = os.path.join(settings['destination'], 'nonmedia_files', 'dummy.pdf') assert os.path.isfile(outfile) outthumb = os.path.join( settings['destination'], 'nonmedia_files', 'thumbnails', 'dummy.tn.jpg' ) assert os.path.isfile(outthumb)
def make_gallery(settings, tmpdir, method): settings['destination'] = str(tmpdir) if "sigal.plugins.compress_assets" not in settings["plugins"]: settings['plugins'] += ["sigal.plugins.compress_assets"] # Set method settings.setdefault('compress_assets_options', {})['method'] = method compress_options = compress_assets.DEFAULT_SETTINGS.copy() # The key was created by the previous setdefault if needed compress_options.update(settings['compress_assets_options']) init_plugins(settings) gal = Gallery(settings) gal.build() return compress_options
def test_nomedia_plugin(settings, tmpdir): settings['destination'] = str(tmpdir) if "plugins"in settings: if not "sigal.plugins.nomedia" in settings["plugins"]: settings['plugins'] += ["sigal.plugins.nomedia"] else: settings["plugins"] = ["sigal.plugins.nomedia"] init_plugins(settings) gal = Gallery(settings) gal.build() for path, dirs, files in os.walk(os.path.join(str(tmpdir), "nomedia")): assert "ignore" not in path for file in files: assert "ignore" not in file
def test_nomedia_plugin(settings, tmpdir): settings['destination'] = str(tmpdir) if "plugins" in settings: if not "sigal.plugins.nomedia" in settings["plugins"]: settings['plugins'] += ["sigal.plugins.nomedia"] else: settings["plugins"] = ["sigal.plugins.nomedia"] init_plugins(settings) gal = Gallery(settings) gal.build() for path, dirs, files in os.walk(os.path.join(str(tmpdir), "nomedia")): assert "ignore" not in path for file in files: assert "ignore" not in file
def make_gallery(settings, tmpdir, method): settings['destination'] = str(tmpdir) # Really speed up testing settings['use_orig'] = True if "sigal.plugins.compress_assets" not in settings["plugins"]: settings['plugins'] += ["sigal.plugins.compress_assets"] # Set method settings.setdefault('compress_assets_options', {})['method'] = method compress_options = compress_assets.DEFAULT_SETTINGS.copy() # The key was created by the previous setdefault if needed compress_options.update(settings['compress_assets_options']) init_plugins(settings) gal = Gallery(settings) gal.build() return compress_options
def test_plugins(settings, tmpdir, disconnect_signals): settings['destination'] = str(tmpdir) if "sigal.plugins.nomedia" not in settings["plugins"]: settings['plugins'] += ["sigal.plugins.nomedia"] if "sigal.plugins.media_page" not in settings["plugins"]: settings['plugins'] += ["sigal.plugins.media_page"] init_plugins(settings) gal = Gallery(settings) gal.build() out_html = os.path.join(settings['destination'], 'dir2', 'KeckObservatory20071020.jpg.html') assert os.path.isfile(out_html) for path, dirs, files in os.walk(os.path.join(str(tmpdir), "nomedia")): assert "ignore" not in path for file in files: assert "ignore" not in file
def test_custom_theme(settings, tmp_path, caplog): theme_path = tmp_path / 'mytheme' tpl_path = theme_path / 'templates' settings['destination'] = str(tmp_path / 'build') settings['source'] = os.path.join(settings['source'], 'encryptTest') settings['theme'] = str(theme_path) settings['title'] = 'My gallery' gal = Gallery(settings, ncpu=1) with pytest.raises(Exception, match='Impossible to find the theme'): gal.build() tpl_path.mkdir(parents=True) (theme_path / 'static').mkdir(parents=True) with pytest.raises(SystemExit): gal.build() assert caplog.records[0].message.startswith( 'The template album.html was not found in template folder') with open(tpl_path / 'album.html', mode='w') as f: f.write(""" {{ settings.title|myfilter }} """) with open(tpl_path / 'album_list.html', mode='w') as f: f.write(""" {{ settings.title|myfilter }} """) with open(theme_path / 'filters.py', mode='w') as f: f.write(""" def myfilter(value): return f'{value} is very nice' """) gal = Gallery(settings, ncpu=1) gal.build() out_html = os.path.join(settings['destination'], 'index.html') assert os.path.isfile(out_html) with open(out_html) as f: html = f.read() assert 'My gallery is very nice' in html
def test_gallery(settings, tmp_path, caplog): "Test the Gallery class." caplog.set_level('ERROR') settings['destination'] = str(tmp_path) settings['user_css'] = str(tmp_path / 'my.css') settings['webm_options'] = ['-missing-option', 'foobar'] gal = Gallery(settings, ncpu=1) gal.build() assert re.match(r'CSS file .* could not be found', caplog.records[3].message) with open(tmp_path / 'my.css', mode='w') as f: f.write('color: red') gal.build() mycss = os.path.join(settings['destination'], 'static', 'my.css') assert os.path.isfile(mycss) out_html = os.path.join(settings['destination'], 'index.html') assert os.path.isfile(out_html) with open(out_html) as f: html = f.read() assert '<title>Sigal test gallery - Sigal test gallery ☺</title>' in html assert '<link rel="stylesheet" href="static/my.css">' in html logger = logging.getLogger('sigal') logger.setLevel(logging.DEBUG) try: gal = Gallery(settings, ncpu=1) with pytest.raises(SubprocessException): gal.build() finally: logger.setLevel(logging.INFO)