Best Python code snippet using localstack_python
test_cmd_create_ssl_cert.py
Source:test_cmd_create_ssl_cert.py  
1"""2	Here we test our main console utility functionality3"""4import os5import sys67import pytest8import mock9from click.testing import CliRunner1011from tbtbot.apptemplate import template_strings as tstrings12from tbtbot.tbtboter import cli13from tbtbot.tbtboter.commands.cmd_create_bot import (14	make_config_file, make_env_file, set_env15)16from tbtbot.tbtboter import commands17181920@pytest.fixture21def runner():22	return CliRunner()232425def test_create_ssl_cert_command_invalid_path(runner):26	""" This test must rise SystemExit because we are giving nonexistent directory name27		which casues to prompt: "Use current dir?" then gets again 'koko' and aborts28	"""29	with runner.isolated_filesystem():30		result = runner.invoke(cli.main, ['create_ssl_cert', '--path'], input='koko\nchocolate')31		assert type(result.exception) == SystemExit32		assert 'Error: --path option requires an argument' in result.output33343536def test_create_certificat_success(runner):37	func = commands.cmd_create_ssl_cert.create_certificate38	with runner.isolated_filesystem():39		with mock.patch('subprocess.call') as mocked_call:40			subprocess_cmd = commands.cmd_create_ssl_cert.get_openssl_command41			mocked_call.return_value = 042			result = func('jeje')43			assert mocked_call.called == True44			assert mocked_call.called_once_with(subprocess_cmd('jeje'))45			assert 'ssl' in result464748def test_create_certificat_fail(runner):49	func = commands.cmd_create_ssl_cert.create_certificate50	with runner.isolated_filesystem():51		with mock.patch('subprocess.call') as mocked_call:52			subprocess_cmd = commands.cmd_create_ssl_cert.get_openssl_command53			mocked_call.return_value = 154			result = func('jeje')55			assert mocked_call.called == True56			assert mocked_call.called_once_with(subprocess_cmd('jeje'))57			assert result == False585960def test_create_ssl_cert_failed(runner):61	"""In this test create_ssl_cert command fails to create keys in koko dir"""62	with runner.isolated_filesystem():63		os.mkdir('koko')64		commands.cmd_create_ssl_cert.create_certificate = mock.Mock(return_value=False)65		func = commands.cmd_create_ssl_cert.create_certificate66		result = runner.invoke(cli.main, ['create_ssl_cert'], input="chocolate\nkoko")67		assert func.called == True68		assert len(func.call_args) is 269		assert len(os.listdir('koko')) == 070		assert type(result.exception) == IOError717273def test_create_ssl_cert_succed(runner):74	"""In this test create_ssl_cert command successfully creates keys in koko dir"""75	with runner.isolated_filesystem():76		os.mkdir('koko')77		commands.cmd_create_ssl_cert.create_certificate = mock.Mock(return_value=True)78		func = commands.cmd_create_ssl_cert.create_certificate79		result = runner.invoke(cli.main, ['create_ssl_cert'], input="chocolate\nkoko")80		assert func.called == True81		assert len(func.call_args) is 2
...pyhttps.py
Source:pyhttps.py  
...18if PY3:19    OpenSslExecutableNotFoundError = FileNotFoundError20else:21    OpenSslExecutableNotFoundError = OSError22def create_ssl_cert():23    if PY3:24        from subprocess import DEVNULL25    else:26        DEVNULL = open(os.devnull, 'wb')27    try:28        ssl_exec_list = ['openssl', 'req', '-new', '-x509', '-keyout', ssl_cert_path,29                         '-out', ssl_cert_path, '-days', '365', '-nodes',30                        ]31        call(ssl_exec_list)32        # call(ssl_exec_list, stdout=DEVNULL, stderr=DEVNULL)33    except OpenSslExecutableNotFoundError:34        logging.error('openssl executable not found!')35        exit(1)36    logging.info('Self signed ssl certificate created at {}'.format(ssl_cert_path))37def exit_handler():38    # remove certificate file at exit39    os.remove(ssl_cert_path)40    logging.info('Bye!')41def main():42    logging.info('pyhttps {}'.format(version))43    create_ssl_cert()44    atexit.register(exit_handler)45    if PY3:46        import http.server47        import socketserver48        import ssl49        logging.info('Server running... https://{}:{}'.format(server_host, server_port))50        httpd = socketserver.TCPServer((server_host, server_port), http.server.SimpleHTTPRequestHandler)51        httpd.socket = ssl.wrap_socket(httpd.socket, certfile=ssl_cert_path, server_side=True)52    else:53        import BaseHTTPServer54        import SimpleHTTPServer55        import ssl56        logging.info('Server running... https://{}:{}'.format(server_host, server_port))57        httpd = BaseHTTPServer.HTTPServer((server_host, server_port), SimpleHTTPServer.SimpleHTTPRequestHandler)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
