triples_template_test.rb
2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require File.dirname(__FILE__) + '/../test_helper'
class TriplesTemplateTest < ActiveSupport::TestCase
def setup
@article = VirtuosoPlugin::TriplesTemplate.new
end
attr_reader :article
should 'evaluate template using query results' do
article.stubs(:plugin).returns(mock)
article.plugin.expects(:virtuoso_readonly_client).at_least_once.returns(mock)
article.plugin.virtuoso_readonly_client.expects(:query).returns([{'var' => 'Hello '}, {'var' => 'World'}])
article.template = "{% for row in results %}{{row.var}}{% endfor %}"
assert_match /Hello World/, article.template_content
end
should 'display error message when failed to execute the query' do
article.stubs(:plugin).returns(mock)
article.plugin.expects(:virtuoso_readonly_client).at_least_once.returns(mock)
article.plugin.virtuoso_readonly_client.expects(:query).raises(RuntimeError.new)
article.template = "{% for row in results %}{{row.var}}{% endfor %}"
assert_equal "Failed to process the template", article.template_content
end
should 'transform css into inline stylesheet' do
article.stubs(:plugin).returns(mock)
article.plugin.expects(:virtuoso_readonly_client).at_least_once.returns(mock)
article.plugin.virtuoso_readonly_client.expects(:query).returns([{'var' => 'Hello '}, {'var' => 'World'}])
article.template = "{% for row in results %}<p>{{row.var}}</p>{% endfor %}"
article.stylesheet = "p {color: red}"
content = article.template_content
assert_match /<p style="color:red">Hello <\/p>/, content
assert_match /<p style="color:red">World<\/p>/, content
end
should 'do not allow js injection' do
article.stubs(:plugin).returns(mock)
article.plugin.expects(:virtuoso_readonly_client).at_least_once.returns(mock)
article.plugin.virtuoso_readonly_client.expects(:query).returns([{'var' => RDF::Literal.new('<script>alert("hello");</script>')}])
article.template = "{% for row in results %}{{row.var}}{% endfor %}"
assert_no_match /<script>/, article.template_content
end
end