|
11 | 11 | import unittest, os, shutil, errno, sys, difflib, cgi, re, StringIO |
12 | 12 |
|
13 | 13 | from roundup.cgi import client, actions, exceptions |
14 | | -from roundup.cgi.exceptions import FormError |
| 14 | +from roundup.cgi.exceptions import FormError, NotFound |
15 | 15 | from roundup.exceptions import UsageError |
16 | 16 | from roundup.cgi.templating import HTMLItem, HTMLRequest, NoTemplate, anti_csrf_nonce |
17 | 17 | from roundup.cgi.templating import HTMLProperty, _HTMLItem |
@@ -1390,6 +1390,105 @@ def testEditCSV(self): |
1390 | 1390 | k = self.db.keyword.getnode('1') |
1391 | 1391 | self.assertEqual(k.name, u'\xe4\xf6\xfc'.encode('utf-8')) |
1392 | 1392 |
|
| 1393 | + def testserve_static_files(self): |
| 1394 | + # make a client instance |
| 1395 | + cl = self._make_client({}) |
| 1396 | + |
| 1397 | + # hijack _serve_file so I can see what is found |
| 1398 | + output = [] |
| 1399 | + def my_serve_file(a, b, c, d): |
| 1400 | + output.append((a,b,c,d)) |
| 1401 | + cl._serve_file = my_serve_file |
| 1402 | + |
| 1403 | + # check case where file is not found. |
| 1404 | + self.assertRaises(NotFound, |
| 1405 | + cl.serve_static_file,"missing.css") |
| 1406 | + |
| 1407 | + # TEMPLATES dir is searched by default. So this file exists. |
| 1408 | + # Check the returned values. |
| 1409 | + cl.serve_static_file("issue.index.html") |
| 1410 | + self.assertEquals(output[0][1], "text/html") |
| 1411 | + self.assertEquals(output[0][3], "_test_cgi_form/html/issue.index.html") |
| 1412 | + del output[0] # reset output buffer |
| 1413 | + |
| 1414 | + # stop searching TEMPLATES for the files. |
| 1415 | + cl.instance.config['STATIC_FILES'] = '-' |
| 1416 | + # previously found file should not be found |
| 1417 | + self.assertRaises(NotFound, |
| 1418 | + cl.serve_static_file,"issue.index.html") |
| 1419 | + |
| 1420 | + # explicitly allow html directory |
| 1421 | + cl.instance.config['STATIC_FILES'] = 'html -' |
| 1422 | + cl.serve_static_file("issue.index.html") |
| 1423 | + self.assertEquals(output[0][1], "text/html") |
| 1424 | + self.assertEquals(output[0][3], "_test_cgi_form/html/issue.index.html") |
| 1425 | + del output[0] # reset output buffer |
| 1426 | + |
| 1427 | + # set the list of files and do not look at the templates directory |
| 1428 | + cl.instance.config['STATIC_FILES'] = 'detectors extensions - ' |
| 1429 | + |
| 1430 | + # find file in first directory |
| 1431 | + cl.serve_static_file("messagesummary.py") |
| 1432 | + self.assertEquals(output[0][1], "text/x-python") |
| 1433 | + self.assertEquals(output[0][3], "_test_cgi_form/detectors/messagesummary.py") |
| 1434 | + del output[0] # reset output buffer |
| 1435 | + |
| 1436 | + # find file in second directory |
| 1437 | + cl.serve_static_file("README.txt") |
| 1438 | + self.assertEquals(output[0][1], "text/plain") |
| 1439 | + self.assertEquals(output[0][3], "_test_cgi_form/extensions/README.txt") |
| 1440 | + del output[0] # reset output buffer |
| 1441 | + |
| 1442 | + # make sure an embedded - ends the searching. |
| 1443 | + cl.instance.config['STATIC_FILES'] = ' detectors - extensions ' |
| 1444 | + self.assertRaises(NotFound, cl.serve_static_file, "README.txt") |
| 1445 | + |
| 1446 | + cl.instance.config['STATIC_FILES'] = ' detectors - extensions ' |
| 1447 | + self.assertRaises(NotFound, cl.serve_static_file, "issue.index.html") |
| 1448 | + |
| 1449 | + # create an empty README.txt in the first directory |
| 1450 | + f = open('_test_cgi_form/detectors/README.txt', 'a').close() |
| 1451 | + # find file now in first directory |
| 1452 | + cl.serve_static_file("README.txt") |
| 1453 | + self.assertEquals(output[0][1], "text/plain") |
| 1454 | + self.assertEquals(output[0][3], "_test_cgi_form/detectors/README.txt") |
| 1455 | + del output[0] # reset output buffer |
| 1456 | + |
| 1457 | + cl.instance.config['STATIC_FILES'] = ' detectors extensions ' |
| 1458 | + # make sure lack of trailing - allows searching TEMPLATES |
| 1459 | + cl.serve_static_file("issue.index.html") |
| 1460 | + self.assertEquals(output[0][1], "text/html") |
| 1461 | + self.assertEquals(output[0][3], "_test_cgi_form/html/issue.index.html") |
| 1462 | + del output[0] # reset output buffer |
| 1463 | + |
| 1464 | + # Make STATIC_FILES a single element. |
| 1465 | + cl.instance.config['STATIC_FILES'] = 'detectors' |
| 1466 | + # find file now in first directory |
| 1467 | + cl.serve_static_file("messagesummary.py") |
| 1468 | + self.assertEquals(output[0][1], "text/x-python") |
| 1469 | + self.assertEquals(output[0][3], "_test_cgi_form/detectors/messagesummary.py") |
| 1470 | + del output[0] # reset output buffer |
| 1471 | + |
| 1472 | + # make sure files found in subdirectory |
| 1473 | + os.mkdir('_test_cgi_form/detectors/css') |
| 1474 | + f = open('_test_cgi_form/detectors/css/README.css', 'a').close() |
| 1475 | + # use subdir in filename |
| 1476 | + cl.serve_static_file("css/README.css") |
| 1477 | + self.assertEquals(output[0][1], "text/css") |
| 1478 | + self.assertEquals(output[0][3], "_test_cgi_form/detectors/css/README.css") |
| 1479 | + del output[0] # reset output buffer |
| 1480 | + |
| 1481 | + |
| 1482 | + # use subdir in static files path |
| 1483 | + cl.instance.config['STATIC_FILES'] = 'detectors html/css' |
| 1484 | + os.mkdir('_test_cgi_form/html/css') |
| 1485 | + f = open('_test_cgi_form/html/css/README1.css', 'a').close() |
| 1486 | + cl.serve_static_file("README1.css") |
| 1487 | + self.assertEquals(output[0][1], "text/css") |
| 1488 | + self.assertEquals(output[0][3], "_test_cgi_form/html/css/README1.css") |
| 1489 | + del output[0] # reset output buffer |
| 1490 | + |
| 1491 | + |
1393 | 1492 | def testRoles(self): |
1394 | 1493 | cl = self._make_client({}) |
1395 | 1494 | self.db.user.set('1', roles='aDmin, uSer') |
|
0 commit comments