Show More
@@ -1,224 +1,225 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors |
|
3 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |
4 | # |
|
4 | # | |
5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | # Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | # you may not use this file except in compliance with the License. |
|
6 | # you may not use this file except in compliance with the License. | |
7 | # You may obtain a copy of the License at |
|
7 | # You may obtain a copy of the License at | |
8 | # |
|
8 | # | |
9 | # http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | # http://www.apache.org/licenses/LICENSE-2.0 | |
10 | # |
|
10 | # | |
11 | # Unless required by applicable law or agreed to in writing, software |
|
11 | # Unless required by applicable law or agreed to in writing, software | |
12 | # distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | # distributed under the License is distributed on an "AS IS" BASIS, | |
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | # See the License for the specific language governing permissions and |
|
14 | # See the License for the specific language governing permissions and | |
15 | # limitations under the License. |
|
15 | # limitations under the License. | |
16 |
|
16 | |||
17 | import datetime |
|
17 | import datetime | |
18 | import logging |
|
18 | import logging | |
19 | import pyelasticsearch |
|
19 | import pyelasticsearch | |
20 | import redis |
|
20 | import redis | |
21 | import os |
|
21 | import os | |
22 | import pkg_resources |
|
22 | import pkg_resources | |
23 | from pkg_resources import iter_entry_points |
|
23 | from pkg_resources import iter_entry_points | |
24 |
|
24 | |||
25 | import appenlight.lib.jinja2_filters as jinja2_filters |
|
25 | import appenlight.lib.jinja2_filters as jinja2_filters | |
26 | import appenlight.lib.encryption as encryption |
|
26 | import appenlight.lib.encryption as encryption | |
27 |
|
27 | |||
28 | from pyramid.config import PHASE3_CONFIG |
|
28 | from pyramid.config import PHASE3_CONFIG | |
29 | from pyramid.authentication import AuthTktAuthenticationPolicy |
|
29 | from pyramid.authentication import AuthTktAuthenticationPolicy | |
30 | from pyramid.authorization import ACLAuthorizationPolicy |
|
30 | from pyramid.authorization import ACLAuthorizationPolicy | |
31 |
from pyramid_mailer. |
|
31 | from pyramid_mailer.interfaces import IMailer | |
32 | from pyramid.renderers import JSON |
|
32 | from pyramid.renderers import JSON | |
33 | from pyramid_redis_sessions import session_factory_from_settings |
|
33 | from pyramid_redis_sessions import session_factory_from_settings | |
34 | from pyramid.settings import asbool, aslist |
|
34 | from pyramid.settings import asbool, aslist | |
35 | from pyramid.security import AllPermissionsList |
|
35 | from pyramid.security import AllPermissionsList | |
36 | from pyramid_authstack import AuthenticationStackPolicy |
|
36 | from pyramid_authstack import AuthenticationStackPolicy | |
37 | from redlock import Redlock |
|
37 | from redlock import Redlock | |
38 | from sqlalchemy import engine_from_config |
|
38 | from sqlalchemy import engine_from_config | |
39 |
|
39 | |||
40 | from appenlight.celery import configure_celery |
|
40 | from appenlight.celery import configure_celery | |
41 | from appenlight.lib.configurator import (CythonCompatConfigurator, |
|
41 | from appenlight.lib.configurator import (CythonCompatConfigurator, | |
42 | register_appenlight_plugin) |
|
42 | register_appenlight_plugin) | |
43 | from appenlight.lib import cache_regions |
|
43 | from appenlight.lib import cache_regions | |
44 | from appenlight.lib.ext_json import json |
|
44 | from appenlight.lib.ext_json import json | |
45 | from appenlight.security import groupfinder, AuthTokenAuthenticationPolicy |
|
45 | from appenlight.security import groupfinder, AuthTokenAuthenticationPolicy | |
46 |
|
46 | |||
47 | __license__ = 'Apache 2.0' |
|
47 | __license__ = 'Apache 2.0' | |
48 | __author__ = 'RhodeCode GmbH' |
|
48 | __author__ = 'RhodeCode GmbH' | |
49 | __url__ = 'http://rhodecode.com' |
|
49 | __url__ = 'http://rhodecode.com' | |
50 | __version__ = pkg_resources.get_distribution("appenlight").parsed_version |
|
50 | __version__ = pkg_resources.get_distribution("appenlight").parsed_version | |
51 |
|
51 | |||
52 | json_renderer = JSON(serializer=json.dumps, indent=4) |
|
52 | json_renderer = JSON(serializer=json.dumps, indent=4) | |
53 |
|
53 | |||
54 | log = logging.getLogger(__name__) |
|
54 | log = logging.getLogger(__name__) | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | def datetime_adapter(obj, request): |
|
57 | def datetime_adapter(obj, request): | |
58 | return obj.isoformat() |
|
58 | return obj.isoformat() | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | def all_permissions_adapter(obj, request): |
|
61 | def all_permissions_adapter(obj, request): | |
62 | return '__all_permissions__' |
|
62 | return '__all_permissions__' | |
63 |
|
63 | |||
64 |
|
64 | |||
65 | json_renderer.add_adapter(datetime.datetime, datetime_adapter) |
|
65 | json_renderer.add_adapter(datetime.datetime, datetime_adapter) | |
66 | json_renderer.add_adapter(AllPermissionsList, all_permissions_adapter) |
|
66 | json_renderer.add_adapter(AllPermissionsList, all_permissions_adapter) | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | def main(global_config, **settings): |
|
69 | def main(global_config, **settings): | |
70 | """ This function returns a Pyramid WSGI application. |
|
70 | """ This function returns a Pyramid WSGI application. | |
71 | """ |
|
71 | """ | |
72 | auth_tkt_policy = AuthTktAuthenticationPolicy( |
|
72 | auth_tkt_policy = AuthTktAuthenticationPolicy( | |
73 | settings['authtkt.secret'], |
|
73 | settings['authtkt.secret'], | |
74 | hashalg='sha512', |
|
74 | hashalg='sha512', | |
75 | callback=groupfinder, |
|
75 | callback=groupfinder, | |
76 | max_age=2592000, |
|
76 | max_age=2592000, | |
77 | secure=asbool(settings.get('authtkt.secure', 'false'))) |
|
77 | secure=asbool(settings.get('authtkt.secure', 'false'))) | |
78 | auth_token_policy = AuthTokenAuthenticationPolicy( |
|
78 | auth_token_policy = AuthTokenAuthenticationPolicy( | |
79 | callback=groupfinder |
|
79 | callback=groupfinder | |
80 | ) |
|
80 | ) | |
81 | authorization_policy = ACLAuthorizationPolicy() |
|
81 | authorization_policy = ACLAuthorizationPolicy() | |
82 | authentication_policy = AuthenticationStackPolicy() |
|
82 | authentication_policy = AuthenticationStackPolicy() | |
83 | authentication_policy.add_policy('auth_tkt', auth_tkt_policy) |
|
83 | authentication_policy.add_policy('auth_tkt', auth_tkt_policy) | |
84 | authentication_policy.add_policy('auth_token', auth_token_policy) |
|
84 | authentication_policy.add_policy('auth_token', auth_token_policy) | |
85 | # set crypto key |
|
85 | # set crypto key | |
86 | encryption.ENCRYPTION_SECRET = settings.get('encryption_secret') |
|
86 | encryption.ENCRYPTION_SECRET = settings.get('encryption_secret') | |
87 | # import this later so encyption key can be monkeypatched |
|
87 | # import this later so encyption key can be monkeypatched | |
88 | from appenlight.models import DBSession, register_datastores |
|
88 | from appenlight.models import DBSession, register_datastores | |
89 |
|
89 | |||
90 | # registration |
|
90 | # registration | |
91 | settings['appenlight.disable_registration'] = asbool( |
|
91 | settings['appenlight.disable_registration'] = asbool( | |
92 | settings.get('appenlight.disable_registration')) |
|
92 | settings.get('appenlight.disable_registration')) | |
93 |
|
93 | |||
94 | # update config with cometd info |
|
94 | # update config with cometd info | |
95 | settings['cometd_servers'] = {'server': settings['cometd.server'], |
|
95 | settings['cometd_servers'] = {'server': settings['cometd.server'], | |
96 | 'secret': settings['cometd.secret']} |
|
96 | 'secret': settings['cometd.secret']} | |
97 |
|
97 | |||
98 | # Create the Pyramid Configurator. |
|
98 | # Create the Pyramid Configurator. | |
99 | settings['_mail_url'] = settings['mailing.app_url'] |
|
99 | settings['_mail_url'] = settings['mailing.app_url'] | |
100 | config = CythonCompatConfigurator( |
|
100 | config = CythonCompatConfigurator( | |
101 | settings=settings, |
|
101 | settings=settings, | |
102 | authentication_policy=authentication_policy, |
|
102 | authentication_policy=authentication_policy, | |
103 | authorization_policy=authorization_policy, |
|
103 | authorization_policy=authorization_policy, | |
104 | root_factory='appenlight.security.RootFactory', |
|
104 | root_factory='appenlight.security.RootFactory', | |
105 | default_permission='view') |
|
105 | default_permission='view') | |
106 | # custom registry variables |
|
106 | # custom registry variables | |
107 |
|
107 | |||
108 | # resource type information |
|
108 | # resource type information | |
109 | config.registry.resource_types = ['resource', 'application'] |
|
109 | config.registry.resource_types = ['resource', 'application'] | |
110 | # plugin information |
|
110 | # plugin information | |
111 | config.registry.appenlight_plugins = {} |
|
111 | config.registry.appenlight_plugins = {} | |
112 |
|
112 | |||
113 | config.set_default_csrf_options(require_csrf=True, header='X-XSRF-TOKEN') |
|
113 | config.set_default_csrf_options(require_csrf=True, header='X-XSRF-TOKEN') | |
114 | config.add_view_deriver('appenlight.predicates.csrf_view', |
|
114 | config.add_view_deriver('appenlight.predicates.csrf_view', | |
115 | name='csrf_view') |
|
115 | name='csrf_view') | |
116 |
|
116 | |||
117 | # later, when config is available |
|
117 | # later, when config is available | |
118 | dogpile_config = {'url': settings['redis.url'], |
|
118 | dogpile_config = {'url': settings['redis.url'], | |
119 | "redis_expiration_time": 86400, |
|
119 | "redis_expiration_time": 86400, | |
120 | "redis_distributed_lock": True} |
|
120 | "redis_distributed_lock": True} | |
121 | cache_regions.regions = cache_regions.CacheRegions(dogpile_config) |
|
121 | cache_regions.regions = cache_regions.CacheRegions(dogpile_config) | |
122 | config.registry.cache_regions = cache_regions.regions |
|
122 | config.registry.cache_regions = cache_regions.regions | |
123 | engine = engine_from_config(settings, 'sqlalchemy.', |
|
123 | engine = engine_from_config(settings, 'sqlalchemy.', | |
124 | json_serializer=json.dumps) |
|
124 | json_serializer=json.dumps) | |
125 | DBSession.configure(bind=engine) |
|
125 | DBSession.configure(bind=engine) | |
126 |
|
126 | |||
127 | # json rederer that serializes datetime |
|
127 | # json rederer that serializes datetime | |
128 | config.add_renderer('json', json_renderer) |
|
128 | config.add_renderer('json', json_renderer) | |
129 | config.set_request_property('appenlight.lib.request.es_conn', 'es_conn') |
|
129 | config.set_request_property('appenlight.lib.request.es_conn', 'es_conn') | |
130 | config.set_request_property('appenlight.lib.request.get_user', 'user', |
|
130 | config.set_request_property('appenlight.lib.request.get_user', 'user', | |
131 | reify=True) |
|
131 | reify=True) | |
132 | config.set_request_property('appenlight.lib.request.get_csrf_token', |
|
132 | config.set_request_property('appenlight.lib.request.get_csrf_token', | |
133 | 'csrf_token', reify=True) |
|
133 | 'csrf_token', reify=True) | |
134 | config.set_request_property('appenlight.lib.request.safe_json_body', |
|
134 | config.set_request_property('appenlight.lib.request.safe_json_body', | |
135 | 'safe_json_body', reify=True) |
|
135 | 'safe_json_body', reify=True) | |
136 | config.set_request_property('appenlight.lib.request.unsafe_json_body', |
|
136 | config.set_request_property('appenlight.lib.request.unsafe_json_body', | |
137 | 'unsafe_json_body', reify=True) |
|
137 | 'unsafe_json_body', reify=True) | |
138 | config.add_request_method('appenlight.lib.request.add_flash_to_headers', |
|
138 | config.add_request_method('appenlight.lib.request.add_flash_to_headers', | |
139 | 'add_flash_to_headers') |
|
139 | 'add_flash_to_headers') | |
140 | config.add_request_method('appenlight.lib.request.get_authomatic', |
|
140 | config.add_request_method('appenlight.lib.request.get_authomatic', | |
141 | 'authomatic', reify=True) |
|
141 | 'authomatic', reify=True) | |
142 |
|
142 | |||
143 | config.include('pyramid_redis_sessions') |
|
143 | config.include('pyramid_redis_sessions') | |
144 | config.include('pyramid_tm') |
|
144 | config.include('pyramid_tm') | |
145 | config.include('pyramid_jinja2') |
|
145 | config.include('pyramid_jinja2') | |
|
146 | config.include('pyramid_mailer') | |||
146 | config.include('appenlight_client.ext.pyramid_tween') |
|
147 | config.include('appenlight_client.ext.pyramid_tween') | |
147 | config.include('ziggurat_foundations.ext.pyramid.sign_in') |
|
148 | config.include('ziggurat_foundations.ext.pyramid.sign_in') | |
148 | es_server_list = aslist(settings['elasticsearch.nodes']) |
|
149 | es_server_list = aslist(settings['elasticsearch.nodes']) | |
149 | redis_url = settings['redis.url'] |
|
150 | redis_url = settings['redis.url'] | |
150 | log.warning('Elasticsearch server list: {}'.format(es_server_list)) |
|
151 | log.warning('Elasticsearch server list: {}'.format(es_server_list)) | |
151 | log.warning('Redis server: {}'.format(redis_url)) |
|
152 | log.warning('Redis server: {}'.format(redis_url)) | |
152 | config.registry.es_conn = pyelasticsearch.ElasticSearch(es_server_list) |
|
153 | config.registry.es_conn = pyelasticsearch.ElasticSearch(es_server_list) | |
153 | config.registry.redis_conn = redis.StrictRedis.from_url(redis_url) |
|
154 | config.registry.redis_conn = redis.StrictRedis.from_url(redis_url) | |
154 |
|
155 | |||
155 | config.registry.redis_lockmgr = Redlock([settings['redis.redlock.url'], ], |
|
156 | config.registry.redis_lockmgr = Redlock([settings['redis.redlock.url'], ], | |
156 | retry_count=0, retry_delay=0) |
|
157 | retry_count=0, retry_delay=0) | |
157 | # mailer |
|
158 | # mailer bw compat | |
158 | config.registry.mailer = Mailer.from_settings(settings) |
|
159 | config.registry.mailer = config.registry.getUtility(IMailer) | |
159 |
|
160 | |||
160 | # Configure sessions |
|
161 | # Configure sessions | |
161 | session_factory = session_factory_from_settings(settings) |
|
162 | session_factory = session_factory_from_settings(settings) | |
162 | config.set_session_factory(session_factory) |
|
163 | config.set_session_factory(session_factory) | |
163 |
|
164 | |||
164 | # Configure renderers and event subscribers |
|
165 | # Configure renderers and event subscribers | |
165 | config.add_jinja2_extension('jinja2.ext.loopcontrols') |
|
166 | config.add_jinja2_extension('jinja2.ext.loopcontrols') | |
166 | config.add_jinja2_search_path('appenlight:templates') |
|
167 | config.add_jinja2_search_path('appenlight:templates') | |
167 | # event subscribers |
|
168 | # event subscribers | |
168 | config.add_subscriber("appenlight.subscribers.application_created", |
|
169 | config.add_subscriber("appenlight.subscribers.application_created", | |
169 | "pyramid.events.ApplicationCreated") |
|
170 | "pyramid.events.ApplicationCreated") | |
170 | config.add_subscriber("appenlight.subscribers.add_renderer_globals", |
|
171 | config.add_subscriber("appenlight.subscribers.add_renderer_globals", | |
171 | "pyramid.events.BeforeRender") |
|
172 | "pyramid.events.BeforeRender") | |
172 | config.add_subscriber('appenlight.subscribers.new_request', |
|
173 | config.add_subscriber('appenlight.subscribers.new_request', | |
173 | 'pyramid.events.NewRequest') |
|
174 | 'pyramid.events.NewRequest') | |
174 | config.add_view_predicate('context_type_class', |
|
175 | config.add_view_predicate('context_type_class', | |
175 | 'appenlight.predicates.contextTypeClass') |
|
176 | 'appenlight.predicates.contextTypeClass') | |
176 |
|
177 | |||
177 | register_datastores(es_conn=config.registry.es_conn, |
|
178 | register_datastores(es_conn=config.registry.es_conn, | |
178 | redis_conn=config.registry.redis_conn, |
|
179 | redis_conn=config.registry.redis_conn, | |
179 | redis_lockmgr=config.registry.redis_lockmgr) |
|
180 | redis_lockmgr=config.registry.redis_lockmgr) | |
180 |
|
181 | |||
181 | # base stuff and scan |
|
182 | # base stuff and scan | |
182 |
|
183 | |||
183 | # need to ensure webassets exists otherwise config.override_asset() |
|
184 | # need to ensure webassets exists otherwise config.override_asset() | |
184 | # throws exception |
|
185 | # throws exception | |
185 | if not os.path.exists(settings['webassets.dir']): |
|
186 | if not os.path.exists(settings['webassets.dir']): | |
186 | os.mkdir(settings['webassets.dir']) |
|
187 | os.mkdir(settings['webassets.dir']) | |
187 | config.add_static_view(path='appenlight:webassets', |
|
188 | config.add_static_view(path='appenlight:webassets', | |
188 | name='static', cache_max_age=3600) |
|
189 | name='static', cache_max_age=3600) | |
189 | config.override_asset(to_override='appenlight:webassets/', |
|
190 | config.override_asset(to_override='appenlight:webassets/', | |
190 | override_with=settings['webassets.dir']) |
|
191 | override_with=settings['webassets.dir']) | |
191 |
|
192 | |||
192 | config.include('appenlight.views') |
|
193 | config.include('appenlight.views') | |
193 | config.include('appenlight.views.admin') |
|
194 | config.include('appenlight.views.admin') | |
194 | config.scan(ignore=['appenlight.migrations', 'appenlight.scripts', |
|
195 | config.scan(ignore=['appenlight.migrations', 'appenlight.scripts', | |
195 | 'appenlight.tests']) |
|
196 | 'appenlight.tests']) | |
196 |
|
197 | |||
197 | config.add_directive('register_appenlight_plugin', |
|
198 | config.add_directive('register_appenlight_plugin', | |
198 | register_appenlight_plugin) |
|
199 | register_appenlight_plugin) | |
199 |
|
200 | |||
200 | for entry_point in iter_entry_points(group='appenlight.plugins'): |
|
201 | for entry_point in iter_entry_points(group='appenlight.plugins'): | |
201 | plugin = entry_point.load() |
|
202 | plugin = entry_point.load() | |
202 | plugin.includeme(config) |
|
203 | plugin.includeme(config) | |
203 |
|
204 | |||
204 | # include other appenlight plugins explictly if needed |
|
205 | # include other appenlight plugins explictly if needed | |
205 | includes = aslist(settings.get('appenlight.includes', [])) |
|
206 | includes = aslist(settings.get('appenlight.includes', [])) | |
206 | for inc in includes: |
|
207 | for inc in includes: | |
207 | config.include(inc) |
|
208 | config.include(inc) | |
208 |
|
209 | |||
209 | # run this after everything registers in configurator |
|
210 | # run this after everything registers in configurator | |
210 |
|
211 | |||
211 | def pre_commit(): |
|
212 | def pre_commit(): | |
212 | jinja_env = config.get_jinja2_environment() |
|
213 | jinja_env = config.get_jinja2_environment() | |
213 | jinja_env.filters['tojson'] = json.dumps |
|
214 | jinja_env.filters['tojson'] = json.dumps | |
214 | jinja_env.filters['toJSONUnsafe'] = jinja2_filters.toJSONUnsafe |
|
215 | jinja_env.filters['toJSONUnsafe'] = jinja2_filters.toJSONUnsafe | |
215 |
|
216 | |||
216 | config.action(None, pre_commit, order=PHASE3_CONFIG + 999) |
|
217 | config.action(None, pre_commit, order=PHASE3_CONFIG + 999) | |
217 |
|
218 | |||
218 | def wrap_config_celery(): |
|
219 | def wrap_config_celery(): | |
219 | configure_celery(config.registry) |
|
220 | configure_celery(config.registry) | |
220 |
|
221 | |||
221 | config.action(None, wrap_config_celery, order=PHASE3_CONFIG + 999) |
|
222 | config.action(None, wrap_config_celery, order=PHASE3_CONFIG + 999) | |
222 |
|
223 | |||
223 | app = config.make_wsgi_app() |
|
224 | app = config.make_wsgi_app() | |
224 | return app |
|
225 | return app |
@@ -1,185 +1,190 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors |
|
3 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |
4 | # |
|
4 | # | |
5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | # Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | # you may not use this file except in compliance with the License. |
|
6 | # you may not use this file except in compliance with the License. | |
7 | # You may obtain a copy of the License at |
|
7 | # You may obtain a copy of the License at | |
8 | # |
|
8 | # | |
9 | # http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | # http://www.apache.org/licenses/LICENSE-2.0 | |
10 | # |
|
10 | # | |
11 | # Unless required by applicable law or agreed to in writing, software |
|
11 | # Unless required by applicable law or agreed to in writing, software | |
12 | # distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | # distributed under the License is distributed on an "AS IS" BASIS, | |
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | # See the License for the specific language governing permissions and |
|
14 | # See the License for the specific language governing permissions and | |
15 | # limitations under the License. |
|
15 | # limitations under the License. | |
16 |
|
16 | |||
17 | import mock |
|
17 | import mock | |
18 | import os |
|
18 | import os | |
19 | import pytest |
|
19 | import pytest | |
20 | import transaction |
|
20 | import transaction | |
21 | from datetime import datetime |
|
21 | from datetime import datetime | |
22 |
|
22 | |||
23 | from alembic.config import Config |
|
23 | from alembic.config import Config | |
24 | from alembic import command |
|
24 | from alembic import command | |
25 | from collections import OrderedDict |
|
25 | from collections import OrderedDict | |
26 | from zope.sqlalchemy import mark_changed |
|
26 | from zope.sqlalchemy import mark_changed | |
27 | from pyramid.paster import get_appsettings |
|
27 | from pyramid.paster import get_appsettings | |
28 | from pyramid import testing |
|
28 | from pyramid import testing | |
29 |
|
29 | |||
30 | from appenlight.models import Base, DBSession |
|
|||
31 |
|
||||
32 |
|
30 | |||
33 | @pytest.fixture |
|
31 | @pytest.fixture | |
34 | def base_app(request): |
|
32 | def base_app(request, mocker): | |
|
33 | # disable email sending | |||
|
34 | mocker.patch('pyramid_mailer.mailer_factory_from_settings', mocker.Mock()) | |||
|
35 | ||||
35 | from appenlight import main |
|
36 | from appenlight import main | |
36 | import transaction |
|
37 | import transaction | |
37 | current_dir = os.path.dirname(os.path.abspath(__file__)) |
|
38 | current_dir = os.path.dirname(os.path.abspath(__file__)) | |
38 | path = os.path.join(current_dir, '../../../../', |
|
39 | path = os.path.join(current_dir, '../../../../', | |
39 | os.environ.get("APPENLIGHT_INI", 'testing.ini')) |
|
40 | os.environ.get("APPENLIGHT_INI", 'testing.ini')) | |
40 | # appsettings from ini |
|
41 | # appsettings from ini | |
41 | app_settings = get_appsettings(path, name="appenlight") |
|
42 | app_settings = get_appsettings(path, name="appenlight") | |
42 | app = main({}, **app_settings) |
|
43 | app = main({}, **app_settings) | |
43 | app_request = testing.DummyRequest(base_url='https://appenlight.com') |
|
44 | app_request = testing.DummyRequest(base_url='https://appenlight.com') | |
44 | app_request.tm = transaction.manager |
|
45 | app_request.tm = transaction.manager | |
45 | app_request.add_flash_to_headers = mock.Mock() |
|
46 | app_request.add_flash_to_headers = mock.Mock() | |
46 | testing.setUp(registry=app.registry, request=app_request) |
|
47 | testing.setUp(registry=app.registry, request=app_request) | |
47 |
|
48 | |||
48 | def teardown(): |
|
49 | def teardown(): | |
49 | testing.tearDown() |
|
50 | testing.tearDown() | |
50 |
|
51 | |||
51 | request.addfinalizer(teardown) |
|
52 | request.addfinalizer(teardown) | |
52 |
|
53 | |||
53 | return app |
|
54 | return app | |
54 |
|
55 | |||
55 |
|
56 | |||
56 | @pytest.fixture |
|
57 | @pytest.fixture | |
57 | def with_migrations(request, base_app): |
|
58 | def with_migrations(request, base_app): | |
58 | settings = base_app.registry.settings |
|
59 | settings = base_app.registry.settings | |
59 | alembic_cfg = Config() |
|
60 | alembic_cfg = Config() | |
60 | alembic_cfg.set_main_option("script_location", |
|
61 | alembic_cfg.set_main_option("script_location", | |
61 | "ziggurat_foundations:migrations") |
|
62 | "ziggurat_foundations:migrations") | |
62 | alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) |
|
63 | alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) | |
63 | command.upgrade(alembic_cfg, "head") |
|
64 | command.upgrade(alembic_cfg, "head") | |
64 | alembic_cfg = Config() |
|
65 | alembic_cfg = Config() | |
65 | alembic_cfg.set_main_option("script_location", "appenlight:migrations") |
|
66 | alembic_cfg.set_main_option("script_location", "appenlight:migrations") | |
66 | alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) |
|
67 | alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) | |
67 | command.upgrade(alembic_cfg, "head") |
|
68 | command.upgrade(alembic_cfg, "head") | |
68 |
|
69 | |||
69 | for plugin_name, config in base_app.registry.appenlight_plugins.items(): |
|
70 | for plugin_name, config in base_app.registry.appenlight_plugins.items(): | |
70 | if config['sqlalchemy_migrations']: |
|
71 | if config['sqlalchemy_migrations']: | |
71 | alembic_cfg = Config() |
|
72 | alembic_cfg = Config() | |
72 | alembic_cfg.set_main_option("script_location", |
|
73 | alembic_cfg.set_main_option("script_location", | |
73 | config['sqlalchemy_migrations']) |
|
74 | config['sqlalchemy_migrations']) | |
74 | alembic_cfg.set_main_option( |
|
75 | alembic_cfg.set_main_option( | |
75 | "sqlalchemy.url", |
|
76 | "sqlalchemy.url", | |
76 | base_app.registry.settings["sqlalchemy.url"]) |
|
77 | base_app.registry.settings["sqlalchemy.url"]) | |
77 | command.upgrade(alembic_cfg, "head") |
|
78 | command.upgrade(alembic_cfg, "head") | |
78 |
|
79 | |||
79 |
|
80 | |||
80 | @pytest.fixture |
|
81 | @pytest.fixture | |
81 | def default_data(base_app): |
|
82 | def default_data(base_app): | |
82 | from appenlight.models.services.config import ConfigService |
|
83 | from appenlight.models.services.config import ConfigService | |
83 | from appenlight.lib import get_callable |
|
84 | from appenlight.lib import get_callable | |
84 | transaction.begin() |
|
85 | transaction.begin() | |
85 | ConfigService.setup_default_values() |
|
86 | ConfigService.setup_default_values() | |
86 | for plugin_name, config in base_app.registry.appenlight_plugins.items(): |
|
87 | for plugin_name, config in base_app.registry.appenlight_plugins.items(): | |
87 | if config['default_values_setter']: |
|
88 | if config['default_values_setter']: | |
88 | get_callable(config['default_values_setter'])() |
|
89 | get_callable(config['default_values_setter'])() | |
89 | transaction.commit() |
|
90 | transaction.commit() | |
90 |
|
91 | |||
91 |
|
92 | |||
92 | @pytest.fixture |
|
93 | @pytest.fixture | |
93 | def clean_tables(request): |
|
94 | def clean_tables(request): | |
|
95 | from appenlight.models import Base, DBSession | |||
|
96 | ||||
94 | def fin(): |
|
97 | def fin(): | |
95 | tables = Base.metadata.tables.keys() |
|
98 | tables = Base.metadata.tables.keys() | |
96 | transaction.begin() |
|
99 | transaction.begin() | |
97 | for t in tables: |
|
100 | for t in tables: | |
98 | if not t.startswith('alembic_'): |
|
101 | if not t.startswith('alembic_'): | |
99 | DBSession.execute('truncate %s cascade' % t) |
|
102 | DBSession.execute('truncate %s cascade' % t) | |
100 | session = DBSession() |
|
103 | session = DBSession() | |
101 | mark_changed(session) |
|
104 | mark_changed(session) | |
102 | transaction.commit() |
|
105 | transaction.commit() | |
103 |
|
106 | |||
104 | request.addfinalizer(fin) |
|
107 | request.addfinalizer(fin) | |
105 |
|
108 | |||
106 |
|
109 | |||
107 | @pytest.fixture |
|
110 | @pytest.fixture | |
108 | def default_user(): |
|
111 | def default_user(): | |
|
112 | from appenlight.models import DBSession | |||
109 | from appenlight.models.user import User |
|
113 | from appenlight.models.user import User | |
110 | from appenlight.models.auth_token import AuthToken |
|
114 | from appenlight.models.auth_token import AuthToken | |
111 | transaction.begin() |
|
115 | transaction.begin() | |
112 | session = DBSession() |
|
116 | session = DBSession() | |
113 | user = User(id=1, |
|
117 | user = User(id=1, | |
114 | user_name='testuser', |
|
118 | user_name='testuser', | |
115 | status=1, |
|
119 | status=1, | |
116 | email='foo@barbaz99.com') |
|
120 | email='foo@barbaz99.com') | |
117 | session.add(user) |
|
121 | session.add(user) | |
118 | token = AuthToken(token='1234') |
|
122 | token = AuthToken(token='1234') | |
119 | user.auth_tokens.append(token) |
|
123 | user.auth_tokens.append(token) | |
120 | session.execute("SELECT nextval('users_id_seq')") |
|
124 | session.execute("SELECT nextval('users_id_seq')") | |
121 | transaction.commit() |
|
125 | transaction.commit() | |
122 | return user |
|
126 | return user | |
123 |
|
127 | |||
124 |
|
128 | |||
125 | @pytest.fixture |
|
129 | @pytest.fixture | |
126 | def default_application(default_user): |
|
130 | def default_application(default_user): | |
|
131 | from appenlight.models import DBSession | |||
127 | from appenlight.models.application import Application |
|
132 | from appenlight.models.application import Application | |
128 |
|
133 | |||
129 | transaction.begin() |
|
134 | transaction.begin() | |
130 | session = DBSession() |
|
135 | session = DBSession() | |
131 | application = Application( |
|
136 | application = Application( | |
132 | resource_id=1, resource_name='testapp', api_key='xxxx') |
|
137 | resource_id=1, resource_name='testapp', api_key='xxxx') | |
133 | session.add(application) |
|
138 | session.add(application) | |
134 | default_user.resources.append(application) |
|
139 | default_user.resources.append(application) | |
135 | session.execute("SELECT nextval('resources_resource_id_seq')") |
|
140 | session.execute("SELECT nextval('resources_resource_id_seq')") | |
136 | transaction.commit() |
|
141 | transaction.commit() | |
137 | return application |
|
142 | return application | |
138 |
|
143 | |||
139 |
|
144 | |||
140 | @pytest.fixture |
|
145 | @pytest.fixture | |
141 | def report_type_matrix(): |
|
146 | def report_type_matrix(): | |
142 | from appenlight.models.report import REPORT_TYPE_MATRIX |
|
147 | from appenlight.models.report import REPORT_TYPE_MATRIX | |
143 | return REPORT_TYPE_MATRIX |
|
148 | return REPORT_TYPE_MATRIX | |
144 |
|
149 | |||
145 |
|
150 | |||
146 | @pytest.fixture |
|
151 | @pytest.fixture | |
147 | def chart_series(): |
|
152 | def chart_series(): | |
148 | series = [] |
|
153 | series = [] | |
149 |
|
154 | |||
150 | for x in range(1, 7): |
|
155 | for x in range(1, 7): | |
151 | tmp_list = [('key', 'X'), ('0_1', x)] |
|
156 | tmp_list = [('key', 'X'), ('0_1', x)] | |
152 | if x % 2 == 0: |
|
157 | if x % 2 == 0: | |
153 | tmp_list.append(('0_2', x)) |
|
158 | tmp_list.append(('0_2', x)) | |
154 | if x % 3 == 0: |
|
159 | if x % 3 == 0: | |
155 | tmp_list.append(('0_3', x)) |
|
160 | tmp_list.append(('0_3', x)) | |
156 |
|
161 | |||
157 | series.append( |
|
162 | series.append( | |
158 | OrderedDict(tmp_list) |
|
163 | OrderedDict(tmp_list) | |
159 | ) |
|
164 | ) | |
160 | return series |
|
165 | return series | |
161 |
|
166 | |||
162 |
|
167 | |||
163 | @pytest.fixture |
|
168 | @pytest.fixture | |
164 | def log_schema(): |
|
169 | def log_schema(): | |
165 | from appenlight.validators import LogListSchema |
|
170 | from appenlight.validators import LogListSchema | |
166 | schema = LogListSchema().bind(utcnow=datetime.utcnow()) |
|
171 | schema = LogListSchema().bind(utcnow=datetime.utcnow()) | |
167 | return schema |
|
172 | return schema | |
168 |
|
173 | |||
169 | @pytest.fixture |
|
174 | @pytest.fixture | |
170 | def general_metrics_schema(): |
|
175 | def general_metrics_schema(): | |
171 | from appenlight.validators import GeneralMetricsListSchema |
|
176 | from appenlight.validators import GeneralMetricsListSchema | |
172 | schema = GeneralMetricsListSchema().bind(utcnow=datetime.utcnow()) |
|
177 | schema = GeneralMetricsListSchema().bind(utcnow=datetime.utcnow()) | |
173 | return schema |
|
178 | return schema | |
174 |
|
179 | |||
175 | @pytest.fixture |
|
180 | @pytest.fixture | |
176 | def request_metrics_schema(): |
|
181 | def request_metrics_schema(): | |
177 | from appenlight.validators import MetricsListSchema |
|
182 | from appenlight.validators import MetricsListSchema | |
178 | schema = MetricsListSchema().bind(utcnow=datetime.utcnow()) |
|
183 | schema = MetricsListSchema().bind(utcnow=datetime.utcnow()) | |
179 | return schema |
|
184 | return schema | |
180 |
|
185 | |||
181 | @pytest.fixture |
|
186 | @pytest.fixture | |
182 | def report_05_schema(): |
|
187 | def report_05_schema(): | |
183 | from appenlight.validators import ReportListSchema_0_5 |
|
188 | from appenlight.validators import ReportListSchema_0_5 | |
184 | schema = ReportListSchema_0_5().bind(utcnow=datetime.utcnow()) |
|
189 | schema = ReportListSchema_0_5().bind(utcnow=datetime.utcnow()) | |
185 | return schema |
|
190 | return schema |
General Comments 0
You need to be logged in to leave comments.
Login now