~launchpad-pqm/launchpad/devel

10322.3.2 by Curtis Hovey
Added a unit test for personnotifications.
1
Person notifications
2
====================
6493.4.2 by Guilherme Salgado
person notification
3
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
4
The PersonNotification table stores notifications that should be sent
5
(or already sent) to a given person.  It stores the person who should
6
receive the notification as well as the email message's body and
7
subject.  A cronscript is then responsible for picking up these
8
notifications and sending them.
9
10320.1.1 by Curtis Hovey
Ran migrator to move registry modules to lp.regisrty.
10
    >>> from lp.registry.interfaces.personnotification import (
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
11
    ...     IPersonNotification, IPersonNotificationSet)
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
12
    >>> from lp.services.webapp.testing import verifyObject
7675.110.3 by Curtis Hovey
Ran the migration script to move registry code to lp.registry.
13
    >>> from lp.registry.interfaces.person import IPersonSet
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
14
    >>> notification_set = getUtility(IPersonNotificationSet)
9105.3.1 by Brad Crittenden
Changed sample data to remove sabdfl.
15
    >>> mark = getUtility(IPersonSet).getByName('mark')
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
16
17
To add a new notification we need the recipient, the email body and its
18
subject.
19
20
    >>> notification = notification_set.addNotification(
9105.3.1 by Brad Crittenden
Changed sample data to remove sabdfl.
21
    ...     mark, 'subject', 'body')
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
22
    >>> verifyObject(IPersonNotification, notification)
6493.4.2 by Guilherme Salgado
person notification
23
    True
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
24
    >>> notification.person.name
9105.3.1 by Brad Crittenden
Changed sample data to remove sabdfl.
25
    u'mark'
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
26
    >>> notification.date_created
27
    datetime.datetime(...
28
    >>> print notification.date_emailed
29
    None
30
31
The notifications that need to be sent can be retrieved with
32
getNotificationsToSend().
33
34
    >>> [n.subject for n in notification_set.getNotificationsToSend()]
35
    [u'subject']
36
37
We can also retrieve notifications that are older than a certain date.
38
39
    >>> import pytz
40
    >>> from datetime import datetime, timedelta
6493.6.5 by Guilherme Salgado
Few changes as suggested by Brad.
41
    >>> now = datetime.now(pytz.timezone('UTC'))
42
    >>> [n.subject for n in notification_set.getNotificationsOlderThan(now)]
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
43
    [u'subject']
44
6493.6.5 by Guilherme Salgado
Few changes as suggested by Brad.
45
    >>> yesterday = now - timedelta(days=1)
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
46
    >>> [n.subject
47
    ...  for n in notification_set.getNotificationsOlderThan(yesterday)]
48
    []
49
50
A notification has a send() method which creates an email message and
51
sends it to the recipient.
52
13082.1.3 by William Grant
Log the email addresses inside PN.send.
53
    >>> from lp.services.log.logger import FakeLogger
54
    >>> notification.send(logger=FakeLogger())
55
    INFO Sending notification to ['Mark Shuttleworth <mark@example.com>'].
8400.1.3 by Tim Penhey
Move c.l.tests.mail_helpers to lp.testing.
56
    >>> from lp.testing.mail_helpers import print_emails
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
57
    >>> print_emails()
58
    From: bounces@canonical.com
9105.3.2 by Brad Crittenden
Changed mark@hbd.com to mark@example.com. Tests that used base64 encoding for basic auth credentials were changed to use plaintext.
59
    To: Mark Shuttleworth <mark@example.com>
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
60
    Subject: subject
61
    body
62
    ----------------------------------------
63
64
The send-person-notifications script will send all pending
65
notifications.
66
67
    >>> notification = notification_set.addNotification(
9105.3.1 by Brad Crittenden
Changed sample data to remove sabdfl.
68
    ...     mark, 'subject2', 'body2')
13432.4.1 by Danilo Segan
Let "personnotification" DB user SELECT from the account table for contacting teams owned by other teams.
69
70
This includes notifications to teams owned by other teams.
71
72
    >>> owning_team = factory.makeTeam()
73
    >>> team = factory.makeTeam(owner=owning_team)
74
    >>> team_notification = notification_set.addNotification(
75
    ...     team, 'subject3', 'body3')
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
76
    >>> [n.subject for n in notification_set.getNotificationsToSend()]
13432.4.1 by Danilo Segan
Let "personnotification" DB user SELECT from the account table for contacting teams owned by other teams.
77
    [u'subject2', u'subject3']
6493.6.4 by Guilherme Salgado
Fix the script which sends notifications and write tests for PersonNotification.
78
    >>> transaction.commit()
79
80
    >>> import subprocess
81
    >>> process = subprocess.Popen(
82
    ...     'cronscripts/send-person-notifications.py -q', shell=True,
83
    ...     stdin=subprocess.PIPE, stdout=subprocess.PIPE,
84
    ...     stderr=subprocess.PIPE)
85
    >>> (out, err) = process.communicate()
86
    >>> out, err
87
    ('', '')
88
    >>> process.returncode
89
    0
90
91
    >>> [n.subject for n in notification_set.getNotificationsToSend()]
92
    []