|
| 1 | +from unittest import TestCase |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from satosa.context import Context |
| 6 | +from satosa.internal import InternalData |
| 7 | +from satosa.state import State |
| 8 | +from satosa.micro_services.idp_hinting import IdpHinting |
| 9 | + |
| 10 | + |
| 11 | +class TestIdpHinting(TestCase): |
| 12 | + def setUp(self): |
| 13 | + context = Context() |
| 14 | + context.state = State() |
| 15 | + internal_data = InternalData() |
| 16 | + |
| 17 | + config = { |
| 18 | + 'allowed_params': ["idp_hinting", "idp_hint", "idphint"] |
| 19 | + } |
| 20 | + |
| 21 | + plugin = IdpHinting( |
| 22 | + config=config, |
| 23 | + name='test_idphinting', |
| 24 | + base_url='https://satosa.example.org', |
| 25 | + ) |
| 26 | + plugin.next = lambda ctx, data: (ctx, data) |
| 27 | + |
| 28 | + self.config = config |
| 29 | + self.context = context |
| 30 | + self.data = internal_data |
| 31 | + self.plugin = plugin |
| 32 | + |
| 33 | + def test_no_query_params(self): |
| 34 | + self.context.qs_params = {} |
| 35 | + new_context, new_data = self.plugin.process(self.context, self.data) |
| 36 | + assert not new_context.get_decoration(Context.KEY_TARGET_ENTITYID) |
| 37 | + |
| 38 | + def test_hint_in_params(self): |
| 39 | + _target = 'https://localhost:8080' |
| 40 | + self.context.qs_params = {'idphint': _target} |
| 41 | + new_context, new_data = self.plugin.process(self.context, self.data) |
| 42 | + assert new_context.get_decoration(Context.KEY_TARGET_ENTITYID) == _target |
| 43 | + |
| 44 | + def test_no_hint_in_params(self): |
| 45 | + _target = 'https://localhost:8080' |
| 46 | + self.context.qs_params = {'param_not_in_allowed_params': _target} |
| 47 | + new_context, new_data = self.plugin.process(self.context, self.data) |
| 48 | + assert not new_context.get_decoration(Context.KEY_TARGET_ENTITYID) |
| 49 | + |
| 50 | + def test_issuer_already_set(self): |
| 51 | + _pre_selected_target = 'https://local.localhost:8080' |
| 52 | + self.context.decorate(Context.KEY_TARGET_ENTITYID, _pre_selected_target) |
| 53 | + _target = 'https://localhost:8080' |
| 54 | + self.context.qs_params = {'idphint': _target} |
| 55 | + new_context, new_data = self.plugin.process(self.context, self.data) |
| 56 | + assert new_context.get_decoration(Context.KEY_TARGET_ENTITYID) == _pre_selected_target |
| 57 | + assert new_context.get_decoration(Context.KEY_TARGET_ENTITYID) != _target |
0 commit comments