test_workflow.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from unittest import mock
  2. from uuid import uuid4
  3. import contexts
  4. from constants import HIDDEN_VALUE
  5. from core.app.segments import FloatVariable, IntegerVariable, SecretVariable, StringVariable
  6. from models.workflow import Workflow
  7. def test_environment_variables():
  8. contexts.tenant_id.set('tenant_id')
  9. # Create a Workflow instance
  10. workflow = Workflow()
  11. # Create some EnvironmentVariable instances
  12. variable1 = StringVariable.model_validate({'name': 'var1', 'value': 'value1', 'id': str(uuid4())})
  13. variable2 = IntegerVariable.model_validate({'name': 'var2', 'value': 123, 'id': str(uuid4())})
  14. variable3 = SecretVariable.model_validate({'name': 'var3', 'value': 'secret', 'id': str(uuid4())})
  15. variable4 = FloatVariable.model_validate({'name': 'var4', 'value': 3.14, 'id': str(uuid4())})
  16. with (
  17. mock.patch('core.helper.encrypter.encrypt_token', return_value='encrypted_token'),
  18. mock.patch('core.helper.encrypter.decrypt_token', return_value='secret'),
  19. ):
  20. # Set the environment_variables property of the Workflow instance
  21. variables = [variable1, variable2, variable3, variable4]
  22. workflow.environment_variables = variables
  23. # Get the environment_variables property and assert its value
  24. assert workflow.environment_variables == variables
  25. def test_update_environment_variables():
  26. contexts.tenant_id.set('tenant_id')
  27. # Create a Workflow instance
  28. workflow = Workflow()
  29. # Create some EnvironmentVariable instances
  30. variable1 = StringVariable.model_validate({'name': 'var1', 'value': 'value1', 'id': str(uuid4())})
  31. variable2 = IntegerVariable.model_validate({'name': 'var2', 'value': 123, 'id': str(uuid4())})
  32. variable3 = SecretVariable.model_validate({'name': 'var3', 'value': 'secret', 'id': str(uuid4())})
  33. variable4 = FloatVariable.model_validate({'name': 'var4', 'value': 3.14, 'id': str(uuid4())})
  34. with (
  35. mock.patch('core.helper.encrypter.encrypt_token', return_value='encrypted_token'),
  36. mock.patch('core.helper.encrypter.decrypt_token', return_value='secret'),
  37. ):
  38. variables = [variable1, variable2, variable3, variable4]
  39. # Set the environment_variables property of the Workflow instance
  40. workflow.environment_variables = variables
  41. assert workflow.environment_variables == [variable1, variable2, variable3, variable4]
  42. # Update the name of variable3 and keep the value as it is
  43. variables[2] = variable3.model_copy(
  44. update={
  45. 'name': 'new name',
  46. 'value': HIDDEN_VALUE,
  47. }
  48. )
  49. workflow.environment_variables = variables
  50. assert workflow.environment_variables[2].name == 'new name'
  51. assert workflow.environment_variables[2].value == variable3.value
  52. def test_to_dict():
  53. contexts.tenant_id.set('tenant_id')
  54. # Create a Workflow instance
  55. workflow = Workflow()
  56. workflow.graph = '{}'
  57. workflow.features = '{}'
  58. # Create some EnvironmentVariable instances
  59. with (
  60. mock.patch('core.helper.encrypter.encrypt_token', return_value='encrypted_token'),
  61. mock.patch('core.helper.encrypter.decrypt_token', return_value='secret'),
  62. ):
  63. # Set the environment_variables property of the Workflow instance
  64. workflow.environment_variables = [
  65. SecretVariable.model_validate({'name': 'secret', 'value': 'secret', 'id': str(uuid4())}),
  66. StringVariable.model_validate({'name': 'text', 'value': 'text', 'id': str(uuid4())}),
  67. ]
  68. workflow_dict = workflow.to_dict()
  69. assert workflow_dict['environment_variables'][0]['value'] == ''
  70. assert workflow_dict['environment_variables'][1]['value'] == 'text'
  71. workflow_dict = workflow.to_dict(include_secret=True)
  72. assert workflow_dict['environment_variables'][0]['value'] == 'secret'
  73. assert workflow_dict['environment_variables'][1]['value'] == 'text'