liquid_migrator_spec.rb 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. require 'rails_helper'
  2. describe LiquidMigrator do
  3. describe "converting JSONPath strings" do
  4. it "should work" do
  5. expect(LiquidMigrator.convert_string("$.data", true)).to eq("{{data}}")
  6. expect(LiquidMigrator.convert_string("$.data.test", true)).to eq("{{data.test}}")
  7. expect(LiquidMigrator.convert_string("$first_title", true)).to eq("{{first_title}}")
  8. end
  9. it "should ignore strings which just contain a JSONPath" do
  10. expect(LiquidMigrator.convert_string("$.data")).to eq("$.data")
  11. expect(LiquidMigrator.convert_string("$first_title")).to eq("$first_title")
  12. expect(LiquidMigrator.convert_string(" $.data", true)).to eq(" $.data")
  13. expect(LiquidMigrator.convert_string("lorem $.data", true)).to eq("lorem $.data")
  14. end
  15. it "should raise an exception when encountering complex JSONPaths" do
  16. expect { LiquidMigrator.convert_string("$.data.test.*", true) }.
  17. to raise_error("JSONPath '$.data.test.*' is too complex, please check your migration.")
  18. end
  19. end
  20. describe "converting escaped JSONPath strings" do
  21. it "should work" do
  22. expect(LiquidMigrator.convert_string("Weather looks like <$.conditions> according to the forecast at <$.pretty_date.time>")).to eq(
  23. "Weather looks like {{conditions}} according to the forecast at {{pretty_date.time}}"
  24. )
  25. end
  26. it "should convert the 'escape' method correctly" do
  27. expect(LiquidMigrator.convert_string("Escaped: <escape $.content.name>\nNot escaped: <$.content.name>")).to eq(
  28. "Escaped: {{content.name | uri_escape}}\nNot escaped: {{content.name}}"
  29. )
  30. end
  31. it "should raise an exception when encountering complex JSONPaths" do
  32. expect { LiquidMigrator.convert_string("Received <$.content.text.*> from <$.content.name> .") }.
  33. to raise_error("JSONPath '$.content.text.*' is too complex, please check your migration.")
  34. end
  35. end
  36. describe "migrating a hash" do
  37. it "should convert every attribute" do
  38. expect(LiquidMigrator.convert_hash({'a' => "$.data", 'b' => "This is a <$.test>"})).to eq(
  39. {'a' => "$.data", 'b' => "This is a {{test}}"}
  40. )
  41. end
  42. it "should work with leading_dollarsign_is_jsonpath" do
  43. expect(LiquidMigrator.convert_hash({'a' => "$.data", 'b' => "This is a <$.test>"}, leading_dollarsign_is_jsonpath: true)).to eq(
  44. {'a' => "{{data}}", 'b' => "This is a {{test}}"}
  45. )
  46. end
  47. it "should use the corresponding *_path attributes when using merge_path_attributes"do
  48. expect(LiquidMigrator.convert_hash({'a' => "default", 'a_path' => "$.data"}, {leading_dollarsign_is_jsonpath: true, merge_path_attributes: true})).to eq(
  49. {'a' => "{{data}}"}
  50. )
  51. end
  52. it "should raise an exception when encountering complex JSONPaths" do
  53. expect { LiquidMigrator.convert_hash({'b' => "This is <$.complex[2]>"}) }.
  54. to raise_error("JSONPath '$.complex[2]' is too complex, please check your migration.")
  55. end
  56. end
  57. describe "migrating the 'make_message' format" do
  58. it "should work" do
  59. expect(LiquidMigrator.convert_make_message('<message>')).to eq('{{message}}')
  60. expect(LiquidMigrator.convert_make_message('<new.message>')).to eq('{{new.message}}')
  61. expect(LiquidMigrator.convert_make_message('Hello <world>. How is <nested.life>')).to eq('Hello {{world}}. How is {{nested.life}}')
  62. end
  63. end
  64. describe "migrating an actual agent" do
  65. before do
  66. valid_params = {
  67. 'auth_token' => 'token',
  68. 'room_name' => 'test',
  69. 'room_name_path' => '',
  70. 'username' => "Huginn",
  71. 'username_path' => '$.username',
  72. 'message' => "Hello from Huginn!",
  73. 'message_path' => '$.message',
  74. 'notify' => false,
  75. 'notify_path' => '',
  76. 'color' => 'yellow',
  77. 'color_path' => '',
  78. }
  79. @agent = Agents::HipchatAgent.new(:name => "somename", :options => valid_params)
  80. @agent.user = users(:jane)
  81. @agent.save!
  82. end
  83. it "should work" do
  84. LiquidMigrator.convert_all_agent_options(@agent)
  85. expect(@agent.reload.options).to eq({"auth_token" => 'token', 'color' => 'yellow', 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'})
  86. end
  87. it "should work with nested hashes" do
  88. @agent.options['very'] = {'nested' => '$.value'}
  89. LiquidMigrator.convert_all_agent_options(@agent)
  90. expect(@agent.reload.options).to eq({"auth_token" => 'token', 'color' => 'yellow', 'very' => {'nested' => '{{value}}'}, 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'})
  91. end
  92. it "should work with nested arrays" do
  93. @agent.options['array'] = ["one", "$.two"]
  94. LiquidMigrator.convert_all_agent_options(@agent)
  95. expect(@agent.reload.options).to eq({"auth_token" => 'token', 'color' => 'yellow', 'array' => ['one', '{{two}}'], 'notify' => false, 'room_name' => 'test', 'username' => '{{username}}', 'message' => '{{message}}'})
  96. end
  97. it "should raise an exception when encountering complex JSONPaths" do
  98. @agent.options['username_path'] = "$.very.complex[*]"
  99. expect { LiquidMigrator.convert_all_agent_options(@agent) }.
  100. to raise_error("JSONPath '$.very.complex[*]' is too complex, please check your migration.")
  101. end
  102. it "should work with the human task agent" do
  103. valid_params = {
  104. 'expected_receive_period_in_days' => 2,
  105. 'trigger_on' => "event",
  106. 'hit' =>
  107. {
  108. 'assignments' => 1,
  109. 'title' => "Sentiment evaluation",
  110. 'description' => "Please rate the sentiment of this message: '<$.message>'",
  111. 'reward' => 0.05,
  112. 'lifetime_in_seconds' => 24 * 60 * 60,
  113. 'questions' =>
  114. [
  115. {
  116. 'type' => "selection",
  117. 'key' => "sentiment",
  118. 'name' => "Sentiment",
  119. 'required' => "true",
  120. 'question' => "Please select the best sentiment value:",
  121. 'selections' =>
  122. [
  123. { 'key' => "happy", 'text' => "Happy" },
  124. { 'key' => "sad", 'text' => "Sad" },
  125. { 'key' => "neutral", 'text' => "Neutral" }
  126. ]
  127. },
  128. {
  129. 'type' => "free_text",
  130. 'key' => "feedback",
  131. 'name' => "Have any feedback for us?",
  132. 'required' => "false",
  133. 'question' => "Feedback",
  134. 'default' => "Type here...",
  135. 'min_length' => "2",
  136. 'max_length' => "2000"
  137. }
  138. ]
  139. }
  140. }
  141. @agent = Agents::HumanTaskAgent.new(:name => "somename", :options => valid_params)
  142. @agent.user = users(:jane)
  143. LiquidMigrator.convert_all_agent_options(@agent)
  144. expect(@agent.reload.options['hit']['description']).to eq("Please rate the sentiment of this message: '{{message}}'")
  145. end
  146. end
  147. end