test_workflow.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from unittest import mock
  2. from uuid import uuid4
  3. import contexts
  4. from constants import HIDDEN_VALUE
  5. from core.variables 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(
  23. {"name": "var1", "value": "value1", "id": str(uuid4()), "selector": ["env", "var1"]}
  24. )
  25. variable2 = IntegerVariable.model_validate(
  26. {"name": "var2", "value": 123, "id": str(uuid4()), "selector": ["env", "var2"]}
  27. )
  28. variable3 = SecretVariable.model_validate(
  29. {"name": "var3", "value": "secret", "id": str(uuid4()), "selector": ["env", "var3"]}
  30. )
  31. variable4 = FloatVariable.model_validate(
  32. {"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]}
  33. )
  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. # Set the environment_variables property of the Workflow instance
  39. variables = [variable1, variable2, variable3, variable4]
  40. workflow.environment_variables = variables
  41. # Get the environment_variables property and assert its value
  42. assert workflow.environment_variables == variables
  43. def test_update_environment_variables():
  44. contexts.tenant_id.set("tenant_id")
  45. # Create a Workflow instance
  46. workflow = Workflow(
  47. tenant_id="tenant_id",
  48. app_id="app_id",
  49. type="workflow",
  50. version="draft",
  51. graph="{}",
  52. features="{}",
  53. created_by="account_id",
  54. environment_variables=[],
  55. conversation_variables=[],
  56. )
  57. # Create some EnvironmentVariable instances
  58. variable1 = StringVariable.model_validate(
  59. {"name": "var1", "value": "value1", "id": str(uuid4()), "selector": ["env", "var1"]}
  60. )
  61. variable2 = IntegerVariable.model_validate(
  62. {"name": "var2", "value": 123, "id": str(uuid4()), "selector": ["env", "var2"]}
  63. )
  64. variable3 = SecretVariable.model_validate(
  65. {"name": "var3", "value": "secret", "id": str(uuid4()), "selector": ["env", "var3"]}
  66. )
  67. variable4 = FloatVariable.model_validate(
  68. {"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]}
  69. )
  70. with (
  71. mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
  72. mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
  73. ):
  74. variables = [variable1, variable2, variable3, variable4]
  75. # Set the environment_variables property of the Workflow instance
  76. workflow.environment_variables = variables
  77. assert workflow.environment_variables == [variable1, variable2, variable3, variable4]
  78. # Update the name of variable3 and keep the value as it is
  79. variables[2] = variable3.model_copy(
  80. update={
  81. "name": "new name",
  82. "value": HIDDEN_VALUE,
  83. }
  84. )
  85. workflow.environment_variables = variables
  86. assert workflow.environment_variables[2].name == "new name"
  87. assert workflow.environment_variables[2].value == variable3.value
  88. def test_to_dict():
  89. contexts.tenant_id.set("tenant_id")
  90. # Create a Workflow instance
  91. workflow = Workflow(
  92. tenant_id="tenant_id",
  93. app_id="app_id",
  94. type="workflow",
  95. version="draft",
  96. graph="{}",
  97. features="{}",
  98. created_by="account_id",
  99. environment_variables=[],
  100. conversation_variables=[],
  101. )
  102. # Create some EnvironmentVariable instances
  103. with (
  104. mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
  105. mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
  106. ):
  107. # Set the environment_variables property of the Workflow instance
  108. workflow.environment_variables = [
  109. SecretVariable.model_validate({"name": "secret", "value": "secret", "id": str(uuid4())}),
  110. StringVariable.model_validate({"name": "text", "value": "text", "id": str(uuid4())}),
  111. ]
  112. workflow_dict = workflow.to_dict()
  113. assert workflow_dict["environment_variables"][0]["value"] == ""
  114. assert workflow_dict["environment_variables"][1]["value"] == "text"
  115. workflow_dict = workflow.to_dict(include_secret=True)
  116. assert workflow_dict["environment_variables"][0]["value"] == "secret"
  117. assert workflow_dict["environment_variables"][1]["value"] == "text"