diff --git a/g2p_social_registry/__init__.py b/g2p_social_registry/__init__.py index 9635e7d..bc9454e 100644 --- a/g2p_social_registry/__init__.py +++ b/g2p_social_registry/__init__.py @@ -1,8 +1,8 @@ # Part of OpenG2P Registry. See LICENSE file for full copyright and licensing details. from . import models - def post_init_hook(env): - partners = env["res.partner"].search([("is_registrant", "=", True), ("ref_id", "=", False)]) + partners = env["res.partner"].search([("is_registrant", "=", True), ("unique_id", "=", False)]) for partner in partners: - env["g2p.pending.reference_id"].create({"registrant_id": partner.id, "status": "failed"}) + env["g2p.pending.reference_id"].create({"registrant_id": partner.id, "id_generation_request_status": "pending", + "id_generation_update_status": "not_applicable"}) diff --git a/g2p_social_registry/__manifest__.py b/g2p_social_registry/__manifest__.py index b08dc49..b1abf16 100644 --- a/g2p_social_registry/__manifest__.py +++ b/g2p_social_registry/__manifest__.py @@ -15,12 +15,9 @@ "external_dependencies": {"python": ["python-jose"]}, "data": [ "security/ir.model.access.csv", - "data/cron_job.xml", - "data/default_ref_id_config.xml", "views/main_view.xml", "views/region.xml", "views/registrant_view.xml", - "views/res_config_settings_view.xml", ], "assets": {}, "demo": [], diff --git a/g2p_social_registry/data/cron_job.xml b/g2p_social_registry/data/cron_job.xml deleted file mode 100644 index cd7a0d5..0000000 --- a/g2p_social_registry/data/cron_job.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - Reference ID Generation Cron Job - - code - model.retry_generate_ref_id() - 1 - minutes - -1 - - - - diff --git a/g2p_social_registry/data/default_ref_id_config.xml b/g2p_social_registry/data/default_ref_id_config.xml deleted file mode 100644 index f706f7f..0000000 --- a/g2p_social_registry/data/default_ref_id_config.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - Default Reference ID Config - - diff --git a/g2p_social_registry/i18n/g2p_social_registry.pot b/g2p_social_registry/i18n/g2p_social_registry.pot index ac64499..ac24d6a 100644 --- a/g2p_social_registry/i18n/g2p_social_registry.pot +++ b/g2p_social_registry/i18n/g2p_social_registry.pot @@ -313,5 +313,5 @@ msgstr "" #. module: g2p_social_registry #: model:ir.model.constraint,message:g2p_social_registry.constraint_res_partner_ref_id_uniq -msgid "ref_id is an unique identifier!" +msgid "unique_id is an unique identifier!" msgstr "" diff --git a/g2p_social_registry/models/__init__.py b/g2p_social_registry/models/__init__.py index b27b348..68d4c27 100644 --- a/g2p_social_registry/models/__init__.py +++ b/g2p_social_registry/models/__init__.py @@ -1,7 +1,5 @@ # Part of OpenG2P. See LICENSE file for full copyright and licensing details. from . import region -from . import ref_id_config -from . import res_config_settings from . import registrant -from . import pending_ref_id +from . import g2p_que_id_generation diff --git a/g2p_social_registry/models/g2p_que_id_generation.py b/g2p_social_registry/models/g2p_que_id_generation.py new file mode 100644 index 0000000..c1ad43d --- /dev/null +++ b/g2p_social_registry/models/g2p_que_id_generation.py @@ -0,0 +1,70 @@ +from odoo import models, fields, api +from datetime import datetime + + +class G2PQueIDGeneration(models.Model): + _name = "g2p.que.id.generation" + _description = "G2P Queue ID Generation" + + registrant_id = fields.Char( + string="Registrant ID", + required=True, + unique=True, + ) + + id_generation_request_status = fields.Selection( + selection=[ + ('pending', 'Pending'), + ('approved', 'Approved'), + ('rejected', 'Rejected'), + ], + string="ID Generation Request Status", + required=True, + default='pending', + ) + + id_generation_update_status = fields.Selection( + selection=[ + ('not_applicable', 'Not Applicable'), + ('in_progress', 'In Progress'), + ('completed', 'Completed'), + ], + string="ID Generation Update Status", + required=True, + default='not_applicable', + ) + + queued_datetime = fields.Datetime( + string="Queued Datetime", + required=True, + default=fields.Datetime.now, + ) + + number_of_attempts_request = fields.Integer( + string="Number of Attempts (Request)", + required=True, + default=0, + ) + + number_of_attempts_update = fields.Integer( + string="Number of Attempts (Update)", + required=True, + default=0, + ) + + last_attempt_error_code_request = fields.Char( + string="Last Attempt Error Code (Request)" + ) + + last_attempt_error_code_update = fields.Char( + string="Last Attempt Error Code (Update)" + ) + + last_attempt_datetime_request = fields.Datetime( + string="Last Attempt Datetime (Request)" + ) + + last_attempt_datetime_update = fields.Datetime( + string="Last Attempt Datetime (Update)" + ) + diff --git a/g2p_social_registry/models/pending_ref_id.py b/g2p_social_registry/models/pending_ref_id.py deleted file mode 100644 index e66c015..0000000 --- a/g2p_social_registry/models/pending_ref_id.py +++ /dev/null @@ -1,36 +0,0 @@ -import logging - -from odoo import api, fields, models - -_logger = logging.getLogger(__name__) - - -class FallbackTable(models.Model): - _name = "g2p.pending.reference_id" - - registrant_id = fields.Many2one("res.partner") - ref_id = fields.Char() - status = fields.Selection([("failed", "Failed"), ("success", "Success")], default="failed") - - @api.model - def retry_generate_ref_id(self): - records = self.search([("status", "=", "failed")]) - for record in records: - partner = record.registrant_id - if partner: - partner.generate_ref_id() - if partner.ref_id: - record.status = "success" - record.ref_id = partner.ref_id - - def generate_ref_id_for_selected(self): - for rec in self.env.context.get("active_ids"): - partner = self.env["res.partner"].browse(rec) - registrant = self.env["g2p.pending.reference_id"].search([("registrant_id", "=", rec)], limit=1) - - if not partner.ref_id: - partner.generate_ref_id() - - if partner.ref_id and registrant: - registrant.status = "success" - registrant.ref_id = partner.ref_id diff --git a/g2p_social_registry/models/ref_id_config.py b/g2p_social_registry/models/ref_id_config.py deleted file mode 100644 index c10acc6..0000000 --- a/g2p_social_registry/models/ref_id_config.py +++ /dev/null @@ -1,76 +0,0 @@ -import logging -import os -from datetime import datetime - -import requests -from jose import jwt - -from odoo import _, api, fields, models -from odoo.exceptions import UserError - -_logger = logging.getLogger(__name__) - -ID_GENERATOR_API_BASE_URL = os.getenv( - "ID_GENERATOR_API_BASE_URL", "https://idgenerator.sandbox.net/v1/idgenerator/uin" -) -ID_GENERATOR_AUTH_URL = os.getenv( - "ID_GENERATOR_AUTH_URL", - "https://keycloak.openg2p.org/realms/master/protocol/openid-connect/token", -) -ID_GENERATOR_AUTH_CLIENT_ID = os.getenv("ID_GENERATOR_AUTH_CLIENT_ID", "") -ID_GENERATOR_AUTH_CLIENT_SECRET = os.getenv("ID_GENERATOR_AUTH_CLIENT_SECRET", "") -ID_GENERATOR_AUTH_GRANT_TYPE = os.getenv("ID_GENERATOR_AUTH_GRANT_TYPE", "client_credentials") - - -class G2PReferenceIdconfig(models.Model): - _name = "g2p.reference_id.config" - _description = "G2P Reference ID Configuration" - - name = fields.Char() - base_api_url = fields.Char(default=ID_GENERATOR_API_BASE_URL) - auth_url = fields.Char(default=ID_GENERATOR_AUTH_URL) - auth_client_id = fields.Char(default=ID_GENERATOR_AUTH_CLIENT_ID) - auth_client_secret = fields.Char(default=ID_GENERATOR_AUTH_CLIENT_SECRET) - auth_grant_type = fields.Char(default=ID_GENERATOR_AUTH_GRANT_TYPE) - api_timeout = fields.Integer(default=10) - - access_token = fields.Char() - access_token_expiry = fields.Datetime() - - @api.model - def get_config(self): - return self.search([], limit=1) - - def get_access_token(self): - self.ensure_one() - - if self.access_token and self.access_token_expiry and self.access_token_expiry > datetime.utcnow(): - return self.access_token - - if not self.auth_url: - raise UserError(_("ID Generator Authentication URL is not set")) - - data = { - "grant_type": self.auth_grant_type, - "client_id": self.auth_client_id, - "client_secret": self.auth_client_secret, - } - - response = requests.post(self.auth_url, data=data, timeout=self.api_timeout) - _logger.debug("ID Generator Authentication API response: %s", response.text) - response.raise_for_status() - - access_token = response.json().get("access_token", None) - token_exp = jwt.get_unverified_claims(access_token).get("exp") - - self.write( - { - "access_token": access_token, - "access_token_expiry": datetime.fromtimestamp(token_exp) - if isinstance(token_exp, int) - else datetime.fromisoformat(token_exp) - if isinstance(token_exp, str) - else token_exp, - } - ) - return access_token diff --git a/g2p_social_registry/models/registrant.py b/g2p_social_registry/models/registrant.py index 57f7597..e1144bb 100644 --- a/g2p_social_registry/models/registrant.py +++ b/g2p_social_registry/models/registrant.py @@ -13,43 +13,22 @@ class ResPartner(models.Model): _inherit = "res.partner" region = fields.Many2one("g2p.region") - ref_id = fields.Char(string="Reference ID", index=True) + unique_id = fields.Char(string="Reference ID", index=True) _sql_constraints = [ - ("ref_id_uniq", "UNIQUE(ref_id)", "ref_id is an unique identifier!"), + ("unique_id_uniq", "UNIQUE(unique_id)", "unique_id is an unique identifier!"), ] @api.model def create(self, vals): record = super().create(vals) if vals.get("is_registrant"): - record.generate_ref_id() + record.generate_unique_id_id() return record - def generate_ref_id(self): + def generate_unique_id_id(self): for rec in self: - try: - config = self.env["g2p.reference_id.config"].get_config() - - access_token = config.get_access_token() - headers = {"Cookie": f"Authorization={access_token}"} - - response = requests.get(config.base_api_url, headers=headers, timeout=config.api_timeout) - _logger.debug("ID Generator API response: %s", response.text) - response.raise_for_status() - res = response.json() - - unique_id = res.get("response")["uin"] - rec.ref_id = rec.get_ref_id_prefix() + unique_id if rec.get_ref_id_prefix() else unique_id - - except Exception as e: - _logger.error("Failed to generate ref_id for partner %s: %s", rec.id, str(e)) - - pending_ref_id_model = self.env["g2p.pending.reference_id"] - - if not pending_ref_id_model.search([("registrant_id", "=", rec.id)]): - pending_ref_id_model.create({"registrant_id": rec.id, "status": "failed"}) - - def get_ref_id_prefix(self): - # Override this method for customization - return + g2p_que_id_model = self.env["g2p.que.id.generation"] + if not g2p_que_id_model.search([("registrant_id", "=", rec.id)]): + g2p_que_id_model.create({"registrant_id": rec.id, "id_generation_request_status": "pending", + "id_generation_update_status": "not_applicable"}) diff --git a/g2p_social_registry/models/res_config_settings.py b/g2p_social_registry/models/res_config_settings.py deleted file mode 100644 index 4f9c560..0000000 --- a/g2p_social_registry/models/res_config_settings.py +++ /dev/null @@ -1,73 +0,0 @@ -# Part of OpenG2P. See LICENSE file for full copyright and licensing details. - -import logging - -from odoo import _, api, fields, models -from odoo.exceptions import UserError - -_logger = logging.getLogger(__name__) - - -class RegistryConfig(models.TransientModel): - _inherit = "res.config.settings" - - id_generator_base_api_url = fields.Char() - id_generator_auth_url = fields.Char() - id_generator_auth_client_id = fields.Char() - id_generator_auth_client_secret = fields.Char() - id_generator_auth_grant_type = fields.Char() - id_generator_api_timeout = fields.Integer() - - def set_values(self): - res = super().set_values() - config = self.env["g2p.reference_id.config"].get_config() - - config.write( - { - "base_api_url": self.id_generator_base_api_url, - "auth_url": self.id_generator_auth_url, - "auth_client_id": self.id_generator_auth_client_id, - "auth_client_secret": self.id_generator_auth_client_secret, - "auth_grant_type": self.id_generator_auth_grant_type, - "api_timeout": self.id_generator_api_timeout, - } - ) - - return res - - @api.model - def get_values(self): - res = super().get_values() - config = self.env["g2p.reference_id.config"].get_config() - - res.update( - { - "id_generator_base_api_url": config.base_api_url, - "id_generator_auth_url": config.auth_url, - "id_generator_auth_client_id": config.auth_client_id, - "id_generator_auth_client_secret": config.auth_client_secret, - "id_generator_auth_grant_type": config.auth_grant_type, - "id_generator_api_timeout": config.api_timeout, - } - ) - - return res - - def add_missing_ref_id_to_retry(self): - query = """ - SELECT id FROM res_partner - WHERE is_registrant = TRUE AND ref_id is Null - """ - - pending_ref_id_model = self.env["g2p.pending.reference_id"] - - try: - self._cr.execute(query) # pylint: disable=sql-injection - - for rec in [record[0] for record in (self._cr.fetchall())]: - if not pending_ref_id_model.search([("registrant_id", "=", rec)], limit=1): - pending_ref_id_model.create({"registrant_id": rec, "status": "failed"}) - - except Exception as e: - _logger.error("Database Query Error: %s", e) - raise UserError(_("Database Query Error: %s") % e) from None diff --git a/g2p_social_registry/tests/__init__.py b/g2p_social_registry/tests/__init__.py index 33a66eb..e7f6e91 100644 --- a/g2p_social_registry/tests/__init__.py +++ b/g2p_social_registry/tests/__init__.py @@ -1,5 +1,3 @@ # Part of OpenG2P Social Registry. See LICENSE file for full copyright and licensing details. -from . import test_ref_id_config -from . import test_res_config_settings -from . import test_pending_ref_id +from . import test_g2p_que_id_generation diff --git a/g2p_social_registry/tests/test_g2p_que_id_generation.py b/g2p_social_registry/tests/test_g2p_que_id_generation.py new file mode 100644 index 0000000..d3cd496 --- /dev/null +++ b/g2p_social_registry/tests/test_g2p_que_id_generation.py @@ -0,0 +1,190 @@ +from odoo.tests import TransactionCase +from odoo.exceptions import ValidationError + + +class TestG2PQueIDGeneration(TransactionCase): + def setUp(self): + super().setUp() + # Create test partners to be used as registrants + self.partner1 = self.env["res.partner"].create({"name": "Test Partner 1"}) + self.partner2 = self.env["res.partner"].create({"name": "Test Partner 2"}) + + # Create an instance of the G2PQueIDGeneration model + self.g2p_queue = self.env["g2p.que.id.generation"].create( + { + "registrant_id": self.partner1.id, + "id_generation_request_status": "pending", + "id_generation_update_status": "not_applicable", + # Other fields will take default values + } + ) + + def test_create_g2p_queue_record(self): + """ + Test that a G2PQueIDGeneration record is created successfully with valid data. + """ + self.assertTrue(self.g2p_queue, "G2PQueIDGeneration record was not created.") + self.assertEqual( + self.g2p_queue.registrant_id, self.partner1, + "Registrant ID does not match the expected partner." + ) + self.assertEqual( + self.g2p_queue.id_generation_request_status, "pending", + "Default ID Generation Request Status should be 'pending'." + ) + self.assertEqual( + self.g2p_queue.id_generation_update_status, "not_applicable", + "Default ID Generation Update Status should be 'not_applicable'." + ) + self.assertEqual( + self.g2p_queue.number_of_attempts_request, 0, + "Default number_of_attempts_request should be 0." + ) + self.assertEqual( + self.g2p_queue.number_of_attempts_update, 0, + "Default number_of_attempts_update should be 0." + ) + self.assertFalse( + self.g2p_queue.last_attempt_error_code_request, + "last_attempt_error_code_request should be empty by default." + ) + self.assertFalse( + self.g2p_queue.last_attempt_error_code_update, + "last_attempt_error_code_update should be empty by default." + ) + self.assertFalse( + self.g2p_queue.last_attempt_datetime_request, + "last_attempt_datetime_request should be empty by default." + ) + self.assertFalse( + self.g2p_queue.last_attempt_datetime_update, + "last_attempt_datetime_update should be empty by default." + ) + self.assertTrue( + self.g2p_queue.queued_datetime, + "queued_datetime should be set by default." + ) + + def test_unique_registrant_id_constraint(self): + """ + Test that the registrant_id must be unique across G2PQueIDGeneration records. + Attempting to create a second record with the same registrant_id should raise a ValidationError. + """ + with self.assertRaises(ValidationError, msg="Duplicate registrant_id should raise ValidationError."): + self.env["g2p.que.id.generation"].create( + { + "registrant_id": self.partner1.id, + "id_generation_request_status": "pending", + "id_generation_update_status": "not_applicable", + } + ) + + def test_multiple_g2p_queue_records(self): + """ + Test creating multiple G2PQueIDGeneration records with unique registrant_ids. + """ + record2 = self.env["g2p.que.id.generation"].create( + { + "registrant_id": self.partner2.id, + "id_generation_request_status": "approved", + "id_generation_update_status": "in_progress", + "last_attempt_error_code_request": "ERR001", + "last_attempt_datetime_request": fields.Datetime.now(), + } + ) + + self.assertTrue(record2, "Second G2PQueIDGeneration record was not created.") + self.assertEqual( + record2.registrant_id, self.partner2, + "Registrant ID for the second record does not match the expected partner." + ) + self.assertEqual( + record2.id_generation_request_status, "approved", + "ID Generation Request Status should be 'approved'." + ) + self.assertEqual( + record2.id_generation_update_status, "in_progress", + "ID Generation Update Status should be 'in_progress'." + ) + + self.assertEqual( + record2.last_attempt_error_code_request, "ERR001", + "last_attempt_error_code_request should be 'ERR001'." + ) + self.assertTrue( + record2.last_attempt_datetime_request, + "last_attempt_datetime_request should be set." + ) + + def test_field_assignments(self): + """ + Test that fields can be updated correctly after record creation. + """ + self.g2p_queue.write( + { + "id_generation_request_status": "approved", + "id_generation_update_status": "completed", + "number_of_attempts_request": 1, + } + ) + + self.assertEqual( + self.g2p_queue.id_generation_request_status, "approved", + "ID Generation Request Status should be updated to 'approved'." + ) + self.assertEqual( + self.g2p_queue.id_generation_update_status, "completed", + "ID Generation Update Status should be updated to 'completed'." + ) + self.assertEqual( + self.g2p_queue.number_of_attempts_request, 1, + "number_of_attempts_request should be updated to 1." + ) + + + def test_default_values_on_creation(self): + """ + Test that default values are correctly set when creating a new record without specifying them. + """ + new_record = self.env["g2p.que.id.generation"].create( + { + "registrant_id": self.partner2.id, + } + ) + + self.assertEqual( + new_record.id_generation_request_status, "pending", + "Default ID Generation Request Status should be 'pending'." + ) + self.assertEqual( + new_record.id_generation_update_status, "not_applicable", + "Default ID Generation Update Status should be 'not_applicable'." + ) + self.assertEqual( + new_record.number_of_attempts_request, 0, + "Default number_of_attempts_request should be 0." + ) + self.assertEqual( + new_record.number_of_attempts_update, 0, + "Default number_of_attempts_update should be 0." + ) + self.assertFalse( + new_record.last_attempt_error_code_request, + "last_attempt_error_code_request should be empty by default." + ) + self.assertFalse( + new_record.last_attempt_error_code_update, + "last_attempt_error_code_update should be empty by default." + ) + self.assertFalse( + new_record.last_attempt_datetime_request, + "last_attempt_datetime_request should be empty by default." + ) + self.assertFalse( + new_record.last_attempt_datetime_update, + "last_attempt_datetime_update should be empty by default." + ) + self.assertTrue( + new_record.queued_datetime, + "queued_datetime should be set by default." + ) diff --git a/g2p_social_registry/tests/test_pending_ref_id.py b/g2p_social_registry/tests/test_pending_ref_id.py deleted file mode 100644 index a05f79a..0000000 --- a/g2p_social_registry/tests/test_pending_ref_id.py +++ /dev/null @@ -1,47 +0,0 @@ -from unittest.mock import patch - -from odoo.tests import TransactionCase - - -class TestFallbackTable(TransactionCase): - def setUp(self): - super().setUp() - self.fallback_table = self.env["g2p.pending.reference_id"].create( - { - "registrant_id": self.env["res.partner"].create({"name": "Test Partner"}).id, - "ref_id": "", - "status": "failed", - } - ) - - @patch("odoo.addons.g2p_social_registry.models.registrant.ResPartner.generate_ref_id") - def test_retry_generate_ref_id_success(self, mock_generate_ref_id): - mock_generate_ref_id.return_value = None - self.fallback_table.registrant_id.ref_id = "some_ref_id" - self.fallback_table.retry_generate_ref_id() - self.assertEqual(self.fallback_table.status, "success") - self.assertEqual(self.fallback_table.ref_id, "some_ref_id") - - @patch("odoo.addons.g2p_social_registry.models.registrant.ResPartner.generate_ref_id") - def test_retry_generate_ref_id_failure(self, mock_generate_ref_id): - mock_generate_ref_id.return_value = False - self.fallback_table.retry_generate_ref_id() - self.assertEqual(self.fallback_table.status, "failed") - - @patch("odoo.addons.g2p_social_registry.models.registrant.ResPartner.generate_ref_id") - def test_generate_ref_id_for_selected_failure(self, mock_generate_ref_id): - mock_generate_ref_id.return_value = False - self.env.context = {"active_ids": [self.fallback_table.registrant_id.id]} - self.fallback_table.generate_ref_id_for_selected() - self.assertEqual(self.fallback_table.status, "failed") - - @patch("odoo.addons.g2p_social_registry.models.registrant.ResPartner.generate_ref_id") - @patch("odoo.addons.g2p_social_registry.models.pending_ref_id.FallbackTable.search") - def test_generate_ref_id_for_selected_no_registrant(self, mock_search, mock_generate_ref_id): - mock_search.return_value = False - mock_generate_ref_id.return_value = True - self.env.context = {"active_ids": [self.fallback_table.registrant_id.id]} - self.fallback_table.generate_ref_id_for_selected() - mock_search.assert_called_once_with( - [("registrant_id", "=", self.fallback_table.registrant_id.id)], limit=1 - ) diff --git a/g2p_social_registry/tests/test_ref_id_config.py b/g2p_social_registry/tests/test_ref_id_config.py deleted file mode 100644 index 4b192bb..0000000 --- a/g2p_social_registry/tests/test_ref_id_config.py +++ /dev/null @@ -1,67 +0,0 @@ -from unittest.mock import MagicMock, patch - -import requests - -from odoo.tests import TransactionCase - - -class TestG2PReferenceIdconfig(TransactionCase): - def setUp(self): - super().setUp() - self.config = self.env["g2p.reference_id.config"].create( - { - "name": "Test Config", - "base_api_url": "https://idgenerator.sandbox.net/v1/idgenerator/uin", - "auth_url": "https://keycloak.openg2p.org/realms/master/protocol/openid-connect/token", - "auth_client_id": "client_id", - "auth_client_secret": "client_secret", - "auth_grant_type": "client_credentials", - "api_timeout": 10, - } - ) - - @patch("requests.post") - def test_get_access_token(self, mock_post): - mock_response = MagicMock() - mock_response.json.return_value = { - "access_token": ( - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." - "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ." - "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" - ) - } - mock_response.raise_for_status = lambda: None - mock_post.return_value = mock_response - access_token = self.config.get_access_token() - self.assertEqual( - access_token, - ( - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." - "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ." - "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" - ), - ) - - @patch("requests.post") - def test_get_access_token_error(self, mock_post): - mock_response = MagicMock() - mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError() - mock_post.return_value = mock_response - with self.assertRaises(requests.exceptions.HTTPError): - self.config.get_access_token() - - @patch("requests.post") - def test_write_access_token(self, mock_post): - mock_response = MagicMock() - mock_response.json.return_value = { - "access_token": ( - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." - "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ." - "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" - ) - } - mock_response.raise_for_status = lambda: None - mock_post.return_value = mock_response - self.config.get_access_token() - self.assertIsNotNone(self.config.access_token) - self.assertIsNotNone(self.config.access_token_expiry) diff --git a/g2p_social_registry/tests/test_res_config_settings.py b/g2p_social_registry/tests/test_res_config_settings.py deleted file mode 100644 index 7aa929f..0000000 --- a/g2p_social_registry/tests/test_res_config_settings.py +++ /dev/null @@ -1,77 +0,0 @@ -from unittest.mock import MagicMock, patch - -from odoo.tests import TransactionCase - - -class TestRegistryConfig(TransactionCase): - def setUp(self): - super().setUp() - self.config = self.env["g2p.reference_id.config"].create( - { - "name": "Test Config", - "base_api_url": "https://idgenerator.sandbox.net/v1/idgenerator/uin", - "auth_url": "https://keycloak.openg2p.org/realms/master/protocol/openid-connect/token", - "auth_client_id": "client_id", - "auth_client_secret": "client_secret", - "auth_grant_type": "client_credentials", - "api_timeout": 10, - } - ) - self.registry_config = self.env["res.config.settings"].create({}) - - @patch("requests.post") - def test_set_values(self, mock_post): - mock_response = MagicMock() - mock_response.json.return_value = {"access_token": "test_access_token"} - mock_response.raise_for_status = lambda: None - mock_post.return_value = mock_response - self.registry_config.id_generator_base_api_url = "https://idgenerator.sandbox.net/v1/idgenerator/uin" - self.registry_config.id_generator_auth_url = ( - "https://keycloak.openg2p.org/realms/master/protocol/openid-connect/token" - ) - self.registry_config.id_generator_auth_client_id = "new_client_id" - self.registry_config.id_generator_auth_client_secret = "new_client_secret" - self.registry_config.id_generator_auth_grant_type = "new_grant_type" - self.registry_config.id_generator_api_timeout = 20 - self.registry_config.set_values() - config = self.env["g2p.reference_id.config"].get_config() - self.assertEqual(config.base_api_url, "https://idgenerator.sandbox.net/v1/idgenerator/uin") - self.assertEqual( - config.auth_url, "https://keycloak.openg2p.org/realms/master/protocol/openid-connect/token" - ) - self.assertEqual(config.auth_client_id, "new_client_id") - self.assertEqual(config.auth_client_secret, "new_client_secret") - self.assertEqual(config.auth_grant_type, "new_grant_type") - self.assertEqual(config.api_timeout, 20) - - @patch("requests.post") - def test_get_values(self, mock_post): - mock_response = MagicMock() - mock_response.json.return_value = {"access_token": "test_access_token"} - mock_response.raise_for_status = lambda: None - mock_post.return_value = mock_response - self.registry_config.get_values() - config = self.env["g2p.reference_id.config"].get_config() - self.assertEqual(self.registry_config.id_generator_base_api_url, config.base_api_url) - self.assertEqual(self.registry_config.id_generator_auth_url, config.auth_url) - self.assertEqual(self.registry_config.id_generator_auth_client_id, config.auth_client_id) - self.assertEqual(self.registry_config.id_generator_auth_client_secret, config.auth_client_secret) - self.assertEqual(self.registry_config.id_generator_auth_grant_type, config.auth_grant_type) - self.assertEqual(self.registry_config.id_generator_api_timeout, config.api_timeout) - - @patch("requests.post") - def test_add_missing_ref_id_to_retry(self, mock_post): - mock_response = MagicMock() - mock_response.json.return_value = {"access_token": "test_access_token"} - mock_response.raise_for_status = lambda: None - mock_post.return_value = mock_response - partner_without_ref = self.env["res.partner"].create( - {"name": "Test Partner", "is_registrant": True, "ref_id": False} - ) - self.registry_config.add_missing_ref_id_to_retry() - pending_records = self.env["g2p.pending.reference_id"].search( - [("registrant_id", "=", partner_without_ref.id)] - ) - self.assertEqual(len(pending_records), 1) - self.assertEqual(pending_records.registrant_id.id, partner_without_ref.id) - self.assertEqual(pending_records.status, "failed") diff --git a/g2p_social_registry/views/registrant_view.xml b/g2p_social_registry/views/registrant_view.xml index 8a004bd..40759fb 100644 --- a/g2p_social_registry/views/registrant_view.xml +++ b/g2p_social_registry/views/registrant_view.xml @@ -33,11 +33,4 @@ - - Generate Reference ID - - - code - action = model.generate_ref_id_for_selected() - diff --git a/g2p_social_registry/views/res_config_settings_view.xml b/g2p_social_registry/views/res_config_settings_view.xml deleted file mode 100644 index 699cb93..0000000 --- a/g2p_social_registry/views/res_config_settings_view.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - - - openg2p.res.config.settings.view.inherit.setup - res.config.settings - - - - - G2P Social Registry Settings - - - - -
-
-
- - - - - - - - - - - - - - - -
-
-
-
-