3
from license_protected_downloads.views import _insert_license_into_db
5
__author__ = 'dooferlad'
10
from django.test import Client, TestCase
11
from license_protected_downloads.buildinfo import BuildInfo
12
from django.conf import settings
14
THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
15
TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")
17
class ViewTests(TestCase):
19
self.client = Client()
20
self.old_served_paths = settings.SERVED_PATHS
21
settings.SERVED_PATHS = [os.path.join(THIS_DIRECTORY,
25
settings.SERVED_PATHS = self.old_served_paths
27
def test_license_directly(self):
28
response = self.client.get('/licenses/license.html', follow=True)
29
self.assertEqual(response.status_code, 200)
30
self.assertContains(response, '/build-info')
32
def test_licensefile_directly_samsung(self):
33
response = self.client.get('/licenses/samsung.html', follow=True)
34
self.assertEqual(response.status_code, 200)
35
self.assertContains(response, '/build-info')
37
def test_licensefile_directly_ste(self):
38
response = self.client.get('/licenses/ste.html', follow=True)
39
self.assertEqual(response.status_code, 200)
40
self.assertContains(response, '/build-info')
42
def test_licensefile_directly_linaro(self):
43
response = self.client.get('/licenses/linaro.html', follow=True)
44
self.assertEqual(response.status_code, 200)
45
self.assertContains(response, '/build-info')
47
def test_redirect_to_license_samsung(self):
48
# Get BuildInfo for target file
49
target_file = "build-info/origen-blob.txt"
50
file_path = os.path.join(TESTSERVER_ROOT, target_file)
51
build_info = BuildInfo(file_path)
53
# Try to fetch file from server - we should be redirected
54
url = urlparse.urljoin("http://testserver/", target_file)
55
response = self.client.get(url, follow=True)
56
digest = hashlib.md5(build_info.get("license-text")).hexdigest()
57
self.assertRedirects(response, '/license?lic=%s&url=%s' %
58
(digest, target_file))
60
# Make sure that we get the license text in the license page
61
self.assertContains(response, build_info.get("license-text"))
63
# Test that we use the "samsung" theme. This contains exynos.png
64
self.assertContains(response, "exynos.png")
66
def test_redirect_to_license_ste(self):
67
# Get BuildInfo for target file
68
target_file = "build-info/snowball-blob.txt"
69
file_path = os.path.join(TESTSERVER_ROOT, target_file)
70
build_info = BuildInfo(file_path)
72
# Try to fetch file from server - we should be redirected
73
url = urlparse.urljoin("http://testserver/", target_file)
74
response = self.client.get(url, follow=True)
75
digest = hashlib.md5(build_info.get("license-text")).hexdigest()
76
self.assertRedirects(response, '/license?lic=%s&url=%s' %
77
(digest, target_file))
79
# Make sure that we get the license text in the license page
80
self.assertContains(response, build_info.get("license-text"))
82
# Test that we use the "stericsson" theme. This contains igloo.png
83
self.assertContains(response, "igloo.png")
85
def test_redirect_to_license_linaro(self):
86
# Get BuildInfo for target file
87
target_file = "build-info/linaro-blob.txt"
88
file_path = os.path.join(TESTSERVER_ROOT, target_file)
89
build_info = BuildInfo(file_path)
91
# Try to fetch file from server - we should be redirected
92
url = urlparse.urljoin("http://testserver/", target_file)
93
response = self.client.get(url, follow=True)
94
digest = hashlib.md5(build_info.get("license-text")).hexdigest()
95
self.assertRedirects(response, '/license?lic=%s&url=%s' %
96
(digest, target_file))
98
# Make sure that we get the license text in the license page
99
self.assertContains(response, build_info.get("license-text"))
101
# Test that we use the "linaro" theme. This contains linaro.png
102
self.assertContains(response, "linaro.png")
104
def set_up_license(self, target_file, index=0):
105
# Get BuildInfo for target file
106
file_path = os.path.join(TESTSERVER_ROOT, target_file)
107
build_info = BuildInfo(file_path)
109
# Insert license information into database
110
text = build_info.get("license-text", index)
111
digest = hashlib.md5(text).hexdigest()
112
theme = build_info.get("theme", index)
113
_insert_license_into_db(digest, text, theme)
116
def test_redirect_to_file_on_accept_license(self):
117
target_file = "build-info/linaro-blob.txt"
118
digest = self.set_up_license(target_file)
120
# Accept the license for our file...
121
accept_url = '/accept-license?lic=%s&url=%s' % (digest, target_file)
122
response = self.client.post(accept_url, {"accept": "accept"})
124
# We should have a license accept cookie.
125
accept_cookie_name = "license_accepted_" + digest
126
self.assertTrue(accept_cookie_name in response.cookies)
128
# We should get redirected back to the original file location.
129
self.assertEqual(response.status_code, 302)
130
url = urlparse.urljoin("http://testserver/", target_file)
131
listing_url = os.path.dirname(url)
132
self.assertEqual(response['Location'],
133
listing_url + "?dl=/" + target_file)
135
def test_redirect_to_decline_page_on_decline_license(self):
136
target_file = "build-info/linaro-blob.txt"
137
digest = self.set_up_license(target_file)
139
# Reject the license for our file...
140
accept_url = '/accept-license?lic=%s&url=%s' % (digest, target_file)
141
response = self.client.post(accept_url, {"reject": "reject"})
143
# We should get a message saying we don't have access to the file.
144
self.assertContains(response, "Without accepting the license, you can"
145
" not download the requested files.")
147
def test_download_file_accepted_license(self):
148
target_file = "build-info/linaro-blob.txt"
149
url = urlparse.urljoin("http://testserver/", target_file)
150
digest = self.set_up_license(target_file)
152
# Accept the license for our file...
153
accept_url = '/accept-license?lic=%s&url=%s' % (digest, target_file)
154
response = self.client.post(accept_url, {"accept": "accept"})
156
# We should get redirected back to the original file location.
157
self.assertEqual(response.status_code, 302)
158
listing_url = os.path.dirname(url)
159
self.assertEqual(response['Location'],
160
listing_url + "?dl=/" + target_file)
162
# We should have a license accept cookie.
163
accept_cookie_name = "license_accepted_" + digest
164
self.assertTrue(accept_cookie_name in response.cookies)
166
# XXX Workaround for seemingly out of sync cookie handling XXX
167
# The cookies in client.cookies are instances of
168
# http://docs.python.org/library/cookie.html once they have been
169
# returned by a client get/post. Unfortunately for the next query
170
# client.cookies needs to be a dictionary keyed by cookie name and
171
# containing a value of whatever is stored in the cookie (or so it
172
# seems). For this reason we start up a new client, erasing all
173
# cookies from the current session, and re-introduce them.
175
client.cookies[accept_cookie_name] = accept_cookie_name
176
response = client.get(url)
178
# If we have access to the file, we will get an X-Sendfile response
179
self.assertEqual(response.status_code, 200)
180
file_path = os.path.join(TESTSERVER_ROOT, target_file)
181
self.assertEqual(response['X-Sendfile'], file_path)
183
def test_OPEN_EULA_txt(self):
184
target_file = '~linaro-android/staging-vexpress-a9/test.txt'
185
url = urlparse.urljoin("http://testserver/", target_file)
186
response = self.client.get(url, follow=True)
188
# If we have access to the file, we will get an X-Sendfile response
189
self.assertEqual(response.status_code, 200)
190
file_path = os.path.join(TESTSERVER_ROOT, target_file)
191
self.assertEqual(response['X-Sendfile'], file_path)
193
def test_never_available_dirs(self):
194
target_file = '~linaro-android/staging-imx53/test.txt'
195
url = urlparse.urljoin("http://testserver/", target_file)
196
response = self.client.get(url, follow=True)
198
# If we don't have access we will get a Forbidden response (403)
199
self.assertEqual(response.status_code, 403)
201
def test_protected_by_EULA_txt(self):
202
# Get BuildInfo for target file
203
target_file = "~linaro-android/staging-origen/test.txt"
205
# Try to fetch file from server - we should be redirected
206
url = urlparse.urljoin("http://testserver/", target_file)
207
response = self.client.get(url, follow=True)
209
eula_path = os.path.join(settings.PROJECT_ROOT,
210
"templates/licenses/samsung.txt")
211
with open(eula_path) as license_file:
212
license_text = license_file.read()
214
digest = hashlib.md5(license_text).hexdigest()
215
self.assertRedirects(response, "/license?lic=%s&url=%s" %
216
(digest, target_file))
218
# Make sure that we get the license text in the license page
219
self.assertContains(response, license_text)
221
# Test that we use the "samsung" theme. This contains exynos.png
222
self.assertContains(response, "exynos.png")
224
# test_internal_host_* are integration, not unit tests and will be
225
# located in another file...
227
def test_per_file_license_samsung(self):
228
# Get BuildInfo for target file
229
target_file = "images/origen-blob.txt"
231
# Try to fetch file from server - we should be redirected
232
url = urlparse.urljoin("http://testserver/", target_file)
233
response = self.client.get(url, follow=True)
235
eula_path = os.path.join(settings.PROJECT_ROOT,
236
"templates/licenses/samsung.txt")
237
with open(eula_path) as license_file:
238
license_text = license_file.read()
240
digest = hashlib.md5(license_text).hexdigest()
241
self.assertRedirects(response, "/license?lic=%s&url=%s" %
242
(digest, target_file))
244
# Make sure that we get the license text in the license page
245
self.assertContains(response, license_text)
247
# Test that we use the "samsung" theme. This contains exynos.png
248
self.assertContains(response, "exynos.png")
250
def test_per_file_non_protected_dirs(self):
251
target_file = "images/MANIFEST"
252
url = urlparse.urljoin("http://testserver/", target_file)
253
response = self.client.get(url, follow=True)
255
# If we have access to the file, we will get an X-Sendfile response
256
self.assertEqual(response.status_code, 200)
257
file_path = os.path.join(TESTSERVER_ROOT, target_file)
258
self.assertEqual(response['X-Sendfile'], file_path)
260
def test_dir_containing_only_dirs(self):
261
target_file = "~linaro-android"
262
url = urlparse.urljoin("http://testserver/", target_file)
263
response = self.client.get(url, follow=True)
265
# If we have access to the file, we will get an X-Sendfile response
266
self.assertContains(response,
267
r"<th></th><th>Name</th><th>Last modified</th><th>Size</th>")
269
def test_not_found_file(self):
270
target_file = "12qwaszx"
271
url = urlparse.urljoin("http://testserver/", target_file)
272
response = self.client.get(url, follow=True)
273
self.assertContains(response, "not found", status_code=404)
275
def test_unprotected_BUILD_INFO(self):
276
target_file = 'build-info/panda-open.txt'
277
url = urlparse.urljoin("http://testserver/", target_file)
278
response = self.client.get(url, follow=True)
280
# If we have access to the file, we will get an X-Sendfile response
281
self.assertEqual(response.status_code, 200)
282
file_path = os.path.join(TESTSERVER_ROOT, target_file)
283
self.assertEqual(response['X-Sendfile'], file_path)
285
def test_redirect_to_file_on_accept_multi_license(self):
286
target_file = "build-info/multi-license.txt"
287
digest = self.set_up_license(target_file)
289
# Accept the first license for our file...
290
accept_url = '/accept-license?lic=%s&url=%s' % (digest, target_file)
291
response = self.client.post(accept_url, {"accept": "accept"})
293
# We should have a license accept cookie.
294
accept_cookie_name = "license_accepted_" + digest
295
self.assertTrue(accept_cookie_name in response.cookies)
297
# We should get redirected back to the original file location.
298
self.assertEqual(response.status_code, 302)
299
url = urlparse.urljoin("http://testserver/", target_file)
300
listing_url = os.path.dirname(url)
301
self.assertEqual(response['Location'],
302
listing_url + "?dl=/" + target_file)
305
client.cookies[accept_cookie_name] = accept_cookie_name
307
digest = self.set_up_license(target_file, 1)
309
# Accept the second license for our file...
310
accept_url = '/accept-license?lic=%s&url=%s' % (digest, target_file)
311
response = client.post(accept_url, {"accept": "accept"})
313
# We should have a license accept cookie.
314
accept_cookie_name1 = "license_accepted_" + digest
315
self.assertTrue(accept_cookie_name1 in response.cookies)
317
# We should get redirected back to the original file location.
318
self.assertEqual(response.status_code, 302)
319
url = urlparse.urljoin("http://testserver/", target_file)
320
listing_url = os.path.dirname(url)
321
self.assertEqual(response['Location'],
322
listing_url + "?dl=/" + target_file)
325
client.cookies[accept_cookie_name] = accept_cookie_name
326
client.cookies[accept_cookie_name1] = accept_cookie_name1
327
response = client.get(url)
329
# If we have access to the file, we will get an X-Sendfile response
330
self.assertEqual(response.status_code, 200)
331
file_path = os.path.join(TESTSERVER_ROOT, target_file)
332
self.assertEqual(response['X-Sendfile'], file_path)
334
if __name__ == '__main__':