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
1=== modified file 'license_protected_downloads/tests/__init__.py'
2--- license_protected_downloads/tests/__init__.py 2012-10-16 07:09:04 +0000
3+++ license_protected_downloads/tests/__init__.py 2012-11-08 09:45:23 +0000
4@@ -2,7 +2,10 @@
5 from license_protected_downloads.tests.test_models import LicenseTestCase
6 from license_protected_downloads.tests.test_pep8 import TestPep8
7 from license_protected_downloads.tests.test_pyflakes import TestPyflakes
8-from license_protected_downloads.tests.test_views import ViewTests
9+from license_protected_downloads.tests.test_views import (
10+ HowtoViewTests,
11+ ViewTests,
12+ )
13 from license_protected_downloads.tests.test_openid_auth import TestOpenIDAuth
14 from license_protected_downloads.tests.test_custom_commands \
15 import SetsuperuserCommandTest
16@@ -13,6 +16,7 @@
17 __test__ = {
18 'LicenseTestCase': LicenseTestCase,
19 'ViewTests': ViewTests,
20+ 'HowtoViewTests': HowtoViewTests,
21 'BuildInfoTests': BuildInfoTests,
22 'TestPep8': TestPep8,
23 'TestPyflakes': TestPyflakes,
24
25=== modified file 'license_protected_downloads/tests/test_views.py'
26--- license_protected_downloads/tests/test_views.py 2012-10-10 18:59:02 +0000
27+++ license_protected_downloads/tests/test_views.py 2012-11-08 09:45:23 +0000
28@@ -6,6 +6,7 @@
29 import os
30 import unittest
31 import urlparse
32+import shutil
33 import tempfile
34
35 from license_protected_downloads import bzr_version
36@@ -21,6 +22,41 @@
37 TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")
38
39
40+class temporary_directory(object):
41+ """Creates a context manager for a temporary directory."""
42+
43+ def __enter__(self):
44+ self.root = tempfile.mkdtemp()
45+ return self
46+
47+ def __exit__(self, *args):
48+ shutil.rmtree(self.root)
49+
50+ def make_file(self, name, data=None, with_buildinfo=False):
51+ """Creates a file in this temporary directory."""
52+ full_path = os.path.join(self.root, name)
53+ dir_name = os.path.dirname(full_path)
54+ try:
55+ os.makedirs(dir_name)
56+ except os.error:
57+ pass
58+ if with_buildinfo:
59+ buildinfo_name = os.path.join(dir_name, 'BUILD-INFO.txt')
60+ base_name = os.path.basename(full_path)
61+ with open(buildinfo_name, 'w') as buildinfo_file:
62+ buildinfo_file.write(
63+ 'Format-Version: 0.1\n\n'
64+ 'Files-Pattern: %s\n'
65+ 'License-Type: open\n' % base_name)
66+ target = open(full_path, "w")
67+ if data is None:
68+ return target
69+ else:
70+ target.write(data)
71+ target.close()
72+ return full_path
73+
74+
75 class ViewTests(TestCase):
76 def setUp(self):
77 self.client = Client()
78@@ -637,5 +673,63 @@
79 fname = os.path.join(TESTSERVER_ROOT, "../file")
80 self.assertFalse(is_same_parent_dir(TESTSERVER_ROOT, fname))
81
82+
83+class HowtoViewTests(TestCase):
84+ def setUp(self):
85+ self.client = Client()
86+ self.old_served_paths = settings.SERVED_PATHS
87+ settings.SERVED_PATHS = [os.path.join(THIS_DIRECTORY,
88+ "testserver_root")]
89+
90+ def tearDown(self):
91+ settings.SERVED_PATHS = self.old_served_paths
92+
93+ def test_no_howtos(self):
94+ with temporary_directory() as serve_root:
95+ settings.SERVED_PATHS = [serve_root.root]
96+ serve_root.make_file(
97+ "build/9/build.tar.bz2", with_buildinfo=True)
98+ response = self.client.get('/build/9/')
99+ self.assertEqual(response.status_code, 200)
100+ self.assertContains(response, 'build.tar.bz2')
101+
102+ def test_howtos_without_license(self):
103+ with temporary_directory() as serve_root:
104+ settings.SERVED_PATHS = [serve_root.root]
105+ serve_root.make_file(
106+ "build/9/build.tar.bz2", with_buildinfo=True)
107+ serve_root.make_file(
108+ "build/9/howto/HOWTO_test.txt", data=".h1 HowTo Test")
109+ response = self.client.get('/build/9/')
110+ self.assertEqual(response.status_code, 200)
111+ self.assertContains(response, 'build.tar.bz2')
112+
113+ def test_howtos_with_license_in_buildinfo(self):
114+ with temporary_directory() as serve_root:
115+ settings.SERVED_PATHS = [serve_root.root]
116+ serve_root.make_file(
117+ "build/9/build.tar.bz2", with_buildinfo=True)
118+ serve_root.make_file(
119+ "build/9/howto/HOWTO_test.txt", data=".h1 HowTo Test",
120+ with_buildinfo=True)
121+ response = self.client.get('/build/9/')
122+ self.assertEqual(response.status_code, 200)
123+ self.assertContains(response, 'howto')
124+
125+ def test_howtos_with_license_in_openeula(self):
126+ with temporary_directory() as serve_root:
127+ settings.SERVED_PATHS = [serve_root.root]
128+ serve_root.make_file(
129+ "build/9/build.tar.bz2", with_buildinfo=True)
130+ serve_root.make_file(
131+ "build/9/howto/HOWTO_test.txt", data=".h1 HowTo Test",
132+ with_buildinfo=False)
133+ serve_root.make_file(
134+ "build/9/howto/OPEN-EULA.txt", with_buildinfo=False)
135+ response = self.client.get('/build/9/')
136+ self.assertEqual(response.status_code, 200)
137+ self.assertContains(response, 'howto')
138+
139+
140 if __name__ == '__main__':
141 unittest.main()
142
143=== modified file 'license_protected_downloads/views.py'
144--- license_protected_downloads/views.py 2012-10-25 13:04:34 +0000
145+++ license_protected_downloads/views.py 2012-11-08 09:45:23 +0000
146@@ -215,9 +215,13 @@
147 def is_protected(path):
148 build_info = None
149 max_index = 1
150- buildinfo_path = os.path.join(os.path.dirname(path), "BUILD-INFO.txt")
151- open_eula_path = os.path.join(os.path.dirname(path), "OPEN-EULA.txt")
152- eula_path = os.path.join(os.path.dirname(path), "EULA.txt")
153+ base_path = path
154+ if not os.path.isdir(base_path):
155+ base_path = os.path.dirname(base_path)
156+
157+ buildinfo_path = os.path.join(base_path, "BUILD-INFO.txt")
158+ open_eula_path = os.path.join(base_path, "OPEN-EULA.txt")
159+ eula_path = os.path.join(base_path, "EULA.txt")
160
161 if os.path.isfile(buildinfo_path):
162 try:
163@@ -255,7 +259,7 @@
164 openid_teams = False
165 with open(license_file, "r") as infile:
166 license_text = infile.read()
167- elif _check_special_eula(os.path.dirname(path) + "/*"):
168+ elif _check_special_eula(base_path + "/*"):
169 return "OPEN"
170 else:
171 return []

Subscribers

People subscribed via source and target branches