def test_dict_default_novalues(capsys, monkeypatch): """Test ask dict with no input values and defaults.""" promptness = pytan3.utils.prompts.Promptness() asks = [ { "key": "num", "text": "Number", "method": "ask_int", "default": 99 }, { "key": "name", "text": "Name", "method": "ask_str", "secure": False, "default": ".*", }, ] inputs = ["", ""] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): v = promptness.ask_dict(asks=asks) assert v == {"num": 99, "name": ".*"}
def test_str_valid(capsys, monkeypatch): """Test str validation.""" promptness = pytan3.utils.prompts.Promptness() inputs = ["Xyz"] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): promptness.ask_str(text="text", validate="Xyz")
def test_missing_cert_user_no(self, tmp_path, httpsbin_cert): """Test exc thrown when user says no to validity prompt.""" server, example_cert = httpsbin_cert url = server() with pytest.warns(pytan3.http_client.exceptions.CertificateNotFoundWarning): with pytest.raises(pytan3.http_client.exceptions.CertificateInvalidError): inputs = ["n", "n", "n"] with input_mocker.InputMocker(inputs=inputs): http_client = pytan3.http_client.HttpClient(url=url, lvl="debug") http_client.certify(path=tmp_path, lvl="debug")
def test_str_value_default(capsys, monkeypatch): """Test valid str value with default works.""" promptness = pytan3.utils.prompts.Promptness() inputs = ["23"] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): v = promptness.ask_str(text="text", default="Alpha") assert v == "23"
def test_str_empty_value_default(capsys, monkeypatch): """Test default str value used when no value provided.""" promptness = pytan3.utils.prompts.Promptness() inputs = [""] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): v = promptness.ask_str(text="text", default="Alpha") assert v == "Alpha"
def test_str_empty_value_nodefault_emptyok(capsys, monkeypatch): """Test empty value returned when no value provided and emptyok=True.""" promptness = pytan3.utils.prompts.Promptness() inputs = [""] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): v = promptness.ask_str(text="text", empty_ok=True) assert v == ""
def test_str_invalid(capsys, monkeypatch): """Test exc thrown when invalid value supplied.""" promptness = pytan3.utils.prompts.Promptness() inputs = ["xyz"] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): with pytest.raises(pytan3.utils.prompts.InvalidValueError): promptness.ask_str(text="text", validate="abc")
def test_str_empty_value_no_default(capsys, monkeypatch): """Test exc thrown no str value provided.""" promptness = pytan3.utils.prompts.Promptness() inputs = [""] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): with pytest.raises(pytan3.utils.prompts.EmptyValueError): promptness.ask_str(text="text")
def test_int_value(capsys, monkeypatch): """Test valid int value works.""" promptness = pytan3.utils.prompts.Promptness() inputs = ["23"] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): v = promptness.ask_int(text="text") assert v == 23
def test_int_invalid_value_no_default(capsys, monkeypatch): """Test exc thrown with invalid int value.""" promptness = pytan3.utils.prompts.Promptness() inputs = ["moo", "moo", "moo", "moo", "moo"] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): with pytest.raises(pytan3.utils.prompts.InvalidValueError): promptness.ask_int(text="text")
def test_bool_value(capsys, monkeypatch): """Test valid bool value.""" promptness = pytan3.utils.prompts.Promptness() inputs = ["y"] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): v = promptness.ask_bool(text="text") assert v is True
def test_choice_empty_value_no_default(capsys, monkeypatch): """Test exc thrown when no choice provided with no default value.""" promptness = pytan3.utils.prompts.Promptness() inputs = ["", "", "", "", ""] with capsys.disabled(): monkeypatch.setattr(promptness.input_stream, "isatty", isatty) monkeypatch.setattr(promptness.output_stream, "isatty", isatty) with input_mocker.InputMocker(inputs=inputs): with pytest.raises(pytan3.utils.prompts.EmptyValueError): promptness.ask_choice(text="text", choices=["not", "a", "choice"])
def test_dict_overrides(capsys, monkeypatch): """Test ask dict with no input values and defaults with overrides.""" promptness = pytan3.utils.prompts.Promptness() asks = [{ "key": "num", "text": "Number", "method": "ask_int", "default": 99 }] inputs = [""] over = {"num": 34} with capsys.disabled(): with input_mocker.InputMocker(inputs=inputs): v = promptness.ask_dict(asks=asks, overrides=over) assert v == {"num": 34}
def test_missing_cert_osenv_yes( self, caplog, capsys, tmp_path, httpsbin_cert, log_check ): """Test setting PYTAN_CERT_VALID env var makes valid prompt default=yes.""" caplog.set_level(logging.DEBUG) server, example_cert = httpsbin_cert url = server() os.environ["PYTAN_CERT_VALID"] = "y" with pytest.warns(pytan3.http_client.exceptions.CertificateNotFoundWarning): with capsys.disabled(): inputs = ["", "", ""] with input_mocker.InputMocker(inputs=inputs): http_client = pytan3.http_client.HttpClient(url=url, lvl="debug") http_client.certify(path=tmp_path, lvl="debug") del (os.environ["PYTAN_CERT_VALID"]) assert http_client.certify.store.subject == {"common_name": "example.com"} entries = ["Wrote certificate for"] log_check(caplog, entries)
def test_missing_cert_user_yes( self, caplog, capsys, tmp_path, httpsbin_cert, log_check, monkeypatch ): """Test use saying yes to is cert valid works.""" caplog.set_level(logging.DEBUG) server, example_cert = httpsbin_cert url = server() monkeypatch.setattr("pytan3.http_client.SHOW_CERT", "yes") monkeypatch.setattr("pytan3.http_client.SHOW_CHAIN", "yes") monkeypatch.setattr("pytan3.http_client.CERT_VALID", "yes") with pytest.warns(pytan3.http_client.exceptions.CertificateNotFoundWarning): with capsys.disabled(): inputs = ["y", "y", "y"] with input_mocker.InputMocker(inputs=inputs): http_client = pytan3.http_client.HttpClient(url=url, lvl="debug") http_client.certify(path=tmp_path, lvl="debug") assert http_client.certify.store.subject == {"common_name": "example.com"} entries = ["Wrote certificate for"] log_check(caplog, entries)