taverntesting / tavern

A command-line tool and Python library and Pytest plugin for automated testing of RESTful APIs, with a simple, concise and flexible YAML-based syntax
https://taverntesting.github.io/
MIT License
1.03k stars 195 forks source link

Unable to use external function in MQTT publish #847

Closed samunnat closed 1 year ago

samunnat commented 1 year ago

I tried using an external function in a MQTT publish request, and the function wasn't getting evaluated.

My request looks like this:

  - id: publish_thing_1
    name: Publish thing 1
    mqtt_publish:
      topic: &ping_topic '/device/123/ping'
      qos: 1
      json:
        $ext: 
          function: utils.testing_utils:my_function
        thing_1: abc
    mqtt_response:
      topic: *ping_topic
      json:
        header: test
        thing_1: abc
      timeout: 5
      qos: 1

It looks like the line below in request.py

update_from_ext(publish_args, ["json"], test_block_config)

should be

update_from_ext(publish_args, ["payload"], test_block_config)

instead, since publish_args looks like this:

{'topic': '/device/123/ping', 'qos': 1, 'payload': '{"$ext": {"function": "utils.testing_utils:my_function"}, "thing_1": "abc"}'}

Note that the payload value is a string, which prevents external function evaluation even after I do the update_from_ext change. Before I go down the rabbit hole too much, I wanted to confirm that I've configured the request properly, and that this feature is expected to work after this PR.

michaelboulton commented 1 year ago

It seems like ext functions weren't being used properly in MQTT, I've opened a PR to try and fix it

michaelboulton commented 1 year ago

Should be fixed in 2.0.4

samunnat commented 1 year ago

Awesome, thanks for the quick fix.