From 2507bc300658fb00468f0edfc30f907918f0a73e Mon Sep 17 00:00:00 2001 From: Dave Lasley Date: Mon, 4 Apr 2016 09:03:02 -0700 Subject: [PATCH] [MIG] Upgrade Gravatar module to v9, make installable, and add tests [MIG] base_user_gravatar: make installable + add tests --- base_user_gravatar/README.rst | 9 ++-- base_user_gravatar/__init__.py | 1 + base_user_gravatar/__openerp__.py | 8 +-- base_user_gravatar/models/res_users.py | 23 +++++---- base_user_gravatar/tests/__init__.py | 2 + base_user_gravatar/tests/test_res_users.py | 60 ++++++++++++++++++++++ 6 files changed, 85 insertions(+), 18 deletions(-) create mode 100755 base_user_gravatar/tests/__init__.py create mode 100755 base_user_gravatar/tests/test_res_users.py diff --git a/base_user_gravatar/README.rst b/base_user_gravatar/README.rst index 5e0a618a3..9f92ab74c 100644 --- a/base_user_gravatar/README.rst +++ b/base_user_gravatar/README.rst @@ -3,18 +3,18 @@ :alt: License: AGPL-3 ========================== -Synchronize Gravatar image +Synchronize Gravatar Image ========================== -Use your Gravatar image in to your Odoo +Use your Gravatar image in your Odoo Usage ===== To configure this module, you need to: -* edit user profile -* press "Get Gravatar image" button +* Edit user profile +* Press "Get Gravatar image" button .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas :alt: Try me on Runbot @@ -35,6 +35,7 @@ Contributors ------------ * Endika Iglesias +* Dave Lasley Maintainer ---------- diff --git a/base_user_gravatar/__init__.py b/base_user_gravatar/__init__.py index c2c2899c9..d81b586a2 100644 --- a/base_user_gravatar/__init__.py +++ b/base_user_gravatar/__init__.py @@ -3,3 +3,4 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from . import models +from . import tests diff --git a/base_user_gravatar/__openerp__.py b/base_user_gravatar/__openerp__.py index d03c5d8c7..3bad60623 100644 --- a/base_user_gravatar/__openerp__.py +++ b/base_user_gravatar/__openerp__.py @@ -3,14 +3,14 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { - 'name': 'Synchronize Gravatar image', - 'version': '8.0.1.0.0', - 'author': 'Endika Iglesias, Odoo Community Association (OCA)', + 'name': 'Synchronize Gravatar Image', + 'version': '9.0.1.0.0', + 'author': 'LasLabs, Endika Iglesias, Odoo Community Association (OCA)', 'category': 'Tools', 'website': 'http://www.endikaiglesias.com/', "license": "AGPL-3", "application": False, - 'installable': False, + 'installable': True, 'depends': ['base'], 'data': [ 'views/res_users_view.xml', diff --git a/base_user_gravatar/models/res_users.py b/base_user_gravatar/models/res_users.py index 8874f3585..a3ac27e93 100644 --- a/base_user_gravatar/models/res_users.py +++ b/base_user_gravatar/models/res_users.py @@ -24,15 +24,18 @@ class ResUsers(models.Model): except urllib2.HTTPError: raise UserError(_('Sorry Gravatar not found.')) - @api.one + @api.multi def get_gravatar_image(self): - email = str(self.email) or '' - fail_example = self._get_gravatar_base64('fail@email.gravatar') - user_gravatar = self._get_gravatar_base64(email) - if fail_example != user_gravatar: - self.write({'image': user_gravatar}) - else: - raise UserError( - _("There is no Gravatar image for this email (%s)" % ( - email))) + for rec_id in self: + email = str(rec_id.email) or '' + fail_example = self._get_gravatar_base64('fail@email.gravatar') + user_gravatar = self._get_gravatar_base64(email) + if fail_example != user_gravatar: + rec_id.write({'image': user_gravatar}) + else: + raise UserError(_( + "There is no Gravatar image for this email (%s)" % ( + email + ) + )) return True diff --git a/base_user_gravatar/tests/__init__.py b/base_user_gravatar/tests/__init__.py new file mode 100755 index 000000000..b986f69a4 --- /dev/null +++ b/base_user_gravatar/tests/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +from . import test_res_users diff --git a/base_user_gravatar/tests/test_res_users.py b/base_user_gravatar/tests/test_res_users.py new file mode 100755 index 000000000..7a64fc837 --- /dev/null +++ b/base_user_gravatar/tests/test_res_users.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# © 2016-TODAY LasLabs, Inc. [https://laslabs.com] +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp.tests.common import TransactionCase +from openerp.addons.base_user_gravatar.models.res_users import ResUsers +import mock +import hashlib + + +MODULE_LOCATION = 'openerp.addons.base_user_gravatar.models.res_users' + + +class TestResUsers(TransactionCase): + + def setUp(self, *args, **kwargs): + super(TestResUsers, self).setUp() + self.model_obj = self.env['res.users'] + self.partner_vals = { + 'name': 'Test', + 'email': 'test@example.com', + } + self.vals = { + 'name': 'Test', + 'login': 'test_login', + } + self.url = 'http://www.gravatar.com/avatar/{}?s=200' + + def _test_record(self, ): + partner_id = self.env['res.partner'].create(self.partner_vals) + self.vals['partner_id'] = partner_id.id + return self.env['res.users'].create(self.vals) + + @mock.patch('%s.urllib2' % MODULE_LOCATION) + def test_get_gravatar_base64_opens_correct_uri(self, mk, ): + """ Test that gravatar is pinged for image """ + self.model_obj._get_gravatar_base64(self.partner_vals['email']) + expect = hashlib.md5(self.partner_vals['email']).hexdigest() + mk.urlopen.assert_called_once_with(self.url.format(expect)) + + @mock.patch('%s.base64' % MODULE_LOCATION) + @mock.patch('%s.urllib2' % MODULE_LOCATION) + def test_get_gravatar_base64_returns_encoded_image(self, mk, b64_mk, ): + """ Test that image result is read """ + expect = 'Expect' + b64_mk.encodestring.return_value = expect + result = self.model_obj._get_gravatar_base64( + self.partner_vals['email'] + ) + self.assertEquals(expect, result) + + def test_get_gravatar_image_writes_image(self, ): + """ Test that the resulting gravatar is written to user """ + with mock.patch.object(ResUsers, 'write') as write_mk: + user_id = self._test_record() + with mock.patch.object(user_id, '_get_gravatar_base64') as mk: + expect = 'Expect' + mk.side_effect = ['Fail', expect] + user_id.get_gravatar_image() + write_mk.assert_called_once_with({'image': expect})