forked from wikimedia/pywikibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth_tests.py
executable file
·125 lines (101 loc) · 3.79 KB
/
oauth_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
#!/usr/bin/env python3
"""Test OAuth functionality."""
#
# (C) Pywikibot team, 2015-2024
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations
import os
import time
from contextlib import suppress
import pywikibot
from pywikibot import config
from pywikibot.exceptions import EditConflictError
from pywikibot.login import OauthLoginManager
from tests.aspects import (
DefaultSiteTestCase,
TestCase,
require_modules,
unittest,
)
@require_modules('mwoauth')
class OAuthSiteTestCase(TestCase):
"""Run tests related to OAuth authentication."""
oauth = True
@staticmethod
def _get_oauth_tokens():
"""Get valid OAuth tokens from environment variables."""
tokens = os.environ.get('PYWIKIBOT_TEST_OAUTH')
return tuple(tokens.split(':')) if tokens is not None else None
def setUp(self):
"""Check if OAuth extension is installed and OAuth tokens are set."""
super().setUp()
self.site = self.get_site()
if not self.site.has_extension('OAuth'):
self.skipTest('OAuth extension not loaded on test site')
tokens = self._get_oauth_tokens()
if tokens is None:
self.skipTest('OAuth tokens not set')
self.assertLength(tokens, 4)
self.consumer_token = tokens[:2]
self.access_token = tokens[2:]
class OAuthEditTest(OAuthSiteTestCase):
"""Run edit test with OAuth enabled."""
family = 'wikipedia'
code = 'test'
write = True
def setUp(self):
"""Set up test by checking site and initialization."""
super().setUp()
self._authenticate = config.authenticate
oauth_tokens = self.consumer_token + self.access_token
config.authenticate[self.site.hostname()] = oauth_tokens
def tearDown(self):
"""Tear down test by resetting config.authenticate."""
super().tearDown()
config.authenticate = self._authenticate
def test_edit(self):
"""Test editing to a page."""
self.site.login()
self.assertTrue(self.site.logged_in())
title = f'User:{self.site.username()}/edit test'
ts = str(time.time())
p = pywikibot.Page(self.site, title)
try:
p.site.editpage(p, appendtext='\n' + ts)
except EditConflictError as e:
self.assertEqual(e.page, p)
else:
revision_id = p.latest_revision_id
p = pywikibot.Page(self.site, title)
t = p.text
if revision_id == p.latest_revision_id:
self.assertTrue(p.text.endswith(ts))
else:
self.assertIn(ts, t)
class TestOauthLoginManager(DefaultSiteTestCase, OAuthSiteTestCase):
"""Test OAuth login manager."""
def _get_login_manager(self):
login_manager = OauthLoginManager(password=self.consumer_token[1],
site=self.site,
user=self.consumer_token[0])
# Set access token directly, discard user interaction token fetching
login_manager.access_token = self.access_token
return login_manager
def test_login(self):
"""Test login."""
login_manager = self._get_login_manager()
login_manager.login()
self.assertEqual(login_manager.consumer_token, self.consumer_token)
self.assertEqual(login_manager.access_token, self.access_token)
def test_identity(self):
"""Test identity."""
login_manager = self._get_login_manager()
self.assertIsNotNone(login_manager.access_token)
self.assertIsInstance(login_manager.identity, dict)
self.assertEqual(login_manager.identity['username'],
self.site.username())
if __name__ == '__main__':
with suppress(SystemExit):
unittest.main()