-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests and g2p_social_registry_model
- Loading branch information
1 parent
c74400b
commit 2011d84
Showing
22 changed files
with
620 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
g2p_registry_id_deduplication/tests/test_res_config_settings.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# G2P Social Registry: Demo | ||
|
||
Refer to https://docs.openg2p.org. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.