Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added g2p_social_registry_model and created tests for module inside openg2p-social-registry #49

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion g2p_registry_id_deduplication/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from . import models


def _uninstall_cleanup(env):
parameter_keys = [
"g2p_registry_id_deduplication.grp_deduplication_id_types_ids",
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
73 changes: 73 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,73 @@
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()
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
45 changes: 45 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,45 @@
import unittest
from unittest.mock import patch, MagicMock
from odoo.tests import TransactionCase
from odoo.exceptions import UserError

class TestFallbackTable(TransactionCase):

def setUp(self):
super(TestFallbackTable, self).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)
50 changes: 50 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,50 @@
from unittest.mock import patch, MagicMock
from odoo.tests import TransactionCase
import requests

class TestG2PReferenceIdconfig(TransactionCase):
def setUp(self):
super(TestG2PReferenceIdconfig, self).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)
89 changes: 89 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,89 @@
from unittest.mock import patch, MagicMock
from odoo.tests import TransactionCase

class TestRegistryConfig(TransactionCase):
def setUp(self):
super(TestRegistryConfig, self).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
})

partner_with_ref = self.env['res.partner'].create({
"name": "Another Partner",
"is_registrant": True,
"ref_id": "existing_ref"
})

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
21 changes: 21 additions & 0 deletions g2p_social_registry_model/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Part of OpenG2P. See LICENSE file for full copyright and licensing details.
{
"name": "G2P Social Registry: Demo",
"category": "G2P",
"version": "17.0.0.0.0",
"sequence": 1,
"author": "OpenG2P",
"website": "https://openg2p.org",
"license": "LGPL-3",
"depends": ["g2p_social_registry"],
"data": [
"views/group_view.xml",
"views/individual_view.xml",
],
"assets": {},
"demo": [],
"images": [],
"application": True,
"installable": True,
"auto_install": False,
}
4 changes: 4 additions & 0 deletions g2p_social_registry_model/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Part of OpenG2P. See LICENSE file for full copyright and licensing details.

from . import odk_client
from . import res_partner
49 changes: 49 additions & 0 deletions g2p_social_registry_model/models/odk_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import odoo.addons.g2p_odk_importer.models.odk_client as base_odk_client

original_individual_data = base_odk_client.ODKClient.get_individual_data


def patched_individual_data(self, record):
res = original_individual_data(self, record)

if record.get("reg_ids"):
res.update(
{
"reg_ids": [
(
0,
0,
{
"id_type": self.env["g2p.id.type"]
.search([("name", "=", reg_id.get("id_type"))], limit=1)
.id,
"value": reg_id.get("value"),
"expiry_date": reg_id.get("expiry_date"),
"status": "valid",
},
)
for reg_id in record.get("reg_ids")
],
"education_level": record.get("education_level", None),
"employment_status": record.get("employment_status", None),
"marital_status": record.get("marital_status", None),
}
)
return res


# def patched_member_relationship(self, source_id, record):
# print(record)
# relation = self.env["g2p.relationship"].search(
# [("name", "=", record.get("household_member").get("relationship_with_household_head"))], limit=1
# )
# print('--- relation', relation)

# if relation and source_id:
# return {"source": source_id, "relation": relation.id, "start_date": datetime.now()}

# return None


base_odk_client.ODKClient.get_individual_data = patched_individual_data
# base_odk_client.ODKClient.get_member_relationship = patched_member_relationship
Loading
Loading