test_workflow.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. tenant_id='tenant_id',
  12. app_id='app_id',
  13. type='workflow',
  14. version='draft',
  15. graph='{}',
  16. features='{}',
  17. created_by='account_id',
  18. environment_variables=[],
  19. conversation_variables=[],
  20. )
  21. # Create some EnvironmentVariable instances
  22. variable1 = StringVariable.model_validate({'name': 'var1', 'value': 'value1', 'id': str(uuid4())})
  23. variable2 = IntegerVariable.model_validate({'name': 'var2', 'value': 123, 'id': str(uuid4())})
  24. variable3 = SecretVariable.model_validate({'name': 'var3', 'value': 'secret', 'id': str(uuid4())})
  25. variable4 = FloatVariable.model_validate({'name': 'var4', 'value': 3.14, 'id': str(uuid4())})
  26. with (
  27. mock.patch('core.helper.encrypter.encrypt_token', return_value='encrypted_token'),
  28. mock.patch('core.helper.encrypter.decrypt_token', return_value='secret'),
  29. ):
  30. # Set the environment_variables property of the Workflow instance
  31. variables = [variable1, variable2, variable3, variable4]
  32. workflow.environment_variables = variables
  33. # Get the environment_variables property and assert its value
  34. assert workflow.environment_variables == variables
  35. def test_update_environment_variables():
  36. contexts.tenant_id.set('tenant_id')
  37. # Create a Workflow instance
  38. workflow = Workflow(
  39. tenant_id='tenant_id',
  40. app_id='app_id',
  41. type='workflow',
  42. version='draft',
  43. graph='{}',
  44. features='{}',
  45. created_by='account_id',
  46. environment_variables=[],
  47. conversation_variables=[],
  48. )
  49. # Create some EnvironmentVariable instances
  50. variable1 = StringVariable.model_validate({'name': 'var1', 'value': 'value1', 'id': str(uuid4())})
  51. variable2 = IntegerVariable.model_validate({'name': 'var2', 'value': 123, 'id': str(uuid4())})
  52. variable3 = SecretVariable.model_validate({'name': 'var3', 'value': 'secret', 'id': str(uuid4())})
  53. variable4 = FloatVariable.model_validate({'name': 'var4', 'value': 3.14, 'id': str(uuid4())})
  54. with (
  55. mock.patch('core.helper.encrypter.encrypt_token', return_value='encrypted_token'),
  56. mock.patch('core.helper.encrypter.decrypt_token', return_value='secret'),
  57. ):
  58. variables = [variable1, variable2, variable3, variable4]
  59. # Set the environment_variables property of the Workflow instance
  60. workflow.environment_variables = variables
  61. assert workflow.environment_variables == [variable1, variable2, variable3, variable4]
  62. # Update the name of variable3 and keep the value as it is
  63. variables[2] = variable3.model_copy(
  64. update={
  65. 'name': 'new name',
  66. 'value': HIDDEN_VALUE,
  67. }
  68. )
  69. workflow.environment_variables = variables
  70. assert workflow.environment_variables[2].name == 'new name'
  71. assert workflow.environment_variables[2].value == variable3.value
  72. def test_to_dict():
  73. contexts.tenant_id.set('tenant_id')
  74. # Create a Workflow instance
  75. workflow = Workflow(
  76. tenant_id='tenant_id',
  77. app_id='app_id',
  78. type='workflow',
  79. version='draft',
  80. graph='{}',
  81. features='{}',
  82. created_by='account_id',
  83. environment_variables=[],
  84. conversation_variables=[],
  85. )
  86. # Create some EnvironmentVariable instances
  87. with (
  88. mock.patch('core.helper.encrypter.encrypt_token', return_value='encrypted_token'),
  89. mock.patch('core.helper.encrypter.decrypt_token', return_value='secret'),
  90. ):
  91. # Set the environment_variables property of the Workflow instance
  92. workflow.environment_variables = [
  93. SecretVariable.model_validate({'name': 'secret', 'value': 'secret', 'id': str(uuid4())}),
  94. StringVariable.model_validate({'name': 'text', 'value': 'text', 'id': str(uuid4())}),
  95. ]
  96. workflow_dict = workflow.to_dict()
  97. assert workflow_dict['environment_variables'][0]['value'] == ''
  98. assert workflow_dict['environment_variables'][1]['value'] == 'text'
  99. workflow_dict = workflow.to_dict(include_secret=True)
  100. assert workflow_dict['environment_variables'][0]['value'] == 'secret'
  101. assert workflow_dict['environment_variables'][1]['value'] == 'text'