forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththanks_tests.py
executable file
·114 lines (94 loc) · 3.84 KB
/
thanks_tests.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
#!/usr/bin/env python3
"""Tests for thanks-related code."""
#
# (C) Pywikibot team, 2016-2024
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import unittest
from contextlib import suppress
from pywikibot.page import Page, User
from tests.aspects import TestCase
NO_THANKABLE_REVS = 'There is no recent change which can be test thanked.'
class TestThankRevision(TestCase):
"""Test thanks for revisions."""
family = 'wikipedia'
code = 'test'
write = True
def test_thank_revision(self):
"""Test thanks for normal revisions.
This test relies on activity in recentchanges, and there must
make edits made before reruns of this test; see :phab:`T137836`.
"""
site = self.get_site()
data = site.recentchanges(total=20)
for rev in data:
revid = rev['revid']
username = rev['user']
user = User(site, username)
if user.is_thankable:
break
else:
self.skipTest(NO_THANKABLE_REVS)
before_time = site.getcurrenttimestamp()
site.thank_revision(revid, source='pywikibot test')
log_entries = site.logevents(logtype='thanks', total=5, page=user,
start=before_time, reverse=True)
self.assertTrue(bool(next(log_entries, None)))
def test_self_thank(self):
"""Test that thanking oneself causes an error.
This test is not in TestThankRevisionErrors because it may require
making a successful edit in order to test the API call thanking
the user running the test.
"""
site = self.get_site()
my_name = self.get_userpage().username
data = site.usercontribs(user=my_name, total=1)
for rev in data:
revid = rev['revid']
break
else:
test_page = Page(site, 'Pywikibot Thanks test')
test_page.text += '* ~~~~\n'
test_page.save('Pywikibot Thanks test')
revid = test_page.latest_revision_id
self.assertAPIError('invalidrecipient', None, site.thank_revision,
revid, source='pywikibot test')
class TestThankRevisionErrors(TestCase):
"""Test errors when thanking revisions."""
family = 'wikipedia'
code = 'test'
write = True
def test_bad_recipient(self):
"""Test that thanking a bad recipient causes an error."""
site = self.get_site()
data = site.recentchanges(total=20)
for rev in data:
revid = rev['revid']
username = rev['user']
user = User(site, username)
if not user.is_thankable:
break
else:
self.skipTest(NO_THANKABLE_REVS)
self.assertAPIError('invalidrecipient', None, site.thank_revision,
revid, source='pywikibot test')
def test_invalid_revision(self):
"""Test that passing an invalid revision ID causes an error."""
site = self.get_site()
invalid_revids = (0.99, (0, -1), (0, -1, 0.99), [0, -1, 0.99], 'zero',
'minus one, and point nine nine')
code = 'invalidrevision' if site.mw_version < '1.35' else 'badinteger'
for invalid_revid in invalid_revids:
with self.subTest(revids=invalid_revid):
self.assertAPIError(code, None, site.thank_revision,
invalid_revid, source='pywikibot test')
for invalid_revid in [0, -1, [0], [-1]]:
with self.subTest(revids=invalid_revid):
self.assertAPIError('invalidrevision', None,
site.thank_revision, invalid_revid,
source='pywikibot test')
if __name__ == '__main__':
with suppress(SystemExit):
unittest.main()