[062892]: / AlluraTest / alluratest / controller.py  Maximize  Restore  History

Download this file

310 lines (251 with data), 11.3 kB

  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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Unit and functional test suite for allura."""
from __future__ import annotations
import os
import six.moves.urllib.request
import six.moves.urllib.parse
import six.moves.urllib.error
import json
import mock
import beaker.session
from formencode import variabledecode
from paste.deploy import loadapp
from paste.deploy.converters import asbool
from paste.script.appinstall import SetupCommand
from tg import tmpl_context as c, app_globals as g
from tg import url, request, response, session
import tg
from tg.wsgiapp import RequestLocals
from webob import Response, Request
import ew
from ming.odm import ThreadLocalODMSession
import ming.odm
import pkg_resources
import requests
import requests_oauthlib
from allura import model as M
from allura.command import CreateTroveCategoriesCommand
import allura.lib.security
from allura.lib.app_globals import Globals
from allura.lib import helpers as h
from allura.websetup.schema import REGISTRY
#from allura.lib.custom_middleware import environ as ENV, MagicalC
from .validation import ValidatingTestApp
import six
DFL_APP_NAME = 'main'
# these are all helpers & base classes, and should never
# be considered test cases when imported into some test module
__test__ = False
def get_config_file(config=None, current_pkg=None):
if not config:
config = 'test.ini'
if not current_pkg:
current_pkg = 'allura'
conf_dir = pkg_resources.resource_filename(current_pkg, '..')
conf_file = os.path.join(conf_dir, config)
# split on "#" since it could be foo.ini#main
if not os.path.exists(conf_file.split('#')[0]) and current_pkg != 'allura':
# if there isn't a forgewiki/test.ini for example, then fall back to regular allura
conf_dir = pkg_resources.resource_filename('allura', '..')
conf_file = os.path.join(conf_dir, config)
if not os.path.exists(conf_file.split('#')[0]):
raise OSError(f'Cannot find .ini config file {conf_file}')
else:
return conf_file
def setup_basic_test(config=None, app_name=DFL_APP_NAME):
'''
Create clean environment for running tests, includes mongodb connection with "mim" (mongo-in-memory) and sample
data created.
A lightweight alternative is setup_config_test which doesn't bootstrap app data.
'''
try:
conf_dir = tg.config.here
except AttributeError:
conf_dir = os.getcwd()
test_file = os.path.join(conf_dir, get_config_file(config))
"""
# setup our app, from TG quickstart example:
from tg.util import Bunch
from gearbox.commands.setup_app import SetupAppCommand
cmd = SetupAppCommand(Bunch(options=Bunch(verbose_level=1)), Bunch())
cmd.run(Bunch(config_file='config:{}'.format(test_file), section_name=None))
"""
# setup our app without depending on gearbox (we still depend on Paste anyway)
# uses [paste.app_install] entry point which call our setup_app()
cmd = SetupCommand('setup-app')
cmd.run([test_file, '--quiet'])
ew.TemplateEngine.initialize({})
# remove unnecessary bootstrap tasks, e.g. search indexing
M.MonQTask.query.remove({'state': 'ready'})
setup_basic_test.__test__ = False # sometimes __test__ above isn't sufficient
def setup_functional_test(config=None, app_name=DFL_APP_NAME, current_pkg=None):
'''Create clean environment for running tests. Also return WSGI test app'''
config = get_config_file(config, current_pkg=current_pkg)
setup_basic_test(config, app_name)
conf_dir = tg.config.here
wsgiapp = loadapp(f'config:{config}#{app_name}',
relative_to=conf_dir)
return wsgiapp
# sometimes __test__ above isn't sufficient
setup_functional_test.__test__ = False
def setup_unit_test():
try:
while True:
REGISTRY.cleanup()
except Exception:
pass
REGISTRY.prepare()
REGISTRY.register(ew.widget_context,
ew.core.WidgetContext('http', ew.ResourceManager()))
REGISTRY.register(allura.credentials, allura.lib.security.Credentials())
# turbogears has its own special magic wired up for its globals, can't use a regular Registry
tgl = RequestLocals()
tgl.app_globals = Globals()
tgl.tmpl_context = mock.Mock()
tgl.url = lambda: None
tgl.request = Request.blank('/', remote_addr='127.0.0.1')
tgl.response = Response()
tg.request_local.context._push_object(tgl)
c.model_cache = None
ThreadLocalODMSession.close_all()
setup_unit_test.__test__ = False # sometimes __test__ above isn't sufficient
def setup_global_objects():
setup_unit_test()
h.set_context('test', 'wiki', neighborhood='Projects')
c.user = M.User.query.get(username='test-admin')
def setup_trove_categories():
create_trove_categories = CreateTroveCategoriesCommand('create_trove_categories')
with mock.patch.object(M.project.TroveCategoryMapperExtension, 'after_insert'),\
mock.patch.object(M.project.TroveCategoryMapperExtension, 'after_update'),\
mock.patch.object(M.project.TroveCategoryMapperExtension, 'after_delete'):
create_trove_categories.run([''])
class TestController:
application_under_test = 'main'
validate_skip = False
def setup_method(self, method=None):
pkg = self.__module__.split('.')[0]
self.app = ValidatingTestApp(
setup_functional_test(app_name=self.application_under_test, current_pkg=pkg))
self.app.extra_environ = {'REMOTE_ADDR': '127.0.0.1'} # remote_addr needed by AntiSpam
if self.validate_skip:
self.app.validate_skip = self.validate_skip
if asbool(tg.config.get('smtp.mock')):
self.smtp_mock = mock.patch('allura.lib.mail_util.smtplib.SMTP')
self.smtp_mock.start()
def teardown_method(self, method=None):
if asbool(tg.config.get('smtp.mock')):
self.smtp_mock.stop()
def webflash(self, response):
"Extract webflash content from current state of WebTest app"
return six.moves.urllib.parse.unquote(self.app.cookies.get('webflash', ''))
def subscription_options(self, response):
"""
Extract subscription options to be passed to React SubscriptionForm
component from the <script> tag
"""
script = None
for s in response.html.findAll('script'):
if s.getText().strip().startswith('document.SUBSCRIPTION_OPTIONS'):
script = s
break
assert script is not None, 'subscription options not found'
_, json_dict = script.getText().split('=')
json_dict = json_dict.strip(' ;\n')
return json.loads(json_dict)
def find_form(self, resp, cond):
"""Find form on the page that meets given condition"""
for f in resp.forms.values():
if cond(f):
return f
class TestRestApiBase(TestController):
def setup_method(self, method):
super().setup_method(method)
self._use_token = None
self._token_cache = {}
def set_api_token(self, token):
self._use_token = token
def token(self, username):
if self._use_token:
return self._use_token
# only create token once, else ming gets dupe key error
if username not in self._token_cache:
user = M.User.query.get(username=username)
consumer_token = M.OAuthConsumerToken(
name='test-%s' % str(user._id),
description='test-app-%s' % str(user._id),
user_id=user._id)
request_token = M.OAuthRequestToken(
consumer_token_id=consumer_token._id,
user_id=user._id,
callback='manual',
validation_pin=h.nonce(20))
token = M.OAuthAccessToken(
consumer_token_id=consumer_token._id,
request_token_id=request_token._id,
user_id=user._id,
is_bearer=True)
ming.odm.session(consumer_token).flush()
ming.odm.session(request_token).flush()
ming.odm.session(token).flush()
self._token_cache[username] = token
return self._token_cache[username]
def _api_call(self, method, path, wrap_args=None, user='test-admin', status=None, **params):
'''
If you need to use one of the method kwargs as a URL parameter,
pass params={...} as a dict instead of **kwargs
'''
if 'params' in params:
params = params['params']
if wrap_args:
params = {wrap_args: params}
if status is None:
status = [200, 201, 301, 302]
if not isinstance(params, str):
params = variabledecode.variable_encode(params, add_repetitions=False)
token = self.token(user).api_key
headers = {
'Authorization': str(f'Bearer {token}')
}
fn = getattr(self.app, method.lower())
response = fn(
str(path),
params=params,
headers=headers,
status=status)
if response.status_int in [301, 302]:
return response.follow()
else:
return response
def api_get(self, path, wrap_args=None, user='test-admin', status=None, **params):
return self._api_call('GET', path, wrap_args, user, status, **params)
def api_post(self, path, wrap_args=None, user='test-admin', status=None, **params):
return self._api_call('POST', path, wrap_args, user, status, **params)
def api_delete(self, path, wrap_args=None, user='test-admin', status=None, **params):
return self._api_call('DELETE', path, wrap_args, user, status, **params)
def oauth1_webtest(url: str, oauth_kwargs: dict, method='GET') -> tuple[str, dict, dict, dict]:
oauth1 = requests_oauthlib.OAuth1(**oauth_kwargs)
req = requests.Request(method, f'http://localhost{url}').prepare()
oauth1(req)
url, params, headers = request2webtest(req)
extra_environ = {'username': '*anonymous'} # we don't want to be magically logged in when hitting /rest/oauth/
return url, params, headers, extra_environ
def request2webtest(req: requests.PreparedRequest) -> tuple[str, dict, dict]:
url = req.url
params = {}
headers = {k: v.decode() for k, v in req.headers.items()}
return url, params, headers