-
Notifications
You must be signed in to change notification settings - Fork 69
/
test_flask_dropzone.py
192 lines (151 loc) · 7.48 KB
/
test_flask_dropzone.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import unittest
from flask import Flask, render_template_string, current_app, url_for
from flask_dropzone import Dropzone, allowed_file_extensions, _Dropzone, get_url
from flask_wtf import CSRFProtect
class DropzoneTestCase(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.testing = True
self.app.secret_key = 'for test'
dropzone = Dropzone(self.app) # noqa
csrf = CSRFProtect(self.app) # noqa
self.dropzone = _Dropzone
@self.app.route('/upload')
def upload():
pass
@self.app.route('/')
def index():
return render_template_string('''
{{ dropzone.load_css() }}\n{{ dropzone.create(action='upload') }}
{{ dropzone.load_js() }}\n{{ dropzone.config() }}''')
self.context = self.app.test_request_context()
self.context.push()
self.client = self.app.test_client()
def tearDown(self):
self.context.pop()
def test_extension_init(self):
self.assertIn('dropzone', current_app.extensions)
def test_load_css(self):
rv = self.dropzone.load_css()
self.assertIn('dropzone.min.css', rv)
rv = self.dropzone.load_css(version='5.1.0')
self.assertIn('dropzone.min.css', rv)
self.assertIn('5.1.0', rv)
def test_load_js(self):
rv = self.dropzone.load_js()
self.assertIn('dropzone.min.js', rv)
rv = self.dropzone.load_js(version='5.1.0')
self.assertIn('dropzone.min.js', rv)
self.assertIn('5.1.0', rv)
def test_local_resources(self):
current_app.config['DROPZONE_SERVE_LOCAL'] = True
css_response = self.client.get('/dropzone/static/dropzone.min.css')
js_response = self.client.get('/dropzone/static/dropzone.min.js')
self.assertNotEqual(css_response.status_code, 404)
self.assertNotEqual(js_response.status_code, 404)
css_rv = self.dropzone.load_css()
js_rv = self.dropzone.load_js()
self.assertIn('/dropzone/static/dropzone.min.css', css_rv)
self.assertIn('/dropzone/static/dropzone.min.js', js_rv)
self.assertNotIn('https://cdn.jsdelivr.net/npm/dropzone@', css_rv)
self.assertNotIn('https://cdn.jsdelivr.net/npm/dropzone@', js_rv)
def test_config_dropzone(self):
current_app.config['DROPZONE_ALLOWED_FILE_TYPE'] = 'image'
current_app.config['DROPZONE_MAX_FILE_SIZE'] = 5
current_app.config['DROPZONE_MAX_FILES'] = 40
current_app.config['DROPZONE_INPUT_NAME'] = 'test'
current_app.config['DROPZONE_ALLOWED_FILE_TYPE'] = 'image'
current_app.config['DROPZONE_DEFAULT_MESSAGE'] = 'Drop file here'
current_app.config['DROPZONE_REDIRECT_VIEW'] = 'index'
current_app.config['DROPZONE_TIMEOUT'] = 10000
rv = self.dropzone.config()
self.assertIn('Dropzone.options.myDropzone', rv)
self.assertIn('paramName: "test"', rv)
self.assertIn('maxFilesize: 5', rv)
self.assertIn('maxFiles: 40,', rv)
self.assertIn('acceptedFiles: "%s"' % allowed_file_extensions['image'], rv)
self.assertIn('dictDefaultMessage: `Drop file here`', rv)
self.assertIn('this.on("queuecomplete", function(file) {', rv)
self.assertIn('window.location = "/";', rv)
self.assertIn('timeout: 10000,', rv)
rv = self.dropzone.config(redirect_url='/redirect')
self.assertIn('this.on("queuecomplete", function(file) {', rv)
self.assertIn('window.location = "/redirect";', rv)
current_app.config['DROPZONE_TIMEOUT'] = None
rv = self.dropzone.config()
self.assertNotIn('timeout:', rv)
def test_config_overwrite(self):
rv = self.dropzone.config(input_name='test', max_file_size=12, max_files=60, allowed_file_type='image',
default_message='Drop file here', redirect_view='index', timeout=10000)
self.assertIn('Dropzone.options.myDropzone', rv)
self.assertIn('paramName: "test"', rv)
self.assertIn('maxFilesize: 12', rv)
self.assertIn('maxFiles: 60,', rv)
self.assertIn('acceptedFiles: "%s"' % allowed_file_extensions['image'], rv)
self.assertIn('dictDefaultMessage: `Drop file here`', rv)
self.assertIn('this.on("queuecomplete", function(file) {', rv)
self.assertIn('window.location = "/";', rv)
self.assertIn('timeout: 10000,', rv)
def test_create_dropzone(self):
rv = self.dropzone.create(action=url_for('upload'))
self.assertIn('<form action="/upload" method="post" class="dropzone" id="myDropzone"', rv)
def test_csrf_field(self):
rv = self.dropzone.config()
self.assertNotIn('X-CSRF-Token', rv)
rv = self.dropzone.config(enable_csrf=True)
self.assertIn('X-CSRF-Token', rv)
current_app.config['DROPZONE_ENABLE_CSRF'] = True
rv = self.dropzone.config()
self.assertIn('X-CSRF-Token', rv)
def test_style_dropzone(self):
rv = self.dropzone.style('width: 500px')
self.assertIn('.dropzone{width: 500px}', rv)
rv = self.dropzone.style('width: 500px', id='myDropzone')
self.assertIn('.dropzone#myDropzone{width: 500px}', rv)
def test_render_template(self):
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertIn('https://cdn.jsdelivr.net/npm/dropzone@', data)
self.assertIn('dropzone.min.js', data)
self.assertIn('dropzone.min.css', data)
self.assertIn('Dropzone.options.myDropzone', data)
self.assertIn('<form action="/upload" method="post" class="dropzone" id="myDropzone"', data)
def test_get_url(self):
url1 = get_url('upload')
url2 = get_url('/upload')
self.assertEqual(url1, url2)
self.assertEqual(get_url(''), None)
def test_click_upload(self):
current_app.config['DROPZONE_UPLOAD_ON_CLICK'] = True
rv = self.dropzone.config()
self.assertIn('dz.processQueue();', rv)
self.assertIn('autoProcessQueue: false,', rv)
def test_in_form(self):
current_app.config['DROPZONE_IN_FORM'] = True
current_app.config['DROPZONE_UPLOAD_ON_CLICK'] = True
current_app.config['DROPZONE_UPLOAD_BTN_ID'] = 'submit'
rv = self.dropzone.create(action=url_for('upload'))
self.assertEqual(rv, '<div class="dropzone" id="myDropzone"></div>')
rv = self.dropzone.config()
self.assertIn('dz.processQueue();', rv)
self.assertIn('e.preventDefault();', rv)
self.assertIn('autoProcessQueue: false,', rv)
self.assertIn('document.getElementById("submit").click();', rv)
def test_custom_js(self):
rv = self.dropzone.config(custom_init='dz = this;')
self.assertIn('dz = this;', rv)
rv = self.dropzone.config(custom_init='dz = this')
self.assertIn('dz = this;', rv)
rv = self.dropzone.config(custom_options='foo = true,')
self.assertIn('foo = true,', rv)
rv = self.dropzone.config(custom_options='foo = true')
self.assertIn('foo = true,', rv)
def test_custom_id(self):
rv = self.dropzone.create(action=url_for('upload'))
self.assertIn('id="myDropzone"', rv)
rv = self.dropzone.config()
self.assertIn('Dropzone.options.myDropzone', rv)
rv = self.dropzone.create(action=url_for('upload'), id='hello')
self.assertIn('id="hello"', rv)
rv = self.dropzone.config(id='hello')
self.assertIn('Dropzone.options.hello', rv)