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.

76 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp.tests import common
  5. import mock
  6. import os
  7. from contextlib import contextmanager
  8. import unittest
  9. @contextmanager
  10. def mock_cursor(cr):
  11. with mock.patch('openerp.sql_db.Connection.cursor') as mocked_cursor_call:
  12. org_close = cr.close
  13. org_autocommit = cr.autocommit
  14. try:
  15. cr.close = mock.Mock()
  16. cr.autocommit = mock.Mock()
  17. mocked_cursor_call.return_value = cr
  18. yield
  19. finally:
  20. cr.close = org_close
  21. cr.autocommit = org_autocommit
  22. class TestResUsers(common.TransactionCase):
  23. def test_login(self):
  24. res_users_obj = self.registry('res.users')
  25. res = res_users_obj.authenticate(
  26. common.get_db_name(), 'admin', 'admin', None)
  27. uid = res
  28. self.assertTrue(res, "Basic login must works as expected")
  29. token = "123456"
  30. res = res_users_obj.authenticate(
  31. common.get_db_name(), 'admin', token, None)
  32. self.assertFalse(res)
  33. # mimic what the new controller do when it find a value in
  34. # the http header (HTTP_REMODE_USER)
  35. res_users_obj.write(self.cr, self.uid, uid, {'sso_key': token})
  36. # Here we need to mock the cursor since the login is natively done
  37. # inside its own connection
  38. with mock_cursor(self.cr):
  39. # We can verifies that the given (uid, token) is authorized for
  40. # the database
  41. res_users_obj.check(common.get_db_name(), uid, token)
  42. # we are able to login with the new token
  43. res = res_users_obj.authenticate(
  44. common.get_db_name(), 'admin', token, None)
  45. self.assertTrue(res)
  46. @unittest.skipIf(os.environ.get('TRAVIS'),
  47. 'When run by travis, tests runs on a database with all '
  48. 'required addons from server-tools and their dependencies'
  49. ' installed. Even if `auth_from_http_remote_user` does '
  50. 'not require the `mail` module, The previous installation'
  51. ' of the mail module has created the column '
  52. '`notification_email_send` as REQUIRED into the table '
  53. 'res_partner. BTW, it\'s no more possible to copy a '
  54. 'res_user without an intefirty error')
  55. def test_copy(self):
  56. '''Check that the sso_key is not copied on copy
  57. '''
  58. res_users_obj = self.registry('res.users')
  59. vals = {'sso_key': '123'}
  60. res_users_obj.write(self.cr, self.uid, self.uid, vals)
  61. read_vals = res_users_obj.read(
  62. self.cr, self.uid, self.uid, ['sso_key'])
  63. self.assertDictContainsSubset(vals, read_vals)
  64. copy = res_users_obj.copy(self.cr, self.uid, self.uid)
  65. read_vals = res_users_obj.read(
  66. self.cr, self.uid, copy, ['sso_key'])
  67. self.assertFalse(read_vals.get('sso_key'))