#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest

import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from DebianChangesBot.mailparsers import SecurityAnnounceParser as p

class TestMailParserSecurityAnnounce(unittest.TestCase):

    def setUp(self):
        self.headers = {
            'List-Id': '<debian-security-announce.lists.debian.org>',
            'Date': 'Sat, 19 Apr 2008 19:18:38 +0100',
            'Subject': '[SECURITY] [DSA 1234-5] New pinafore packages ' \
                'fix inertial dampener problem',
        }

    def testSimple(self):
        msg = p.parse(self.headers, [])

        self.assert_(msg)
        self.assertEqual(msg.dsa_number, 1234)
        self.assertEqual(msg.dsa_revision, 5)
        self.assertEqual(msg.package, 'pinafore')
        self.assertEqual(msg.problem, 'inertial dampener problem')
        self.assertEqual(msg.year, 2008)

    def testNoDate(self):
        del self.headers['Date']
        self.failIf(p.parse(self.headers, []))

    def testNoSubject(self):
        del self.headers['Subject']
        self.failIf(p.parse(self.headers, []))

    def testNoListId(self):
        del self.headers['List-Id']
        self.failIf(p.parse(self.headers, []))

    def testWrongListId(self):
        self.headers['List-Id'] = '<debian-ponies-announce.lists.debian.org>'
        self.failIf(p.parse(self.headers, []))

    def testFixtures(self):
        from glob import glob
        from DebianChangesBot.utils import parse_mail

        dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), \
            'fixtures', 'security_announce', '*')
        for filename in glob(dir):
            try:
                mail = parse_mail(file(filename))
                msg = p.parse(*mail)
                self.assert_(msg)
            except Exception:
                print "Exception when parsing %s" % filename
                raise

if __name__ == "__main__":
    unittest.main()

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest

import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from DebianChangesBot.mailparsers import SecurityAnnounceParser as p
from DebianChangesBot.utils import parse_mail

def parse(number):
    filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), \
        'fixtures', 'security_announce', '%d.txt' % number)
    mail = parse_mail(file(filename))
    msg = p.parse(*mail)
    assert msg
    return msg.__dict__

class TestMailParserSecurityAnnounce(unittest.TestCase):

    def setUp(self):
        self.headers = {
            'List-Id': '<debian-security-announce.lists.debian.org>',
            'Date': 'Sat, 19 Apr 2008 19:18:38 +0100',
            'Subject': '[SECURITY] [DSA 1234-5] New pinafore packages ' \
                'fix inertial dampener problem',
        }

    def testSimple(self):
        msg = p.parse(self.headers, [])

        self.assert_(msg)
        self.assertEqual(msg.dsa_number, 1234)
        self.assertEqual(msg.dsa_revision, 5)
        self.assertEqual(msg.package, 'pinafore')
        self.assertEqual(msg.problem, 'inertial dampener problem')
        self.assertEqual(msg.year, 2008)

    def testNoDate(self):
        del self.headers['Date']
        self.failIf(p.parse(self.headers, []))

    def testNoSubject(self):
        del self.headers['Subject']
        self.failIf(p.parse(self.headers, []))

    def testNoListId(self):
        del self.headers['List-Id']
        self.failIf(p.parse(self.headers, []))

    def testWrongListId(self):
        self.headers['List-Id'] = '<debian-ponies-announce.lists.debian.org>'
        self.failIf(p.parse(self.headers, []))

    def test1(self):
        self.assertEqual(parse(1), {
            'dsa_revision': 1,
            'problem': 'cross-site request forgery',
            'year': 2008,
            'dsa_number': 1553,
            'package': 'ikiwiki',
        })

if __name__ == "__main__":
    unittest.main()

Modify SecurityAnnounce tests