You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016-TODAY LasLabs, Inc. [https://laslabs.com]
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo.tests.common import TransactionCase
  5. from odoo.addons.base_user_gravatar.models.res_users import ResUsers
  6. import mock
  7. import hashlib
  8. MODULE_LOCATION = 'openerp.addons.base_user_gravatar.models.res_users'
  9. class TestResUsers(TransactionCase):
  10. def setUp(self, *args, **kwargs):
  11. super(TestResUsers, self).setUp()
  12. self.model_obj = self.env['res.users']
  13. self.partner_vals = {
  14. 'name': 'Test',
  15. 'email': 'test@example.com',
  16. }
  17. self.vals = {
  18. 'name': 'Test',
  19. 'login': 'test_login',
  20. }
  21. self.url = 'https://www.gravatar.com/avatar/{}?s=200'
  22. def _test_record(self, ):
  23. partner_id = self.env['res.partner'].create(self.partner_vals)
  24. self.vals['partner_id'] = partner_id.id
  25. return self.env['res.users'].create(self.vals)
  26. @mock.patch('%s.urllib2' % MODULE_LOCATION)
  27. def test_get_gravatar_base64_opens_correct_uri(self, mk, ):
  28. """ Test that gravatar is pinged for image """
  29. self.model_obj._get_gravatar_base64(self.partner_vals['email'])
  30. expect = hashlib.md5(self.partner_vals['email']).hexdigest()
  31. mk.urlopen.assert_called_once_with(self.url.format(expect))
  32. @mock.patch('%s.base64' % MODULE_LOCATION)
  33. @mock.patch('%s.urllib2' % MODULE_LOCATION)
  34. def test_get_gravatar_base64_returns_encoded_image(self, mk, b64_mk, ):
  35. """ Test that image result is read """
  36. expect = 'Expect'
  37. b64_mk.encodestring.return_value = expect
  38. result = self.model_obj._get_gravatar_base64(
  39. self.partner_vals['email']
  40. )
  41. self.assertEquals(expect, result)
  42. def test_get_gravatar_image_writes_image(self, ):
  43. """ Test that the resulting gravatar is written to user """
  44. with mock.patch.object(ResUsers, 'write') as write_mk:
  45. user_id = self._test_record()
  46. with mock.patch.object(user_id, '_get_gravatar_base64') as mk:
  47. expect = 'Expect'
  48. mk.side_effect = ['Fail', expect]
  49. user_id.get_gravatar_image()
  50. write_mk.assert_called_once_with({'image': expect})