forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdry_site_tests.py
executable file
·112 lines (80 loc) · 3.4 KB
/
dry_site_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
#!/usr/bin/env python3
"""Tests against a fake Site object."""
#
# (C) Pywikibot team, 2012-2022
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import unittest
import pywikibot
from pywikibot.comms.http import user_agent
from tests.aspects import DefaultDrySiteTestCase
class TestDrySite(DefaultDrySiteTestCase):
"""Tests against a fake Site object."""
dry = True
def test_logged_in(self):
"""Test logged_in() method."""
x = self.get_site()
x._userinfo = {'name': None, 'groups': [], 'id': 1}
x._username = 'user'
with self.subTest(variant='name: None'):
self.assertFalse(x.logged_in())
x._userinfo['name'] = 'user'
with self.subTest(variant='name: user'):
self.assertTrue(x.logged_in())
x._userinfo['name'] = 'otheruser'
with self.subTest(variant='name: otheruseer'):
self.assertFalse(x.logged_in())
x._userinfo['id'] = 0
x._userinfo['name'] = 'user'
with self.subTest(variant='id: 0'):
self.assertFalse(x.logged_in())
x._userinfo['id'] = 1
with self.subTest(variant='id: 1'):
self.assertTrue(x.logged_in())
x._userinfo['anon'] = ''
with self.subTest(variant='anon'):
self.assertFalse(x.logged_in())
del x._userinfo['anon']
x._userinfo['groups'] = ['sysop']
with self.subTest(variant='sysop'):
self.assertTrue(x.logged_in())
def test_user_agent(self):
"""Test different variants of user agents."""
x = self.get_site()
x._userinfo = {'name': 'foo'}
x._username = 'foo'
self.assertEqual('Pywikibot/' + pywikibot.__version__,
user_agent(x, format_string='{pwb}'))
self.assertEqual(x.family.name,
user_agent(x, format_string='{family}'))
self.assertEqual(x.code,
user_agent(x, format_string='{lang}'))
self.assertEqual(x.family.name + ' ' + x.code,
user_agent(x, format_string='{family} {lang}'))
self.assertEqual(x.username(),
user_agent(x, format_string='{username}'))
x._userinfo = {'name': '!'}
x._username = '!'
self.assertEqual('!', user_agent(x, format_string='{username}'))
x._userinfo = {'name': 'foo bar'}
x._username = 'foo bar'
self.assertEqual('foo_bar', user_agent(x, format_string='{username}'))
old_config = '{script}/{version} Pywikibot/6.0 (User:{username})'
script_value = (pywikibot.calledModuleName() + '/'
+ pywikibot.version.getversiondict()['rev'])
self.assertEqual(script_value + ' Pywikibot/6.0 (User:foo_bar)',
user_agent(x, format_string=old_config))
x._userinfo = {'name': '⁂'}
x._username = '⁂'
self.assertEqual('%E2%81%82',
user_agent(x, format_string='{username}'))
x._userinfo = {'name': '127.0.0.1'}
x._username = None
self.assertEqual('Foo', user_agent(x, format_string='Foo {username}'))
self.assertEqual('Foo (' + x.family.name + ':' + x.code + ')',
user_agent(x,
format_string='Foo ({script_comments})'))
if __name__ == '__main__':
unittest.main()