1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
from zope.security.proxy import removeSecurityProxy
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 (
15
from lp.soyuz.enums import PackageUploadCustomFormat
16
from lp.testing import TestCaseWithFactory
17
from lp.testing.mail_helpers import pop_notifications
20
class TestNotification(TestCaseWithFactory):
22
layer = LaunchpadZopelessLayer
24
def test_fetch_information_changes(self):
27
'Changed-By': 'Foo Bar <foo.bar@canonical.com>',
28
'Maintainer': 'Foo Bar <foo.bar@canonical.com>',
31
(changesfile, date, changedby, maintainer) = fetch_information(
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)
38
def test_fetch_information_spr(self):
39
spr = self.factory.makeSourcePackageRelease()
40
(changesfile, date, changedby, maintainer) = fetch_information(
42
self.assertEqual(date, spr.dateuploaded)
43
self.assertEqual(changesfile, spr.changelog_entry)
44
self.assertEqual(changedby, format_address_for_person(spr.creator))
46
maintainer, format_address_for_person(spr.maintainer))
48
def test_fetch_information_bprs(self):
49
bpr = self.factory.makeBinaryPackageRelease()
50
(changesfile, date, changedby, maintainer) = fetch_information(
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))
57
maintainer, format_address_for_person(spr.maintainer))
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)
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)
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,
98
self.assertEqual(expected_subject, subject)
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()
109
person, None, [bpr], [], archive, distroseries, pocket,
111
notifications = pop_notifications()
112
self.assertEqual(0, len(notifications))
114
def test_notify_bpr_rejected(self):
115
# If we notify about a rejected bpr with no source, a notification is
117
bpr = self.factory.makeBinaryPackageRelease()
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()
125
person, None, [bpr], [], archive, distroseries, pocket,
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)