-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathtest_mail.py
131 lines (111 loc) · 7.02 KB
/
test_mail.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
import pytest
import datetime
from coprs.mail import PermissionRequestMessage, PermissionChangeMessage, LegalFlagMessage, OutdatedChrootMessage, filter_allowlisted_recipients
from coprs import app, models
from tests.coprs_test_case import CoprsTestCase
class TestMail(CoprsTestCase):
def test_permissions_request_message(self, f_users, f_coprs, f_copr_permissions, f_db):
msg = PermissionRequestMessage(self.c1, self.u2, {"new_builder": 1, "new_admin": 0})
assert msg.subject == "[Copr] user1/foocopr: user2 is requesting permissions change"
assert msg.text == ("user2 asked for these changes:\n\n"
"Builder: nothing -> request\n"
"Admin: nothing -> nothing\n\n"
"Project: user1/foocopr")
msg = PermissionRequestMessage(self.c1, self.u2, {"old_admin": 1, "new_admin": 0})
assert msg.subject == "[Copr] user1/foocopr: user2 is requesting permissions change"
assert msg.text == ("user2 asked for these changes:\n\n"
"Admin: request -> nothing\n\n"
"Project: user1/foocopr")
def test_permissions_change_message(self, f_users, f_coprs, f_copr_permissions, f_db):
msg = PermissionChangeMessage(self.c1, {"old_builder": 0, "old_admin": 2, "new_builder": 2, "new_admin": 0})
assert msg.subject == "[Copr] user1/foocopr: Your permissions have changed"
assert msg.text == ("Your permissions have changed:\n\n"
"Builder: nothing -> approved\n"
"Admin: approved -> nothing\n\n"
"Project: user1/foocopr")
msg = PermissionChangeMessage(self.c1, {"old_builder": 0, "new_builder": 1})
assert msg.subject == "[Copr] user1/foocopr: Your permissions have changed"
assert msg.text == ("Your permissions have changed:\n\n"
"Builder: nothing -> request\n\n"
"Project: user1/foocopr")
msg = PermissionChangeMessage(self.c1, {"old_admin": 1, "new_admin": 0})
assert msg.subject == "[Copr] user1/foocopr: Your permissions have changed"
assert msg.text == ("Your permissions have changed:\n\n"
"Admin: request -> nothing\n\n"
"Project: user1/foocopr")
def test_legal_flag_message(self, f_users, f_coprs, f_db):
app.config["SERVER_NAME"] = "localhost"
with app.app_context():
msg = LegalFlagMessage(self.c1, self.u2, "There are forbidden packages in the project")
assert msg.subject == "Legal flag raised on foocopr"
assert msg.text == ("There are forbidden packages in the project\n"
"Navigate to http://localhost/admin/legal-flag/\n"
"Contact on owner is: user1 <[email protected]>\n"
"Reported by user2 <[email protected]>")
def test_outdated_chroot_message(self, f_users, f_coprs, f_mock_chroots, f_db):
chroots = self.c1.copr_chroots + self.c2.copr_chroots + self.c3.copr_chroots
# Create more chroots within one project to later make sure line
# wrapping works as expected
for i in range(30, 38):
mc = models.MockChroot(os_release="fedora", os_version=i,
arch="x86_64", is_active=True)
f_db.session.add(mc)
mc.distgit_branch = models.DistGitBranch.query.first()
cc = models.CoprChroot()
cc.mock_chroot = mc
cc.copr = self.c2
chroots.append(cc)
now = datetime.datetime.now()
for chroot in chroots:
# 7 days = 6d, 23h, 59m, ...
if chroot.copr.full_name == "user2/barcopr" \
and chroot.name == "fedora-18-x86_64":
# special-case one chroot to make a message variation
chroot.delete_after = now + datetime.timedelta(days=5 + 1)
else:
chroot.delete_after = now + datetime.timedelta(days=7 + 1)
app.config["SERVER_NAME"] = "localhost"
app.config["DELETE_EOL_CHROOTS_AFTER"] = 123
with app.app_context():
msg = OutdatedChrootMessage(chroots)
assert msg.subject == "[Copr] upcoming deletion of outdated chroots in your projects"
assert msg.text == ("You have been notified because you are an admin of projects, "
"that have some builds in outdated chroots\n\n"
"According to the 'Copr outdated chroots removal policy'\n"
"https://docs.pagure.org/copr.copr/copr_outdated_chroots_removal_policy.html\n"
"data are going to be preserved 123 days after the chroot is EOL "
"and then automatically deleted, unless you decide to prolong the expiration period.\n\n"
"Please, visit the projects settings if you want to extend the time.\n\n"
"Project: user1/foocopr\n"
"Remaining time:\n"
" 7 days:\n"
" fedora-18-x86_64\n"
"https://localhost/coprs/user1/foocopr/repositories/\n\n"
"Project: user2/foocopr\n"
"Remaining time:\n"
" 7 days:\n"
" fedora-17-i386 fedora-17-x86_64 fedora-30-x86_64 fedora-31-x86_64\n"
" fedora-32-x86_64 fedora-33-x86_64 fedora-34-x86_64 fedora-35-x86_64\n"
" fedora-36-x86_64 fedora-37-x86_64\n"
"https://localhost/coprs/user2/foocopr/repositories/\n\n"
"Project: user2/barcopr\n"
"Remaining time:\n"
" 5 days:\n"
" fedora-18-x86_64\n"
" 7 days:\n"
" fedora-rawhide-i386\n"
"https://localhost/coprs/user2/barcopr/repositories/\n\n")
def test_outdated_chroot_message_empty_chroots(self):
with pytest.raises(AttributeError) as ex:
OutdatedChrootMessage(copr_chroots=[])
assert "No outdated chroots" in str(ex)
def test_filter_recipients(self):
app.config["ALLOWLIST_EMAILS"] = ["[email protected]"]
recipient = filter_allowlisted_recipients(["[email protected]", "[email protected]"])
assert recipient == ["[email protected]"]
app.config["ALLOWLIST_EMAILS"] = ["[email protected]", "[email protected]"]
recipient = filter_allowlisted_recipients(["[email protected]", "[email protected]"])
assert recipient == ["[email protected]", "[email protected]"]
app.config["ALLOWLIST_EMAILS"] = []
recipient = filter_allowlisted_recipients(["[email protected]", "[email protected]"])
assert recipient == ["[email protected]", "[email protected]"]