From 6e64a2efb6bcd9d9e3ca8b1543d5a73b7068ec6b Mon Sep 17 00:00:00 2001 From: sonhd91 Date: Fri, 24 May 2024 18:23:29 +0700 Subject: [PATCH] [MIG] fastapi_auth_api_key: Migration to 17.0 --- fastapi_auth_api_key/README.rst | 16 +++++++++----- fastapi_auth_api_key/__manifest__.py | 2 +- fastapi_auth_api_key/dependencies.py | 4 ++-- fastapi_auth_api_key/readme/CONTRIBUTORS.rst | 1 + fastapi_auth_api_key/readme/CREDITS.rst | 1 + .../test_fastapi_api_key_dependencies.py | 22 ++++++++++++++----- .../views/fastapi_endpoint.xml | 2 +- 7 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 fastapi_auth_api_key/readme/CREDITS.rst diff --git a/fastapi_auth_api_key/README.rst b/fastapi_auth_api_key/README.rst index bd0a9aaf7..c6a861d15 100644 --- a/fastapi_auth_api_key/README.rst +++ b/fastapi_auth_api_key/README.rst @@ -17,13 +17,13 @@ Fastapi Auth Api Key :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github - :target: https://github.com/OCA/rest-framework/tree/16.0/fastapi_auth_api_key + :target: https://github.com/OCA/rest-framework/tree/17.0/fastapi_auth_api_key :alt: OCA/rest-framework .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi_auth_api_key + :target: https://translation.odoo-community.org/projects/rest-framework-17-0/rest-framework-17-0-fastapi_auth_api_key :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png - :target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=16.0 + :target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=17.0 :alt: Try me on Runboat |badge1| |badge2| |badge3| |badge4| |badge5| @@ -78,7 +78,7 @@ Bug Tracker Bugs are tracked on `GitHub Issues `_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -94,6 +94,12 @@ Contributors ~~~~~~~~~~~~ * Matthieu Méquignon +* Son Ho + +Other credits +~~~~~~~~~~~~~ + +The migration of this module from 16.0 to 17.0 was financially supported by Camptocamp Maintainers ~~~~~~~~~~~ @@ -116,6 +122,6 @@ Current `maintainer `__: |maintainer-mmequignon| -This module is part of the `OCA/rest-framework `_ project on GitHub. +This module is part of the `OCA/rest-framework `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/fastapi_auth_api_key/__manifest__.py b/fastapi_auth_api_key/__manifest__.py index 1735c59be..539efdcc1 100644 --- a/fastapi_auth_api_key/__manifest__.py +++ b/fastapi_auth_api_key/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Fastapi Auth Api Key", - "version": "16.0.1.0.0", + "version": "17.0.1.0.0", "category": "Others", "website": "https://github.com/OCA/rest-framework", "author": "Camptocamp, Odoo Community Association (OCA)", diff --git a/fastapi_auth_api_key/dependencies.py b/fastapi_auth_api_key/dependencies.py index 3b71e6d58..aa1838ab0 100644 --- a/fastapi_auth_api_key/dependencies.py +++ b/fastapi_auth_api_key/dependencies.py @@ -1,7 +1,7 @@ # Copyright 2024 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) -from typing_extensions import Annotated +from typing import Annotated from odoo import SUPERUSER_ID from odoo.api import Environment @@ -34,7 +34,7 @@ def authenticated_auth_api_key( except ValidationError as error: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, - detail=error.name, + detail=error.args, headers={"WWW-Authenticate": "HTTP-API-KEY"}, ) from error # Ensure the api key is authorized for the current endpoint. diff --git a/fastapi_auth_api_key/readme/CONTRIBUTORS.rst b/fastapi_auth_api_key/readme/CONTRIBUTORS.rst index bca4ee0ca..1f953c296 100644 --- a/fastapi_auth_api_key/readme/CONTRIBUTORS.rst +++ b/fastapi_auth_api_key/readme/CONTRIBUTORS.rst @@ -1 +1,2 @@ * Matthieu Méquignon +* Son Ho \ No newline at end of file diff --git a/fastapi_auth_api_key/readme/CREDITS.rst b/fastapi_auth_api_key/readme/CREDITS.rst new file mode 100644 index 000000000..268326161 --- /dev/null +++ b/fastapi_auth_api_key/readme/CREDITS.rst @@ -0,0 +1 @@ +The migration of this module from 16.0 to 17.0 was financially supported by Camptocamp diff --git a/fastapi_auth_api_key/tests/test_fastapi_api_key_dependencies.py b/fastapi_auth_api_key/tests/test_fastapi_api_key_dependencies.py index d746e1ccc..dde6088ca 100644 --- a/fastapi_auth_api_key/tests/test_fastapi_api_key_dependencies.py +++ b/fastapi_auth_api_key/tests/test_fastapi_api_key_dependencies.py @@ -17,8 +17,17 @@ def setUpClass(cls): super().setUpClass() cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) # Is it valid? We need an env without superpowers - demo_user = cls.env.ref("base.partner_demo") + demo_user = cls.env.ref("base.user_demo") cls.demo_env = demo_user.with_user(demo_user).env + cls.demo_endpoint = cls.env["fastapi.endpoint"].create( + { + "name": "Test Enpoint", + "app": "demo", + "root_path": "/path_demo", + "demo_auth_method": "api_key", + "user_id": demo_user.id, + } + ) cls.setUpClassApiKey() @classmethod @@ -63,19 +72,22 @@ def setUpClassApiKey(cls): def test_authenticated_auth_api_key(self): # An exception is raised when no api key is used with self.assertRaises(HTTPException) as error: - authenticated_auth_api_key(False, self.demo_env) + authenticated_auth_api_key(False, self.demo_env, self.demo_endpoint) self.assertEqual(error.exception.detail, "No HTTP-API-KEY provided") # An exception is raised when no api key record is found with self.assertRaises(HTTPException) as error: - authenticated_auth_api_key("404", self.demo_env) - self.assertEqual(error.exception.detail, "The key 404 is not allowed") + authenticated_auth_api_key("404", self.demo_env, self.demo_endpoint) + self.assertEqual(error.exception.detail, ("The key 404 is not allowed",)) # TODO enable this when we know how to filter keys based # on endpoint's api key group. # An exception is raised when unauthorized api key record is found # with self.assertRaises(HTTPException) as error: # authenticated_auth_api_key("not_authorized", self.demo_env) + self.demo_endpoint.auth_api_key_group_id = ( + self.authorized_api_key.auth_api_key_group_ids[0] + ) result_key = authenticated_auth_api_key( - self.authorized_api_key.key, self.demo_env + self.authorized_api_key.key, self.demo_env, self.demo_endpoint ) self.assertEqual(result_key, self.authorized_api_key) diff --git a/fastapi_auth_api_key/views/fastapi_endpoint.xml b/fastapi_auth_api_key/views/fastapi_endpoint.xml index bd8ba2a90..1005dd501 100644 --- a/fastapi_auth_api_key/views/fastapi_endpoint.xml +++ b/fastapi_auth_api_key/views/fastapi_endpoint.xml @@ -8,7 +8,7 @@ fastapi.endpoint - +