forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_failure_tests.py
executable file
·225 lines (185 loc) · 7.79 KB
/
edit_failure_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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python3
"""Tests for edit failures.
These tests should never write to the wiki, unless something has broken
badly.
"""
#
# (C) Pywikibot team, 2014-2024
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import unittest
from contextlib import suppress
from unittest.mock import patch
import pywikibot
from pywikibot import config
from pywikibot.exceptions import (
Error,
LockedPageError,
NoCreateError,
NoPageError,
OtherPageSaveError,
PageCreatedConflictError,
PageSaveRelatedError,
SpamblacklistError,
TitleblacklistError,
)
from tests.aspects import TestCase, WikibaseTestCase
from tests.utils import skipping
class TestSaveFailure(TestCase):
"""Test cases for edits which should fail to save."""
write = True
family = 'wikipedia'
code = 'test'
def test_nobots(self):
"""Test that {{nobots}} raise the appropriate exception."""
page = pywikibot.Page(self.site, 'User:John Vandenberg/nobots')
with patch.object(config, 'ignore_bot_templates', False), \
self.assertRaisesRegex(OtherPageSaveError, 'nobots'):
page.save()
def test_touch(self):
"""Test that Page.touch() does not do a real edit."""
page = pywikibot.Page(self.site, 'User:Xqt/sandbox')
old_text = page.text
page.text += '\n*Add a new line to page'
page.touch()
new_text = page.get(force=True)
self.assertEqual(old_text, new_text)
def test_createonly(self):
"""Test that Page.save with createonly fails if page exists."""
page = pywikibot.Page(self.site, 'User:Xqt/sandbox')
with self.assertRaises(PageCreatedConflictError):
page.save(createonly=True)
def test_nocreate(self):
"""Test that Page.save with nocreate fails if page does not exist."""
page = pywikibot.Page(self.site, 'User:John_Vandenberg/no_recreate')
with self.assertRaises(NoCreateError):
page.save(nocreate=True)
def test_no_recreate(self):
"""Test that Page.save with recreate disabled fails if page existed."""
page = pywikibot.Page(self.site, 'User:John_Vandenberg/no_recreate')
with self.assertRaisesRegex(
OtherPageSaveError,
"Page .* doesn't exist"):
page.save(recreate=False)
class TestNonSysopSaveFailure(TestCase):
"""Tests for edits which should fail to save for non-sysop accounts."""
write = True
family = 'wikipedia'
code = 'test'
@classmethod
def setUpClass(cls):
"""Skip tests for sysop accounts."""
super().setUpClass()
if cls.site.has_group('sysop'):
raise unittest.SkipTest('Testing failure with a sysop account')
def test_protected(self):
"""Test that protected titles raise the appropriate exception."""
page = pywikibot.Page(self.site, 'Wikipedia:Create a new page')
with self.assertRaises(LockedPageError):
page.save()
def test_spam(self):
"""Test that spam in content raise the appropriate exception."""
page = pywikibot.Page(self.site, 'Wikipedia:Sandbox')
page.text = 'http://badsite.com'
with skipping(OtherPageSaveError), self.assertRaisesRegex(
SpamblacklistError, 'badsite.com'):
page.save()
def test_titleblacklist(self):
"""Test that title blacklist raise the appropriate exception."""
page = pywikibot.Page(self.site, 'User:UpsandDowns1234/Blacklisttest')
with self.assertRaises(TitleblacklistError):
page.save()
class TestActionFailure(TestCase):
"""Test cases for actions which should fail to save."""
write = True
family = 'wikipedia'
code = 'test'
def test_movepage(self):
"""Test that site.movepage raises the appropriate exceptions."""
mysite = self.get_site()
mainpage = self.get_mainpage()
with self.assertRaises(Error):
mysite.movepage(mainpage, mainpage.title(), 'test')
page_from = self.get_missing_article()
if not page_from.exists():
with self.assertRaises(NoPageError):
mysite.movepage(page_from, 'Main Page', 'test')
class TestWikibaseSaveTest(WikibaseTestCase):
"""Test case for WikibasePage.save on Wikidata test site."""
family = 'wikidata'
code = 'test'
write = True
def test_itempage_save(self):
"""Test ItemPage save method inherited from superclass Page."""
repo = self.get_repo()
item = pywikibot.ItemPage(repo, 'Q6')
with self.assertRaises(PageSaveRelatedError):
item.save()
@staticmethod
def _make_WbMonolingualText_claim(repo, text, language):
"""Make a WbMonolingualText and set its value."""
claim = pywikibot.page.Claim(repo, 'P271', datatype='monolingualtext')
target = pywikibot.WbMonolingualText(text=text, language=language)
claim.setTarget(target)
return claim
def test_WbMonolingualText_invalid_language(self):
"""Attempt adding a monolingual text with an invalid language."""
repo = self.get_repo()
item = pywikibot.ItemPage(repo, 'Q68')
claim = self._make_WbMonolingualText_claim(repo, text='Test this!',
language='foo')
with self.assertRaisesRegex(
OtherPageSaveError,
r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
r'modification-failed: "foo" is not a known language code.'):
item.addClaim(claim)
def test_WbMonolingualText_invalid_text(self):
"""Attempt adding a monolingual text with invalid non-string text."""
repo = self.get_repo()
item = pywikibot.ItemPage(repo, 'Q68')
claim = self._make_WbMonolingualText_claim(repo, text=123456,
language='en')
with self.assertRaisesRegex(
OtherPageSaveError,
r'Edit to page \[\[(wikidata:test:)?Q68]] failed:'):
item.addClaim(claim)
def test_math_invalid_function(self):
"""Attempt adding invalid latex to a math claim."""
repo = self.get_repo()
item = pywikibot.ItemPage(repo, 'Q68')
claim = pywikibot.page.Claim(repo, 'P717', datatype='math')
claim.setTarget('\foo')
with self.assertRaisesRegex(
OtherPageSaveError,
r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
r'modification-failed: Malformed input:'):
item.addClaim(claim)
def test_url_malformed_url(self):
"""Attempt adding a malformed URL to a url claim."""
repo = self.get_repo()
item = pywikibot.ItemPage(repo, 'Q68')
claim = pywikibot.page.Claim(repo, 'P506', datatype='url')
claim.setTarget('Not a URL at all')
with self.assertRaisesRegex(
OtherPageSaveError,
r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
r'modification-failed: This URL misses a scheme like '
r'"https://": '
r'Not a URL at all'):
item.addClaim(claim)
def test_url_invalid_protocol(self):
"""Attempt adding a URL with an invalid protocol to a url claim."""
repo = self.get_repo()
item = pywikibot.ItemPage(repo, 'Q68')
claim = pywikibot.page.Claim(repo, 'P506', datatype='url')
claim.setTarget('wtf://wikiba.se')
with self.assertRaisesRegex(
OtherPageSaveError,
r'Edit to page \[\[(wikidata:test:)?Q68]] failed:\n'
r'modification-failed: An URL scheme "wtf" is not supported.'):
item.addClaim(claim)
if __name__ == '__main__':
with suppress(SystemExit):
unittest.main()