Merge lp:~danilo/linaro-license-protection/bug-1075999 into lp:~linaro-automation/linaro-license-protection/trunk

Proposed by Данило Шеган
Status: Merged
Approved by: Stevan Radaković
Approved revision: 149
Merged at revision: 148
Proposed branch: lp:~danilo/linaro-license-protection/bug-1075999
Merge into: lp:~linaro-automation/linaro-license-protection/trunk
Diff against target: 171 lines (+107/-5)
3 files modified
license_protected_downloads/tests/__init__.py (+5/-1)
license_protected_downloads/tests/test_views.py (+94/-0)
license_protected_downloads/views.py (+8/-4)
To merge this branch: bzr merge lp:~danilo/linaro-license-protection/bug-1075999
Reviewer Review Type Date Requested Status
Stevan Radaković Approve
Review via email: mp+133431@code.launchpad.net

Description of the change

When loading a directory listing which contains subdirectories without BUILD-INFO file, we'd get an exception as seen in bug 1075999.

The problem is that BuildInfo(path) looks for BUILD-INFO.txt file in
path/BUILD-INFO.txt if path is a directory, and in dirname(path)/BUILD-INFO.txt
if it's not. However, the pre-check in is_protected() only checks for
dirname(path)/BUILD-INFO.txt, and for a subdirectory without the trailing "/",
this returns the parent directory, and no BUILD-INFO.txt file is found.

I am not entirely sure of the intent of this code, so I am fixing it to work
and look for BUILD-INFO/EULA/OPEN-EULA inside the subdirectory. I've added
a few tests to ensure this works as I intended, but I plan to rework this to
be a bit saner and understandable (iow, encapsulated in the right places).

Tests
=====
To run the new tests

  ./manage.py test license_protected_downloads.HowtoViewTests

QA
==

Create a directory with a file, BUILD-INFO.txt that opens it up and an empty
subdirectory. It should not crash when you browse into it.

To post a comment you must log in.
149. By Данило Шеган

Use the same logic for finding build-info file in is_protected as used in BuildInfo constructor.

Revision history for this message
Stevan Radaković (stevanr) wrote :

Looks good, thanks for the fix.
Approve +1.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'license_protected_downloads/tests/__init__.py'
--- license_protected_downloads/tests/__init__.py 2012-10-16 07:09:04 +0000
+++ license_protected_downloads/tests/__init__.py 2012-11-08 09:45:23 +0000
@@ -2,7 +2,10 @@
2from license_protected_downloads.tests.test_models import LicenseTestCase2from license_protected_downloads.tests.test_models import LicenseTestCase
3from license_protected_downloads.tests.test_pep8 import TestPep83from license_protected_downloads.tests.test_pep8 import TestPep8
4from license_protected_downloads.tests.test_pyflakes import TestPyflakes4from license_protected_downloads.tests.test_pyflakes import TestPyflakes
5from license_protected_downloads.tests.test_views import ViewTests5from license_protected_downloads.tests.test_views import (
6 HowtoViewTests,
7 ViewTests,
8 )
6from license_protected_downloads.tests.test_openid_auth import TestOpenIDAuth9from license_protected_downloads.tests.test_openid_auth import TestOpenIDAuth
7from license_protected_downloads.tests.test_custom_commands \10from license_protected_downloads.tests.test_custom_commands \
8 import SetsuperuserCommandTest11 import SetsuperuserCommandTest
@@ -13,6 +16,7 @@
13__test__ = {16__test__ = {
14 'LicenseTestCase': LicenseTestCase,17 'LicenseTestCase': LicenseTestCase,
15 'ViewTests': ViewTests,18 'ViewTests': ViewTests,
19 'HowtoViewTests': HowtoViewTests,
16 'BuildInfoTests': BuildInfoTests,20 'BuildInfoTests': BuildInfoTests,
17 'TestPep8': TestPep8,21 'TestPep8': TestPep8,
18 'TestPyflakes': TestPyflakes,22 'TestPyflakes': TestPyflakes,
1923
=== modified file 'license_protected_downloads/tests/test_views.py'
--- license_protected_downloads/tests/test_views.py 2012-10-10 18:59:02 +0000
+++ license_protected_downloads/tests/test_views.py 2012-11-08 09:45:23 +0000
@@ -6,6 +6,7 @@
6import os6import os
7import unittest7import unittest
8import urlparse8import urlparse
9import shutil
9import tempfile10import tempfile
1011
11from license_protected_downloads import bzr_version12from license_protected_downloads import bzr_version
@@ -21,6 +22,41 @@
21TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")22TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")
2223
2324
25class temporary_directory(object):
26 """Creates a context manager for a temporary directory."""
27
28 def __enter__(self):
29 self.root = tempfile.mkdtemp()
30 return self
31
32 def __exit__(self, *args):
33 shutil.rmtree(self.root)
34
35 def make_file(self, name, data=None, with_buildinfo=False):
36 """Creates a file in this temporary directory."""
37 full_path = os.path.join(self.root, name)
38 dir_name = os.path.dirname(full_path)
39 try:
40 os.makedirs(dir_name)
41 except os.error:
42 pass
43 if with_buildinfo:
44 buildinfo_name = os.path.join(dir_name, 'BUILD-INFO.txt')
45 base_name = os.path.basename(full_path)
46 with open(buildinfo_name, 'w') as buildinfo_file:
47 buildinfo_file.write(
48 'Format-Version: 0.1\n\n'
49 'Files-Pattern: %s\n'
50 'License-Type: open\n' % base_name)
51 target = open(full_path, "w")
52 if data is None:
53 return target
54 else:
55 target.write(data)
56 target.close()
57 return full_path
58
59
24class ViewTests(TestCase):60class ViewTests(TestCase):
25 def setUp(self):61 def setUp(self):
26 self.client = Client()62 self.client = Client()
@@ -637,5 +673,63 @@
637 fname = os.path.join(TESTSERVER_ROOT, "../file")673 fname = os.path.join(TESTSERVER_ROOT, "../file")
638 self.assertFalse(is_same_parent_dir(TESTSERVER_ROOT, fname))674 self.assertFalse(is_same_parent_dir(TESTSERVER_ROOT, fname))
639675
676
677class HowtoViewTests(TestCase):
678 def setUp(self):
679 self.client = Client()
680 self.old_served_paths = settings.SERVED_PATHS
681 settings.SERVED_PATHS = [os.path.join(THIS_DIRECTORY,
682 "testserver_root")]
683
684 def tearDown(self):
685 settings.SERVED_PATHS = self.old_served_paths
686
687 def test_no_howtos(self):
688 with temporary_directory() as serve_root:
689 settings.SERVED_PATHS = [serve_root.root]
690 serve_root.make_file(
691 "build/9/build.tar.bz2", with_buildinfo=True)
692 response = self.client.get('/build/9/')
693 self.assertEqual(response.status_code, 200)
694 self.assertContains(response, 'build.tar.bz2')
695
696 def test_howtos_without_license(self):
697 with temporary_directory() as serve_root:
698 settings.SERVED_PATHS = [serve_root.root]
699 serve_root.make_file(
700 "build/9/build.tar.bz2", with_buildinfo=True)
701 serve_root.make_file(
702 "build/9/howto/HOWTO_test.txt", data=".h1 HowTo Test")
703 response = self.client.get('/build/9/')
704 self.assertEqual(response.status_code, 200)
705 self.assertContains(response, 'build.tar.bz2')
706
707 def test_howtos_with_license_in_buildinfo(self):
708 with temporary_directory() as serve_root:
709 settings.SERVED_PATHS = [serve_root.root]
710 serve_root.make_file(
711 "build/9/build.tar.bz2", with_buildinfo=True)
712 serve_root.make_file(
713 "build/9/howto/HOWTO_test.txt", data=".h1 HowTo Test",
714 with_buildinfo=True)
715 response = self.client.get('/build/9/')
716 self.assertEqual(response.status_code, 200)
717 self.assertContains(response, 'howto')
718
719 def test_howtos_with_license_in_openeula(self):
720 with temporary_directory() as serve_root:
721 settings.SERVED_PATHS = [serve_root.root]
722 serve_root.make_file(
723 "build/9/build.tar.bz2", with_buildinfo=True)
724 serve_root.make_file(
725 "build/9/howto/HOWTO_test.txt", data=".h1 HowTo Test",
726 with_buildinfo=False)
727 serve_root.make_file(
728 "build/9/howto/OPEN-EULA.txt", with_buildinfo=False)
729 response = self.client.get('/build/9/')
730 self.assertEqual(response.status_code, 200)
731 self.assertContains(response, 'howto')
732
733
640if __name__ == '__main__':734if __name__ == '__main__':
641 unittest.main()735 unittest.main()
642736
=== modified file 'license_protected_downloads/views.py'
--- license_protected_downloads/views.py 2012-10-25 13:04:34 +0000
+++ license_protected_downloads/views.py 2012-11-08 09:45:23 +0000
@@ -215,9 +215,13 @@
215def is_protected(path):215def is_protected(path):
216 build_info = None216 build_info = None
217 max_index = 1217 max_index = 1
218 buildinfo_path = os.path.join(os.path.dirname(path), "BUILD-INFO.txt")218 base_path = path
219 open_eula_path = os.path.join(os.path.dirname(path), "OPEN-EULA.txt")219 if not os.path.isdir(base_path):
220 eula_path = os.path.join(os.path.dirname(path), "EULA.txt")220 base_path = os.path.dirname(base_path)
221
222 buildinfo_path = os.path.join(base_path, "BUILD-INFO.txt")
223 open_eula_path = os.path.join(base_path, "OPEN-EULA.txt")
224 eula_path = os.path.join(base_path, "EULA.txt")
221225
222 if os.path.isfile(buildinfo_path):226 if os.path.isfile(buildinfo_path):
223 try:227 try:
@@ -255,7 +259,7 @@
255 openid_teams = False259 openid_teams = False
256 with open(license_file, "r") as infile:260 with open(license_file, "r") as infile:
257 license_text = infile.read()261 license_text = infile.read()
258 elif _check_special_eula(os.path.dirname(path) + "/*"):262 elif _check_special_eula(base_path + "/*"):
259 return "OPEN"263 return "OPEN"
260 else:264 else:
261 return []265 return []

Subscribers

People subscribed via source and target branches