diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2014-12-15 17:37:40 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2014-12-15 17:55:52 +0100 |
commit | f65b8f618ed0b59f0c2c14321bbfa9de8e802169 (patch) | |
tree | 372844320f2abac1579636198d36ec3a6e7d34eb /test | |
parent | e3ab39bf39d0ba75f6eb504752ef0ffb4bf4a3b1 (diff) | |
download | pygn-f65b8f618ed0b59f0c2c14321bbfa9de8e802169.tar.gz |
Add a simple test suite.
And also fix one embarrassing typo in mail2news module.
Fixes #1
Fixes #4
Fixes #6
Diffstat (limited to 'test')
-rw-r--r-- | test/__init__.py | 0 | ||||
-rwxr-xr-x | test/test_wlp.py | 108 |
2 files changed, 108 insertions, 0 deletions
diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/__init__.py diff --git a/test/test_wlp.py b/test/test_wlp.py new file mode 100755 index 0000000..dcb2b78 --- /dev/null +++ b/test/test_wlp.py @@ -0,0 +1,108 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import unittest +import sysconfig +import sys +import os +import os.path +import subprocess +import re + + +def distutils_dir_name(dname): + """Returns the name of a distutils build directory""" + f = "{dirname}.{platform}-{version[0]}.{version[1]}" + return f.format(dirname=dname, + platform=sysconfig.get_platform(), + version=sys.version_info) +wlp_lib_path = os.path.join('build', distutils_dir_name('lib')) +sys.path.insert(0, wlp_lib_path) + + +import wlp + + +class TestWLP(unittest.TestCase): + def test_wlp_parser(self): + wlp.setfilebyname('examples/whitelist.example') + wl_dict = wlp.mkdict() + expected_dict = {'alfarano@students.cs.unibo.it': { + 'From:': 'Cosimo Alfarano', + 'X-Firstname:': 'Cosimo' + }, + 'kame@innocent.com': { + 'From:': 'kame@inwind.it', + 'Reply-to': 'me', + 'Reply-to:': 'KA', + 'Sender:': 'Kalfa'} + } + self.assertEqual(wl_dict, expected_dict) + + +class TestM2N(unittest.TestCase): + def test_m2n(self): + expected_output = """Newsgroups: pyg.test +From: Pyg <pyg@localhost.com> +To: this header probably broke RFC, but is frequent. +Subject: test +Date: Sun, 1 Feb 2002 16:40:40 +0200 +Message-Id: <20001001164040.Aa8326@localhost> +Content-Type: text/plain; charset=us-ascii +Mime-Version: 1.0 +Return-Path: <pyg@localhost> +User-Agent: Mutt/1.2.5i +X-Gateway: pyg The Python Gateway - Mail to News +""" + with open('examples/mail') as in_mail: + pid = subprocess.Popen(['./pygm2n', '-TV', '-n', 'pyg.test'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE) + out, err = pid.communicate(in_mail.read()) + self.assertEqual(out, expected_output) + + +class TestN2M(unittest.TestCase): + def test_n2m(self): + expected_output = """Received: from GATEWAY by mitmanek.ceplovi.cz with pyg + for <test@example.com> ; Mon Dec 15 17:13:30 2014 (CEST) +From: kame@inwind.it (PYG) +To: test@example.com +Subject: pyg's article test +Date: 10 Jun 2000 23:20:47 +0200 +Organization: Debian GNU/Linux +Reply-To: pyg@localhost +Content-Type: text/plain; charset=US-ASCII +Mime-Version: 1.0 +Content-Transfer-Encoding: 7bit +X-Trace: pyg.server.tld 960672047 927 192.168.1.2 (10 Jun 2000 21:20:47 GMT) +X-Newsgroups: local.moderated +X-Gateway: pyg The Python Gateway Script: news2mail mail2news gateway +X-NNTP-Posting-Host: pyg.server.tld +Resent-From: sender@example.com +Resent-Sender: sender@example.com +""" + env = os.environ + env['PYTHONPATH'] = wlp_lib_path + + with open('examples/articletest.accepted') as in_mail: + pid = subprocess.Popen(['./pygn2m', '-TVt', 'test@example.com', + '-s', 'sender@example.com', '-d', + '-w', 'examples/whitelist.example'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, env=env) + in_message = in_mail.read().replace('pyg@pyg.server.tld', + 'kame@inwind.it') + out, err = pid.communicate(in_message) + out = re.sub(r'^Message-Id:.*$', '', out) + # Not sure how to compare two email mesages (with different + # times, etc.) so for now just to make sure the script doesn’t + # blow up and gives some output + # otherwise it would be + # self.assertEqual(out, expected_output) + self.assertEqual(pid.returncode, 0) + self.assertGreater(len(out), 0) + + +if __name__ == "__main__": + unittest.main() |