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.

88 lines
3.7 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Laurent Mignon
  5. # Copyright 2014 'ACSONE SA/NV'
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.tests import common
  22. import mock
  23. import os
  24. from contextlib import contextmanager
  25. import unittest
  26. @contextmanager
  27. def mock_cursor(cr):
  28. with mock.patch('openerp.sql_db.Connection.cursor') as mocked_cursor_call:
  29. org_close = cr.close
  30. org_autocommit = cr.autocommit
  31. try:
  32. cr.close = mock.Mock()
  33. cr.autocommit = mock.Mock()
  34. mocked_cursor_call.return_value = cr
  35. yield
  36. finally:
  37. cr.close = org_close
  38. cr.autocommit = org_autocommit
  39. class test_res_users(common.TransactionCase):
  40. def test_login(self):
  41. res_users_obj = self.registry('res.users')
  42. res = res_users_obj.authenticate(common.DB, 'admin', 'admin', None)
  43. uid = res
  44. self.assertTrue(res, "Basic login must works as expected")
  45. token = "123456"
  46. res = res_users_obj.authenticate(common.DB, 'admin', token, None)
  47. self.assertFalse(res)
  48. # mimic what the new controller do when it find a value in
  49. # the http header (HTTP_REMODE_USER)
  50. res_users_obj.write(self.cr, self.uid, uid, {'sso_key': token})
  51. # Here we need to mock the cursor since the login is natively done
  52. # inside its own connection
  53. with mock_cursor(self.cr):
  54. # We can verifies that the given (uid, token) is authorized for
  55. # the database
  56. res_users_obj.check(common.DB, uid, token)
  57. # we are able to login with the new token
  58. res = res_users_obj.authenticate(common.DB, 'admin', token, None)
  59. self.assertTrue(res)
  60. @unittest.skipIf(os.environ.get('TRAVIS'),
  61. 'When run by travis, tests runs on a database with all '
  62. 'required addons from server-tools and their dependencies '
  63. 'installed. Even if `auth_from_http_remote_user` does not '
  64. 'require the `mail` module, The previous installation of '
  65. 'the mail module has created the column '
  66. '`notification_email_send` as REQUIRED into the table '
  67. 'res_partner. BTW, it\'s no more possible to copy a '
  68. 'res_user without an intefirty error')
  69. def test_copy(self):
  70. '''Check that the sso_key is not copied on copy
  71. '''
  72. res_users_obj = self.registry('res.users')
  73. vals = {'sso_key': '123'}
  74. res_users_obj.write(self.cr, self.uid, self.uid, vals)
  75. read_vals = res_users_obj.read(self.cr, self.uid, self.uid, ['sso_key'])
  76. self.assertDictContainsSubset(vals, read_vals)
  77. copy = res_users_obj.copy(self.cr, self.uid, self.uid)
  78. read_vals = res_users_obj.read(self.cr, self.uid, copy, ['sso_key'])
  79. self.assertFalse(read_vals.get('sso_key'))