|
| 1 | +from satosa.internal_data import InternalResponse, AuthenticationInformation |
| 2 | +from satosa.micro_services.attribute_authorization import AttributeAuthorization |
| 3 | +from satosa.exception import SATOSAAuthenticationError |
| 4 | +from satosa.context import Context |
| 5 | + |
| 6 | +class TestAttributeAuthorization: |
| 7 | + def create_authz_service(self, attribute_allow, attribute_deny): |
| 8 | + authz_service = AttributeAuthorization(config=dict(attribute_allow=attribute_allow,attribute_deny=attribute_deny), name="test_authz", |
| 9 | + base_url="https://satosa.example.com") |
| 10 | + authz_service.next = lambda ctx, data: data |
| 11 | + return authz_service |
| 12 | + |
| 13 | + def test_authz_allow_success(self): |
| 14 | + attribute_allow = { |
| 15 | + "": { "default": {"a0": ['.+@.+']} } |
| 16 | + } |
| 17 | + attribute_deny = {} |
| 18 | + authz_service = self.create_authz_service(attribute_allow, attribute_deny) |
| 19 | + resp = InternalResponse(AuthenticationInformation(None, None, None)) |
| 20 | + resp.attributes = { |
| 21 | + "a0": ["test@example.com"], |
| 22 | + } |
| 23 | + try: |
| 24 | + ctx = Context() |
| 25 | + ctx.state = dict() |
| 26 | + authz_service.process(ctx, resp) |
| 27 | + except SATOSAAuthenticationError as ex: |
| 28 | + assert False |
| 29 | + |
| 30 | + def test_authz_allow_fail(self): |
| 31 | + attribute_allow = { |
| 32 | + "": { "default": {"a0": ['foo1','foo2']} } |
| 33 | + } |
| 34 | + attribute_deny = {} |
| 35 | + authz_service = self.create_authz_service(attribute_allow, attribute_deny) |
| 36 | + resp = InternalResponse(AuthenticationInformation(None, None, None)) |
| 37 | + resp.attributes = { |
| 38 | + "a0": ["bar"], |
| 39 | + } |
| 40 | + try: |
| 41 | + ctx = Context() |
| 42 | + ctx.state = dict() |
| 43 | + authz_service.process(ctx, resp) |
| 44 | + assert False |
| 45 | + except SATOSAAuthenticationError as ex: |
| 46 | + assert True |
| 47 | + |
| 48 | + def test_authz_allow_second(self): |
| 49 | + attribute_allow = { |
| 50 | + "": { "default": {"a0": ['foo1','foo2']} } |
| 51 | + } |
| 52 | + attribute_deny = {} |
| 53 | + authz_service = self.create_authz_service(attribute_allow, attribute_deny) |
| 54 | + resp = InternalResponse(AuthenticationInformation(None, None, None)) |
| 55 | + resp.attributes = { |
| 56 | + "a0": ["foo2","kaka"], |
| 57 | + } |
| 58 | + try: |
| 59 | + ctx = Context() |
| 60 | + ctx.state = dict() |
| 61 | + authz_service.process(ctx, resp) |
| 62 | + except SATOSAAuthenticationError as ex: |
| 63 | + assert False |
| 64 | + |
| 65 | + def test_authz_deny_success(self): |
| 66 | + attribute_deny = { |
| 67 | + "": { "default": {"a0": ['foo1','foo2']} } |
| 68 | + } |
| 69 | + attribute_allow = {} |
| 70 | + authz_service = self.create_authz_service(attribute_allow, attribute_deny) |
| 71 | + resp = InternalResponse(AuthenticationInformation(None, None, None)) |
| 72 | + resp.attributes = { |
| 73 | + "a0": ["foo2"], |
| 74 | + } |
| 75 | + try: |
| 76 | + ctx = Context() |
| 77 | + ctx.state = dict() |
| 78 | + authz_service.process(ctx, resp) |
| 79 | + assert False |
| 80 | + except SATOSAAuthenticationError as ex: |
| 81 | + assert True |
| 82 | + |
| 83 | + def test_authz_deny_fail(self): |
| 84 | + attribute_deny = { |
| 85 | + "": { "default": {"a0": ['foo1','foo2']} } |
| 86 | + } |
| 87 | + attribute_allow = {} |
| 88 | + authz_service = self.create_authz_service(attribute_allow, attribute_deny) |
| 89 | + resp = InternalResponse(AuthenticationInformation(None, None, None)) |
| 90 | + resp.attributes = { |
| 91 | + "a0": ["foo3"], |
| 92 | + } |
| 93 | + try: |
| 94 | + ctx = Context() |
| 95 | + ctx.state = dict() |
| 96 | + authz_service.process(ctx, resp) |
| 97 | + except SATOSAAuthenticationError as ex: |
| 98 | + assert False |
0 commit comments