~danilo/linaro-license-protection/django-refactor-deployment

« back to all changes in this revision

Viewing changes to license_protected_downloads/tests/test_views.py

Full refactoring with some cleanups (bzr mv files instead of deleting then adding them in a different place.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import re
 
2
import urlparse
 
3
from license_protected_downloads.views import _insert_license_into_db
 
4
 
 
5
__author__ = 'dooferlad'
 
6
 
 
7
import os
 
8
import unittest
 
9
import hashlib
 
10
from django.test import Client, TestCase
 
11
from license_protected_downloads.buildinfo import BuildInfo
 
12
from django.conf import settings
 
13
 
 
14
THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
 
15
TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")
 
16
 
 
17
class ViewTests(TestCase):
 
18
    def setUp(self):
 
19
        self.client = Client()
 
20
        self.old_served_paths = settings.SERVED_PATHS
 
21
        settings.SERVED_PATHS = [os.path.join(THIS_DIRECTORY,
 
22
                                             "testserver_root")]
 
23
 
 
24
    def tearDown(self):
 
25
        settings.SERVED_PATHS = self.old_served_paths
 
26
 
 
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')
 
31
 
 
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')
 
36
 
 
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')
 
41
 
 
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')
 
46
 
 
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)
 
52
 
 
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))
 
59
 
 
60
        # Make sure that we get the license text in the license page
 
61
        self.assertContains(response, build_info.get("license-text"))
 
62
 
 
63
        # Test that we use the "samsung" theme. This contains exynos.png
 
64
        self.assertContains(response, "exynos.png")
 
65
 
 
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)
 
71
 
 
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))
 
78
 
 
79
        # Make sure that we get the license text in the license page
 
80
        self.assertContains(response, build_info.get("license-text"))
 
81
 
 
82
        # Test that we use the "stericsson" theme. This contains igloo.png
 
83
        self.assertContains(response, "igloo.png")
 
84
 
 
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)
 
90
 
 
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))
 
97
 
 
98
        # Make sure that we get the license text in the license page
 
99
        self.assertContains(response, build_info.get("license-text"))
 
100
 
 
101
        # Test that we use the "linaro" theme. This contains linaro.png
 
102
        self.assertContains(response, "linaro.png")
 
103
 
 
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)
 
108
 
 
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)
 
114
        return digest
 
115
 
 
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)
 
119
 
 
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"})
 
123
 
 
124
        # We should have a license accept cookie.
 
125
        accept_cookie_name = "license_accepted_" + digest
 
126
        self.assertTrue(accept_cookie_name in response.cookies)
 
127
 
 
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)
 
134
 
 
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)
 
138
 
 
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"})
 
142
 
 
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.")
 
146
 
 
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)
 
151
 
 
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"})
 
155
 
 
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)
 
161
 
 
162
        # We should have a license accept cookie.
 
163
        accept_cookie_name = "license_accepted_" + digest
 
164
        self.assertTrue(accept_cookie_name in response.cookies)
 
165
 
 
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.
 
174
        client = Client()
 
175
        client.cookies[accept_cookie_name] = accept_cookie_name
 
176
        response = client.get(url)
 
177
 
 
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)
 
182
 
 
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)
 
187
 
 
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)
 
192
 
 
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)
 
197
 
 
198
        # If we don't have access we will get a Forbidden response (403)
 
199
        self.assertEqual(response.status_code, 403)
 
200
 
 
201
    def test_protected_by_EULA_txt(self):
 
202
        # Get BuildInfo for target file
 
203
        target_file = "~linaro-android/staging-origen/test.txt"
 
204
 
 
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)
 
208
 
 
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()
 
213
 
 
214
        digest = hashlib.md5(license_text).hexdigest()
 
215
        self.assertRedirects(response, "/license?lic=%s&url=%s" %
 
216
                                       (digest, target_file))
 
217
 
 
218
        # Make sure that we get the license text in the license page
 
219
        self.assertContains(response, license_text)
 
220
 
 
221
        # Test that we use the "samsung" theme. This contains exynos.png
 
222
        self.assertContains(response, "exynos.png")
 
223
 
 
224
    # test_internal_host_* are integration, not unit tests and will be
 
225
    # located in another file...
 
226
 
 
227
    def test_per_file_license_samsung(self):
 
228
        # Get BuildInfo for target file
 
229
        target_file = "images/origen-blob.txt"
 
230
 
 
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)
 
234
 
 
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()
 
239
 
 
240
        digest = hashlib.md5(license_text).hexdigest()
 
241
        self.assertRedirects(response, "/license?lic=%s&url=%s" %
 
242
                                       (digest, target_file))
 
243
 
 
244
        # Make sure that we get the license text in the license page
 
245
        self.assertContains(response, license_text)
 
246
 
 
247
        # Test that we use the "samsung" theme. This contains exynos.png
 
248
        self.assertContains(response, "exynos.png")
 
249
 
 
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)
 
254
 
 
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)
 
259
 
 
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)
 
264
 
 
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>")
 
268
 
 
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)
 
274
 
 
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)
 
279
 
 
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)
 
284
 
 
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)
 
288
 
 
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"})
 
292
 
 
293
        # We should have a license accept cookie.
 
294
        accept_cookie_name = "license_accepted_" + digest
 
295
        self.assertTrue(accept_cookie_name in response.cookies)
 
296
 
 
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)
 
303
 
 
304
        client = Client()
 
305
        client.cookies[accept_cookie_name] = accept_cookie_name
 
306
 
 
307
        digest = self.set_up_license(target_file, 1)
 
308
 
 
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"})
 
312
 
 
313
        # We should have a license accept cookie.
 
314
        accept_cookie_name1 = "license_accepted_" + digest
 
315
        self.assertTrue(accept_cookie_name1 in response.cookies)
 
316
 
 
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)
 
323
 
 
324
        client = Client()
 
325
        client.cookies[accept_cookie_name] = accept_cookie_name
 
326
        client.cookies[accept_cookie_name1] = accept_cookie_name1
 
327
        response = client.get(url)
 
328
 
 
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)
 
333
 
 
334
if __name__ == '__main__':
 
335
    unittest.main()