Skip to content

Commit d01007d

Browse files
committed
[MIG] endpoint_auth_api_key: Migration to 19.0
1 parent b106e9f commit d01007d

7 files changed

Lines changed: 136 additions & 45 deletions

File tree

endpoint_auth_api_key/README.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ Endpoint Auth API key
2121
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
2222
:alt: License: LGPL-3
2323
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb--api-lightgray.png?logo=github
24-
:target: https://github.com/OCA/web-api/tree/18.0/endpoint_auth_api_key
24+
:target: https://github.com/OCA/web-api/tree/19.0/endpoint_auth_api_key
2525
:alt: OCA/web-api
2626
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
27-
:target: https://translation.odoo-community.org/projects/web-api-18-0/web-api-18-0-endpoint_auth_api_key
27+
:target: https://translation.odoo-community.org/projects/web-api-19-0/web-api-19-0-endpoint_auth_api_key
2828
:alt: Translate me on Weblate
2929
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
30-
:target: https://runboat.odoo-community.org/builds?repo=OCA/web-api&target_branch=18.0
30+
:target: https://runboat.odoo-community.org/builds?repo=OCA/web-api&target_branch=19.0
3131
:alt: Try me on Runboat
3232

3333
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -50,7 +50,7 @@ Bug Tracker
5050
Bugs are tracked on `GitHub Issues <https://github.com/OCA/web-api/issues>`_.
5151
In case of trouble, please check there if your issue has already been reported.
5252
If you spotted it first, help us to smash it by providing a detailed and welcomed
53-
`feedback <https://github.com/OCA/web-api/issues/new?body=module:%20endpoint_auth_api_key%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
53+
`feedback <https://github.com/OCA/web-api/issues/new?body=module:%20endpoint_auth_api_key%0Aversion:%2019.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
5454

5555
Do not contact contributors directly about support or help with technical issues.
5656

@@ -74,8 +74,7 @@ Contributors
7474
Other credits
7575
-------------
7676

77-
The migration of this module from 14.0 to 16.0 was financially supported
78-
by Camptocamp
77+
7978

8079
Maintainers
8180
-----------
@@ -98,6 +97,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
9897

9998
|maintainer-simahawk|
10099

101-
This module is part of the `OCA/web-api <https://github.com/OCA/web-api/tree/18.0/endpoint_auth_api_key>`_ project on GitHub.
100+
This module is part of the `OCA/web-api <https://github.com/OCA/web-api/tree/19.0/endpoint_auth_api_key>`_ project on GitHub.
102101

103102
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

endpoint_auth_api_key/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "Endpoint Auth API key",
66
"summary": """Provide API key auth for endpoints.""",
7-
"version": "18.0.1.0.1",
7+
"version": "19.0.1.0.0",
88
"license": "LGPL-3",
99
"development_status": "Alpha",
1010
"author": "Camptocamp, Odoo Community Association (OCA)",
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
The migration of this module from 14.0 to 16.0 was financially supported
2-
by Camptocamp

endpoint_auth_api_key/static/description/index.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ <h1>Endpoint Auth API key</h1>
374374
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
375375
!! source digest: sha256:cfd515c9f4adfa18204af3865093c9d6d00f92e79b06ce7d7e6ca353a4fc6934
376376
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
377-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Alpha" src="https://img.shields.io/badge/maturity-Alpha-red.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/web-api/tree/18.0/endpoint_auth_api_key"><img alt="OCA/web-api" src="https://img.shields.io/badge/github-OCA%2Fweb--api-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/web-api-18-0/web-api-18-0-endpoint_auth_api_key"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/web-api&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
377+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Alpha" src="https://img.shields.io/badge/maturity-Alpha-red.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/web-api/tree/19.0/endpoint_auth_api_key"><img alt="OCA/web-api" src="https://img.shields.io/badge/github-OCA%2Fweb--api-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/web-api-19-0/web-api-19-0-endpoint_auth_api_key"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/web-api&amp;target_branch=19.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
378378
<p>Provide API key auth for endpoints.</p>
379379
<div class="admonition important">
380380
<p class="first admonition-title">Important</p>
@@ -400,7 +400,7 @@ <h2><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h2>
400400
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/web-api/issues">GitHub Issues</a>.
401401
In case of trouble, please check there if your issue has already been reported.
402402
If you spotted it first, help us to smash it by providing a detailed and welcomed
403-
<a class="reference external" href="https://github.com/OCA/web-api/issues/new?body=module:%20endpoint_auth_api_key%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
403+
<a class="reference external" href="https://github.com/OCA/web-api/issues/new?body=module:%20endpoint_auth_api_key%0Aversion:%2019.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
404404
<p>Do not contact contributors directly about support or help with technical issues.</p>
405405
</div>
406406
<div class="section" id="credits">
@@ -427,8 +427,6 @@ <h3><a class="toc-backref" href="#toc-entry-4">Contributors</a></h3>
427427
</div>
428428
<div class="section" id="other-credits">
429429
<h3><a class="toc-backref" href="#toc-entry-5">Other credits</a></h3>
430-
<p>The migration of this module from 14.0 to 16.0 was financially supported
431-
by Camptocamp</p>
432430
</div>
433431
<div class="section" id="maintainers">
434432
<h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
@@ -441,7 +439,7 @@ <h3><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h3>
441439
promote its widespread use.</p>
442440
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
443441
<p><a class="reference external image-reference" href="https://github.com/simahawk"><img alt="simahawk" src="https://github.com/simahawk.png?size=40px" /></a></p>
444-
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/web-api/tree/18.0/endpoint_auth_api_key">OCA/web-api</a> project on GitHub.</p>
442+
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/web-api/tree/19.0/endpoint_auth_api_key">OCA/web-api</a> project on GitHub.</p>
445443
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
446444
</div>
447445
</div>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

endpoint_auth_api_key/tests/test_endpoint.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55

66
import werkzeug
77

8+
from odoo.tests.common import TransactionCase
89
from odoo.tools.misc import mute_logger
910

10-
from odoo.addons.endpoint.tests.common import CommonEndpoint
11+
from .common import EndpointAuthAPIKeyTestMixin
1112

1213

13-
class TestEndpoint(CommonEndpoint):
14+
class TestEndpoint(TransactionCase, EndpointAuthAPIKeyTestMixin):
1415
@classmethod
15-
def _setup_records(cls):
16-
super()._setup_records()
17-
cls.endpoint = cls.env.ref("endpoint_auth_api_key.endpoint_demo_1")
18-
cls.key_group = cls.env.ref("endpoint_auth_api_key.auth_api_key_group_demo")
19-
cls.api_key = cls.env.ref("endpoint_auth_api_key.auth_api_key_demo")
20-
cls.api_key2 = cls.env.ref("endpoint_auth_api_key.auth_api_key_demo2")
21-
return
16+
def setUpClass(cls):
17+
super().setUpClass()
18+
cls._setup_env()
19+
cls._setup_records()
2220

2321
@mute_logger("endpoint.endpoint")
2422
def test_endpoint_validate_request_no_key(self):

endpoint_auth_api_key/tests/test_endpoint_controller.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,44 @@
55
import os
66
import unittest
77

8-
import requests
9-
10-
from odoo import tools
118
from odoo.tests.common import HttpCase
129
from odoo.tools.misc import mute_logger
1310

11+
from .common import EndpointAuthAPIKeyTestMixin
12+
1413

15-
@unittest.skipIf(os.getenv("SKIP_HTTP_CASE"), "EndpoinAuthApikeytHttpCase skipped")
16-
class EndpoinAuthApikeytHttpCase(HttpCase):
14+
@unittest.skipIf(os.getenv("SKIP_HTTP_CASE"), "EndpointAuthApiKeyHttpCase skipped")
15+
class EndpointAuthApiKeyHttpCase(HttpCase, EndpointAuthAPIKeyTestMixin):
1716
@classmethod
1817
def setUpClass(cls):
1918
super().setUpClass()
20-
cls.api_key = cls.env.ref("endpoint_auth_api_key.auth_api_key_demo")
21-
cls.api_key2 = cls.env.ref("endpoint_auth_api_key.auth_api_key_demo2")
22-
# force sync for demo records
19+
cls._setup_env()
20+
cls._setup_records()
21+
# force sync for test records
2322
cls.env["endpoint.endpoint"].search([])._handle_registry_sync()
2423

2524
def tearDown(self):
2625
# Clear cache for method ``ir.http.routing_map()``
2726
self.env.registry.clear_cache("routing")
2827
super().tearDown()
2928

30-
def _make_url(self, route):
31-
return f"http://127.0.0.1:{tools.config['http_port']}{route}"
32-
3329
def _make_request(self, route, api_key=None, headers=None):
34-
# use requests because you cannot easily manipulate the request w/ `url_open`
35-
headers = headers or {}
30+
headers = dict(headers or {})
3631
if api_key:
37-
headers.update({"API-KEY": api_key.key})
38-
return requests.get(self._make_url(route), headers=headers, timeout=60)
32+
headers["API-KEY"] = api_key.key
33+
return self.url_open(route, headers=headers, timeout=60)
3934

4035
@mute_logger("odoo.addons.auth_api_key.models.ir_http", "odoo.http")
4136
def test_call_no_key(self):
42-
route = "/demo/api/key"
43-
response = self._make_request(route)
37+
response = self._make_request("/test/api/key")
4438
self.assertEqual(response.status_code, 401)
4539

4640
def test_call_good_key(self):
47-
route = "/demo/api/key"
48-
response = self._make_request(route, api_key=self.api_key)
41+
response = self._make_request("/test/api/key", api_key=self.api_key)
4942
self.assertEqual(response.status_code, 200)
5043
self.assertEqual(response.content, b"ok")
5144

5245
@mute_logger("endpoint.endpoint")
5346
def test_call_bad_key(self):
54-
route = "/demo/api/key"
55-
response = self._make_request(route, api_key=self.api_key2)
47+
response = self._make_request("/test/api/key", api_key=self.api_key2)
5648
self.assertEqual(response.status_code, 403)

0 commit comments

Comments
 (0)