Пример #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)
Пример #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
Пример #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)
Пример #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')
Пример #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')
Пример #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__))
Пример #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__))
Пример #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')
Пример #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')
Пример #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
Пример #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')
Пример #12
0
 def test_norequiretls(self, capsys, mocker):
     mocker.patch("aiosmtpd.main.PROGRAM", "smtpd")
     parser, args = parseargs(("--no-requiretls",))
     assert args.requiretls is False
Пример #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"
Пример #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
Пример #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
Пример #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)
Пример #17
0
 def test_host_no_port(self):
     parser, args = parseargs(args=('-l', 'foo'))
     self.assertEqual(args.host, 'foo')
     self.assertEqual(args.port, 8025)
Пример #18
0
 def test_host_no_port(self):
     parser, args = parseargs(args=('-l', 'foo'))
     self.assertEqual(args.host, 'foo')
     self.assertEqual(args.port, 8025)
Пример #19
0
 def test_default_host_port(self):
     parser, args = parseargs(args=())
     self.assertEqual(args.host, 'localhost')
     self.assertEqual(args.port, 8025)
Пример #20
0
 def test_default_host_port(self):
     parser, args = parseargs(args=())
     self.assertEqual(args.host, 'localhost')
     self.assertEqual(args.port, 8025)
Пример #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)
Пример #22
0
 def test_host_no_host(self):
     parser, args = parseargs(args=('-l', ':25'))
     self.assertEqual(args.host, 'localhost')
     self.assertEqual(args.port, 25)
Пример #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)
Пример #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)
Пример #25
0
 def test_listen(self):
     parser, args = parseargs(args=('--listen', 'foo:25'))
     self.assertEqual(args.host, 'foo')
     self.assertEqual(args.port, 25)
Пример #26
0
 def test_listen(self):
     parser, args = parseargs(args=('--listen', 'foo:25'))
     self.assertEqual(args.host, 'foo')
     self.assertEqual(args.port, 25)
Пример #27
0
 def test_handler_no_from_cli(self):
     parser, args = parseargs(("-c", "aiosmtpd.tests.test_main.NullHandler"))
     assert isinstance(args.handler, NullHandler)
Пример #28
0
 def test_host_no_host(self):
     parser, args = parseargs(args=('-l', ':25'))
     self.assertEqual(args.host, 'localhost')
     self.assertEqual(args.port, 25)
Пример #29
0
 def test_handler_from_cli_exception(self):
     with pytest.raises(TypeError):
         parseargs(("-c", "aiosmtpd.tests.test_main.FromCliHandler", "FOO", "BAR"))
Пример #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"
Пример #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')