Merge lp:~stevanr/linaro-license-protection/bug-1085007 into lp:~linaro-automation/linaro-license-protection/trunk

Proposed by Stevan Radaković
Status: Merged
Approved by: Данило Шеган
Approved revision: 174
Merged at revision: 171
Proposed branch: lp:~stevanr/linaro-license-protection/bug-1085007
Merge into: lp:~linaro-automation/linaro-license-protection/trunk
Diff against target: 197 lines (+105/-4)
6 files modified
license_protected_downloads/tests/helpers.py (+43/-0)
license_protected_downloads/tests/test_views.py (+29/-1)
license_protected_downloads/views.py (+17/-0)
settings.py (+6/-0)
templates/header.html (+6/-3)
urls.py (+4/-0)
To merge this branch: bzr merge lp:~stevanr/linaro-license-protection/bug-1085007
Reviewer Review Type Date Requested Status
Данило Шеган (community) Approve
Review via email: mp+150646@code.launchpad.net

Description of the change

Additional fix for bug 1085007.
When certificates for www.linaro.org are fixed, this code will become obsolete and may be removed.

To post a comment you must log in.
Revision history for this message
Данило Шеган (danilo) wrote :
Download full text (3.2 KiB)

Hi Stevan,

Thanks for working on this. It's very close to being ready to land :)

У уто, 26. 02 2013. у 19:43 +0000, Stevan Radaković пише:
> === modified file 'license_protected_downloads/views.py'

> @@ -479,3 +480,13 @@
> path = result[1]
>
> return HttpResponse(json.dumps(RenderTextFiles.find_and_render(path)))
> +
> +
> +def get_remote_static(request):
> +

Please provide a docstring explaining what this does. And replace this blank line with it.

> + name = request.GET.get("name")
> + if name not in settings.SUPPORTED_REMOTE_STATIC_FILES:
> + raise Http404

Please add a message to this exception: it's always a good practice to
do that.

> +
> + data = urllib2.urlopen(settings.SUPPORTED_REMOTE_STATIC_FILES[name])
> + return HttpResponse(data)
>
> === modified file 'settings.py'
> --- settings.py 2012-11-29 09:36:04 +0000
> +++ settings.py 2013-02-26 19:42:24 +0000
> @@ -191,3 +191,9 @@
> 'Building From Source',
> 'Firmware Update',
> 'RTSM']
> +
> +SUPPORTED_REMOTE_STATIC_FILES = {
> + "linarofamily.js": "http://www.linaro.org/remote/js/linarofamily.js",
> + "init.css": "http://www.linaro.org/remote/css/init.css",
> + "remote.css": "http://www.linaro.org/remote/css/remote.css",
> + }
>
> === modified file 'templates/header.html'
> --- templates/header.html 2013-02-21 08:48:00 +0000
> +++ templates/header.html 2013-02-26 19:42:24 +0000
> @@ -5,9 +5,9 @@
> {% endif %}
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
> <title>Linaro Snapshots</title>
> - <link href="//www.linaro.org/remote/css/init.css" rel="stylesheet" type="text/css" >
> - <link href="//www.linaro.org/remote/css/remote.css" rel="stylesheet" type="text/css" >
> - <script language="javascript" type="text/javascript" src="//www.linaro.org/remote/js/linarofamily.js"></script>
> + <link href="/get-remote-static?name=init.css" rel="stylesheet" type="text/css" >
> + <link href="/get-remote-static?name=remote.css" rel="stylesheet" type="text/css" >
> + <script language="javascript" type="text/javascript" src="/get-remote-static?name=linarofamily.js"></script>

This seems to break the line length limit: please fix this by breaking
into multiple lines where needed to meet our 79-character maximum line
length.

>
> <script type="text/javascript" src="/js/jquery-1.7.2.js"></script>
>
> === modified file 'urls.py'
> --- urls.py 2012-10-22 12:48:48 +0000
> +++ urls.py 2013-02-26 19:42:24 +0000
> @@ -22,6 +22,10 @@
> url(r'^css/(?P<path>.*)$', 'django.views.static.serve',
> {'document_root': settings.CSS_PATH}),
>
> + url(r'^get-remote-static',
> + 'license_protected_downloads.views.get_remote_static',
> + name='get_remote_static'),
> +
> # The license page...
> url(r'^license$',
> 'license_protected_downloads.views.show_license',

Btw, any reason there're no tests for this? Seems pretty simple to test
the get_remote_static() view (you might want to override settings in a
test though). It would be good to test the "negative" case as well: the
fact that it throws a 404 wh...

Read more...

review: Needs Fixing
171. By Stevan Radaković

Add tests, docstrings and fix pep8 in html.

172. By Stevan Radaković

Add test server for urllib testing.

173. By Stevan Radaković

Add test server helper from danilos branch.

174. By Stevan Radaković

Add test for non-existing file. Add exception handling for HTTPError.

Revision history for this message
Данило Шеган (danilo) wrote :

Thanks for the improvements. Please extend the comment in get_remote_static to say how we should switch the error to Http404 once we start sending the email to infrastructure-errors.

review: Approve
175. By Stevan Radaković

Add additional comment for re-raising error.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'license_protected_downloads/tests/helpers.py'
--- license_protected_downloads/tests/helpers.py 2012-11-29 09:36:04 +0000
+++ license_protected_downloads/tests/helpers.py 2013-02-28 10:08:20 +0000
@@ -4,6 +4,9 @@
4import os4import os
5import shutil5import shutil
6import tempfile6import tempfile
7import threading
8import BaseHTTPServer
9import SocketServer
710
811
9class temporary_directory(object):12class temporary_directory(object):
@@ -39,3 +42,43 @@
39 target.write(data)42 target.write(data)
40 target.close()43 target.close()
41 return full_path44 return full_path
45
46
47class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
48 def do_GET(self):
49 if self.path in self.urls:
50 self.send_response(200, "OK")
51 self.end_headers()
52 self.request.sendall(self.urls[self.path])
53 else:
54 self.send_error(404, 'URL %s not found' % self.path)
55 self.end_headers()
56
57
58class ThreadedHTTPServer(SocketServer.ThreadingMixIn,
59 BaseHTTPServer.HTTPServer):
60 pass
61
62
63class TestHttpServer(object):
64 """Creates a context manager for a temporary directory."""
65
66 def __init__(self, urls):
67 self.handler = HttpHandler
68 self.handler.urls = urls
69
70 def __enter__(self):
71 self.server = ThreadedHTTPServer(
72 ("localhost", 0), self.handler)
73 self.ip, self.port = self.server.server_address
74 self.thread = threading.Thread(target=self.server.serve_forever)
75 self.thread.daemon = True
76 self.thread.start()
77 return self
78
79 def __exit__(self, *args):
80 self.server.shutdown()
81
82 @property
83 def base_url(self):
84 return "http://%s:%s" % (self.ip, self.port)
4285
=== modified file 'license_protected_downloads/tests/test_views.py'
--- license_protected_downloads/tests/test_views.py 2013-02-26 08:38:32 +0000
+++ license_protected_downloads/tests/test_views.py 2013-02-28 10:08:20 +0000
@@ -6,18 +6,19 @@
6import os6import os
7import tempfile7import tempfile
8import unittest8import unittest
9import urllib2
9import urlparse10import urlparse
1011
11from license_protected_downloads import bzr_version12from license_protected_downloads import bzr_version
12from license_protected_downloads.buildinfo import BuildInfo13from license_protected_downloads.buildinfo import BuildInfo
13from license_protected_downloads.config import INTERNAL_HOSTS14from license_protected_downloads.config import INTERNAL_HOSTS
14from license_protected_downloads.tests.helpers import temporary_directory15from license_protected_downloads.tests.helpers import temporary_directory
16from license_protected_downloads.tests.helpers import TestHttpServer
15from license_protected_downloads.views import _insert_license_into_db17from license_protected_downloads.views import _insert_license_into_db
16from license_protected_downloads.views import _process_include_tags18from license_protected_downloads.views import _process_include_tags
17from license_protected_downloads.views import _sizeof_fmt19from license_protected_downloads.views import _sizeof_fmt
18from license_protected_downloads.views import is_same_parent_dir20from license_protected_downloads.views import is_same_parent_dir
1921
20
21THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))22THIS_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
22TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")23TESTSERVER_ROOT = os.path.join(THIS_DIRECTORY, "testserver_root")
2324
@@ -664,6 +665,33 @@
664 fname = os.path.join(TESTSERVER_ROOT, "../file")665 fname = os.path.join(TESTSERVER_ROOT, "../file")
665 self.assertFalse(is_same_parent_dir(TESTSERVER_ROOT, fname))666 self.assertFalse(is_same_parent_dir(TESTSERVER_ROOT, fname))
666667
668 def test_get_remote_static_unsupported_file(self):
669 response = self.client.get('/get-remote-static?name=unsupported.css')
670 self.assertEqual(response.status_code, 404)
671
672 def test_get_remote_static_nonexisting_file(self):
673 pages = {"/": "index"}
674
675 with TestHttpServer(pages) as http_server:
676 css_url = '%s/init.css' % http_server.base_url
677 settings.SUPPORTED_REMOTE_STATIC_FILES = {
678 'init.css': css_url}
679
680 self.assertRaises(urllib2.HTTPError, self.client.get,
681 '/get-remote-static?name=init.css')
682
683 def test_get_remote_static(self):
684 pages = {"/": "index", "/init.css": "test CSS"}
685
686 with TestHttpServer(pages) as http_server:
687 css_url = '%s/init.css' % http_server.base_url
688 settings.SUPPORTED_REMOTE_STATIC_FILES = {
689 'init.css': css_url}
690
691 response = self.client.get('/get-remote-static?name=init.css')
692 self.assertEqual(response.status_code, 200)
693 self.assertContains(response, 'test CSS')
694
667695
668class HowtoViewTests(BaseServeViewTest):696class HowtoViewTests(BaseServeViewTest):
669 def test_no_howtos(self):697 def test_no_howtos(self):
670698
=== modified file 'license_protected_downloads/views.py'
--- license_protected_downloads/views.py 2013-02-21 12:09:12 +0000
+++ license_protected_downloads/views.py 2013-02-28 10:08:20 +0000
@@ -4,6 +4,7 @@
4import mimetypes4import mimetypes
5import os5import os
6import re6import re
7import urllib2
7from mimetypes import guess_type8from mimetypes import guess_type
8from datetime import datetime9from datetime import datetime
910
@@ -479,3 +480,19 @@
479 path = result[1]480 path = result[1]
480481
481 return HttpResponse(json.dumps(RenderTextFiles.find_and_render(path)))482 return HttpResponse(json.dumps(RenderTextFiles.find_and_render(path)))
483
484
485def get_remote_static(request):
486 """Fetches remote static files based on the dict map in settings.py."""
487 name = request.GET.get("name")
488 if name not in settings.SUPPORTED_REMOTE_STATIC_FILES:
489 raise Http404("File name not supported.")
490
491 try:
492 data = urllib2.urlopen(settings.SUPPORTED_REMOTE_STATIC_FILES[name])
493 except urllib2.HTTPError:
494 # TODO: send an email to infrastructure-errors list,
495 # then implement raising of Http404 instead of HTTPError
496 raise
497
498 return HttpResponse(data)
482499
=== modified file 'settings.py'
--- settings.py 2012-11-29 09:36:04 +0000
+++ settings.py 2013-02-28 10:08:20 +0000
@@ -191,3 +191,9 @@
191 'Building From Source',191 'Building From Source',
192 'Firmware Update',192 'Firmware Update',
193 'RTSM']193 'RTSM']
194
195SUPPORTED_REMOTE_STATIC_FILES = {
196 "linarofamily.js": "http://www.linaro.org/remote/js/linarofamily.js",
197 "init.css": "http://www.linaro.org/remote/css/init.css",
198 "remote.css": "http://www.linaro.org/remote/css/remote.css",
199 }
194200
=== modified file 'templates/header.html'
--- templates/header.html 2013-02-21 08:48:00 +0000
+++ templates/header.html 2013-02-28 10:08:20 +0000
@@ -5,9 +5,12 @@
5 {% endif %}5 {% endif %}
6 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">6 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
7 <title>Linaro Snapshots</title>7 <title>Linaro Snapshots</title>
8 <link href="//www.linaro.org/remote/css/init.css" rel="stylesheet" type="text/css" >8 <link href="/get-remote-static?name=init.css"
9 <link href="//www.linaro.org/remote/css/remote.css" rel="stylesheet" type="text/css" >9 rel="stylesheet" type="text/css" >
10 <script language="javascript" type="text/javascript" src="//www.linaro.org/remote/js/linarofamily.js"></script>10 <link href="/get-remote-static?name=remote.css"
11 rel="stylesheet" type="text/css" >
12 <script language="javascript" type="text/javascript"
13 src="/get-remote-static?name=linarofamily.js"></script>
1114
1215
13 <script type="text/javascript" src="/js/jquery-1.7.2.js"></script>16 <script type="text/javascript" src="/js/jquery-1.7.2.js"></script>
1417
=== modified file 'urls.py'
--- urls.py 2012-10-22 12:48:48 +0000
+++ urls.py 2013-02-28 10:08:20 +0000
@@ -22,6 +22,10 @@
22 url(r'^css/(?P<path>.*)$', 'django.views.static.serve',22 url(r'^css/(?P<path>.*)$', 'django.views.static.serve',
23 {'document_root': settings.CSS_PATH}),23 {'document_root': settings.CSS_PATH}),
2424
25 url(r'^get-remote-static',
26 'license_protected_downloads.views.get_remote_static',
27 name='get_remote_static'),
28
25 # The license page...29 # The license page...
26 url(r'^license$',30 url(r'^license$',
27 'license_protected_downloads.views.show_license',31 'license_protected_downloads.views.show_license',

Subscribers

People subscribed via source and target branches