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.

121 lines
4.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 SYLEAM
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import mock
  5. import logging
  6. from datetime import datetime, timedelta
  7. from werkzeug.test import Client
  8. from werkzeug.wrappers import BaseResponse
  9. from openerp import fields
  10. from openerp.service import wsgi_server
  11. from openerp.tests.common import TransactionCase
  12. from openerp.tools.misc import consteq
  13. _logger = logging.getLogger(__name__)
  14. class OAuthProviderControllerTransactionCase(TransactionCase):
  15. def setUp(self, application_type):
  16. super(OAuthProviderControllerTransactionCase, self).setUp()
  17. # Initialize controller test stuff
  18. self.werkzeug_environ = {
  19. 'REMOTE_ADDR': '127.0.0.1',
  20. }
  21. self.user = self.env.ref('base.user_demo')
  22. self.logged_user = False
  23. self.initialize_test_client()
  24. # Initialize common stuff
  25. self.redirect_uri_base = 'http://example.com'
  26. self.filter = self.env['ir.filters'].create({
  27. 'name': 'Current user',
  28. 'model_id': 'res.users',
  29. 'domain': "[('id', '=', uid)]",
  30. })
  31. self.client = self.env['oauth.provider.client'].create({
  32. 'name': 'Client',
  33. 'identifier': 'client',
  34. 'application_type': application_type,
  35. 'redirect_uri_ids': [(0, 0, {'name': self.redirect_uri_base})],
  36. 'scope_ids': [(0, 0, {
  37. 'name': 'Email',
  38. 'code': 'email',
  39. 'description': 'Access to your email address.',
  40. 'model_id': self.env.ref('base.model_res_users').id,
  41. 'filter_id': self.filter.id,
  42. 'field_ids': [
  43. (6, 0, [self.env.ref('base.field_res_users_email').id]),
  44. ],
  45. }), (0, 0, {
  46. 'name': 'Profile',
  47. 'code': 'profile',
  48. 'description': 'Access to your profile details (name, etc.)',
  49. 'model_id': self.env.ref('base.model_res_users').id,
  50. 'filter_id': self.filter.id,
  51. 'field_ids': [
  52. (6, 0, [
  53. self.env.ref('base.field_res_users_name').id,
  54. self.env.ref('base.field_res_users_city').id,
  55. ]),
  56. ],
  57. })],
  58. })
  59. def initialize_test_client(self):
  60. # Instantiate a test client
  61. self.test_client = Client(wsgi_server.application, BaseResponse)
  62. # Select the database
  63. self.get_request('/web', data={'db': self.env.cr.dbname})
  64. def login(self, username, password):
  65. # Login as demo user
  66. self.post_request('/web/login', data={
  67. 'login': username,
  68. 'password': password,
  69. })
  70. self.logged_user = self.env['res.users'].search([
  71. ('login', '=', username)])
  72. def logout(self):
  73. # Login as demo user
  74. self.get_request('/web/session/logout')
  75. self.logged_user = False
  76. @mock.patch('openerp.http.WebRequest.env', new_callable=mock.PropertyMock)
  77. def get_request(self, uri, request_env, data=None, headers=None):
  78. """ Execute a GET request on the test client """
  79. # Mock the http request's environ to allow it to see test records
  80. user = self.logged_user or self.env.ref('base.public_user')
  81. request_env.return_value = self.env(user=user)
  82. return self.test_client.get(
  83. uri, query_string=data, environ_base=self.werkzeug_environ,
  84. headers=headers)
  85. @mock.patch('openerp.http.WebRequest.env', new_callable=mock.PropertyMock)
  86. @mock.patch('openerp.http.WebRequest.validate_csrf')
  87. def post_request(
  88. self, uri, validate_csrf, request_env, data=None, headers=None):
  89. """ Execute a POST request on the test client """
  90. # Mock the http request's environ to allow it to see test records
  91. user = self.logged_user or self.env.ref('base.public_user')
  92. request_env.return_value = self.env(user=user)
  93. # Disable CSRF tokens check during tests
  94. validate_csrf.return_value = consteq('', '')
  95. return self.test_client.post(
  96. uri, data=data, environ_base=self.werkzeug_environ,
  97. headers=headers)
  98. def new_token(self):
  99. return self.env['oauth.provider.token'].create({
  100. 'token': 'token',
  101. 'token_type': 'Bearer',
  102. 'refresh_token': 'refresh token',
  103. 'client_id': self.client.id,
  104. 'user_id': self.user.id,
  105. 'expires_at': fields.Datetime.to_string(
  106. datetime.now() + timedelta(seconds=3600)),
  107. })