1
0

liquid_interpolatable_spec.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. require 'rails_helper'
  2. require 'nokogiri'
  3. describe LiquidInterpolatable::Filters do
  4. before do
  5. @filter = Class.new do
  6. include LiquidInterpolatable::Filters
  7. end.new
  8. end
  9. describe 'uri_escape' do
  10. it 'should escape a string for use in URI' do
  11. expect(@filter.uri_escape('abc:/?=')).to eq('abc%3A%2F%3F%3D')
  12. end
  13. it 'should not raise an error when an operand is nil' do
  14. expect(@filter.uri_escape(nil)).to be_nil
  15. end
  16. end
  17. describe 'validations' do
  18. class Agents::InterpolatableAgent < Agent
  19. include LiquidInterpolatable
  20. def check
  21. create_event :payload => {}
  22. end
  23. def validate_options
  24. interpolated['foo']
  25. end
  26. end
  27. it "should finish without raising an exception" do
  28. agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{{bar}' })
  29. expect(agent.valid?).to eq(false)
  30. expect(agent.errors[:options].first).to match(/not properly terminated/)
  31. end
  32. end
  33. describe 'unescape' do
  34. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  35. it 'should unescape basic HTML entities' do
  36. agent.interpolation_context['something'] = '&#39;&lt;foo&gt; &amp; bar&#x27;'
  37. agent.options['cleaned'] = '{{ something | unescape }}'
  38. expect(agent.interpolated['cleaned']).to eq("'<foo> & bar'")
  39. end
  40. end
  41. describe "json" do
  42. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  43. it 'serializes data to json' do
  44. agent.interpolation_context['something'] = {foo: 'bar'}
  45. agent.options['cleaned'] = '{{ something | json }}'
  46. expect(agent.interpolated['cleaned']).to eq('{"foo":"bar"}')
  47. end
  48. end
  49. describe 'to_xpath' do
  50. before do
  51. def @filter.to_xpath_roundtrip(string)
  52. Nokogiri::XML('').xpath(to_xpath(string))
  53. end
  54. end
  55. it 'should escape a string for use in XPath expression' do
  56. [
  57. %q{abc}.freeze,
  58. %q{'a"bc'dfa""fds''fa}.freeze,
  59. ].each { |string|
  60. expect(@filter.to_xpath_roundtrip(string)).to eq(string)
  61. }
  62. end
  63. it 'should stringify a non-string operand' do
  64. expect(@filter.to_xpath_roundtrip(nil)).to eq('')
  65. expect(@filter.to_xpath_roundtrip(1)).to eq('1')
  66. end
  67. end
  68. describe 'to_uri' do
  69. before do
  70. @agent = Agents::InterpolatableAgent.new(name: "test", options: { 'foo' => '{% assign u = s | to_uri %}{{ u.path }}' })
  71. @agent.interpolation_context['s'] = 'http://example.com/dir/1?q=test'
  72. end
  73. it 'should parse an absolute URI' do
  74. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  75. end
  76. it 'should parse an absolute URI with a base URI specified' do
  77. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  78. end
  79. it 'should parse a relative URI with a base URI specified' do
  80. expect(@filter.to_uri('foo/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/foo/index.html'))
  81. end
  82. it 'should parse an absolute URI with a base URI specified' do
  83. expect(@filter.to_uri('http://example.net/index.html', 'http://example.com/dir/1')).to eq(URI('http://example.net/index.html'))
  84. end
  85. it 'should stringify a non-string operand' do
  86. expect(@filter.to_uri(123, 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/123'))
  87. end
  88. it 'should normalize a URL' do
  89. expect(@filter.to_uri('a[]', 'http://example.com/dir/1')).to eq(URI('http://example.com/dir/a%5B%5D'))
  90. end
  91. it 'should return a URI value in interpolation' do
  92. expect(@agent.interpolated['foo']).to eq('/dir/1')
  93. end
  94. it 'should return a URI value resolved against a base URI in interpolation' do
  95. @agent.options['foo'] = '{% assign u = s | to_uri:"http://example.com/dir/1" %}{{ u.path }}'
  96. @agent.interpolation_context['s'] = 'foo/index.html'
  97. expect(@agent.interpolated['foo']).to eq('/dir/foo/index.html')
  98. end
  99. it 'should normalize a URI value if an empty base URI is given' do
  100. @agent.options['foo'] = '{{ u | to_uri: b }}'
  101. @agent.interpolation_context['u'] = "\u{3042}"
  102. @agent.interpolation_context['b'] = ""
  103. expect(@agent.interpolated['foo']).to eq('%E3%81%82')
  104. @agent.interpolation_context['b'] = nil
  105. expect(@agent.interpolated['foo']).to eq('%E3%81%82')
  106. end
  107. end
  108. describe 'uri_expand' do
  109. before do
  110. stub_request(:head, 'https://t.co.x/aaaa').
  111. to_return(status: 301, headers: { Location: 'https://bit.ly.x/bbbb' })
  112. stub_request(:head, 'https://bit.ly.x/bbbb').
  113. to_return(status: 301, headers: { Location: 'http://tinyurl.com.x/cccc' })
  114. stub_request(:head, 'http://tinyurl.com.x/cccc').
  115. to_return(status: 301, headers: { Location: 'http://www.example.com/welcome' })
  116. stub_request(:head, 'http://www.example.com/welcome').
  117. to_return(status: 200)
  118. (1..5).each do |i|
  119. stub_request(:head, "http://2many.x/#{i}").
  120. to_return(status: 301, headers: { Location: "http://2many.x/#{i+1}" })
  121. end
  122. stub_request(:head, 'http://2many.x/6').
  123. to_return(status: 301, headers: { 'Content-Length' => '5' })
  124. end
  125. it 'should handle inaccessible URIs' do
  126. expect(@filter.uri_expand(nil)).to eq('')
  127. expect(@filter.uri_expand('')).to eq('')
  128. expect(@filter.uri_expand(5)).to eq('5')
  129. expect(@filter.uri_expand([])).to eq('%5B%5D')
  130. expect(@filter.uri_expand({})).to eq('%7B%7D')
  131. expect(@filter.uri_expand(URI('/'))).to eq('/')
  132. expect(@filter.uri_expand(URI('http:google.com'))).to eq('http:google.com')
  133. expect(@filter.uri_expand(URI('http:/google.com'))).to eq('http:/google.com')
  134. expect(@filter.uri_expand(URI('ftp://ftp.freebsd.org/pub/FreeBSD/README.TXT'))).to eq('ftp://ftp.freebsd.org/pub/FreeBSD/README.TXT')
  135. end
  136. it 'should follow redirects' do
  137. expect(@filter.uri_expand('https://t.co.x/aaaa')).to eq('http://www.example.com/welcome')
  138. end
  139. it 'should respect the limit for the number of redirects' do
  140. expect(@filter.uri_expand('http://2many.x/1')).to eq('http://2many.x/1')
  141. expect(@filter.uri_expand('http://2many.x/1', 6)).to eq('http://2many.x/6')
  142. end
  143. it 'should detect a redirect loop' do
  144. stub_request(:head, 'http://bad.x/aaaa').
  145. to_return(status: 301, headers: { Location: 'http://bad.x/bbbb' })
  146. stub_request(:head, 'http://bad.x/bbbb').
  147. to_return(status: 301, headers: { Location: 'http://bad.x/aaaa' })
  148. expect(@filter.uri_expand('http://bad.x/aaaa')).to eq('http://bad.x/aaaa')
  149. end
  150. it 'should be able to handle an FTP URL' do
  151. stub_request(:head, 'http://downloads.x/aaaa').
  152. to_return(status: 301, headers: { Location: 'http://downloads.x/download?file=aaaa.zip' })
  153. stub_request(:head, 'http://downloads.x/download').
  154. with(query: { file: 'aaaa.zip' }).
  155. to_return(status: 301, headers: { Location: 'ftp://downloads.x/pub/aaaa.zip' })
  156. expect(@filter.uri_expand('http://downloads.x/aaaa')).to eq('ftp://downloads.x/pub/aaaa.zip')
  157. end
  158. describe 'used in interpolation' do
  159. before do
  160. @agent = Agents::InterpolatableAgent.new(name: "test")
  161. end
  162. it 'should follow redirects' do
  163. @agent.interpolation_context['short_url'] = 'https://t.co.x/aaaa'
  164. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  165. expect(@agent.interpolated['long_url']).to eq('http://www.example.com/welcome')
  166. end
  167. it 'should respect the limit for the number of redirects' do
  168. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  169. @agent.options['long_url'] = '{{ short_url | uri_expand }}'
  170. expect(@agent.interpolated['long_url']).to eq('http://2many.x/1')
  171. @agent.interpolation_context['short_url'] = 'http://2many.x/1'
  172. @agent.options['long_url'] = '{{ short_url | uri_expand:6 }}'
  173. expect(@agent.interpolated['long_url']).to eq('http://2many.x/6')
  174. end
  175. end
  176. end
  177. describe 'regex_replace_first' do
  178. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  179. it 'should replace the first occurrence of a string using regex' do
  180. agent.interpolation_context['something'] = 'foobar foobar'
  181. agent.options['cleaned'] = '{{ something | regex_replace_first: "\S+bar", "foobaz" }}'
  182. expect(agent.interpolated['cleaned']).to eq('foobaz foobar')
  183. end
  184. it 'should support escaped characters' do
  185. agent.interpolation_context['something'] = "foo\\1\n\nfoo\\bar\n\nfoo\\baz"
  186. agent.options['test'] = "{{ something | regex_replace_first: '\\\\(\\w{2,})', '\\1\\\\' | regex_replace_first: '\\n+', '\\n' }}"
  187. expect(agent.interpolated['test']).to eq("foo\\1\nfoobar\\\n\nfoo\\baz")
  188. end
  189. end
  190. describe 'regex_replace' do
  191. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  192. it 'should replace the all occurrences of a string using regex' do
  193. agent.interpolation_context['something'] = 'foobar foobar'
  194. agent.options['cleaned'] = '{{ something | regex_replace: "\S+bar", "foobaz" }}'
  195. expect(agent.interpolated['cleaned']).to eq('foobaz foobaz')
  196. end
  197. it 'should support escaped characters' do
  198. agent.interpolation_context['something'] = "foo\\1\n\nfoo\\bar\n\nfoo\\baz"
  199. agent.options['test'] = "{{ something | regex_replace: '\\\\(\\w{2,})', '\\1\\\\' | regex_replace: '\\n+', '\\n' }}"
  200. expect(agent.interpolated['test']).to eq("foo\\1\nfoobar\\\nfoobaz\\")
  201. end
  202. end
  203. describe 'regex_replace_first block' do
  204. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  205. it 'should replace the first occurrence of a string using regex' do
  206. agent.interpolation_context['something'] = 'foobar zoobar'
  207. agent.options['cleaned'] = '{% regex_replace_first "(?<word>\S+)(?<suffix>bar)" in %}{{ something }}{% with %}{{ word | upcase }}{{ suffix }}{% endregex_replace_first %}'
  208. expect(agent.interpolated['cleaned']).to eq('FOObar zoobar')
  209. end
  210. it 'should be able to take a pattern in a variable' do
  211. agent.interpolation_context['something'] = 'foobar zoobar'
  212. agent.interpolation_context['pattern'] = "(?<word>\\S+)(?<suffix>bar)"
  213. agent.options['cleaned'] = '{% regex_replace_first pattern in %}{{ something }}{% with %}{{ word | upcase }}{{ suffix }}{% endregex_replace_first %}'
  214. expect(agent.interpolated['cleaned']).to eq('FOObar zoobar')
  215. end
  216. it 'should define a variable named "match" in a "with" block' do
  217. agent.interpolation_context['something'] = 'foobar zoobar'
  218. agent.interpolation_context['pattern'] = "(?<word>\\S+)(?<suffix>bar)"
  219. agent.options['cleaned'] = '{% regex_replace_first pattern in %}{{ something }}{% with %}{{ match.word | upcase }}{{ match["suffix"] }}{% endregex_replace_first %}'
  220. expect(agent.interpolated['cleaned']).to eq('FOObar zoobar')
  221. end
  222. end
  223. describe 'regex_replace block' do
  224. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  225. it 'should replace the all occurrences of a string using regex' do
  226. agent.interpolation_context['something'] = 'foobar zoobar'
  227. agent.options['cleaned'] = '{% regex_replace "(?<word>\S+)(?<suffix>bar)" in %}{{ something }}{% with %}{{ word | upcase }}{{ suffix }}{% endregex_replace %}'
  228. expect(agent.interpolated['cleaned']).to eq('FOObar ZOObar')
  229. end
  230. end
  231. context 'as_object' do
  232. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  233. it 'returns an array that was splitted in liquid tags' do
  234. agent.interpolation_context['something'] = 'test,string,abc'
  235. agent.options['array'] = "{{something | split: ',' | as_object}}"
  236. expect(agent.interpolated['array']).to eq(['test', 'string', 'abc'])
  237. end
  238. it 'returns an object that was not modified in liquid' do
  239. agent.interpolation_context['something'] = {'nested' => {'abc' => 'test'}}
  240. agent.options['object'] = "{{something.nested | as_object}}"
  241. expect(agent.interpolated['object']).to eq({"abc" => 'test'})
  242. end
  243. context 'as_json' do
  244. def ensure_safety(obj)
  245. JSON.parse(JSON.dump(obj))
  246. end
  247. it 'it converts "complex" objects' do
  248. agent.interpolation_context['something'] = {'nested' => Service.new}
  249. agent.options['object'] = "{{something | as_object}}"
  250. expect(agent.interpolated['object']).to eq({'nested'=> ensure_safety(Service.new.as_json)})
  251. end
  252. it 'works with AgentDrops' do
  253. agent.interpolation_context['something'] = agent
  254. agent.options['object'] = "{{something | as_object}}"
  255. expect(agent.interpolated['object']).to eq(ensure_safety(agent.to_liquid.as_json.stringify_keys))
  256. end
  257. it 'works with EventDrops' do
  258. event = Event.new(payload: {some: 'payload'}, agent: agent, created_at: Time.now)
  259. agent.interpolation_context['something'] = event
  260. agent.options['object'] = "{{something | as_object}}"
  261. expect(agent.interpolated['object']).to eq(ensure_safety(event.to_liquid.as_json.stringify_keys))
  262. end
  263. it 'works with MatchDataDrops' do
  264. match = "test string".match(/\A(?<word>\w+)\s(.+?)\z/)
  265. agent.interpolation_context['something'] = match
  266. agent.options['object'] = "{{something | as_object}}"
  267. expect(agent.interpolated['object']).to eq(ensure_safety(match.to_liquid.as_json.stringify_keys))
  268. end
  269. it 'works with URIDrops' do
  270. uri = URI.parse("https://google.com?q=test")
  271. agent.interpolation_context['something'] = uri
  272. agent.options['object'] = "{{something | as_object}}"
  273. expect(agent.interpolated['object']).to eq(ensure_safety(uri.to_liquid.as_json.stringify_keys))
  274. end
  275. end
  276. end
  277. describe 'rebase_hrefs' do
  278. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  279. let(:fragment) { <<HTML }
  280. <ul>
  281. <li>
  282. <a href="downloads/file1"><img src="/images/iconA.png" srcset="/images/iconA.png 1x, /images/iconA@2x.png 2x">file1</a>
  283. </li>
  284. <li>
  285. <a href="downloads/file2"><img src="/images/iconA.png" srcset="/images/iconA.png 1x, /images/iconA@2x.png 2x">file2</a>
  286. </li>
  287. <li>
  288. <a href="downloads/file3"><img src="/images/iconB.png" srcset="/images/iconB.png 1x, /images/iconB@2x.png 2x">file3</a>
  289. </li>
  290. </ul>
  291. HTML
  292. let(:replaced_fragment) { <<HTML }
  293. <ul>
  294. <li>
  295. <a href="http://example.com/support/downloads/file1"><img src="http://example.com/images/iconA.png" srcset="http://example.com/images/iconA.png 1x, http://example.com/images/iconA@2x.png 2x">file1</a>
  296. </li>
  297. <li>
  298. <a href="http://example.com/support/downloads/file2"><img src="http://example.com/images/iconA.png" srcset="http://example.com/images/iconA.png 1x, http://example.com/images/iconA@2x.png 2x">file2</a>
  299. </li>
  300. <li>
  301. <a href="http://example.com/support/downloads/file3"><img src="http://example.com/images/iconB.png" srcset="http://example.com/images/iconB.png 1x, http://example.com/images/iconB@2x.png 2x">file3</a>
  302. </li>
  303. </ul>
  304. HTML
  305. it 'rebases relative URLs in a fragment' do
  306. agent.interpolation_context['content'] = fragment
  307. agent.options['template'] = "{{ content | rebase_hrefs: 'http://example.com/support/files.html' }}"
  308. expect(agent.interpolated['template']).to eq(replaced_fragment)
  309. end
  310. end
  311. describe 'digest filters' do
  312. let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
  313. it 'computes digest values from string input' do
  314. agent.interpolation_context['value'] = 'Huginn'
  315. agent.interpolation_context['key'] = 'Muninn'
  316. agent.options['template'] = "{{ value | md5 }}"
  317. expect(agent.interpolated['template']).to eq('5fca9fe120027bc87fa9923cc926f8fe')
  318. agent.options['template'] = "{{ value | sha1 }}"
  319. expect(agent.interpolated['template']).to eq('647d81f6dae6ff474cdcef3e9b74f038206af680')
  320. agent.options['template'] = "{{ value | sha256 }}"
  321. expect(agent.interpolated['template']).to eq('62c6099ec14502176974aadf0991525f50332ba552500556fea583ffdf0ba076')
  322. agent.options['template'] = "{{ value | hmac_sha1: key }}"
  323. expect(agent.interpolated['template']).to eq('9bd7cdebac134e06ba87258c28d2deea431407ac')
  324. agent.options['template'] = "{{ value | hmac_sha256: key }}"
  325. expect(agent.interpolated['template']).to eq('38b98bc2625a8cac33369f6204e784482be5e172b242699406270856a841d1ec')
  326. end
  327. end
  328. describe 'group_by' do
  329. let(:events) do
  330. [
  331. { "date" => "2019-07-30", "type" => "Snap" },
  332. { "date" => "2019-07-30", "type" => "Crackle" },
  333. { "date" => "2019-07-29", "type" => "Pop" },
  334. { "date" => "2019-07-29", "type" => "Bam" },
  335. { "date" => "2019-07-29", "type" => "Pow" },
  336. ]
  337. end
  338. it "should group an enumerable by the given attribute" do
  339. expect(@filter.group_by(events, "date")).to eq(
  340. [
  341. {
  342. "name" => "2019-07-30", "items" => [
  343. { "date" => "2019-07-30", "type" => "Snap" },
  344. { "date" => "2019-07-30", "type" => "Crackle" }
  345. ]
  346. },
  347. {
  348. "name" => "2019-07-29", "items" => [
  349. { "date" => "2019-07-29", "type" => "Pop" },
  350. { "date" => "2019-07-29", "type" => "Bam" },
  351. { "date" => "2019-07-29", "type" => "Pow" }
  352. ]
  353. }
  354. ]
  355. )
  356. end
  357. it "should leave non-groupables alone" do
  358. expect(@filter.group_by("some string", "anything")).to eq("some string")
  359. end
  360. end
  361. end