Skip to Content
Menu
This question has been flagged

Summary: I am trying to make an Unit test to validate an API request that my controller does, however, I am having trouble fetching the proper URL. How do I properly test the API call on a test.py file?


Hello, everyone,

I wrote a few unit tests for my controller, and I am having difficulties validating its functionality. I am not very familiar with unit tests, but I settled for this framework to run them:

  1. Upload starting code with my test_api.py file to staging branch through a commit
  2. On Odoo SH Editor, open the file and add loggers for testing
  3. On Odoo SH Shell, run:
odoo-bin --test-tags /quality_saft_api_client

This works. It manages to run the methods alright, and every change that I make seems to be recognized and updated each time. So, the test file works. The API call that I am trying to make, however, does not. Here is the code:

class TestQualityApiClient(HttpCase):

​​@tagged("at_install")
​def test_controller_response(self):
​_logger.warning("--begin test_controller_response")

​ICP = self.env['ir.config_parameter']
  ​base_url = ICP.sudo().get_param('web.base.url')
​a_simple_url = base_url + controller._webhook_url

​headers = {
            'Content-Type': 'application/json'
        }

​post_data = {
            "params": {
                "api_token": "test",
                "data": {
                    "date_start": "2024-09-01",
                    "date_end": "2024-09-02"
                }
            }
        }
​_logger.warning("URL HERE: %s", a_simple_url)
​​# response = self.url_open(url=a_simple_url, data=json.dumps(post_data), headers=headers)
​​_logger.info("Response: %s", response.json())


 If I run the same commands on the Odoo Shell inside the SH Editor, I correctly get the URL I need to make the API Request. However, if I run it on the main Odoo Shell (accessible through the brach view on Odoo SH (...dev.odoo.com/odoo-sh/webshell/ws) it always fetched the wrong base url. It gets this one instead:


...test_client_api: Starting TestQualityApiClient.test_controller_response ...

...test_client_api: ------------------------------------------------ begin

...test_client_api: URL HERE: http://127.0.0.x:8069/export/example


The url_open method works fine if I hard code the proper URL, but when I run the odoo-bin test command it gets that 8069 URL instead. 


How do I properly test the API call on a test.py file?


Thank you!

Avatar
Discard
Related Posts Replies Views Activity
0
Jan 23
2007
0
Aug 20
3467
0
Jun 20
2428
2
Nov 24
724
1
Sep 24
1039