Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
60 changes: 60 additions & 0 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,66 @@ def test_default_values():

assert sorted(actual["data"]["default_names"]) == sorted(expected_default_names)

def test_odoo_registration():
bot_settings = BotSettings.objects(bot=pytest.bot).get()
bot_settings.pos_enabled = True
bot_settings.save()
with patch("kairon.pos.definitions.factory.POSFactory.get_instance") as mock_factory:

mock_pos = MagicMock()
mock_pos.return_value.onboarding.return_value = {"ok": True}

mock_factory.return_value = mock_pos

payload = {
"client_name": "XYZ_Pvt_Ltd"
}


response = client.post(
url=f"/api/bot/{pytest.bot}/pos/odoo/register",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)

actual = response.json()
assert actual["success"]
assert actual["data"] == {'ok': True}
mock_factory.assert_called_once_with("odoo")
mock_pos.return_value.onboarding.assert_called_once_with(
client_name="XYZ_Pvt_Ltd",
bot=pytest.bot,
user='integration@demo.ai'
)
Comment on lines +2760 to +2786
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix mock configuration to match actual factory usage pattern.

The mock is incorrectly set up with mock_pos.return_value.onboarding.return_value, but POSFactory.get_instance("odoo") returns an OdooPOS instance directly (not a callable). The endpoint code calls pos_instance.onboarding(...) where pos_instance is the returned instance, not a function to be invoked.

Correct the mock setup:

 mock_pos = MagicMock()
-mock_pos.return_value.onboarding.return_value = {"ok": True}
+mock_pos.onboarding.return_value = {"ok": True}

 mock_factory.return_value = mock_pos
 ...
-mock_pos.return_value.onboarding.assert_called_once_with(
+mock_pos.onboarding.assert_called_once_with(
     client_name="XYZ_Pvt_Ltd",
     bot=pytest.bot,
     user='integration@demo.ai'
 )

The same issue exists in test_odoo_loginmock_pos.return_value.authenticate should be mock_pos.authenticate.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
with patch("kairon.pos.definitions.factory.POSFactory.get_instance") as mock_factory:
mock_pos = MagicMock()
mock_pos.return_value.onboarding.return_value = {"ok": True}
mock_factory.return_value = mock_pos
payload = {
"client_name": "XYZ_Pvt_Ltd"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/pos/odoo/register",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
assert actual["data"] == {'ok': True}
mock_factory.assert_called_once_with("odoo")
mock_pos.return_value.onboarding.assert_called_once_with(
client_name="XYZ_Pvt_Ltd",
bot=pytest.bot,
user='integration@demo.ai'
)
with patch("kairon.pos.definitions.factory.POSFactory.get_instance") as mock_factory:
mock_pos = MagicMock()
mock_pos.onboarding.return_value = {"ok": True}
mock_factory.return_value = mock_pos
payload = {
"client_name": "XYZ_Pvt_Ltd"
}
response = client.post(
url=f"/api/bot/{pytest.bot}/pos/odoo/register",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)
actual = response.json()
assert actual["success"]
assert actual["data"] == {'ok': True}
mock_factory.assert_called_once_with("odoo")
mock_pos.onboarding.assert_called_once_with(
client_name="XYZ_Pvt_Ltd",
bot=pytest.bot,
user='integration@demo.ai'
)
🤖 Prompt for AI Agents
In tests/integration_test/services_test.py around lines 2760 to 2786, the mock
config uses mock_pos.return_value.* but POSFactory.get_instance("odoo") returns
an instance directly, so set expectations on the mock instance itself: assign
mock_factory.return_value = mock_pos and replace uses of
mock_pos.return_value.onboarding.return_value with
mock_pos.onboarding.return_value (and similarly change
mock_pos.return_value.onboarding.assert_called_once_with to
mock_pos.onboarding.assert_called_once_with). Do the same fix for the other test
(test_odoo_login): use mock_pos.authenticate and
mock_pos.authenticate.assert_called_once_with instead of
mock_pos.return_value.authenticate variants so the mocked methods match how the
factory is used.


def test_odoo_login():
with patch("kairon.pos.definitions.factory.POSFactory.get_instance") as mock_factory:

mock_pos = MagicMock()
mock_pos.return_value.authenticate.return_value = {"ok": True}

mock_factory.return_value = mock_pos

payload = {
"client_name": "XYZ_Pvt_Ltd",
"page_type": "pos_products"
}

response = client.post(
url=f"/api/bot/{pytest.bot}/pos/odoo/login",
json=payload,
headers={"Authorization": pytest.token_type + " " + pytest.access_token},
)

actual = response.json()
assert actual == {'ok': True}
mock_factory.assert_called_once_with("odoo")
mock_pos.return_value.authenticate.assert_called_once_with(
client_name="XYZ_Pvt_Ltd",
page_type="pos_products",
bot=pytest.bot
)

def test_bulk_save_success():
request_body = {
"payload": [
Expand Down
71 changes: 71 additions & 0 deletions tests/unit_test/pos_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from unittest.mock import patch

mock_env = {
"pos": {
"odoo": {
"odoo_url": "http://localhost:8080",
"odoo_master_password":"admin@123"
}
}
}

patcher = patch("kairon.Utility.environment", mock_env)
patcher.start()

from kairon.pos.odoo.odoo_pos import OdooPOS
from kairon.shared.pos.processor import POSProcessor

import pytest

@pytest.fixture
def odoo_pos():
return OdooPOS()


def teardown_module(module):
"""Stop environment patch after all tests."""
patcher.stop()


def test_products_list(odoo_pos):
resp = odoo_pos.products_list()
assert resp["url"].startswith("http://localhost:8080")


def test_orders_list(odoo_pos):
resp = odoo_pos.orders_list()
assert resp["url"].startswith("http://localhost:8080")


def test_onboarding(odoo_pos):
with patch.object(POSProcessor, "onboarding_client", return_value={"ok": True}) as mock_fn:
resp = odoo_pos.onboarding(client_name="demo", bot="test_bot", user="aniket.kharkia@nimblework.com")
mock_fn.assert_called_once()
assert resp == {"ok": True}

def test_authenticate_products(odoo_pos):
with (
patch.object(POSProcessor, "pos_login", return_value={"session": "s1"}) as p_login,
patch.object(OdooPOS, "products_list", return_value={"url": "p_url"}) as p_list,
patch.object(POSProcessor, "set_odoo_session_cookie", return_value={"done": True}) as p_cookie,
):
resp = odoo_pos.authenticate(client_name="demo", bot="test_bot")

p_login.assert_called_once()
p_list.assert_called_once()
p_cookie.assert_called_once_with({"session": "s1", "url": "p_url"})

assert resp == {"done": True}

def test_authenticate_orders(odoo_pos):
with (
patch.object(POSProcessor, "pos_login", return_value={"session": "xyz"}) as p_login,
patch.object(OdooPOS, "orders_list", return_value={"url": "o_url"}) as o_list,
patch.object(POSProcessor, "set_odoo_session_cookie", return_value={"ok": 1}) as p_cookie
):
resp = odoo_pos.authenticate(client_name="C", bot="B", page_type="pos_orders")

p_login.assert_called_once()
o_list.assert_called_once()
p_cookie.assert_called_once_with({"session": "xyz", "url": "o_url"})
assert resp == {"ok": 1}
Loading