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.

93 lines
3.8 KiB

10 years ago
10 years ago
10 years ago
  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(
  43. common.get_db_name(), 'admin', 'admin', None)
  44. uid = res
  45. self.assertTrue(res, "Basic login must works as expected")
  46. token = "123456"
  47. res = res_users_obj.authenticate(
  48. common.get_db_name(), 'admin', token, None)
  49. self.assertFalse(res)
  50. # mimic what the new controller do when it find a value in
  51. # the http header (HTTP_REMODE_USER)
  52. res_users_obj.write(self.cr, self.uid, uid, {'sso_key': token})
  53. # Here we need to mock the cursor since the login is natively done
  54. # inside its own connection
  55. with mock_cursor(self.cr):
  56. # We can verifies that the given (uid, token) is authorized for
  57. # the database
  58. res_users_obj.check(common.get_db_name(), uid, token)
  59. # we are able to login with the new token
  60. res = res_users_obj.authenticate(
  61. common.get_db_name(), 'admin', token, None)
  62. self.assertTrue(res)
  63. @unittest.skipIf(os.environ.get('TRAVIS'),
  64. 'When run by travis, tests runs on a database with all '
  65. 'required addons from server-tools and their dependencies'
  66. ' installed. Even if `auth_from_http_remote_user` does '
  67. 'not require the `mail` module, The previous installation'
  68. ' of the mail module has created the column '
  69. '`notification_email_send` as REQUIRED into the table '
  70. 'res_partner. BTW, it\'s no more possible to copy a '
  71. 'res_user without an intefirty error')
  72. def test_copy(self):
  73. '''Check that the sso_key is not copied on copy
  74. '''
  75. res_users_obj = self.registry('res.users')
  76. vals = {'sso_key': '123'}
  77. res_users_obj.write(self.cr, self.uid, self.uid, vals)
  78. read_vals = res_users_obj.read(
  79. self.cr, self.uid, self.uid, ['sso_key'])
  80. self.assertDictContainsSubset(vals, read_vals)
  81. copy = res_users_obj.copy(self.cr, self.uid, self.uid)
  82. read_vals = res_users_obj.read(
  83. self.cr, self.uid, copy, ['sso_key'])
  84. self.assertFalse(read_vals.get('sso_key'))