-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_interface.py
185 lines (123 loc) · 5.77 KB
/
test_interface.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
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
"""
Copyright (c) 2020 Keitaro AB
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
from collections import defaultdict
import pytest
import ckan.model as model
import ckan.plugins as plugins
from ckan.tests import factories
from ckanext.saml2auth.interfaces import ISaml2Auth
from ckanext.saml2auth.tests.test_blueprint_get_request import (
_prepare_unsigned_response
)
here = os.path.dirname(os.path.abspath(__file__))
extras_folder = os.path.join(here, 'extras')
class ExampleISaml2AuthPlugin(plugins.SingletonPlugin):
plugins.implements(ISaml2Auth, inherit=True)
def __init__(self, *args, **kwargs):
self.calls = defaultdict(int)
def before_saml2_user_update(self, user_dict, saml_attributes):
self.calls['before_saml2_user_update'] += 1
user_dict['fullname'] += ' TEST UPDATE'
user_dict['plugin_extras']['my_plugin'] = {}
user_dict['plugin_extras']['my_plugin']['key1'] = 'value1'
def before_saml2_user_create(self, user_dict, saml_attributes):
self.calls['before_saml2_user_create'] += 1
user_dict['fullname'] += ' TEST CREATE'
user_dict['plugin_extras']['my_plugin'] = {}
user_dict['plugin_extras']['my_plugin']['key2'] = 'value2'
def after_saml2_login(self, resp, saml_attributes):
self.calls['after_saml2_login'] += 1
resp.headers['X-Custom-header'] = 'test'
return resp
@pytest.mark.usefixtures(u'clean_db', u'with_plugins')
@pytest.mark.ckan_config(u'ckan.plugins', u'saml2auth')
@pytest.mark.ckan_config(u'ckanext.saml2auth.entity_id', u'urn:gov:gsa:SAML:2.0.profiles:sp:sso:test:entity')
@pytest.mark.ckan_config(u'ckanext.saml2auth.idp_metadata.location', u'local')
@pytest.mark.ckan_config(u'ckanext.saml2auth.idp_metadata.local_path', os.path.join(extras_folder, 'provider0', 'idp.xml'))
@pytest.mark.ckan_config(u'ckanext.saml2auth.want_response_signed', u'False')
@pytest.mark.ckan_config(u'ckanext.saml2auth.want_assertions_signed', u'False')
@pytest.mark.ckan_config(u'ckanext.saml2auth.want_assertions_or_response_signed', u'False')
class TestInterface(object):
def test_after_login_is_called(self, app):
encoded_response = _prepare_unsigned_response()
url = '/acs'
data = {
'SAMLResponse': encoded_response
}
with plugins.use_plugin("test_saml2auth") as plugin:
response = app.post(url=url, params=data, follow_redirects=False)
assert 302 == response.status_code
assert plugin.calls["after_saml2_login"] == 1, plugin.calls
assert response.headers['X-Custom-header'] == 'test'
def test_before_create_is_called(self, app):
encoded_response = _prepare_unsigned_response()
url = '/acs'
data = {
'SAMLResponse': encoded_response
}
with plugins.use_plugin("test_saml2auth") as plugin:
response = app.post(url=url, params=data, follow_redirects=False)
assert 302 == response.status_code
assert plugin.calls["before_saml2_user_create"] == 1, plugin.calls
user = model.User.by_email('test@example.com')
if isinstance(user, list):
user = user[0]
assert user.fullname.endswith('TEST CREATE')
assert user.plugin_extras['my_plugin']['key2'] == 'value2'
assert 'saml_id' in user.plugin_extras['saml2auth']
def test_before_update_is_called_on_saml_user(self, app):
# From unsigned0.xml
saml_id = '_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7'
user = factories.User(
email='test@example.com',
plugin_extras={
'saml2auth': {
'saml_id': saml_id,
}
}
)
encoded_response = _prepare_unsigned_response()
url = '/acs'
data = {
'SAMLResponse': encoded_response
}
with plugins.use_plugin("test_saml2auth") as plugin:
response = app.post(url=url, params=data, follow_redirects=False)
assert 302 == response.status_code
assert plugin.calls["before_saml2_user_update"] == 1, plugin.calls
user = model.User.by_email('test@example.com')
if isinstance(user, list):
user = user[0]
assert user.fullname.endswith('TEST UPDATE')
assert user.plugin_extras['my_plugin']['key1'] == 'value1'
assert user.plugin_extras['saml2auth']['saml_id'] == saml_id
def test_before_update_is_called_on_ckan_user(self, app):
user = factories.User(
email='test@example.com',
)
encoded_response = _prepare_unsigned_response()
url = '/acs'
data = {
'SAMLResponse': encoded_response
}
with plugins.use_plugin("test_saml2auth") as plugin:
response = app.post(url=url, params=data, follow_redirects=False)
assert 302 == response.status_code
assert plugin.calls["before_saml2_user_update"] == 1, plugin.calls
user = model.User.by_email('test@example.com')
if isinstance(user, list):
user = user[0]
assert user.fullname.endswith('TEST UPDATE')
assert 'saml_id' in user.plugin_extras['saml2auth']