forked from mozilla/zamboni
/
test_views.py
591 lines (478 loc) · 20.6 KB
/
test_views.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# coding=utf-8
import json
import os
import shutil
import urlparse
from django.conf import settings
from django.core.cache import cache
from django.utils.encoding import iri_to_uri
from django.utils.http import http_date
from cache_nuggets.lib import Message
from mock import Mock, patch
from nose.tools import eq_
from pyquery import PyQuery as pq
from waffle.models import Switch
import amo
import amo.tests
from amo.urlresolvers import reverse
from addons.models import Addon
from files.helpers import DiffHelper, FileViewer
from files.models import File, Platform
from users.models import UserProfile
dictionary = 'apps/files/fixtures/files/dictionary-test.xpi'
unicode_filenames = 'apps/files/fixtures/files/unicode-filenames.xpi'
not_binary = 'install.js'
binary = 'dictionaries/ar.dic'
class FilesBase:
def login_as_editor(self):
assert self.client.login(username='editor@mozilla.com',
password='password')
def setUp(self):
self.addon = Addon.objects.get(pk=3615)
self.dev = self.addon.authors.all()[0]
self.regular = UserProfile.objects.get(pk=999)
self.version = self.addon.versions.latest()
self.file = self.version.all_files[0]
p = Platform.objects.filter(id__in=(amo.PLATFORM_WIN.id,
amo.PLATFORM_LINUX.id,
amo.PLATFORM_MAC.id))
self.file.update(platform=p[0])
self.files = [self.file,
File.objects.create(version=self.version,
platform=p[1],
hash='abc123',
filename='dictionary-test.xpi'),
File.objects.create(version=self.version,
platform=p[2],
hash='abc123',
filename='dictionary-test.xpi')]
self.login_as_editor()
for file_obj in self.files:
src = os.path.join(settings.ROOT, dictionary)
try:
os.makedirs(os.path.dirname(file_obj.file_path))
except OSError:
pass
shutil.copyfile(src, file_obj.file_path)
self.file_viewer = FileViewer(self.file)
# Setting this to True, so we are delaying the extraction of files,
# in the tests, the files won't be extracted.
# Most of these tests extract as needed to.
Switch.objects.get_or_create(name='delay-file-viewer', active=True)
def tearDown(self):
self.file_viewer.cleanup()
def files_redirect(self, file):
return reverse('files.redirect', args=[self.file.pk, file])
def files_serve(self, file):
return reverse('files.serve', args=[self.file.pk, file])
def test_view_access_anon(self):
self.client.logout()
self.check_urls(403)
def test_view_access_anon_view_source(self):
self.addon.update(view_source=True)
self.file_viewer.extract()
self.client.logout()
self.check_urls(200)
def test_view_access_editor(self):
self.file_viewer.extract()
self.check_urls(200)
def test_view_access_editor_view_source(self):
self.addon.update(view_source=True)
self.file_viewer.extract()
self.check_urls(200)
def test_view_access_developer(self):
self.client.logout()
assert self.client.login(username=self.dev.email, password='password')
self.file_viewer.extract()
self.check_urls(200)
def test_view_access_reviewed(self):
self.addon.update(view_source=True)
self.file_viewer.extract()
self.client.logout()
for status in amo.UNREVIEWED_STATUSES:
self.addon.update(status=status)
self.check_urls(403)
for status in amo.REVIEWED_STATUSES:
self.addon.update(status=status)
self.check_urls(200)
def test_view_access_developer_view_source(self):
self.client.logout()
assert self.client.login(username=self.dev.email, password='password')
self.addon.update(view_source=True)
self.file_viewer.extract()
self.check_urls(200)
def test_view_access_another_developer(self):
self.client.logout()
assert self.client.login(username=self.regular.email,
password='password')
self.file_viewer.extract()
self.check_urls(403)
def test_view_access_another_developer_view_source(self):
self.client.logout()
assert self.client.login(username=self.regular.email,
password='password')
self.addon.update(view_source=True)
self.file_viewer.extract()
self.check_urls(200)
def test_poll_extracted(self):
self.file_viewer.extract()
res = self.client.get(self.poll_url())
eq_(res.status_code, 200)
eq_(json.loads(res.content)['status'], True)
def test_poll_not_extracted(self):
res = self.client.get(self.poll_url())
eq_(res.status_code, 200)
eq_(json.loads(res.content)['status'], False)
def test_poll_extracted_anon(self):
self.client.logout()
res = self.client.get(self.poll_url())
eq_(res.status_code, 403)
def test_content_headers(self):
self.file_viewer.extract()
res = self.client.get(self.file_url('install.js'))
assert 'etag' in res._headers
assert 'last-modified' in res._headers
def test_content_headers_etag(self):
self.file_viewer.extract()
self.file_viewer.select('install.js')
obj = getattr(self.file_viewer, 'left', self.file_viewer)
etag = obj.selected.get('md5')
res = self.client.get(self.file_url('install.js'),
HTTP_IF_NONE_MATCH=etag)
eq_(res.status_code, 304)
def test_content_headers_if_modified(self):
self.file_viewer.extract()
self.file_viewer.select('install.js')
obj = getattr(self.file_viewer, 'left', self.file_viewer)
date = http_date(obj.selected.get('modified'))
res = self.client.get(self.file_url('install.js'),
HTTP_IF_MODIFIED_SINCE=date)
eq_(res.status_code, 304)
def test_file_header(self):
self.file_viewer.extract()
res = self.client.get(self.file_url(not_binary))
url = res.context['file_link']['url']
eq_(url, reverse('editors.review', args=[self.addon.slug]))
def test_file_header_anon(self):
self.client.logout()
self.file_viewer.extract()
self.addon.update(view_source=True)
res = self.client.get(self.file_url(not_binary))
url = res.context['file_link']['url']
eq_(url, reverse('addons.detail', args=[self.addon.pk]))
def test_content_no_file(self):
self.file_viewer.extract()
res = self.client.get(self.file_url())
doc = pq(res.content)
eq_(len(doc('#content')), 0)
def test_no_files(self):
res = self.client.get(self.file_url())
eq_(res.status_code, 200)
assert 'files' not in res.context
@patch('waffle.switch_is_active')
def test_no_files_switch(self, switch_is_active):
switch_is_active.return_value = False
# By setting the switch to False, we are not delaying the file
# extraction. The files will be extracted and there will be
# files in context.
res = self.client.get(self.file_url())
eq_(res.status_code, 200)
assert 'files' in res.context
def test_files(self):
self.file_viewer.extract()
res = self.client.get(self.file_url())
eq_(res.status_code, 200)
assert 'files' in res.context
def test_files_anon(self):
self.client.logout()
res = self.client.get(self.file_url())
eq_(res.status_code, 403)
def test_files_file(self):
self.file_viewer.extract()
res = self.client.get(self.file_url(not_binary))
eq_(res.status_code, 200)
assert 'selected' in res.context
def test_files_back_link(self):
self.file_viewer.extract()
res = self.client.get(self.file_url(not_binary))
doc = pq(res.content)
eq_(doc('#commands td:last').text(), 'Back to review')
def test_files_back_link_anon(self):
self.file_viewer.extract()
self.client.logout()
self.addon.update(view_source=True)
res = self.client.get(self.file_url(not_binary))
eq_(res.status_code, 200)
doc = pq(res.content)
eq_(doc('#commands td:last').text(), 'Back to addon')
def test_diff_redirect(self):
ids = self.files[0].id, self.files[1].id
res = self.client.post(self.file_url(),
{'left': ids[0], 'right': ids[1]})
self.assert3xx(res, reverse('files.compare', args=ids))
def test_browse_redirect(self):
ids = self.files[0].id,
res = self.client.post(self.file_url(), {'left': ids[0]})
self.assert3xx(res, reverse('files.list', args=ids))
def test_browse_404(self):
res = self.client.get('/files/browse/file/dont/exist.png', follow=True)
eq_(res.status_code, 404)
def test_invalid_redirect(self):
res = self.client.post(self.file_url(), {})
self.assert3xx(res, self.file_url())
def test_file_chooser(self):
res = self.client.get(self.file_url())
doc = pq(res.content)
left = doc('#id_left')
eq_(len(left), 1)
ver = left('optgroup')
eq_(len(ver), 1)
eq_(ver.attr('label'), self.version.version)
files = ver('option')
eq_(len(files), 2)
def test_file_chooser_coalescing(self):
res = self.client.get(self.file_url())
doc = pq(res.content)
files = doc('#id_left > optgroup > option')
eq_([f.text for f in files],
[str(self.files[0].platform),
'%s, %s' % (self.files[1].platform, self.files[2].platform)])
eq_(files.eq(0).attr('value'), str(self.files[0].id))
eq_(files.eq(1).attr('value'), str(self.files[1].id))
def test_file_chooser_disabled_coalescing(self):
self.files[1].update(status=amo.STATUS_DISABLED)
res = self.client.get(self.file_url())
doc = pq(res.content)
files = doc('#id_left > optgroup > option')
eq_(files.eq(1).attr('value'), str(self.files[2].id))
class TestFileViewer(FilesBase, amo.tests.TestCase):
fixtures = ['base/addon_3615', 'base/platforms', 'base/users']
def poll_url(self):
return reverse('files.poll', args=[self.file.pk])
def file_url(self, file=None):
args = [self.file.pk]
if file:
args.extend(['file', file])
return reverse('files.list', args=args)
def check_urls(self, status):
for url in [self.poll_url(), self.file_url()]:
eq_(self.client.get(url).status_code, status)
def add_file(self, name, contents):
dest = os.path.join(self.file_viewer.dest, name)
open(dest, 'w').write(contents)
def test_files_xss(self):
self.file_viewer.extract()
self.add_file('<script>alert("foo")', '')
res = self.client.get(self.file_url())
doc = pq(res.content)
# Note: this is text, not a DOM element, so escaped correctly.
assert '<script>alert("' in doc('#files li a').text()
def test_content_file(self):
self.file_viewer.extract()
res = self.client.get(self.file_url('install.js'))
doc = pq(res.content)
eq_(len(doc('#content')), 1)
def test_content_no_file(self):
self.file_viewer.extract()
res = self.client.get(self.file_url())
doc = pq(res.content)
eq_(len(doc('#content')), 1)
eq_(res.context['key'], 'install.rdf')
def test_content_xss(self):
self.file_viewer.extract()
for name in ['file.txt', 'file.html', 'file.htm']:
# If you are adding files, you need to clear out the memcache
# file listing.
cache.clear()
self.add_file(name, '<script>alert("foo")</script>')
res = self.client.get(self.file_url(name))
doc = pq(res.content)
# Note: this is text, not a DOM element, so escaped correctly.
assert doc('#content').text().startswith('<script')
def test_binary(self):
self.file_viewer.extract()
self.add_file('file.php', '<script>alert("foo")</script>')
res = self.client.get(self.file_url('file.php'))
eq_(res.status_code, 200)
assert self.file_viewer.get_files()['file.php']['md5'] in res.content
def test_tree_no_file(self):
self.file_viewer.extract()
res = self.client.get(self.file_url('doesnotexist.js'))
eq_(res.status_code, 404)
def test_directory(self):
self.file_viewer.extract()
res = self.client.get(self.file_url('doesnotexist.js'))
eq_(res.status_code, 404)
def test_unicode(self):
self.file_viewer.src = unicode_filenames
self.file_viewer.extract()
res = self.client.get(self.file_url(u'\u1109\u1161\u11a9'))
eq_(res.status_code, 200)
def test_serve_no_token(self):
self.file_viewer.extract()
res = self.client.get(self.files_serve(binary))
eq_(res.status_code, 403)
def test_serve_fake_token(self):
self.file_viewer.extract()
res = self.client.get(self.files_serve(binary) + '?token=aasd')
eq_(res.status_code, 403)
def test_serve_bad_token(self):
self.file_viewer.extract()
res = self.client.get(self.files_serve(binary) + '?token=a asd')
eq_(res.status_code, 403)
def test_serve_get_token(self):
self.file_viewer.extract()
res = self.client.get(self.files_redirect(binary))
eq_(res.status_code, 302)
url = res['Location']
assert url.startswith(settings.STATIC_URL)
assert urlparse.urlparse(url).query.startswith('token=')
def test_memcache_goes_bye_bye(self):
self.file_viewer.extract()
res = self.client.get(self.files_redirect(binary))
url = res['Location'][len(settings.STATIC_URL) - 1:]
cache.clear()
res = self.client.get(url)
eq_(res.status_code, 403)
def test_bounce(self):
self.file_viewer.extract()
res = self.client.get(self.files_redirect(binary), follow=True)
eq_(res.status_code, 200)
eq_(res[settings.XSENDFILE_HEADER],
self.file_viewer.get_files().get(binary)['full'])
@patch.object(settings, 'FILE_VIEWER_SIZE_LIMIT', 5)
def test_file_size(self):
self.file_viewer.extract()
res = self.client.get(self.file_url(not_binary))
doc = pq(res.content)
assert doc('.error').text().startswith('File size is')
def test_poll_failed(self):
msg = Message('file-viewer:%s' % self.file_viewer)
msg.save('I like cheese.')
res = self.client.get(self.poll_url())
eq_(res.status_code, 200)
data = json.loads(res.content)
eq_(data['status'], False)
eq_(data['msg'], ['I like cheese.'])
def test_file_chooser_selection(self):
res = self.client.get(self.file_url())
doc = pq(res.content)
eq_(doc('#id_left option[selected]').attr('value'),
str(self.files[0].id))
eq_(len(doc('#id_right option[value][selected]')), 0)
@patch.object(amo.PLATFORM_LINUX, 'name', u'所有移动平台')
def test_file_chooser_non_ascii_platform(self):
with self.activate(locale='zh-CN'):
PLATFORM_NAME = u'所有移动平台'
f = self.files[0]
eq_(unicode(f.platform), PLATFORM_NAME)
res = self.client.get(self.file_url())
doc = pq(res.content.decode('utf-8'))
eq_(doc('#id_left option[value=%d]' % f.id).text(),
PLATFORM_NAME)
class TestDiffViewer(FilesBase, amo.tests.TestCase):
fixtures = ['base/addon_3615', 'base/platforms', 'base/users']
def setUp(self):
super(TestDiffViewer, self).setUp()
self.file_viewer = DiffHelper(self.files[0], self.files[1])
def poll_url(self):
return reverse('files.compare.poll', args=[self.files[0].pk,
self.files[1].pk])
def add_file(self, file_obj, name, contents):
dest = os.path.join(file_obj.dest, name)
open(dest, 'w').write(contents)
def file_url(self, file=None):
args = [self.files[0].pk, self.files[1].pk]
if file:
args.extend(['file', file])
return reverse('files.compare', args=args)
def check_urls(self, status):
for url in [self.poll_url(), self.file_url()]:
eq_(self.client.get(url).status_code, status)
def test_tree_no_file(self):
self.file_viewer.extract()
res = self.client.get(self.file_url('doesnotexist.js'))
eq_(res.status_code, 404)
def test_content_file(self):
self.file_viewer.extract()
res = self.client.get(self.file_url(not_binary))
doc = pq(res.content)
eq_(len(doc('pre')), 3)
def test_binary_serve_links(self):
self.file_viewer.extract()
res = self.client.get(self.file_url(binary))
doc = pq(res.content)
node = doc('#content-wrapper a')
eq_(len(node), 2)
assert node[0].text.startswith('Download ar.dic')
def test_view_both_present(self):
self.file_viewer.extract()
res = self.client.get(self.file_url(not_binary))
doc = pq(res.content)
eq_(len(doc('pre')), 3)
eq_(len(doc('#content-wrapper p')), 2)
def test_view_one_missing(self):
self.file_viewer.extract()
os.remove(os.path.join(self.file_viewer.right.dest, 'install.js'))
res = self.client.get(self.file_url(not_binary))
doc = pq(res.content)
eq_(len(doc('pre')), 3)
eq_(len(doc('#content-wrapper p')), 1)
def test_view_left_binary(self):
self.file_viewer.extract()
filename = os.path.join(self.file_viewer.left.dest, 'install.js')
open(filename, 'w').write('MZ')
res = self.client.get(self.file_url(not_binary))
assert 'This file is not viewable online' in res.content
def test_view_right_binary(self):
self.file_viewer.extract()
filename = os.path.join(self.file_viewer.right.dest, 'install.js')
open(filename, 'w').write('MZ')
assert not self.file_viewer.is_diffable()
res = self.client.get(self.file_url(not_binary))
assert 'This file is not viewable online' in res.content
def test_different_tree(self):
self.file_viewer.extract()
os.remove(os.path.join(self.file_viewer.left.dest, not_binary))
res = self.client.get(self.file_url(not_binary))
doc = pq(res.content)
eq_(doc('h4:last').text(), 'Deleted files:')
eq_(len(doc('ul.root')), 2)
def test_file_chooser_selection(self):
res = self.client.get(self.file_url())
doc = pq(res.content)
eq_(doc('#id_left option[selected]').attr('value'),
str(self.files[0].id))
eq_(doc('#id_right option[selected]').attr('value'),
str(self.files[1].id))
def test_file_chooser_selection_same_hash(self):
"""
In cases where multiple files are coalesced, the file selector may not
have an actual entry for certain files. Instead, the entry with the
identical hash should be selected.
"""
res = self.client.get(reverse('files.compare',
args=(self.files[0].id,
self.files[2].id)))
doc = pq(res.content)
eq_(doc('#id_left option[selected]').attr('value'),
str(self.files[0].id))
eq_(doc('#id_right option[selected]').attr('value'),
str(self.files[1].id))
class TestBuilderPingback(amo.tests.TestCase):
def post(self, data):
return self.client.post(reverse('amo.builder-pingback'), data)
@patch('files.tasks.repackage_jetpack')
def test_success(self, repackage_jetpack):
repackage_jetpack.delay = Mock()
r = self.post({'result': '', 'msg': '', 'filename': '',
'location': '', 'request': '',
'secret': settings.BUILDER_SECRET_KEY})
assert repackage_jetpack.called
eq_(r.status_code, 200)
def test_bad_secret(self):
r = self.post({'secret': 1})
eq_(r.status_code, 400)
def test_bad_data(self):
r = self.post({'wut': 0})
eq_(r.status_code, 400)