Skip to content

Commit

Permalink
added unit tests and g2p_social_registry_model
Browse files Browse the repository at this point in the history
  • Loading branch information
vin0dkhichar committed Oct 18, 2024
1 parent c74400b commit 2011d84
Show file tree
Hide file tree
Showing 22 changed files with 620 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ addon | version | maintainers | summary
[g2p_registry_g2p_connect_rest_api](g2p_registry_g2p_connect_rest_api/) | 17.0.0.0.0 | | OpenG2P Registry: G2P Connect REST API
[g2p_registry_id_deduplication](g2p_registry_id_deduplication/) | 17.0.0.0.0 | | OpenG2P Registry ID Deduplication
[g2p_social_registry](g2p_social_registry/) | 17.0.0.0.0 | | OpenG2P Social Registry
[g2p_social_registry_model](g2p_social_registry_model/) | 17.0.0.0.0 | | G2P Social Registry: Demo
[g2p_social_registry_theme](g2p_social_registry_theme/) | 17.0.0.0.0 | | OpenG2P Social Registry: Theme

[//]: # (end addons)
Expand Down
1 change: 1 addition & 0 deletions g2p_registry_id_deduplication/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"/g2p_registry_id_deduplication/static/src/xml/duplicate_template.xml",
],
},
# 'test': ['tests/test_registry_config.py', 'tests/test_registrant.py'],
"demo": [],
"images": [],
"application": True,
Expand Down
2 changes: 1 addition & 1 deletion g2p_registry_id_deduplication/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Part of OpenG2P. See LICENSE file for full copyright and licensing details.

from . import test_registrant
from . import test_registrant, test_res_config_settings
15 changes: 0 additions & 15 deletions g2p_registry_id_deduplication/tests/test_registrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,20 @@
class TestResPartnerIDDeduplication(TransactionCase):
def setUp(self):
super().setUp()

self.partner_model = self.env["res.partner"]
self.id_type_model = self.env["g2p.id.type"]
self.reg_id_model = self.env["g2p.reg.id"]
self.group_membership_model = self.env["g2p.group.membership"]
self.dedup_grp_kind_config_model = self.env["g2p.group.kind.deduplication.config"]
self.group_kind_model = self.env["g2p.group.kind"]
self.config_parameter_model = self.env["ir.config_parameter"]

# Create individual partners
self.individual_1 = self.partner_model.create(
{"name": "Individual 1", "is_registrant": True, "is_group": False}
)
self.individual_2 = self.partner_model.create(
{"name": "Individual 2", "is_registrant": True, "is_group": False}
)

# Create group kind
self.group_kind = self.group_kind_model.create({"name": "Test Kind"})

# Create group partners
self.group_1 = self.partner_model.create(
{
"name": "Group 1",
Expand All @@ -45,11 +38,9 @@ def setUp(self):
}
)

# Create ID types
self.id_type_1 = self.id_type_model.create({"name": "Household ID"})
self.id_type_2 = self.id_type_model.create({"name": "National ID"})

# Create registration IDs for partners
self.reg_id_1 = self.reg_id_model.create(
{
"partner_id": self.individual_1.id,
Expand Down Expand Up @@ -87,15 +78,13 @@ def setUp(self):
}
)

# Create group kind deduplication config
self.grp_dedup_config = self.dedup_grp_kind_config_model.create(
{
"kind_id": self.group_kind.id,
"id_type_ids": [self.id_type_1.id],
}
)

# Set configuration parameters for deduplication
self.config_parameter_model.set_param(
"g2p_registry_id_deduplication.ind_deduplication_id_types_ids", f"[{self.id_type_2.id}]"
)
Expand All @@ -105,15 +94,13 @@ def setUp(self):

def test_deduplicate_registrants_individuals(self):
self.partner_model.with_context(default_is_group=False).deduplicate_registrants()

self.assertTrue(self.individual_1.is_duplicated)
self.assertTrue(self.individual_2.is_duplicated)
self.assertFalse(self.group_1.is_duplicated)
self.assertFalse(self.group_2.is_duplicated)

def test_deduplicate_registrants_groups(self):
self.partner_model.with_context(default_is_group=True).deduplicate_registrants()

self.assertFalse(self.individual_1.is_duplicated)
self.assertFalse(self.individual_2.is_duplicated)
self.assertTrue(self.group_1.is_duplicated)
Expand All @@ -123,13 +110,11 @@ def test_deduplicate_registrants_missing_configuration(self):
self.config_parameter_model.set_param(
"g2p_registry_id_deduplication.ind_deduplication_id_types_ids", "[]"
)

with self.assertRaises(UserError, msg="Deduplication is not configured"):
self.partner_model.with_context(default_is_group=False).deduplicate_registrants()

def test_reset_duplicate_flag(self):
self.partner_model.with_context(default_is_group=False).reset_duplicate_flag(False)

self.assertFalse(self.individual_1.is_duplicated)
self.assertFalse(self.individual_2.is_duplicated)
self.assertFalse(self.group_1.is_duplicated)
Expand Down
71 changes: 71 additions & 0 deletions g2p_registry_id_deduplication/tests/test_res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from odoo.tests.common import TransactionCase
from odoo.tools.safe_eval import safe_eval


class TestRegistryConfig(TransactionCase):
def setUp(self):
super().setUp()
self.registry_config_model = self.env["res.config.settings"]
self.id_type_model = self.env["g2p.id.type"]
self.grp_dedup_config_model = self.env["g2p.group.kind.deduplication.config"]
self.id_type_1 = self.id_type_model.create({"name": "National ID"})
self.id_type_2 = self.id_type_model.create({"name": "Household ID"})
self.grp_dedup_config = self.grp_dedup_config_model.create(
{
"kind_id": self.env["g2p.group.kind"].create({"name": "Test Group Kind"}).id,
"id_type_ids": [(6, 0, [self.id_type_1.id, self.id_type_2.id])],
}
)

def test_set_and_get_values(self):
config_settings = self.registry_config_model.create({})
config_settings.grp_deduplication_id_types_ids = [(4, self.grp_dedup_config.id)]
config_settings.ind_deduplication_id_types_ids = [(4, self.id_type_1.id), (4, self.id_type_2.id)]
config_settings.set_values()
config_settings.get_values()
self.assertIn(self.grp_dedup_config.id, config_settings.grp_deduplication_id_types_ids.mapped("id"))
self.assertIn(self.id_type_1.id, config_settings.ind_deduplication_id_types_ids.mapped("id"))
self.assertIn(self.id_type_2.id, config_settings.ind_deduplication_id_types_ids.mapped("id"))

def test_delete_id_type_updates_config(self):
config_settings = self.registry_config_model.create({})
config_settings.ind_deduplication_id_types_ids = [(4, self.id_type_1.id), (4, self.id_type_2.id)]
config_settings.set_values()
self.id_type_1.unlink()
ir_config = self.env["ir.config_parameter"].sudo()
updated_id_types_param = ir_config.get_param(
"g2p_registry_id_deduplication.ind_deduplication_id_types_ids"
)
updated_id_types_ids = safe_eval(updated_id_types_param)
self.assertNotIn(self.id_type_1.id, updated_id_types_ids)
self.assertIn(self.id_type_2.id, updated_id_types_ids)

def test_set_values_empty_ids(self):
config_settings = self.registry_config_model.create({})
config_settings.set_values()
ir_config = self.env["ir.config_parameter"].sudo()
ir_config.set_param("g2p_registry_id_deduplication.grp_deduplication_id_types_ids", "[]")
ir_config.set_param("g2p_registry_id_deduplication.ind_deduplication_id_types_ids", "[]")
grp_id_types_param = ir_config.get_param(
"g2p_registry_id_deduplication.grp_deduplication_id_types_ids"
)
ind_id_types_param = ir_config.get_param(
"g2p_registry_id_deduplication.ind_deduplication_id_types_ids"
)
self.assertEqual(grp_id_types_param, "[]")
self.assertEqual(ind_id_types_param, "[]")

def test_unlink_id_type_with_no_references(self):
new_id_type = self.id_type_model.create({"name": "New ID Type"})
self.assertTrue(new_id_type.exists(), "New ID Type should exist before unlinking.")
new_id_type.unlink()
self.assertFalse(
self.id_type_model.browse(new_id_type.id).exists(),
"New ID Type should not exist after unlinking.",
)

def test_unlink_id_type_with_references(self):
config_settings = self.registry_config_model.create({})
config_settings.ind_deduplication_id_types_ids = [(4, self.id_type_1.id)]
config_settings.set_values()
self.id_type_1.unlink()
4 changes: 4 additions & 0 deletions g2p_social_registry/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# 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
47 changes: 47 additions & 0 deletions g2p_social_registry/tests/test_pending_ref_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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
)
67 changes: 67 additions & 0 deletions g2p_social_registry/tests/test_ref_id_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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)
77 changes: 77 additions & 0 deletions g2p_social_registry/tests/test_res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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")
3 changes: 3 additions & 0 deletions g2p_social_registry_model/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# G2P Social Registry: Demo

Refer to https://docs.openg2p.org.
3 changes: 3 additions & 0 deletions g2p_social_registry_model/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Part of OpenG2P. See LICENSE file for full copyright and licensing details.

from . import models
Loading

0 comments on commit 2011d84

Please sign in to comment.