Merge lp:~james-w/pkgme-service/base64-icon-data into lp:pkgme-service

Proposed by James Westby
Status: Merged
Approved by: James Westby
Approved revision: 156
Merged at revision: 155
Proposed branch: lp:~james-w/pkgme-service/base64-icon-data
Merge into: lp:pkgme-service
Diff against target: 49 lines (+7/-3)
2 files modified
src/djpkgme/tasks.py (+3/-1)
src/djpkgme/tests/test_tasks.py (+4/-2)
To merge this branch: bzr merge lp:~james-w/pkgme-service/base64-icon-data
Reviewer Review Type Date Requested Status
Martin Albisetti (community) Approve
Review via email: mp+203807@code.launchpad.net

Commit message

base64 the icon data so that it can be safely transmitted as json.

Description of the change

Hi,

Current the icon data, if any, is put raw in to the json string. It's safer
to encode it for transmission as base64.

Thanks,

James

To post a comment you must log in.
Revision history for this message
Martin Albisetti (beuno) :
review: Approve
156. By James Westby

Add a comment about why we base64. Thanks Martin.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/djpkgme/tasks.py'
2--- src/djpkgme/tasks.py 2014-01-15 15:27:18 +0000
3+++ src/djpkgme/tasks.py 2014-01-29 18:34:40 +0000
4@@ -1,3 +1,4 @@
5+import base64
6 import json
7 import os
8 import shutil
9@@ -344,7 +345,8 @@
10 icon_data = None
11 try:
12 full_path = os.path.join(package, icon_path)
13- icon_data = open(full_path, 'rb').read()
14+ # base64 icon data, as it is binary to be sent in json
15+ icon_data = base64.b64encode(open(full_path, 'rb').read())
16 except:
17 # gracefully handle not being able to read icon data
18 logger.warning(
19
20=== modified file 'src/djpkgme/tests/test_tasks.py'
21--- src/djpkgme/tests/test_tasks.py 2014-01-15 15:27:18 +0000
22+++ src/djpkgme/tests/test_tasks.py 2014-01-29 18:34:40 +0000
23@@ -1,6 +1,7 @@
24 # Run tasks eagerly for tests. Taken from
25 # http://ask.github.com/celery/cookbook/unit-testing.html
26
27+import base64
28 import json
29 import logging
30 import os
31@@ -816,7 +817,8 @@
32
33 @patch('__builtin__.open')
34 def test_get_package_icon_ok(self, mock_open):
35- mock_data = mock_open.return_value.read.return_value
36+ mock_icon_data = 'some icon'
37+ mock_open.return_value.read.return_value = mock_icon_data
38 mock_logger = Mock()
39 job = ClickPackageInfoJob()
40 info = job.get_package_icon('/unpacked/',
41@@ -827,7 +829,7 @@
42 self.assertEqual(info, {
43 'path': 'path/to/icon.png',
44 'filename': 'icon.png',
45- 'data': mock_data,
46+ 'data': base64.b64encode(mock_icon_data),
47 })
48
49 @patch('__builtin__.open')

Subscribers

People subscribed via source and target branches