def test_init():
    """
    Test creation of the flask application succeeds
    """
    # Make sure we have a valid configuration file
    Config.config_file = os.path.join(
        os.path.dirname(os.path.abspath(__file__)), 'conf.yaml')
    assert not create_app().testing
    assert create_app({'TESTING': True}).testing
def test_upload_dirs_write_protected(directory):
    """
    Verifies that upload dir checks fail if any upload dir is write protected
    """
    with tempfile.TemporaryDirectory() as upload_dir:
        testdir = os.path.join(upload_dir, directory)
        os.mkdir(testdir)
        os.chmod(testdir, stat.S_IREAD)

        with pytest.raises(SystemExit) as expected_exit:
            # This should fail
            create_app({'TESTING': True, 'UPLOAD_DIR': upload_dir})

        assert expected_exit.type == SystemExit
        assert expected_exit.value.code == 1
Exemplo n.º 3
0
def app():
    """
    A test flask app. Uses config file in this directory and temporary directory for upload.

    :return: An app instance
    :rtype: Flask
    """
    # Copy config to temporary directory, to have always a clean config even on writes (e.g. on updates of api keys)
    with tempfile.NamedTemporaryFile() as temp_config:
        with open(
                os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             'conf.yaml')) as test_config:
            temp_config.write(test_config.read().encode('utf-8'))
            temp_config.flush()

        Config.config_file = temp_config.name

        # Use temp dir for image uploads
        with tempfile.TemporaryDirectory() as upload_dir:
            app = create_app({
                'TESTING': True,
                'SECRET_KEY': 'dev',
                'UPLOAD_DIR': upload_dir
            })
            yield app
def test_upload_dir_creation_relative():
    """
    Verifies that upload dirs can be successfully created if they do not exist. Upload dir is a relative path.
    """
    testdir = os.path.join(os.path.dirname(berry_cam_server.__file__),
                           'test_uploads')
    assert not os.path.exists(testdir)

    # Create app with relative path
    try:
        app = create_app({'TESTING': True, 'UPLOAD_DIR': 'test_uploads'})
        assert app
        assert os.path.exists(testdir)

    # Cleanup the directory
    finally:
        shutil.rmtree(testdir)
def test_upload_dir_creation_absolute():
    """
    Verifies that upload dirs can be successfully created if they do not exist. Upload dir is an absolute path.
    """
    # Create a temporary directory and delete it directly to be sure it doesn't exist in the next step
    with tempfile.TemporaryDirectory() as upload_dir:
        tmpdir = upload_dir

    # Create app with temporary directory
    try:
        app = create_app({'TESTING': True, 'UPLOAD_DIR': tmpdir})
        assert app
        assert os.path.exists(tmpdir)

    # Cleanup the directory
    finally:
        shutil.rmtree(tmpdir)