Beispiel #1
0
def test_add_credential_to_nonempty(auth_config, tmpdir):
    old_cred = auth.Auth("username", "password")
    new_cred = auth.Auth("other_user", "other_pass")

    with temp_config(tmpdir, auth_config) as config_dir, temp_environ(HOME=config_dir), \
            patch("hop.auth.read_new_credential", MagicMock(return_value=new_cred)):
        args = MagicMock()
        args.cred_file = None
        args.force = False
        auth.add_credential(args)
        check_credential_file(configure.get_config_path("auth"), old_cred)
        check_credential_file(configure.get_config_path("auth"), new_cred)
Beispiel #2
0
def test_add_credential_to_nonempty_hostname_no_conflict(tmpdir):
    # unfortunately, we must permit duplicate usernames if one has a hostname and the other does not
    old_cred = auth.Auth("username", "password")
    new_cred = auth.Auth("username", "other_pass", "example.com")

    with temp_environ(HOME=str(tmpdir)), \
            patch("hop.auth.load_auth", MagicMock(return_value=[old_cred])), \
            patch("hop.auth.read_new_credential", MagicMock(return_value=new_cred)):
        args = MagicMock()
        args.cred_file = None
        args.force = False
        auth.add_credential(args)
        check_credential_file(configure.get_config_path("auth"), old_cred)
        check_credential_file(configure.get_config_path("auth"), new_cred)
Beispiel #3
0
def test_prune_outdated_auth(tmpdir):
    config_data = """
        foo = "bar"
        auth = [{
        username = "******",
        password = "******",
        hostname = "example.com"
        }]
        baz = "quux"
        """
    with temp_environ(XDG_CONFIG_HOME=str(tmpdir)):
        config_path = configure.get_config_path()
        os.makedirs(os.path.dirname(config_path), exist_ok=True)
        with open(config_path, "w") as f:
            f.write(config_data)
        auth.prune_outdated_auth()
        # the auth data should be gone
        new_config_data = check_no_auth_data(config_path)
        # all other data should remain untouched
        assert "foo" in new_config_data
        assert "baz" in new_config_data

        # the same should work for files in non-default locations
        alt_config_path = f"{os.path.dirname(config_path)}/other.toml"
        with open(alt_config_path, "w") as f:
            f.write(config_data)
        auth.prune_outdated_auth(alt_config_path)
        # the auth data should be gone
        new_config_data = check_no_auth_data(alt_config_path)
        # all other data should remain untouched
        assert "foo" in new_config_data
        assert "baz" in new_config_data
Beispiel #4
0
def test_setup_auth(script_runner, tmpdir):
    with temp_environ(XDG_CONFIG_HOME=str(tmpdir)):
        credentials_file = tmpdir / "credentials.csv"
        username = "******"
        password = "******"
        with open(credentials_file, "w") as f:
            f.write("username,password\n")
            f.write(username + "," + password + "\n")

        # check on new configuration file is written using credential file
        ret1 = script_runner.run("hop", "configure", "setup", "--import",
                                 str(credentials_file))
        assert ret1.success
        assert "hop : INFO : Generated configuration at:" in ret1.stderr
        configuration_file = configure.get_config_path()
        cf = open(configuration_file, "r")
        config_file_text = cf.read()
        assert username in config_file_text
        assert password in config_file_text
        os.remove(credentials_file)

        # hop configure setup (need --force)
        warning_message = \
            "hop : WARNING : Configuration already exists, overwrite file with --force"
        ret2 = script_runner.run("hop", "configure", "setup")
        assert warning_message in ret2.stderr
Beispiel #5
0
def test_get_config_path(tmpdir):
    with temp_environ(HOME=str(tmpdir)):
        if "XDG_CONFIG_HOME" in os.environ:
            # this change will revert at the end of the with block
            del os.environ["XDG_CONFIG_HOME"]

        # with HOME set but not XDG_CONFIG_HOME the config location should resolve to inside
        # ${HOME}/.config
        expected_path = os.path.join(tmpdir, ".config", "hop", "config.toml")
        config_loc = configure.get_config_path()
        assert config_loc == expected_path

        with temp_environ(XDG_CONFIG_HOME=str(tmpdir)):
            # with XDG_CONFIG_HOME set, no .config path component should be assumed
            expected_path = os.path.join(tmpdir, "hop", "config.toml")
            config_loc = configure.get_config_path()
            assert config_loc == expected_path
Beispiel #6
0
def test_add_credential_conflict_no_host(tmpdir, caplog):
    old_cred = auth.Auth("username", "password")
    new_cred = auth.Auth("username", "other_pass")

    with temp_environ(HOME=str(tmpdir)), \
            patch("hop.auth.read_new_credential", MagicMock(return_value=new_cred)):
        auth.write_auth_data(configure.get_config_path("auth"), [old_cred])
        args = MagicMock()
        args.cred_file = None
        args.force = False
        auth.add_credential(args)
        # without the force option, the old credential should not be overwritten
        check_credential_file(configure.get_config_path("auth"), old_cred)
        assert "Credential already exists; overwrite with --force" in caplog.text

        args.force = True
        auth.add_credential(args)
        # with the force option, the old credential should be overwritten
        check_credential_file(configure.get_config_path("auth"), new_cred)
Beispiel #7
0
def test_add_credential_to_empty(tmpdir):
    new_cred = auth.Auth("user", "pass")

    with temp_environ(HOME=str(tmpdir)), \
            patch("hop.auth.read_new_credential", MagicMock(return_value=new_cred)):
        args = MagicMock()
        args.cred_file = None
        args.force = False
        auth.add_credential(args)
        check_credential_file(configure.get_config_path("auth"), new_cred)
Beispiel #8
0
def test_prune_outdated_malformed(tmpdir):
    with temp_environ(XDG_CONFIG_HOME=str(tmpdir)):
        config_path = configure.get_config_path()
        os.makedirs(os.path.dirname(config_path), exist_ok=True)
        with open(config_path, "w") as f:
            f.write("not valid TOML IGIUF T J2(YHFOh q3pi8hoU *AHou7w3ht")
        # a RuntimeError should be raised when the file is unparseable garbage
        with pytest.raises(RuntimeError) as err:
            auth.prune_outdated_auth()
        assert f"configuration file {config_path} is malformed" in err.value.args[
            0]
Beispiel #9
0
def test_prune_outdated_empty(tmpdir):
    with temp_environ(XDG_CONFIG_HOME=str(tmpdir)):
        # when the config file does not exist, this function should successfully do nothing
        auth.prune_outdated_auth()

        config_path = configure.get_config_path()
        os.makedirs(os.path.dirname(config_path), exist_ok=True)
        with open(config_path, "w"):
            pass
        # should also work when the file exists but is empty
        auth.prune_outdated_auth()
        check_no_auth_data(config_path)
Beispiel #10
0
def test_delete_credential_with_hostname(tmpdir):
    with temp_environ(HOME=str(tmpdir)):
        with patch("hop.auth.load_auth",
                   MagicMock(return_value=delete_input_creds.copy())):
            auth.delete_credential("*****@*****.**")
        check_credential_deletion(configure.get_config_path("auth"), 2)
Beispiel #11
0
def test_get_config_path_invalid(tmpdir):
    with pytest.raises(ValueError):
        configure.get_config_path("hairstyle")