def setup_page(tmp_path, record_pages): """Prepares a page to upload files to""" config_path, config = generate_local_config(tmp_path, pages=1) post_page_run_cmd = generate_run_cmd( runner=runner, app=app, default_args=["post-page"] ) create_page = partial(run_with_config, default_run_cmd=post_page_run_cmd) result = create_page( config_file=config_path, record_pages=record_pages, input="Y\nN\nY\n" ) assert result.exit_code == 0 return get_page_id_from_stdout(result.stdout), config_path
def setup_two_pages(tmp_path, record_pages): config_path, config = generate_local_config(tmp_path, pages=2) post_page_run_cmd = generate_run_cmd( runner=runner, app=app, default_args=["post-page"] ) create_page = partial(run_with_config, default_run_cmd=post_page_run_cmd) result = create_page( config_file=config_path, record_pages=record_pages, input="Y\nN\nY\nY\nN\nY\n" ) assert result.exit_code == 0 page_ids = get_pages_ids_from_stdout(result.stdout) return config_path, config, page_ids
def _create_pages(page_count: int = 1) -> Tuple[str, List[Tuple[int, str]]]: page_list = [] config_file, config = generate_local_config(tmp_path, pages=page_count) runner = CliRunner() run_cmd = generate_run_cmd(runner=runner, app=app, default_args=["post-page"]) result = run_with_config( input=join_input(user_input=("Y", "N", "Y") * page_count), config_file=config_file, record_pages=record_pages, default_run_cmd=run_cmd, ) assert result.exit_code == 0 created_page_ids = get_pages_ids_from_stdout(result.stdout) for number, page in enumerate(config.pages): page_list += (created_page_ids[number], page.page_title) return config_file, page_list
def test_upload_files_single_page_title_supplied(setup_page, gen_attachments): """Runs confluence_poster --config <config> --page-title <some page name> post-page --upload-files file1 file2... And makes sure the files were attached to the proper page """ page_id, config_file = setup_page file_to_upload, filename = gen_attachments[0] new_title = next(fake_title_generator) # Create the page first create_page = run_with_config( config_file=config_file, pre_args=["--page-title", new_title], default_run_cmd=generate_run_cmd(runner, app, default_args=["post-page"]), input="Y\nN\nY\n", ) created_page_id = get_page_id_from_stdout(create_page.stdout) result: Result = upload_files( config_file=config_file, other_args=[file_to_upload], pre_args=["--page-title", new_title], ) assert result.exit_code == 0 assert ( len( confluence_instance.get_attachments_from_content(created_page_id)["results"] ) == 1 ) assert ( confluence_instance.get_attachments_from_content(created_page_id)["results"][0][ "title" ] == filename ) assert ( len(confluence_instance.get_attachments_from_content(page_id)["results"]) == 0 ), "The original page should not have any attachments"
from utils import ( replace_new_author, rewrite_page_file, run_with_config, generate_run_cmd, get_page_body, check_body_and_title, ) from confluence_poster.main import app from confluence_poster.poster_config import Config from functools import partial pytestmark = pytest.mark.online runner = CliRunner() default_run_cmd = generate_run_cmd(runner=runner, app=app, default_args=["post-page"]) run_with_config = partial(run_with_config, default_run_cmd=default_run_cmd) @pytest.mark.parametrize( "force_flag", [True, False], ids=lambda flag: f"Runs confluence_poster {'--force '* flag} post-page", ) def test_post_force_overwrite_other_author(force_flag, tmp_path, setup_page): """Checks that force flag overwrites page if the author is different""" config_file, (page_id, page_title) = setup_page(1) original_username = Config(config_file).author new_config = replace_new_author(config_file=config_file, tmp_path=tmp_path) new_text = rewrite_page_file(Config(new_config).pages[0].page_file) new_username = Config(new_config).author
from typer.testing import CliRunner, Result from confluence_poster.main import app from utils import clone_local_config, generate_run_cmd, run_with_config import pytest from functools import partial """This module requires an instance of confluence running. If run, this test uses 'local_config.toml' file which is not provided in the repo for security reasons. If you do not have a Confluence instance to test, feel free to reach out to the project maintainer.""" pytestmark = pytest.mark.online runner = CliRunner(mix_stderr=False) mk_local_config = clone_local_config() default_run_cmd = generate_run_cmd(runner=runner, app=app, default_args=["validate"]) run_with_config = partial(run_with_config, default_run_cmd=default_run_cmd) def test_all_ok(make_one_page_config): """Validates that the validate --online does not break during execution""" config_file, config = make_one_page_config result: Result = run_with_config(config_file=config_file, other_args=["--online"]) assert "Validating settings" in result.stdout assert "Trying to get" in result.stdout assert "Validation successful" in result.stdout assert "Got space id" in result.stdout
from confluence_poster.main import app from utils import ( generate_run_cmd, run_with_config, generate_fake_page, create_single_page_input, get_page_id_from_stdout, get_page_body, ) pytestmark = pytest.mark.online runner = CliRunner(mix_stderr=False) default_run_cmd = generate_run_cmd(runner=runner, app=app, default_args=["--page-file"]) run_with_config = partial(run_with_config, default_run_cmd=default_run_cmd) def test_post_page_with_real_file(make_one_page_config, tmp_path): config_file, config = make_one_page_config _, content, page_file = generate_fake_page(tmp_path) result: Result = run_with_config( config_file=config_file, other_args=[page_file, "post-page"], input=create_single_page_input, ) assert result.exit_code == 0 assert get_page_body(get_page_id_from_stdout(
from typer.testing import CliRunner, Result from confluence_poster.main import app from utils import ( generate_run_cmd, run_with_config, generate_local_config, mk_fake_file, confluence_instance, ) from atlassian.confluence import Confluence from functools import partial pytestmark = pytest.mark.online runner = CliRunner() default_run_cmd = generate_run_cmd( runner=runner, app=app, default_args=["post-page", "--version-comment"]) run_with_config = partial(run_with_config, default_run_cmd=default_run_cmd) def _get_version_comment(page_title: str, space: str, confluence: Confluence) -> str: page_id = confluence.get_page_id(space=space, title=page_title) history = confluence.history(page_id) return history["lastUpdated"]["message"] @pytest.mark.parametrize("mode", ("create", "update"), ids=lambda mode: f"User {mode}s page with comment") def test_single_page_with_comment(mode, tmp_path): config_file, config = generate_local_config(tmp_path=tmp_path, pages=1)
from confluence_poster.main import app from utils import ( generate_run_cmd, run_with_config, join_input, generate_local_config, get_pages_ids_from_stdout, confluence_instance, ) from functools import partial """Tests the --create-in-space-root flag of post-page""" pytestmark = pytest.mark.online runner = CliRunner(mix_stderr=False) default_run_cmd = generate_run_cmd( runner=runner, app=app, default_args=["post-page", "--create-in-space-root"]) run_with_config = partial(run_with_config, default_run_cmd=default_run_cmd) @pytest.mark.parametrize( "page_count", list(range(1, 3)), ids=lambda c: f"Create {c} pages in the root with the --create-in-space-root flag", ) def test_create_one_page_in_space_root(tmp_path, page_count): config_file, config = generate_local_config(tmp_path, pages=page_count) result: Result = run_with_config(config_file=config_file, input=join_input(user_input=("Y", ) * page_count))
import pytest from typer.testing import CliRunner from confluence_poster.main import app from utils import generate_run_cmd, run_with_config from functools import partial from pathlib import Path pytestmark = pytest.mark.online runner = CliRunner(mix_stderr=False) default_run_cmd = generate_run_cmd( runner=runner, app=app, default_args=["convert-markdown"] ) run_with_config = partial(run_with_config, default_run_cmd=default_run_cmd) def test_convert_command_default_behavior(make_one_page_config): """Makes sure that there is no extra output in stdout when running the tool""" config_file, config = make_one_page_config Path(config.pages[0].page_file).write_text("# Title\n* one\n* two") result = run_with_config(config_file=config_file, other_args=[]) assert result.exit_code == 0 assert ( result.stdout == """<h1>Title</h1> <ul> <li>one</li> <li>two</li> </ul>
confluence_instance, run_with_config, generate_local_config, get_page_id_from_stdout, fake_content_generator, page_created, fake_title_generator, get_pages_ids_from_stdout, ) from functools import partial pytestmark = pytest.mark.online runner = CliRunner() default_run_cmd = generate_run_cmd( runner=runner, app=app, default_args=["post-page", "--upload-files"] ) upload_files = partial(run_with_config, default_run_cmd=partial(default_run_cmd)) @pytest.fixture(scope="function") def setup_page(tmp_path, record_pages): """Prepares a page to upload files to""" config_path, config = generate_local_config(tmp_path, pages=1) post_page_run_cmd = generate_run_cmd( runner=runner, app=app, default_args=["post-page"] ) create_page = partial(run_with_config, default_run_cmd=post_page_run_cmd) result = create_page( config_file=config_path, record_pages=record_pages, input="Y\nN\nY\n" )
import pytest from typer.testing import CliRunner, Result from confluence_poster.main import app from utils import generate_run_cmd, run_with_config from functools import partial pytestmark = pytest.mark.online runner = CliRunner() default_run_cmd = generate_run_cmd( runner=runner, app=app, default_args=["--quiet", "validate", "--online"] ) run_with_config = partial(run_with_config, default_run_cmd=default_run_cmd) def test_validate_quiet(make_one_page_config): """Tests quiet flag execution: checks that the stdout is empty and the exit code is 0""" config_file, config = make_one_page_config result: Result = run_with_config(config_file=config_file) assert result.exit_code == 0 assert result.stdout is ""
from typer.testing import CliRunner, Result from confluence_poster.main import app, default_config_name from utils import generate_run_cmd from pathlib import Path import pytest pytestmark = pytest.mark.offline runner = CliRunner() default_run_cmd = generate_run_cmd(runner=runner, app=app, default_args=["create-config"]) @pytest.fixture(autouse=True) def setup_env_dirs(tmp_path, monkeypatch): new_home = tmp_path / "home" cwd = tmp_path / "cwd" (new_home / "confluence_poster").mkdir(parents=True) cwd.mkdir() monkeypatch.setenv("XDG_CONFIG_HOME", str(new_home)) monkeypatch.setattr("xdg.BaseDirectory.xdg_config_home", str(new_home)) monkeypatch.chdir(cwd) # args and kwargs here suppress errors about extra arguments # noinspection PyUnusedLocal def mock_config_dialog(filename, *args, **kwargs): """Mocks the config dialog run by just creating the file and returning True"""