~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/soyuz/adapters/tests/test_notification.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-06-07 23:33:42 UTC
  • mfrom: (13136.1.13 copies-use-notify)
  • Revision ID: launchpad@pqm.canonical.com-20110607233342-m7uh1bom3haavvid
[r=allenap,
 wgrant][bug=786852] Add the ability to send e-mail notifications from
 package copies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
from zope.security.proxy import removeSecurityProxy
 
5
 
 
6
from canonical.testing.layers import LaunchpadZopelessLayer
 
7
from lp.archivepublisher.utils import get_ppa_reference
 
8
from lp.services.mail.sendmail import format_address_for_person
 
9
from lp.soyuz.adapters.notification import (
 
10
    calculate_subject,
 
11
    fetch_information,
 
12
    person_to_email,
 
13
    notify,
 
14
    )
 
15
from lp.soyuz.enums import PackageUploadCustomFormat
 
16
from lp.testing import TestCaseWithFactory
 
17
from lp.testing.mail_helpers import pop_notifications
 
18
 
 
19
 
 
20
class TestNotification(TestCaseWithFactory):
 
21
 
 
22
    layer = LaunchpadZopelessLayer
 
23
 
 
24
    def test_fetch_information_changes(self):
 
25
        changes = {
 
26
            'Date': '2001-01-01',
 
27
            'Changed-By': 'Foo Bar <foo.bar@canonical.com>',
 
28
            'Maintainer': 'Foo Bar <foo.bar@canonical.com>',
 
29
            'Changes': ' * Foo!'
 
30
            }
 
31
        (changesfile, date, changedby, maintainer) = fetch_information(
 
32
            None, None, changes)
 
33
        self.assertEqual('2001-01-01', date)
 
34
        self.assertEqual(' * Foo!', changesfile)
 
35
        for field in (changedby, maintainer):
 
36
            self.assertEqual('Foo Bar <foo.bar@canonical.com>', field)
 
37
 
 
38
    def test_fetch_information_spr(self):
 
39
        spr = self.factory.makeSourcePackageRelease()
 
40
        (changesfile, date, changedby, maintainer) = fetch_information(
 
41
            spr, None, None)
 
42
        self.assertEqual(date, spr.dateuploaded)
 
43
        self.assertEqual(changesfile, spr.changelog_entry)
 
44
        self.assertEqual(changedby, format_address_for_person(spr.creator))
 
45
        self.assertEqual(
 
46
            maintainer, format_address_for_person(spr.maintainer))
 
47
 
 
48
    def test_fetch_information_bprs(self):
 
49
        bpr = self.factory.makeBinaryPackageRelease()
 
50
        (changesfile, date, changedby, maintainer) = fetch_information(
 
51
            None, [bpr], None)
 
52
        spr = bpr.build.source_package_release
 
53
        self.assertEqual(date, spr.dateuploaded)
 
54
        self.assertEqual(changesfile, spr.changelog_entry)
 
55
        self.assertEqual(changedby, format_address_for_person(spr.creator))
 
56
        self.assertEqual(
 
57
            maintainer, format_address_for_person(spr.maintainer))
 
58
 
 
59
    def test_calculate_subject_spr(self):
 
60
        spr = self.factory.makeSourcePackageRelease()
 
61
        archive = self.factory.makeArchive()
 
62
        pocket = self.factory.getAnyPocket()
 
63
        distroseries = self.factory.makeDistroSeries()
 
64
        expected_subject = '[PPA %s] [%s/%s] %s %s (Accepted)' % (
 
65
            get_ppa_reference(archive), distroseries.distribution.name,
 
66
            distroseries.getSuite(pocket), spr.name, spr.version)  
 
67
        subject = calculate_subject(
 
68
            spr, [], [], archive, distroseries, pocket, 'accepted')
 
69
        self.assertEqual(expected_subject, subject)
 
70
 
 
71
    def test_calculate_subject_bprs(self):
 
72
        bpr = self.factory.makeBinaryPackageRelease()
 
73
        archive = self.factory.makeArchive()
 
74
        pocket = self.factory.getAnyPocket()
 
75
        distroseries = self.factory.makeDistroSeries()
 
76
        expected_subject = '[PPA %s] [%s/%s] %s %s (Accepted)' % (
 
77
            get_ppa_reference(archive), distroseries.distribution.name,
 
78
            distroseries.getSuite(pocket),
 
79
            bpr.build.source_package_release.name, bpr.version)
 
80
        subject = calculate_subject(
 
81
            None, [bpr], [], archive, distroseries, pocket, 'accepted')
 
82
        self.assertEqual(expected_subject, subject)
 
83
 
 
84
    def test_calculate_subject_customfile(self):
 
85
        lfa = self.factory.makeLibraryFileAlias()
 
86
        package_upload = self.factory.makePackageUpload()
 
87
        customfile = package_upload.addCustom(
 
88
            lfa, PackageUploadCustomFormat.DEBIAN_INSTALLER)
 
89
        archive = self.factory.makeArchive()
 
90
        pocket = self.factory.getAnyPocket()
 
91
        distroseries = self.factory.makeDistroSeries()
 
92
        expected_subject = '[PPA %s] [%s/%s] %s - (Accepted)' % (
 
93
            get_ppa_reference(archive), distroseries.distribution.name,
 
94
            distroseries.getSuite(pocket), lfa.filename)
 
95
        subject = calculate_subject(
 
96
            None, [], [customfile], archive, distroseries, pocket,
 
97
            'accepted')
 
98
        self.assertEqual(expected_subject, subject)
 
99
 
 
100
    def test_notify_bpr(self):
 
101
        # If we notify about an accepted bpr with no source, it is from a
 
102
        # build, and no notification is sent.
 
103
        bpr = self.factory.makeBinaryPackageRelease()
 
104
        archive = self.factory.makeArchive()
 
105
        pocket = self.factory.getAnyPocket()
 
106
        distroseries = self.factory.makeDistroSeries()
 
107
        person = self.factory.makePerson()
 
108
        notify(
 
109
            person, None, [bpr], [], archive, distroseries, pocket,
 
110
            action='accepted')
 
111
        notifications = pop_notifications()
 
112
        self.assertEqual(0, len(notifications))
 
113
 
 
114
    def test_notify_bpr_rejected(self):
 
115
        # If we notify about a rejected bpr with no source, a notification is
 
116
        # sent.
 
117
        bpr = self.factory.makeBinaryPackageRelease()
 
118
        removeSecurityProxy(
 
119
            bpr.build.source_package_release).changelog_entry = '* Foo!'
 
120
        archive = self.factory.makeArchive()
 
121
        pocket = self.factory.getAnyPocket()
 
122
        distroseries = self.factory.makeDistroSeries()
 
123
        person = self.factory.makePerson()
 
124
        notify(
 
125
            person, None, [bpr], [], archive, distroseries, pocket,
 
126
            action='rejected')
 
127
        [notification] = pop_notifications()
 
128
        body = notification.as_string()
 
129
        self.assertEqual(person_to_email(person), notification['To'])
 
130
        self.assertIn('Rejected by archive administrator.\n\n* Foo!\n', body)