Add unit tests on the parser/domain helpers and end-to-end tests of the public JSON endpoint: - ``tests/test_res_partner.py``: pins the Gogocarto data contract (default domain and field set, company-selected fields, parser mapping of scalar/relational/binary/unsupported ttypes). A silent regression there corrupts the map feed while the endpoint keeps answering 200. - ``tests/test_controller.py``: anonymous ``HttpCase`` calls on ``/web/<company_id>/get_http_gogocarto_elements`` covering the flagged-partners payload, the exclusion of unflagged partners and the graceful degradation on unknown company ids. Verified on a local 18.0 instance: 9 tests, 0 failed, 0 error.
85 lines
3.1 KiB
Python
85 lines
3.1 KiB
Python
from odoo.tests.common import HttpCase
|
|
|
|
_ROUTE = "/web/{company_id}/get_http_gogocarto_elements"
|
|
|
|
|
|
class TestGogocartoExportHttp(HttpCase):
|
|
"""
|
|
End-to-end tests of the public JSON endpoint.
|
|
|
|
Requests are made anonymously on purpose: the route is declared
|
|
``auth="public"`` and Gogocarto servers fetch it without credentials.
|
|
"""
|
|
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
super().setUpClass()
|
|
cls.partner = cls.env["res.partner"].create(
|
|
{
|
|
"name": "Gogo Test Partner",
|
|
"street": "1 rue du Test",
|
|
"city": "Paris",
|
|
"in_gogocarto": True,
|
|
"partner_latitude": 48.8566,
|
|
"partner_longitude": 2.3522,
|
|
}
|
|
)
|
|
cls.env["res.partner"].create({"name": "Hidden Partner", "in_gogocarto": False})
|
|
company = cls.env["res.company"].browse(1)
|
|
company.export_gogocarto_fields = (
|
|
cls.env["ir.model.fields"]
|
|
.sudo()
|
|
.search(
|
|
[
|
|
("model_id", "=", "res.partner"),
|
|
("name", "in", ["street", "city"]),
|
|
]
|
|
)
|
|
)
|
|
|
|
def _get_elements(self, company_id="1"):
|
|
"""
|
|
Call the export endpoint anonymously and return the response.
|
|
|
|
:param company_id: company id to embed in the route
|
|
:return: the HTTP response for the endpoint
|
|
"""
|
|
# ``authenticate(None, None)`` opens an anonymous session bound to
|
|
# the test database; a bare cookie-less request would not resolve
|
|
# the database when the instance hosts several of them.
|
|
self.authenticate(None, None)
|
|
return self.url_open(_ROUTE.format(company_id=company_id))
|
|
|
|
def test_returns_flagged_partners_as_json(self):
|
|
"""
|
|
The endpoint returns the flagged partner with its exported data.
|
|
"""
|
|
response = self._get_elements()
|
|
self.assertEqual(response.status_code, 200)
|
|
entries = [e for e in response.json() if e["id"] == self.partner.id]
|
|
self.assertEqual(len(entries), 1)
|
|
entry = entries[0]
|
|
self.assertEqual(entry["name"], "Gogo Test Partner")
|
|
self.assertEqual(entry["street"], "1 rue du Test")
|
|
self.assertEqual(entry["city"], "Paris")
|
|
self.assertEqual(entry["partner_latitude"], 48.8566)
|
|
self.assertEqual(entry["partner_longitude"], 2.3522)
|
|
|
|
def test_excludes_unflagged_partners(self):
|
|
"""
|
|
Partners without the ``in_gogocarto`` flag are never exported.
|
|
"""
|
|
data = self._get_elements().json()
|
|
self.assertFalse(any(e["name"] == "Hidden Partner" for e in data))
|
|
|
|
def test_unknown_company_returns_default_fields(self):
|
|
"""
|
|
An unknown company id degrades gracefully to the default fields.
|
|
"""
|
|
response = self._get_elements(company_id="99999999")
|
|
self.assertEqual(response.status_code, 200)
|
|
entry = [e for e in response.json() if e["id"] == self.partner.id][0]
|
|
self.assertNotIn("street", entry) # no company fields selected
|
|
self.assertIn("name", entry)
|
|
self.assertIn("partner_latitude", entry)
|