-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_preview.py
127 lines (106 loc) · 4.02 KB
/
test_preview.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pylons
import paste.fixture
import pylons.config as config
import ckan.logic as logic
import ckan.model as model
import ckan.tests as tests
import ckan.plugins as plugins
import ckan.lib.helpers as h
import ckanext.textpreview.plugin as previewplugin
import ckan.lib.create_test_data as create_test_data
import ckan.config.middleware as middleware
class TestTextPreview(tests.WsgiAppCase):
@classmethod
def setup_class(cls):
wsgiapp = middleware.make_app(config['global_conf'], **config)
plugins.load('text_preview')
cls.app = paste.fixture.TestApp(wsgiapp)
cls.p = previewplugin.TextPreview()
#cls.p.proxy_is_enabled = False
# create test resource
create_test_data.CreateTestData.create()
context = {
'model': model,
'session': model.Session,
'user': model.User.get('testsysadmin').name
}
cls.package = model.Package.get('annakarenina')
cls.resource = logic.get_action('resource_show')(
context, {'id': cls.package.resources[1].id})
cls.resource['url'] = pylons.config.get(
'ckan.site_url', '//localhost:5000')
logic.action.update.resource_update(context, cls.resource)
@classmethod
def teardown_class(cls):
plugins.unload('text_preview')
create_test_data.CreateTestData.delete()
def test_can_preview(self):
data_dict = {
'resource': {
'format': 'jsonp'
}
}
assert self.p.can_preview(data_dict)['can_preview']
data_dict = {
'resource': {
'format': 'json',
'on_same_domain': True
}
}
assert self.p.can_preview(data_dict)['can_preview']
data_dict = {
'resource': {
'format': 'xml',
'on_same_domain': True
}
}
assert self.p.can_preview(data_dict)['can_preview']
data_dict = {
'resource': {
'format': 'txt',
'on_same_domain': True
}
}
assert self.p.can_preview(data_dict)['can_preview']
data_dict = {
'resource': {
'format': 'foo',
'on_same_domain': True
}
}
assert not self.p.can_preview(data_dict)['can_preview']
data_dict = {
'resource': {
'format': 'json',
'on_same_domain': False
}
}
assert not self.p.can_preview(data_dict)['can_preview']
def test_js_included(self):
res_id = self.resource['id']
pack_id = self.package.name
url = '/dataset/{0}/resource/{1}/preview'.format(pack_id, res_id)
result = self.app.get(url, status='*')
assert result.status == 200, result.status
assert ((('preview_text.js' in result.body)
or ('preview_text.min.js' in result.body))), result.body
assert ((('highlight.pack.js' in result.body)
or ('highlight.pack.js' in result.body))), result.body
assert 'preload_resource' in result.body, result.body
assert 'data-module="textpreview"' in result.body, result.body
def test_css_included(self):
res_id = self.resource['id']
pack_id = self.package.name
url = '/dataset/{0}/resource/{1}/preview'.format(pack_id, res_id)
result = self.app.get(url, status='*')
assert result.status == 200, result.status
assert (('text.css' in result.body)
or ('text.min.css' in result.body)), result.body
assert (('github.css' in result.body)
or ('github.min.css' in result.body)), result.body
def test_iframe_is_shown(self):
url = h.url_for(controller='package', action='resource_read',
id=self.package.name, resource_id=self.resource['id'])
result = self.app.get(url)
assert 'data-module="data-viewer"' in result.body
assert '<iframe' in result.body