|
1 | 1 | from base64 import urlsafe_b64encode |
| 2 | +from unittest import TestCase |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 |
|
5 | 6 | from satosa.context import Context |
| 7 | +from satosa.state import State |
6 | 8 | from satosa.exception import SATOSAError, SATOSAConfigurationError, SATOSAStateError |
7 | 9 | from satosa.internal import InternalData |
8 | 10 | from satosa.micro_services.custom_routing import DecideIfRequesterIsAllowed |
| 11 | +from satosa.micro_services.custom_routing import DecideBackendByDiscoIdP |
9 | 12 | from satosa.micro_services.custom_routing import DecideBackendByTargetIdP |
10 | 13 | from satosa.micro_services.custom_routing import CustomRoutingError |
11 | 14 |
|
| 15 | + |
12 | 16 | TARGET_ENTITY = "entity1" |
13 | 17 |
|
14 | 18 |
|
@@ -160,61 +164,92 @@ def test_missing_target_entity_id_from_context(self, context): |
160 | 164 | decide_service.process(context, req) |
161 | 165 |
|
162 | 166 |
|
163 | | -class TestDecideBackendByTargetIdP: |
164 | | - rules = { |
165 | | - 'default_backend': 'Saml2', |
166 | | - 'endpoint_paths': ['.*/disco'], |
167 | | - 'target_mapping': {'http://idpspid.testunical.it:8088': 'spidSaml2'} |
168 | | - } |
169 | | - |
170 | | - def create_decide_service(self, rules): |
171 | | - decide_service = DecideBackendByTargetIdP( |
172 | | - config=rules, |
173 | | - name="test_decide_service", |
174 | | - base_url="https://satosa.example.com" |
175 | | - ) |
176 | | - decide_service.next = lambda ctx, data: data |
177 | | - return decide_service |
| 167 | +class TestDecideBackendByTargetIdP(TestCase): |
| 168 | + def setUp(self): |
| 169 | + context = Context() |
| 170 | + context.state = State() |
178 | 171 |
|
179 | | - |
180 | | - def test_missing_state(self, target_context): |
181 | | - decide_service = self.create_decide_service(self.rules) |
182 | | - target_context.request = { |
183 | | - 'entityID': 'http://idpspid.testunical.it:8088', |
| 172 | + config = { |
| 173 | + 'default_backend': 'default_backend', |
| 174 | + 'target_mapping': { |
| 175 | + 'mapped_idp.example.org': 'mapped_backend', |
| 176 | + }, |
| 177 | + 'disco_endpoints': [ |
| 178 | + '.*/disco', |
| 179 | + ], |
184 | 180 | } |
185 | | - req = InternalData(requester="test_requester") |
186 | | - req.requester = "somebody else" |
187 | | - assert decide_service.process(target_context, req) |
188 | | - |
189 | | - with pytest.raises(SATOSAStateError): |
190 | | - decide_service.backend_by_entityid(target_context) |
191 | 181 |
|
192 | | - |
193 | | - def test_unmatching_target(self, target_context): |
194 | | - """ |
195 | | - It would rely on the default backend |
196 | | - """ |
197 | | - decide_service = self.create_decide_service(self.rules) |
198 | | - target_context.request = { |
199 | | - 'entityID': 'unknow-entity-id', |
| 182 | + plugin = DecideBackendByTargetIdP( |
| 183 | + config=config, |
| 184 | + name='test_decide_service', |
| 185 | + base_url='https://satosa.example.org', |
| 186 | + ) |
| 187 | + plugin.next = lambda ctx, data: (ctx, data) |
| 188 | + |
| 189 | + self.config = config |
| 190 | + self.context = context |
| 191 | + self.plugin = plugin |
| 192 | + |
| 193 | + def test_when_target_is_not_set_do_skip(self): |
| 194 | + data = InternalData(requester='test_requester') |
| 195 | + newctx, newdata = self.plugin.process(self.context, data) |
| 196 | + assert not newctx.target_backend |
| 197 | + |
| 198 | + def test_when_target_is_not_mapped_choose_default_backend(self): |
| 199 | + self.context.decorate(Context.KEY_TARGET_ENTITYID, 'idp.example.org') |
| 200 | + data = InternalData(requester='test_requester') |
| 201 | + newctx, newdata = self.plugin.process(self.context, data) |
| 202 | + assert newctx.target_backend == 'default_backend' |
| 203 | + |
| 204 | + def test_when_target_is_mapped_choose_mapping_backend(self): |
| 205 | + self.context.decorate(Context.KEY_TARGET_ENTITYID, 'mapped_idp.example.org') |
| 206 | + data = InternalData(requester='test_requester') |
| 207 | + data.requester = 'somebody else' |
| 208 | + newctx, newdata = self.plugin.process(self.context, data) |
| 209 | + assert newctx.target_backend == 'mapped_backend' |
| 210 | + |
| 211 | + |
| 212 | +class TestDecideBackendByDiscoIdP(TestCase): |
| 213 | + def setUp(self): |
| 214 | + context = Context() |
| 215 | + context.state = State() |
| 216 | + |
| 217 | + config = { |
| 218 | + 'default_backend': 'default_backend', |
| 219 | + 'target_mapping': { |
| 220 | + 'mapped_idp.example.org': 'mapped_backend', |
| 221 | + }, |
| 222 | + 'disco_endpoints': [ |
| 223 | + '.*/disco', |
| 224 | + ], |
200 | 225 | } |
201 | | - target_context.state['ROUTER'] = 'Saml2' |
202 | 226 |
|
203 | | - req = InternalData(requester="test_requester") |
204 | | - assert decide_service.process(target_context, req) |
| 227 | + plugin = DecideBackendByDiscoIdP( |
| 228 | + config=config, |
| 229 | + name='test_decide_service', |
| 230 | + base_url='https://satosa.example.org', |
| 231 | + ) |
| 232 | + plugin.next = lambda ctx, data: (ctx, data) |
205 | 233 |
|
206 | | - res = decide_service.backend_by_entityid(target_context) |
207 | | - assert isinstance(res, InternalData) |
| 234 | + self.config = config |
| 235 | + self.context = context |
| 236 | + self.plugin = plugin |
208 | 237 |
|
209 | | - def test_matching_target(self, target_context): |
210 | | - decide_service = self.create_decide_service(self.rules) |
211 | | - target_context.request = { |
212 | | - 'entityID': 'http://idpspid.testunical.it:8088-entity-id' |
| 238 | + def test_when_target_is_not_set_raise_error(self): |
| 239 | + self.context.request = {} |
| 240 | + with pytest.raises(CustomRoutingError): |
| 241 | + self.plugin._handle_disco_response(self.context) |
| 242 | + |
| 243 | + def test_when_target_is_not_mapped_choose_default_backend(self): |
| 244 | + self.context.request = { |
| 245 | + 'entityID': 'idp.example.org', |
213 | 246 | } |
214 | | - target_context.state['ROUTER'] = 'Saml2' |
| 247 | + newctx, newdata = self.plugin._handle_disco_response(self.context) |
| 248 | + assert newctx.target_backend == 'default_backend' |
215 | 249 |
|
216 | | - req = InternalData(requester="test_requester") |
217 | | - req.requester = "somebody else" |
218 | | - assert decide_service.process(target_context, req) |
219 | | - res = decide_service.backend_by_entityid(target_context) |
220 | | - assert isinstance(res, InternalData) |
| 250 | + def test_when_target_is_mapped_choose_mapping_backend(self): |
| 251 | + self.context.request = { |
| 252 | + 'entityID': 'mapped_idp.example.org', |
| 253 | + } |
| 254 | + newctx, newdata = self.plugin._handle_disco_response(self.context) |
| 255 | + assert newctx.target_backend == 'mapped_backend' |
0 commit comments