/
test_notification.py
179 lines (146 loc) · 7.78 KB
/
test_notification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
# rdiffweb, A web interface to rdiff-backup repositories
# Copyright (C) 2012-2021 rdiffweb contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Created on Feb 13, 2016
@author: Patrik Dufresne <patrik@ikus-soft.com>
"""
from unittest.mock import MagicMock
import cherrypy
import rdiffweb.core.notification
import rdiffweb.test
from rdiffweb.core.model import RepoObject, UserObject
class NotificationJobTest(rdiffweb.test.WebCase):
def setUp(self):
self.listener = MagicMock()
cherrypy.engine.subscribe('queue_mail', self.listener.queue_email, priority=50)
return super().setUp()
def tearDown(self):
cherrypy.engine.unsubscribe('queue_mail', self.listener.queue_email)
return super().tearDown()
def test_notification_job(self):
"""
Run the notification and check if mails are sent
"""
# Given a user with an email address and a repository with a maxage
# Set user config
user = UserObject.get_user(self.USERNAME)
user.email = 'test@test.com'
user.add()
repo = RepoObject.query.filter(RepoObject.user == user, RepoObject.repopath == self.REPO).first()
repo.maxage = 1
repo.add()
# When running notification_job
cherrypy.notification.notification_job()
# Then an email is queue for this user
self.listener.queue_email.assert_called_once_with(
to='test@test.com',
subject='Notification',
message="<html>\n <head></head>\n <body>\n Hey admin,\n <p>\n You are receiving this email to notify you about your backups. The\n following repositories are inactive for some time. We invite you to have a look\n at your last backup schedule.\n </p>\n <ul>\n <li>testcases</li>\n </ul>\n <p>\n If you don't want to be notify about this. You need to review your\n user preferences.\n </p>\n </body>\n</html>",
)
def test_notification_job_undefined_last_backup_date(self):
# Given a valid user with a repository configured for notification
user = UserObject.get_user(self.USERNAME)
user.email = 'test@test.com'
user.add()
# Given a repo with last_backup_date None
repo = RepoObject.query.filter(RepoObject.user == user, RepoObject.repopath == 'broker-repo').first()
repo.maxage = 1
repo.add()
self.assertIsNone(repo.last_backup_date)
# When Notification job is running
cherrypy.notification.notification_job()
# Then a notification is sent to the user.
self.listener.queue_email.assert_called_once_with(
to='test@test.com',
subject='Notification',
message="<html>\n <head></head>\n <body>\n Hey admin,\n <p>\n You are receiving this email to notify you about your backups. The\n following repositories are inactive for some time. We invite you to have a look\n at your last backup schedule.\n </p>\n <ul>\n <li>broker-repo</li>\n </ul>\n <p>\n If you don't want to be notify about this. You need to review your\n user preferences.\n </p>\n </body>\n</html>",
)
def test_notification_job_without_notification(self):
# Given a valid user with a repository configured without notification (-1)
user = UserObject.get_user(self.USERNAME)
user.email = 'test@test.com'
user.add()
repo = RepoObject.query.filter(RepoObject.user == user, RepoObject.repopath == self.REPO).first()
repo.maxage = -1
repo.add()
# Call notification.
cherrypy.notification.notification_job()
# Expect it to be called.
self.listener.queue_email.assert_not_called()
class NotificationPluginTest(rdiffweb.test.WebCase):
default_config = {
'email-send-changed-notification': True,
}
def setUp(self):
self.listener = MagicMock()
cherrypy.engine.subscribe('queue_mail', self.listener.queue_email, priority=50)
return super().setUp()
def tearDown(self):
cherrypy.engine.unsubscribe('queue_mail', self.listener.queue_email)
return super().tearDown()
def test_email_changed(self):
# Given a user with an email address
user = UserObject.get_user(self.USERNAME)
user.email = 'original_email@test.com'
user.add()
self.listener.queue_email.reset_mock()
# When updating the user's email
user = UserObject.get_user(self.USERNAME)
user.email = 'email_changed@test.com'
user.add()
# Then a email is queue to notify the user.
self.listener.queue_email.assert_called_once_with(
to='original_email@test.com',
subject='Email address changed',
message='<html>\n <head></head>\n <body>\n Hey admin,\n <p>You recently changed the email address associated with your Rdiffweb account.</p>\n <p>\n If you did not make this change and believe your account has been compromised, please contact your administrator.\n </p>\n </body>\n</html>',
)
def test_email_updated_with_same_value(self):
# Given a user with an email
user = UserObject.get_user(self.USERNAME)
user.email = 'email_changed@test.com'
self.listener.queue_email.reset_mock()
# When updating the user's email with the same value
user.email = 'email_changed@test.com'
# Then no email are sent to the user
self.listener.queue_email.assert_not_called()
def test_password_change_notification(self):
# Given a user with a email.
user = UserObject.get_user(self.USERNAME)
user.email = 'password_change@test.com'
self.listener.queue_email.reset_mock()
# When updating the user password
user.set_password('new_password')
# Then a email is send to the user
self.listener.queue_email.assert_called_once_with(
to='password_change@test.com',
subject='Password changed',
message='<html>\n <head></head>\n <body>\n Hey admin,\n <p>You recently changed the password associated with your Rdiffweb account.</p>\n <p>\n If you did not make this change and believe your account has been compromised, please contact your administrator.\n </p>\n </body>\n</html>',
)
def test_password_change_with_same_value(self):
# Given a user with a email.
user = UserObject.get_user(self.USERNAME)
user.email = 'password_change@test.com'
user.set_password('new_password')
self.listener.queue_email.reset_mock()
# When updating the user password with the same value
user.set_password('new_password')
# Then an email is sent to the user
self.listener.queue_email.assert_called_once_with(
to='password_change@test.com',
subject='Password changed',
message='<html>\n <head></head>\n <body>\n Hey admin,\n <p>You recently changed the password associated with your Rdiffweb account.</p>\n <p>\n If you did not make this change and believe your account has been compromised, please contact your administrator.\n </p>\n </body>\n</html>',
)