|
| 1 | +# Copyright 2026 Camptocamp SA |
| 2 | +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). |
| 3 | +import contextlib |
| 4 | + |
| 5 | +from odoo import Command |
| 6 | +from odoo.tools import DotDict |
| 7 | + |
| 8 | +from odoo.addons.http_routing.tests.common import MockRequest |
| 9 | + |
| 10 | + |
| 11 | +def _setup_test_api_keys(env, user): |
| 12 | + """Create API keys for tests.""" |
| 13 | + api_key_model = env["auth.api.key"] |
| 14 | + |
| 15 | + api_key_1 = api_key_model.create( |
| 16 | + { |
| 17 | + "name": "Endpoint API key test", |
| 18 | + "key": "tcZ6dF2UQwNcm", |
| 19 | + "user_id": user.id, |
| 20 | + } |
| 21 | + ) |
| 22 | + api_key_2 = api_key_model.create( |
| 23 | + { |
| 24 | + "name": "Endpoint API key test 2", |
| 25 | + "key": "tV47QyOTC5mS", |
| 26 | + "user_id": user.id, |
| 27 | + } |
| 28 | + ) |
| 29 | + return api_key_1, api_key_2 |
| 30 | + |
| 31 | + |
| 32 | +def _setup_test_api_key_group(env, api_key_1): |
| 33 | + """Create API key group for tests.""" |
| 34 | + return env["auth.api.key.group"].create( |
| 35 | + { |
| 36 | + "name": "Test Group 1", |
| 37 | + "code": "test_group1", |
| 38 | + "auth_api_key_ids": [Command.set(api_key_1.ids)], |
| 39 | + } |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def _setup_test_endpoint(env, api_key_group): |
| 44 | + """Create endpoint for tests.""" |
| 45 | + return env["endpoint.endpoint"].create( |
| 46 | + { |
| 47 | + "name": "Test Endpoint - auth api key", |
| 48 | + "route": "/test/api/key", |
| 49 | + "request_method": "GET", |
| 50 | + "auth_type": "api_key", |
| 51 | + "auth_api_key_group_ids": [Command.set(api_key_group.ids)], |
| 52 | + "exec_mode": "code", |
| 53 | + "code_snippet": 'result = {"response": Response("ok")}', |
| 54 | + } |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +class EndpointAuthAPIKeyTestMixin: |
| 59 | + # Keep these helpers separately instead of inheriting from |
| 60 | + # endpoint.tests.common.CommonEndpoint. Importing that test class |
| 61 | + # from test_endpoint.py also pulls in the endpoint test asset bundle, which |
| 62 | + # makes these auth API key tests fail during asset loading. |
| 63 | + # WARNING odoo odoo.addons.base.models.assetsbundle: |
| 64 | + # Error: Undefined variable: "$black". |
| 65 | + @classmethod |
| 66 | + def _setup_env(cls): |
| 67 | + cls.env = cls.env(context=cls._setup_context()) |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def _setup_context(cls): |
| 71 | + return dict( |
| 72 | + cls.env.context, |
| 73 | + tracking_disable=True, |
| 74 | + ) |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def _setup_records(cls): |
| 78 | + cls.api_key, cls.api_key2 = _setup_test_api_keys( |
| 79 | + cls.env, |
| 80 | + cls.env.user, |
| 81 | + ) |
| 82 | + cls.key_group = _setup_test_api_key_group( |
| 83 | + cls.env, |
| 84 | + cls.api_key, |
| 85 | + ) |
| 86 | + cls.endpoint = _setup_test_endpoint( |
| 87 | + cls.env, |
| 88 | + cls.key_group, |
| 89 | + ) |
| 90 | + |
| 91 | + @contextlib.contextmanager |
| 92 | + def _get_mocked_request( |
| 93 | + self, httprequest=None, extra_headers=None, request_attrs=None |
| 94 | + ): |
| 95 | + with MockRequest(self.env) as mocked_request: |
| 96 | + mocked_request.httprequest = ( |
| 97 | + DotDict(httprequest) if httprequest else mocked_request.httprequest |
| 98 | + ) |
| 99 | + headers = {} |
| 100 | + headers.update(extra_headers or {}) |
| 101 | + mocked_request.httprequest.headers = headers |
| 102 | + request_attrs = request_attrs or {} |
| 103 | + for k, v in request_attrs.items(): |
| 104 | + setattr(mocked_request, k, v) |
| 105 | + mocked_request.make_response = lambda data, **kw: data |
| 106 | + yield mocked_request |
0 commit comments