|
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
|
6 |
| -from satosa.exception import SATOSAError, SATOSAConfigurationError |
| 7 | +from satosa.state import State |
| 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 DecideBackendByTargetIssuer |
| 12 | +from satosa.micro_services.custom_routing import CustomRoutingError |
| 13 | + |
9 | 14 |
|
10 | 15 | TARGET_ENTITY = "entity1"
|
11 | 16 |
|
@@ -156,3 +161,45 @@ def test_missing_target_entity_id_from_context(self, context):
|
156 | 161 | req = InternalData(requester="test_requester")
|
157 | 162 | with pytest.raises(SATOSAError):
|
158 | 163 | decide_service.process(context, req)
|
| 164 | + |
| 165 | + |
| 166 | +class TestDecideBackendByTargetIssuer(TestCase): |
| 167 | + def setUp(self): |
| 168 | + context = Context() |
| 169 | + context.state = State() |
| 170 | + |
| 171 | + config = { |
| 172 | + 'default_backend': 'default_backend', |
| 173 | + 'target_mapping': { |
| 174 | + 'mapped_idp.example.org': 'mapped_backend', |
| 175 | + }, |
| 176 | + } |
| 177 | + |
| 178 | + plugin = DecideBackendByTargetIssuer( |
| 179 | + config=config, |
| 180 | + name='test_decide_service', |
| 181 | + base_url='https://satosa.example.org', |
| 182 | + ) |
| 183 | + plugin.next = lambda ctx, data: (ctx, data) |
| 184 | + |
| 185 | + self.config = config |
| 186 | + self.context = context |
| 187 | + self.plugin = plugin |
| 188 | + |
| 189 | + def test_when_target_is_not_set_do_skip(self): |
| 190 | + data = InternalData(requester='test_requester') |
| 191 | + newctx, newdata = self.plugin.process(self.context, data) |
| 192 | + assert not newctx.target_backend |
| 193 | + |
| 194 | + def test_when_target_is_not_mapped_choose_default_backend(self): |
| 195 | + self.context.decorate(Context.KEY_TARGET_ENTITYID, 'idp.example.org') |
| 196 | + data = InternalData(requester='test_requester') |
| 197 | + newctx, newdata = self.plugin.process(self.context, data) |
| 198 | + assert newctx.target_backend == 'default_backend' |
| 199 | + |
| 200 | + def test_when_target_is_mapped_choose_mapping_backend(self): |
| 201 | + self.context.decorate(Context.KEY_TARGET_ENTITYID, 'mapped_idp.example.org') |
| 202 | + data = InternalData(requester='test_requester') |
| 203 | + data.requester = 'somebody else' |
| 204 | + newctx, newdata = self.plugin.process(self.context, data) |
| 205 | + assert newctx.target_backend == 'mapped_backend' |
0 commit comments