def test_cli_boolean_args(): """test parser for boolean arguments""" args = CLI.parse_args(["--version"]) assert args.version is True args = CLI.parse_args(["--test"]) assert args.test is True args = CLI.parse_args(["--print-config-file"]) assert args.print_config_file is True args = CLI.parse_args(["-T"]) assert args.check_login is True
def test_cli_with_test_args(): """Test command line arguments with --test""" testargs = [ "yessssms", "--test", "-l", "06641234567", "-p", "passw0rd", "-t", "+43676564736", ] with mock.patch.object(sys, "argv", testargs): with requests_mock.Mocker() as m: m.register_uri( "POST", _LOGIN_URL, status_code=302, # pylint: disable=protected-access headers={"location": _KONTOMANAGER_URL}, ) m.register_uri("GET", _KONTOMANAGER_URL, status_code=200) m.register_uri( "POST", _WEBSMS_URL, status_code=200, text="<h1>Ihre SMS wurde erfolgreich " + "verschickt!</h1>", ) m.register_uri("GET", _LOGOUT_URL, status_code=200) val = CLI().exit_status assert val == 0
def test_cli_with_invalid_test_login_arg(capsys): """Test check-login argument""" testargs = [ "yessssms", "-m", "test", "-l", "06641234567", "-p", "passw0rd", "-T" ] with mock.patch.object(sys, "argv", testargs): with requests_mock.Mocker() as m: m.register_uri( "POST", # pylint: disable=protected-access _LOGIN_URL, status_code=200, text="Bla bla<strong>Login nicht erfolgreichBlaBla", ) m.register_uri( "GET", # pylint: disable=protected-access _LOGOUT_URL, status_code=200, ) val = CLI().exit_status captured = capsys.readouterr() assert val == 1 assert "error: login data is NOT valid" in captured.out
def test_cli_with_printconfigfile_arg(capsys): """Test print-config-file parameter""" testargs = ["yessssms", "--print-config-file"] with mock.patch.object(sys, "argv", testargs): CLI() captured = capsys.readouterr() assert captured.out == CONFIG_FILE_CONTENT
def test_cli_with_version_arg(capsys): """Test version cli argument""" testargs = ["yessssms", "--version"] with mock.patch.object(sys, "argv", testargs): CLI() captured = capsys.readouterr() assert captured.out == "yessssms " + VERSION + "\n"
def test_cli_with_no_arg(capsys): """Test handling of no arguments""" testargs = ["yessssms"] with mock.patch.object(sys, "argv", testargs): CLI() captured = capsys.readouterr() assert "usage: yessssms " in captured.out
def test_cli_config_file(valid_connection, config_file): """Test CLI config file.""" testargs = ["yessssms", "-m", "Blablabla", "-t", "03141512345"] with mock.patch.object(sys, "argv", testargs): cli = CLI() assert cli.message == "Blablabla" assert cli.recipient == "03141512345" assert cli.yessssms._logindata["login_rufnummer"] == "03141592653" assert cli.yessssms._logindata["login_passwort"] == "MySecr3t" assert cli.yessssms._provider == "yesss"
def test_cli_with_no_login_or_password(capsys, valid_connection): """Test empty login parameters""" testargs = ["yessssms", "-m", "test"] # "-l", "\"\"", "-p", "\"\""] # print("test:..." + str(YesssSMS.const.CONFIG_FILE_PATHS)) with (mock.patch.object(sys, "argv", testargs)): with pytest.raises(SystemExit) as wrapped_e: CLI() assert wrapped_e.type == SystemExit assert wrapped_e.value.code == 2 captured = capsys.readouterr() assert "error: no username or password defined " in captured.out
def test_cli_with_mvno_simfonie_arg(): """Test command line arguments with --mvno""" from YesssSMS.const import PROVIDER_URLS PROVIDER = PROVIDER_URLS["SIMfonie".lower()] _LOGIN_URL = PROVIDER["LOGIN_URL"] _LOGOUT_URL = PROVIDER["LOGOUT_URL"] _KONTOMANAGER_URL = PROVIDER["KONTOMANAGER_URL"] _WEBSMS_URL = PROVIDER["WEBSMS_URL"] testargs = [ "yessssms", "--test", "-l", "06641234567", "-p", "passw0rd", "-t", "+43676564736", "--mvno", "SIMfonie", ] with mock.patch.object(sys, "argv", testargs): with requests_mock.Mocker() as m: m.register_uri( "POST", _LOGIN_URL, status_code=302, # pylint: disable=protected-access headers={"location": _KONTOMANAGER_URL}, ) m.register_uri("GET", _KONTOMANAGER_URL, status_code=200) m.register_uri( "POST", _WEBSMS_URL, status_code=200, text="<h1>Ihre SMS wurde erfolgreich " + "verschickt!</h1>", ) m.register_uri("GET", _LOGOUT_URL, status_code=200) sms = CLI().yessssms assert "simfonie" == sms._provider assert _LOGIN_URL == sms._login_url assert _LOGOUT_URL == sms._logout_url assert _KONTOMANAGER_URL == sms._kontomanager assert _WEBSMS_URL == sms._websms_url assert _LOGIN_URL == "https://simfonie.kontomanager.at/index.php" assert (_LOGOUT_URL == "https://simfonie.kontomanager.at/index.php?dologout=2") assert (_KONTOMANAGER_URL == "https://simfonie.kontomanager.at/kundendaten.php") assert _WEBSMS_URL == "https://simfonie.kontomanager.at/websms_send.php"
def test_cli_with_mvno_div_arg(): """Test command line arguments with --mvno""" from YesssSMS.const import PROVIDER_URLS all_providers = PROVIDER_URLS.keys() for provider in all_providers: PROVIDER = PROVIDER_URLS[provider.lower()] _LOGIN_URL = PROVIDER["LOGIN_URL"] _LOGOUT_URL = PROVIDER["LOGOUT_URL"] _KONTOMANAGER_URL = PROVIDER["KONTOMANAGER_URL"] _WEBSMS_URL = PROVIDER["WEBSMS_URL"] testargs = [ "yessssms", "--test", "-l", "06641234567", "-p", "passw0rd", "-t", "+43676564736", "--mvno", provider.upper(), ] with mock.patch.object(sys, "argv", testargs): with requests_mock.Mocker() as m: m.register_uri( "POST", _LOGIN_URL, status_code=302, # pylint: disable=protected-access headers={"location": _KONTOMANAGER_URL}, ) m.register_uri("GET", _KONTOMANAGER_URL, status_code=200) m.register_uri( "POST", _WEBSMS_URL, status_code=200, text="<h1>Ihre SMS wurde erfolgreich " + "verschickt!</h1>", ) m.register_uri("GET", _LOGOUT_URL, status_code=200) cli = CLI() sms = cli.yessssms assert provider == sms._provider assert _LOGIN_URL == sms._login_url assert _LOGOUT_URL == sms._logout_url assert _KONTOMANAGER_URL == sms._kontomanager assert _WEBSMS_URL == sms._websms_url
def test_cli_sending_error(valid_mock_connection, mocked_config_file_custom_provider, sending_error, capsys): """Test CLI SMS sending error.""" if int(sys.version[2]) < 7: # don't run test on 3.5, 3.6 pytest.skip("issue with mock_open") testargs = ["yessssms", "-m", "Bilde mir nicht ein was rechts zu wissen"] with mock.patch.object(sys, "argv", testargs): with pytest.raises(SystemExit) as wrapped_e: CLI() assert wrapped_e.type == SystemExit assert wrapped_e.value.code == 5 assert "error: could not send SMS" in capsys.readouterr().out
def test_cli_suspended_error(valid_mock_connection, mocked_config_file_custom_provider, suspended_error, capsys): """Test CLI suspended error.""" if int(sys.version[2]) < 7: # don't run test on 3.5, 3.6 pytest.skip("issue with mock_open") testargs = ["yessssms", "-m", "Bilde mir nicht ein was rechts zu wissen"] with mock.patch.object(sys, "argv", testargs): with pytest.raises(SystemExit) as wrapped_e: CLI() assert wrapped_e.type == SystemExit assert wrapped_e.value.code == 4 assert ( "error: your account was suspended because of 3 failed login attempts." in capsys.readouterr().out)
def test_cli_connection_error(connection_error, capsys): """Test connection error.""" testargs = [ "yessssms", "--test", "-l", "06641234567", "-p", "passw0rd", "-t", "+43676564736", ] with mock.patch.object(sys, "argv", testargs): with pytest.raises(SystemExit) as wrapped_e: CLI() assert wrapped_e.type == SystemExit assert wrapped_e.value.code == 3 assert "error: could not connect to provider. " in capsys.readouterr().out
def test_cli_unsupported_chars_error( valid_mock_connection, mocked_config_file_custom_provider, unsupported_chars_error, capsys, ): """Test CLI unsupported chars error.""" if int(sys.version[2]) < 7: # don't run test on 3.5, 3.6 pytest.skip("issue with mock_open") testargs = ["yessssms", "-m", "Bilde mir nicht ein was rechts zu wissen"] with mock.patch.object(sys, "argv", testargs): with pytest.raises(SystemExit) as wrapped_e: CLI() assert wrapped_e.type == SystemExit assert wrapped_e.value.code == 6 assert "error: message contains unsupported character(s)" in capsys.readouterr( ).out
def test_cli_mocked_config_file(valid_mock_connection, mocked_config_file_custom_provider): """Test CLI config file.""" if int(sys.version[2]) < 7: # don't run test on 3.5, 3.6 pytest.skip("issue with mock_open") testargs = ["yessssms", "-m", "Bilde mir nicht ein was rechts zu wissen"] with mock.patch.object(sys, "argv", testargs): cli = CLI() print(cli.config_files) assert cli.yessssms._logindata["login_rufnummer"] == "06501234567" assert cli.yessssms._logindata["login_passwort"] == "MySecre3tPassw0rd" assert cli.yessssms._login_url == "mock://kontomanager.at/index.php" assert cli.yessssms._logout_url == "mock://kontomanager.at/index.php?dologout=2" assert cli.yessssms._kontomanager == "mock://kontomanager.at/kundendaten.php" assert cli.yessssms._websms_url == "mock://kontomanager.at/websms_send.php" assert cli.recipient == "+43664123123123" assert cli.message == "Bilde mir nicht ein was rechts zu wissen"
def test_cli_with_test_login_arg(capsys): """Test check-login argument""" testargs = [ "yessssms", "-m", "test", "-l", "06641234567", "-p", "passw0rd", "-T" ] with mock.patch.object(sys, "argv", testargs): with requests_mock.Mocker() as m: m.register_uri( "POST", # pylint: disable=protected-access _LOGIN_URL, status_code=302, # pylint: disable=protected-access headers={"location": _KONTOMANAGER_URL}, ) m.register_uri( "GET", # pylint: disable=protected-access _KONTOMANAGER_URL, status_code=200, ) m.register_uri( "POST", # pylint: disable=protected-access _WEBSMS_URL, status_code=200, text="<h1>Ihre SMS wurde erfolgreich " + "verschickt!</h1>", ) m.register_uri( "GET", # pylint: disable=protected-access _LOGOUT_URL, status_code=200, ) val = CLI().exit_status captured = capsys.readouterr() assert val == 0 assert captured.out == "ok: login data is valid.\n"
def test_cli_mocked_config_file_error(valid_mock_connection, mocked_config_file_error, capsys): """Test CLI config file error.""" if int(sys.version[2]) < 7: # don't run test on 3.5, 3.6 pytest.skip("issue with mock_open") testargs = [ "yessssms", "-m", "Bilde mir nicht ein was rechts zu wissen", "-c", "/tmp/custom_settings.conf", ] with mock.patch.object(sys, "argv", testargs): with pytest.raises(SystemExit) as wrapped_e: CLI() assert "error: missing settings or invalid settings." in capsys.readouterr( ).out assert wrapped_e.type == SystemExit assert wrapped_e.value.code == 8 assert "/tmp/custom_settings.conf" in CONFIG_FILE_PATHS
def test_cli_version_info(capsys): """test for correct version info print""" CLI.version_info() captured = capsys.readouterr() assert captured.out == "yessssms " + VERSION + "\n"
def test_cli_argparse(): """test parser for different arguments""" args = CLI.parse_args(["-t", "0664123456"]) assert args.recipient == "0664123456" args = CLI.parse_args(["--to", "0664123456"]) assert args.recipient == "0664123456" args = CLI.parse_args(["-l", "0676456789123"]) assert args.login == "0676456789123" args = CLI.parse_args(["--login", "0676456789123"]) assert args.login == "0676456789123" args = CLI.parse_args(["-p", "s3cret..11"]) assert args.password == "s3cret..11" args = CLI.parse_args(["--password", "s3cret..11"]) assert args.password == "s3cret..11" args = CLI.parse_args(["-c", ".yessssms.config"]) assert args.configfile == ".yessssms.config" args = CLI.parse_args(["--configfile", ".yessssms.config"]) assert args.configfile == ".yessssms.config" args = CLI.parse_args(["--message", "testmessage 123 - can you see this?"]) assert args.message == "testmessage 123 - can you see this?" args = CLI.parse_args(["-m", "testmessage 123 - can you see this?"]) assert args.message == "testmessage 123 - can you see this?" args = CLI.parse_args(["--mvno", "YESSS"]) assert args.provider == "YESSS" args = CLI.parse_args(["--mvno", "EDUCOM"]) assert args.provider == "EDUCOM" args = CLI.parse_args(["--mvno", "SIMfonie"]) assert args.provider == "SIMfonie" args = CLI.parse_args(["--mvno", "BLABLABLA"]) assert args.provider == "BLABLABLA"
def test_cli_print_config_file(capsys): """test for correct config file output""" CLI.print_config_file() captured = capsys.readouterr() assert captured.out == CONFIG_FILE_CONTENT
def test_cli_stdin(): """Test command line with stdin""" from YesssSMS.YesssSMS import MAX_MESSAGE_LENGTH_STDIN testargs = [ "yessssms", "--test", "-l", "06641234567", "-p", "passw0rd", "-m", "-" ] in_message = """Da steh’ ich nun, ich armer Thor! Und bin so klug als wie zuvor; Heiße Magister, heiße Doctor gar, Und ziehe schon an die zehen Jahr, Herauf, herab und quer und krumm, Meine Schüler an der Nase herum – Und sehe, daß wir nichts wissen können! Das will mir schier das Herz verbrennen. Zwar bin ich gescheidter als alle die Laffen, Doctoren, Magister, Schreiber und Pfaffen; Mich plagen keine Scrupel noch Zweifel, Fürchte mich weder vor Hölle noch Teufel – Dafür ist mir auch alle Freud’ entrissen, Bilde mir nicht ein was rechts zu wissen, Bilde mir nicht ein, ich könnte was lehren, Die Menschen zu bessern und zu bekehren. Auch hab’ ich weder Gut noch Geld, Noch Ehr’ und Herrlichkeit der Welt. Es möchte kein Hund so länger leben! Drum hab’ ich mich der Magie ergeben, Ob mir durch Geistes Kraft und Mund Nicht manch Geheimniß würde kund; Daß ich nicht mehr mit sauerm Schweiß, Zu sagen brauche, was ich nicht weiß;""" with mock.patch.object(sys, "argv", testargs): with mock.patch.object(sys, "stdin", in_message): with requests_mock.Mocker() as m: m.register_uri( "POST", # pylint: disable=protected-access _LOGIN_URL, status_code=302, # pylint: disable=protected-access headers={"location": _KONTOMANAGER_URL}, ) m.register_uri( "GET", # pylint: disable=protected-access _KONTOMANAGER_URL, status_code=200, ) m.register_uri( "POST", # pylint: disable=protected-access _WEBSMS_URL, status_code=200, text="<h1>Ihre SMS wurde erfolgreich " + "verschickt!</h1>", ) m.register_uri( "GET", # pylint: disable=protected-access _LOGOUT_URL, status_code=200, ) message = CLI().message assert message.startswith("""Da steh’ ich nun, ich armer Thor! Und bin so klug als wie zuvor;""") assert message == in_message[:MAX_MESSAGE_LENGTH_STDIN]