| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from mistral_lib.actions import context |
| 14 |
from mistral_lib.tests import base as tests_base |
| 15 |
|
| 16 |
|
| 17 |
def _fake_context(): |
| 18 |
security_ctx = context.SecurityContext( |
| 19 |
auth_uri='auth_uri', |
| 20 |
auth_cacert='auth_cacert', |
| 21 |
insecure='insecure', |
| 22 |
service_catalog='service_catalog', |
| 23 |
region_name='region_name', |
| 24 |
is_trust_scoped='is_trust_scoped', |
| 25 |
redelivered='redelivered', |
| 26 |
expires_at='expires_at', |
| 27 |
trust_id='trust_id', |
| 28 |
is_target='is_target', |
| 29 |
project_id='project_id') |
| 30 |
|
| 31 |
execution_ctx = context.ExecutionContext( |
| 32 |
workflow_execution_id='workflow_execution_id', |
| 33 |
task_execution_id='task_execution_id', |
| 34 |
action_execution_id='action_execution_id', |
| 35 |
workflow_name='workflow_name', |
| 36 |
callback_url='callback_url', |
| 37 |
with_items_index=0, |
| 38 |
task_rerun_no=0, |
| 39 |
task_rerun_id='task_rerun_id') |
| 40 |
|
| 41 |
return context.ActionContext(security_ctx, execution_ctx) |
| 42 |
|
| 43 |
|
| 44 |
class TestActionsBase(tests_base.TestCase): |
| 45 |
|
| 46 |
def test_empty_context(self): |
| 47 |
ctx = context.ActionContext( |
| 48 |
context.SecurityContext(), |
| 49 |
context.ExecutionContext() |
| 50 |
) |
| 51 |
|
| 52 |
self.assertIsInstance(ctx.security, context.SecurityContext) |
| 53 |
self.assertIsInstance(ctx.execution, context.ExecutionContext) |
| 54 |
|
| 55 |
self.assertEqual(ctx.security.auth_uri, None) |
| 56 |
self.assertEqual(ctx.execution.workflow_name, None) |
| 57 |
|
| 58 |
def test_deprecated_properties(self): |
| 59 |
ctx = _fake_context() |
| 60 |
|
| 61 |
deprecated_properties = [ |
| 62 |
'auth_cacert', 'auth_token', 'auth_uri', 'expires_at', |
| 63 |
'insecure', 'is_target', 'is_trust_scoped', 'project_id', |
| 64 |
'project_name', 'redelivered', 'region_name', 'service_catalog', |
| 65 |
'trust_id', 'user_name' |
| 66 |
] |
| 67 |
|
| 68 |
for deprecated in deprecated_properties: |
| 69 |
old = getattr(ctx, deprecated) |
| 70 |
new = getattr(ctx.security, deprecated) |
| 71 |
|
| 72 |
self.assertEqual(old, new) |
| 73 |
|
| 74 |
|
| 75 |
class TestActionContextSerializer(tests_base.TestCase): |
| 76 |
|
| 77 |
def test_serialization(self): |
| 78 |
ctx = _fake_context() |
| 79 |
|
| 80 |
serialiser = context.ActionContextSerializer() |
| 81 |
|
| 82 |
dict_ctx = serialiser.serialize_to_dict(ctx) |
| 83 |
|
| 84 |
self.assertEqual(dict_ctx['security'], vars(ctx.security)) |
| 85 |
self.assertEqual(dict_ctx['execution'], vars(ctx.execution)) |
| 86 |
|
| 87 |
def test_deserialization(self): |
| 88 |
ctx = _fake_context() |
| 89 |
|
| 90 |
serialiser = context.ActionContextSerializer() |
| 91 |
|
| 92 |
dict_ctx = serialiser.serialize_to_dict(ctx) |
| 93 |
ctx_2 = serialiser.deserialize_from_dict(dict_ctx) |
| 94 |
|
| 95 |
self.assertEqual(ctx.security.auth_uri, ctx_2.security.auth_uri) |
| 96 |
self.assertEqual( |
| 97 |
ctx.execution.workflow_name, |
| 98 |
ctx_2.execution.workflow_name |
| 99 |
) |