Show More
@@ -1,184 +1,185 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # AppEnlight Enterprise Edition, including its added features, Support |
|
19 | 19 | # services, and proprietary license terms, please see |
|
20 | 20 | # https://rhodecode.com/licenses/ |
|
21 | 21 | |
|
22 | 22 | import mock |
|
23 | 23 | import os |
|
24 | 24 | import pytest |
|
25 | 25 | import transaction |
|
26 | 26 | from datetime import datetime |
|
27 | 27 | |
|
28 | 28 | from alembic.config import Config |
|
29 | 29 | from alembic import command |
|
30 | 30 | from collections import OrderedDict |
|
31 | 31 | from zope.sqlalchemy import mark_changed |
|
32 | 32 | from pyramid.paster import get_appsettings |
|
33 | 33 | from pyramid import testing |
|
34 | 34 | |
|
35 | 35 | from appenlight.models import Base, DBSession |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | @pytest.fixture |
|
39 | 39 | def base_app(request): |
|
40 | 40 | from appenlight import main |
|
41 | 41 | import transaction |
|
42 | 42 | current_dir = os.path.dirname(os.path.abspath(__file__)) |
|
43 | 43 | path = os.path.join(current_dir, '../../../../', |
|
44 | 44 | os.environ.get("APPENLIGHT_INI", 'testing.ini')) |
|
45 | 45 | # appsettings from ini |
|
46 | 46 | app_settings = get_appsettings(path, name="appenlight") |
|
47 | 47 | app = main({}, **app_settings) |
|
48 | 48 | app_request = testing.DummyRequest(base_url='https://appenlight.com') |
|
49 | 49 | app_request.tm = transaction.manager |
|
50 | 50 | app_request.add_flash_to_headers = mock.Mock() |
|
51 | 51 | testing.setUp(registry=app.registry, request=app_request) |
|
52 | 52 | |
|
53 | 53 | def teardown(): |
|
54 | 54 | testing.tearDown() |
|
55 | 55 | |
|
56 | 56 | request.addfinalizer(teardown) |
|
57 | 57 | |
|
58 | 58 | return app |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | @pytest.fixture |
|
62 | 62 | def with_migrations(request, base_app): |
|
63 | 63 | settings = base_app.registry.settings |
|
64 | 64 | alembic_cfg = Config() |
|
65 | 65 | alembic_cfg.set_main_option("script_location", |
|
66 | 66 | "ziggurat_foundations:migrations") |
|
67 | 67 | alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) |
|
68 | 68 | command.upgrade(alembic_cfg, "head") |
|
69 | 69 | alembic_cfg = Config() |
|
70 | 70 | alembic_cfg.set_main_option("script_location", "appenlight:migrations") |
|
71 | 71 | alembic_cfg.set_main_option("sqlalchemy.url", settings["sqlalchemy.url"]) |
|
72 | 72 | command.upgrade(alembic_cfg, "head") |
|
73 | 73 | |
|
74 | 74 | for plugin_name, config in base_app.registry.appenlight_plugins.items(): |
|
75 | 75 | if config['sqlalchemy_migrations']: |
|
76 | 76 | alembic_cfg = Config() |
|
77 | 77 | alembic_cfg.set_main_option("script_location", |
|
78 | 78 | config['sqlalchemy_migrations']) |
|
79 | 79 | alembic_cfg.set_main_option( |
|
80 | 80 | "sqlalchemy.url", |
|
81 | 81 | base_app.registry.settings["sqlalchemy.url"]) |
|
82 | 82 | command.upgrade(alembic_cfg, "head") |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | @pytest.fixture |
|
86 | 86 | def default_data(base_app): |
|
87 | 87 | from appenlight.models.services.config import ConfigService |
|
88 | 88 | from appenlight.lib import get_callable |
|
89 | 89 | transaction.begin() |
|
90 | 90 | ConfigService.setup_default_values() |
|
91 | 91 | for plugin_name, config in base_app.registry.appenlight_plugins.items(): |
|
92 | 92 | if config['default_values_setter']: |
|
93 | 93 | get_callable(config['default_values_setter'])() |
|
94 | 94 | transaction.commit() |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | @pytest.fixture |
|
98 | 98 | def clean_tables(): |
|
99 | 99 | tables = Base.metadata.tables.keys() |
|
100 | 100 | transaction.begin() |
|
101 | 101 | for t in tables: |
|
102 | 102 | if not t.startswith('alembic_'): |
|
103 | 103 | DBSession.execute('truncate %s cascade' % t) |
|
104 | 104 | session = DBSession() |
|
105 | 105 | mark_changed(session) |
|
106 | 106 | transaction.commit() |
|
107 | 107 | |
|
108 | 108 | |
|
109 | 109 | @pytest.fixture |
|
110 | 110 | def default_user(): |
|
111 | 111 | from appenlight.models.user import User |
|
112 | 112 | from appenlight.models.auth_token import AuthToken |
|
113 | 113 | transaction.begin() |
|
114 |
user = User( |
|
|
114 | user = User(id=1, | |
|
115 | user_name='testuser', | |
|
115 | 116 | status=1, |
|
116 |
email=' |
|
|
117 | email='foo@barbaz99.com') | |
|
117 | 118 | DBSession.add(user) |
|
118 | 119 | token = AuthToken(token='1234') |
|
119 | 120 | user.auth_tokens.append(token) |
|
120 | 121 | DBSession.flush() |
|
121 | 122 | transaction.commit() |
|
122 | 123 | return user |
|
123 | 124 | |
|
124 | 125 | |
|
125 | 126 | @pytest.fixture |
|
126 | 127 | def default_application(default_user): |
|
127 | 128 | from appenlight.models.application import Application |
|
128 | 129 | |
|
129 | 130 | transaction.begin() |
|
130 | 131 | application = Application( |
|
131 | 132 | resource_id=1, resource_name='testapp', api_key='xxxx') |
|
132 | 133 | DBSession.add(application) |
|
133 | 134 | default_user.resources.append(application) |
|
134 | 135 | DBSession.flush() |
|
135 | 136 | transaction.commit() |
|
136 | 137 | return application |
|
137 | 138 | |
|
138 | 139 | |
|
139 | 140 | @pytest.fixture |
|
140 | 141 | def report_type_matrix(): |
|
141 | 142 | from appenlight.models.report import REPORT_TYPE_MATRIX |
|
142 | 143 | return REPORT_TYPE_MATRIX |
|
143 | 144 | |
|
144 | 145 | |
|
145 | 146 | @pytest.fixture |
|
146 | 147 | def chart_series(): |
|
147 | 148 | series = [] |
|
148 | 149 | |
|
149 | 150 | for x in range(1, 7): |
|
150 | 151 | tmp_list = [('key', 'X'), ('0_1', x)] |
|
151 | 152 | if x % 2 == 0: |
|
152 | 153 | tmp_list.append(('0_2', x)) |
|
153 | 154 | if x % 3 == 0: |
|
154 | 155 | tmp_list.append(('0_3', x)) |
|
155 | 156 | |
|
156 | 157 | series.append( |
|
157 | 158 | OrderedDict(tmp_list) |
|
158 | 159 | ) |
|
159 | 160 | return series |
|
160 | 161 | |
|
161 | 162 | |
|
162 | 163 | @pytest.fixture |
|
163 | 164 | def log_schema(): |
|
164 | 165 | from appenlight.validators import LogListSchema |
|
165 | 166 | schema = LogListSchema().bind(utcnow=datetime.utcnow()) |
|
166 | 167 | return schema |
|
167 | 168 | |
|
168 | 169 | @pytest.fixture |
|
169 | 170 | def general_metrics_schema(): |
|
170 | 171 | from appenlight.validators import GeneralMetricsListSchema |
|
171 | 172 | schema = GeneralMetricsListSchema().bind(utcnow=datetime.utcnow()) |
|
172 | 173 | return schema |
|
173 | 174 | |
|
174 | 175 | @pytest.fixture |
|
175 | 176 | def request_metrics_schema(): |
|
176 | 177 | from appenlight.validators import MetricsListSchema |
|
177 | 178 | schema = MetricsListSchema().bind(utcnow=datetime.utcnow()) |
|
178 | 179 | return schema |
|
179 | 180 | |
|
180 | 181 | @pytest.fixture |
|
181 | 182 | def report_05_schema(): |
|
182 | 183 | from appenlight.validators import ReportListSchema_0_5 |
|
183 | 184 | schema = ReportListSchema_0_5().bind(utcnow=datetime.utcnow()) |
|
184 | 185 | return schema |
@@ -1,115 +1,115 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # AppEnlight Enterprise Edition, including its added features, Support |
|
19 | 19 | # services, and proprietary license terms, please see |
|
20 | 20 | # https://rhodecode.com/licenses/ |
|
21 | 21 | import pytest |
|
22 | 22 | import json |
|
23 | 23 | from webtest import TestApp |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | @pytest.mark.usefixtures('base_app', 'with_migrations', 'clean_tables', |
|
27 | 27 | 'default_application') |
|
28 |
class Test |
|
|
28 | class TestAPIReportsView(object): | |
|
29 | 29 | def test_no_json_payload(self, base_app): |
|
30 | 30 | app = TestApp(base_app) |
|
31 | 31 | url_path = '/api/reports' |
|
32 | 32 | headers = {'x-appenlight-api-key': 'xxxx'} |
|
33 | 33 | res = app.post(url_path, {}, status=400, |
|
34 | 34 | headers=headers) |
|
35 | 35 | |
|
36 | 36 | def test_wrong_json_payload(self, base_app): |
|
37 | 37 | app = TestApp(base_app) |
|
38 | 38 | url_path = '/api/reports' |
|
39 | 39 | headers = {'x-appenlight-api-key': 'xxxx'} |
|
40 | 40 | res = app.post(url_path, {}, status=400, headers=headers) |
|
41 | 41 | |
|
42 | 42 | def test_correct_json_payload(self, base_app): |
|
43 | 43 | import appenlight.tests.payload_examples as payload_examples |
|
44 | 44 | app = TestApp(base_app) |
|
45 | 45 | url_path = '/api/reports' |
|
46 | 46 | headers = {'x-appenlight-api-key': 'xxxx'} |
|
47 | 47 | res = app.post_json(url_path, [payload_examples.PYTHON_PAYLOAD_0_5], |
|
48 | 48 | headers=headers) |
|
49 | 49 | |
|
50 | 50 | def test_json_payload_wrong_key(self, base_app): |
|
51 | 51 | import appenlight.tests.payload_examples as payload_examples |
|
52 | 52 | app = TestApp(base_app) |
|
53 | 53 | url_path = '/api/reports' |
|
54 | 54 | res = app.post(url_path, |
|
55 | 55 | json.dumps([payload_examples.PYTHON_PAYLOAD_0_5]), |
|
56 | 56 | status=403) |
|
57 | 57 | |
|
58 | 58 | |
|
59 | 59 | @pytest.mark.usefixtures('base_app', 'with_migrations', 'clean_tables', |
|
60 | 60 | 'default_data', 'default_application') |
|
61 |
class Test |
|
|
61 | class TestRegistrationView(object): | |
|
62 | 62 | def test_register_empty(self, base_app): |
|
63 | 63 | url_path = '/register' |
|
64 | 64 | app = TestApp(base_app) |
|
65 | 65 | resp = app.get('/') |
|
66 | 66 | cookies = resp.headers.getall('Set-Cookie') |
|
67 | 67 | cookie = None |
|
68 | 68 | for name, value in [c.split('=', 1) for c in cookies]: |
|
69 | 69 | if name == 'XSRF-TOKEN': |
|
70 | 70 | cookie = value.split(';')[0] |
|
71 | 71 | headers = {'X-XSRF-TOKEN': cookie} |
|
72 | 72 | res = app.post(url_path, |
|
73 | 73 | params={'user_name': '', |
|
74 | 74 | 'user_password': '', |
|
75 | 75 | 'email': ''}, |
|
76 | 76 | headers=headers) |
|
77 | 77 | assert 'This field is required.' in res |
|
78 | 78 | |
|
79 | 79 | def test_register_proper(self, base_app): |
|
80 | 80 | url_path = '/register' |
|
81 | 81 | app = TestApp(base_app) |
|
82 | 82 | resp = app.get('/') |
|
83 | 83 | cookies = resp.headers.getall('Set-Cookie') |
|
84 | 84 | cookie = None |
|
85 | 85 | for name, value in [c.split('=', 1) for c in cookies]: |
|
86 | 86 | if name == 'XSRF-TOKEN': |
|
87 | 87 | cookie = value.split(';')[0] |
|
88 | 88 | headers = {'X-XSRF-TOKEN': cookie} |
|
89 | 89 | res = app.post(url_path, |
|
90 | 90 | params={'user_name': 'user_foo', |
|
91 | 91 | 'user_password': 'passbar', |
|
92 | 92 | 'email': 'foobar@blablabla.com'}, |
|
93 | 93 | headers=headers, |
|
94 | 94 | status=302) |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | @pytest.mark.usefixtures('base_app', 'with_migrations', 'clean_tables', |
|
98 | 98 | 'default_data', 'default_application') |
|
99 |
class Test |
|
|
99 | class TestRegistrationAuthTokenView(object): | |
|
100 | 100 | |
|
101 | 101 | def test_create_application_bad(self, base_app): |
|
102 | 102 | url_path = '/applications' |
|
103 | 103 | app = TestApp(base_app) |
|
104 | 104 | headers = {'x-appenlight-auth-token': ''} |
|
105 | 105 | app.post_json(url_path, |
|
106 | 106 | params={'resource_name': 'user_foo'}, |
|
107 | 107 | headers=headers, status=403) |
|
108 | 108 | |
|
109 | 109 | def test_create_application_proper(self, base_app): |
|
110 | 110 | url_path = '/applications' |
|
111 | 111 | app = TestApp(base_app) |
|
112 | 112 | headers = {'x-appenlight-auth-token': '1234'} |
|
113 | 113 | app.post_json(url_path, |
|
114 | 114 | params={'resource_name': 'user_foo'}, |
|
115 | 115 | headers=headers, status=200) |
General Comments 0
You need to be logged in to leave comments.
Login now