Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
 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)
Exemplo n.º 4
0
 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')
Exemplo n.º 5
0
 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')
Exemplo n.º 6
0
 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__))
Exemplo n.º 7
0
 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__))
Exemplo n.º 8
0
 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')
Exemplo n.º 9
0
 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')
Exemplo n.º 10
0
 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
Exemplo n.º 11
0
 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')
Exemplo n.º 12
0
 def test_norequiretls(self, capsys, mocker):
     mocker.patch("aiosmtpd.main.PROGRAM", "smtpd")
     parser, args = parseargs(("--no-requiretls",))
     assert args.requiretls is False
Exemplo n.º 13
0
 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"
Exemplo n.º 14
0
 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
Exemplo n.º 15
0
 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
Exemplo n.º 16
0
 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)
Exemplo n.º 17
0
 def test_host_no_port(self):
     parser, args = parseargs(args=('-l', 'foo'))
     self.assertEqual(args.host, 'foo')
     self.assertEqual(args.port, 8025)
Exemplo n.º 18
0
 def test_host_no_port(self):
     parser, args = parseargs(args=('-l', 'foo'))
     self.assertEqual(args.host, 'foo')
     self.assertEqual(args.port, 8025)
Exemplo n.º 19
0
 def test_default_host_port(self):
     parser, args = parseargs(args=())
     self.assertEqual(args.host, 'localhost')
     self.assertEqual(args.port, 8025)
Exemplo n.º 20
0
 def test_default_host_port(self):
     parser, args = parseargs(args=())
     self.assertEqual(args.host, 'localhost')
     self.assertEqual(args.port, 8025)
Exemplo n.º 21
0
 def test_ipv6_host_port(self):
     parser, args = parseargs(args=('-l', '::0:25'))
     self.assertEqual(args.host, '::0')
     self.assertEqual(args.port, 25)
Exemplo n.º 22
0
 def test_host_no_host(self):
     parser, args = parseargs(args=('-l', ':25'))
     self.assertEqual(args.host, 'localhost')
     self.assertEqual(args.port, 25)
Exemplo n.º 23
0
 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)
Exemplo n.º 24
0
 def test_ipv6_host_port(self):
     parser, args = parseargs(args=('-l', '::0:25'))
     self.assertEqual(args.host, '::0')
     self.assertEqual(args.port, 25)
Exemplo n.º 25
0
 def test_listen(self):
     parser, args = parseargs(args=('--listen', 'foo:25'))
     self.assertEqual(args.host, 'foo')
     self.assertEqual(args.port, 25)
Exemplo n.º 26
0
 def test_listen(self):
     parser, args = parseargs(args=('--listen', 'foo:25'))
     self.assertEqual(args.host, 'foo')
     self.assertEqual(args.port, 25)
Exemplo n.º 27
0
 def test_handler_no_from_cli(self):
     parser, args = parseargs(("-c", "aiosmtpd.tests.test_main.NullHandler"))
     assert isinstance(args.handler, NullHandler)
Exemplo n.º 28
0
 def test_host_no_host(self):
     parser, args = parseargs(args=('-l', ':25'))
     self.assertEqual(args.host, 'localhost')
     self.assertEqual(args.port, 25)
Exemplo n.º 29
0
 def test_handler_from_cli_exception(self):
     with pytest.raises(TypeError):
         parseargs(("-c", "aiosmtpd.tests.test_main.FromCliHandler", "FOO", "BAR"))
Exemplo n.º 30
0
 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"
Exemplo n.º 31
0
 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')