def test_tls(self, capsys, mocker, args): mocker.patch("aiosmtpd.main.PROGRAM", "smtpd") with pytest.raises(SystemExit) as exc: parseargs(args) assert exc.value.code == 2 assert ("--tlscert and --tlskey must be specified together" in capsys.readouterr().err)
def test_ssl_files_err(self, capsys, mocker, meth, certfile, keyfile, expect): mocker.patch("aiosmtpd.main.PROGRAM", "smtpd") with pytest.raises(SystemExit) as exc: parseargs((f"--{meth}cert", certfile, f"--{meth}key", keyfile)) assert exc.value.code == 2 assert expect in capsys.readouterr().err
def test_handler_no_from_cli_exception(self, capsys): with pytest.raises(SystemExit) as excinfo: parseargs( ("-c", "aiosmtpd.tests.test_main.NullHandler", "FOO", "BAR")) assert excinfo.value.code == 2 assert ("Handler class aiosmtpd.tests.test_main takes no arguments" in capsys.readouterr().err)
def test_bad_port_number(self): stderr = StringIO() with patch('sys.stderr', stderr): with self.assertRaises(SystemExit) as cm: parseargs(('-l', ':foo')) self.assertEqual(cm.exception.code, 2) usage_lines = stderr.getvalue().splitlines() self.assertEqual(usage_lines[-1][-24:], 'Invalid port number: foo')
def test_v(self): stdout = StringIO() with ExitStack() as resources: resources.enter_context(patch('sys.stdout', stdout)) resources.enter_context(patch('aiosmtpd.main.PROGRAM', 'smtpd')) cm = resources.enter_context(self.assertRaises(SystemExit)) parseargs(('-v',)) self.assertEqual(cm.exception.code, 0) self.assertEqual(stdout.getvalue(), 'smtpd {}\n'.format(__version__))
def test_v(self): stdout = StringIO() with ExitStack() as resources: resources.enter_context(patch('sys.stdout', stdout)) resources.enter_context(patch('aiosmtpd.main.PROGRAM', 'smtpd')) cm = resources.enter_context(self.assertRaises(SystemExit)) parseargs(('-v', )) self.assertEqual(cm.exception.code, 0) self.assertEqual(stdout.getvalue(), 'smtpd {}\n'.format(__version__))
def test_handler_no_from_cli_exception(self): stderr = StringIO() with patch('sys.stderr', stderr): with self.assertRaises(SystemExit) as cm: parseargs(('-c', 'aiosmtpd.tests.test_main.TestHandler2', 'FOO', 'BAR')) self.assertEqual(cm.exception.code, 2) usage_lines = stderr.getvalue().splitlines() self.assertEqual( usage_lines[-1][-57:], 'Handler class aiosmtpd.tests.test_main takes no arguments')
def test_handler_no_from_cli_exception(self): stderr = StringIO() with patch('sys.stderr', stderr): with self.assertRaises(SystemExit) as cm: parseargs( ('-c', 'aiosmtpd.tests.test_main.TestHandler2', 'FOO', 'BAR')) self.assertEqual(cm.exception.code, 2) usage_lines = stderr.getvalue().splitlines() self.assertEqual( usage_lines[-1][-57:], 'Handler class aiosmtpd.tests.test_main takes no arguments')
def test_defaults(self): parser, args = parseargs(tuple()) assert args.classargs == tuple() assert args.classpath == "aiosmtpd.handlers.Debugging" assert args.debug == 0 assert isinstance(args.handler, Debugging) assert args.host == "localhost" assert args.listen is None assert args.port == 8025 assert args.setuid is True assert args.size is None assert args.smtputf8 is False assert args.smtpscert is None assert args.smtpskey is None assert args.tlscert is None assert args.tlskey is None assert args.requiretls is True
def test_handler_from_cli(self): # Ignore the host:port positional argument. parser, args = parseargs( ('-c', 'aiosmtpd.tests.test_main.TestHandler1', '--', 'FOO')) self.assertIsInstance(args.handler, TestHandler1) self.assertEqual(args.handler.called, 'FOO')
def test_norequiretls(self, capsys, mocker): mocker.patch("aiosmtpd.main.PROGRAM", "smtpd") parser, args = parseargs(("--no-requiretls",)) assert args.requiretls is False
def test_version(self, capsys, mocker, opt): mocker.patch("aiosmtpd.main.PROGRAM", "smtpd") with pytest.raises(SystemExit) as excinfo: parseargs((opt,)) assert excinfo.value.code == 0 assert capsys.readouterr().out == f"smtpd {__version__}\n"
def test_bad_port_number(self, capsys): with pytest.raises(SystemExit) as excinfo: parseargs(("-l", ":foo")) assert excinfo.value.code == 2 assert "Invalid port number: foo" in capsys.readouterr().err
def test_host_port(self, args, exp_host, exp_port): parser, args_ = parseargs(args=args) assert args_.host == exp_host assert args_.port == exp_port
def test_handler_no_from_cli(self): # Ignore the host:port positional argument. parser, args = parseargs( ('-c', 'aiosmtpd.tests.test_main.TestHandler2')) self.assertIsInstance(args.handler, TestHandler2)
def test_host_no_port(self): parser, args = parseargs(args=('-l', 'foo')) self.assertEqual(args.host, 'foo') self.assertEqual(args.port, 8025)
def test_default_host_port(self): parser, args = parseargs(args=()) self.assertEqual(args.host, 'localhost') self.assertEqual(args.port, 8025)
def test_ipv6_host_port(self): parser, args = parseargs(args=('-l', '::0:25')) self.assertEqual(args.host, '::0') self.assertEqual(args.port, 25)
def test_host_no_host(self): parser, args = parseargs(args=('-l', ':25')) self.assertEqual(args.host, 'localhost') self.assertEqual(args.port, 25)
def test_listen(self): parser, args = parseargs(args=('--listen', 'foo:25')) self.assertEqual(args.host, 'foo') self.assertEqual(args.port, 25)
def test_handler_no_from_cli(self): parser, args = parseargs(("-c", "aiosmtpd.tests.test_main.NullHandler")) assert isinstance(args.handler, NullHandler)
def test_handler_from_cli_exception(self): with pytest.raises(TypeError): parseargs(("-c", "aiosmtpd.tests.test_main.FromCliHandler", "FOO", "BAR"))
def test_handler_from_cli(self): parser, args = parseargs( ("-c", "aiosmtpd.tests.test_main.FromCliHandler", "--", "FOO") ) assert isinstance(args.handler, FromCliHandler) assert args.handler.called == "FOO"