def test_get_backup_filename(freezer, monkeypatch, app, fmt, filename, now): freezer.move_to("2020-08-18 19:25:33") monkeypatch.setitem(app.config, 'BACKUP_FILE_TIME_FORMAT', fmt) fn = filename[:filename.rfind('.')] now_dt = datetime.fromisoformat(now) if isinstance(now, str) else None bfn = get_backup_filename(fn, now_dt) assert bfn == filename
def test_init_audit_keys_cmd(app, tmp_path, runner, freezer, args, input, output, has_file, makes_file, check_backup, result): freezer.move_to("2020-08-18 19:25:33") private_key_file = tmp_path / "private.pem" public_key_file = tmp_path / "public.pem" app.config["AUDIT_PRIVATE_KEY_FILE"] = str(private_key_file) app.config["AUDIT_PUBLIC_KEY_FILE"] = str(public_key_file) PRIVATE_KEY = "EXISTING PRIVATE KEY" if has_file: private_key_file.write_text(PRIVATE_KEY) else: assert not private_key_file.exists() cmd_result = runner.invoke(main, args, input=input) assert cmd_result.exit_code == result for s in output: assert s in cmd_result.output if makes_file: assert private_key_file.exists() check_key_validity(private_key_file.read_text(), 'RSA PRIVATE') assert public_key_file.exists() check_key_validity(public_key_file.read_text(), 'PUBLIC') if check_backup: backup_file = Path(get_backup_filename(str(private_key_file))) assert backup_file.exists() assert backup_file.read_text() == PRIVATE_KEY
def test_init_audit_keys_cmd( app, audit_keys: AuditKeys, runner, freezer, args, input, output, has_file, makes_file, check_backup, result, ): freezer.move_to("2020-08-18 19:25:33") PRIVATE_KEY = "EXISTING PRIVATE KEY" if has_file: audit_keys.private.write_text(PRIVATE_KEY) else: assert not audit_keys.private.exists() cmd_result = runner.invoke(main, args, input=input) assert cmd_result.exit_code == result for s in output: assert s in cmd_result.output if makes_file: assert audit_keys.private.exists() check_key_validity(audit_keys.private.read_text(), "RSA PRIVATE") assert audit_keys.public.exists() check_key_validity(audit_keys.public.read_text(), "PUBLIC") if check_backup: backup_file = Path(get_backup_filename(str(audit_keys.private))) assert backup_file.exists() assert backup_file.read_text() == PRIVATE_KEY
def _make_backup(what: str, filename: str) -> bool: backup_filename = get_backup_filename(filename) try: os.replace(filename, backup_filename) current_app.echo(f"Moved existing {what} to {backup_filename}", v=1) except OSError as ex: current_app.echo(f"Error moving {what} to {backup_filename}: {ex!s}") return False return True
def test_get_backup_filename( freezer, monkeypatch, app, fmt, input_filename, expected_filename, now, ): freezer.move_to("2020-08-18 19:25:33") monkeypatch.setitem(app.config, "BACKUP_FILE_TIME_FORMAT", fmt) if now is None: actual_filename = get_backup_filename(input_filename, ) else: actual_filename = get_backup_filename( input_filename, datetime.fromisoformat(now), ) assert actual_filename == expected_filename