Skip to content
Open
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
5 changes: 3 additions & 2 deletions src/langbot/libs/dingtalk_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,10 @@ async def create_and_deliver_card(
if not await self.check_access_token():
await self.get_access_token()

cardData: dict = {'cardParamMap': _stringify_card_param_map(card_param_map)}
merged_param_map = dict(card_param_map or {})
if card_data_config is not None:
cardData['config'] = json.dumps(card_data_config)
merged_param_map['config'] = card_data_config
cardData: dict = {'cardParamMap': _stringify_card_param_map(merged_param_map)}

body: dict = {
'cardTemplateId': card_template_id,
Expand Down
39 changes: 38 additions & 1 deletion tests/unit_tests/platform/test_dingtalk_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Tests for DingTalk API payload helpers."""

import json
from contextlib import asynccontextmanager
from unittest.mock import AsyncMock

from langbot.libs.dingtalk_api.api import _stringify_card_param_map
from langbot.libs.dingtalk_api.api import DingTalkClient, _stringify_card_param_map
from langbot.pkg.utils import httpclient


def test_dingtalk_card_param_map_stringifies_select_component_arrays():
Expand Down Expand Up @@ -40,3 +43,37 @@ def test_dingtalk_card_param_map_stringifies_unregistered_structures():

assert params['other'] == '["A"]'
assert params['empty'] == ''


async def test_create_and_deliver_card_places_config_in_card_param_map(monkeypatch):
response = type('Response', (), {'status_code': 200})()
post = AsyncMock(return_value=response)
http_client = type('HttpClient', (), {'post': post})()

@asynccontextmanager
async def http_client_context():
yield http_client

client = object.__new__(DingTalkClient)
client.access_token = 'token'
client.robot_code = 'robot-code'
client.key = 'client-id'
client.logger = None
client.check_access_token = AsyncMock(return_value=True)
client._http_client_context = http_client_context
monkeypatch.setattr(httpclient, 'response_text', AsyncMock(return_value='{}'))

success = await client.create_and_deliver_card(
card_template_id='template-id',
out_track_id='track-id',
open_space_id='dtv1.card//IM_ROBOT.user-id',
is_group=False,
card_param_map={'content': ''},
card_data_config={'autoLayout': True},
)

assert success is True
body = post.await_args.kwargs['json']
assert body['cardData']['cardParamMap']['content'] == ''
assert body['cardData']['cardParamMap']['config'] == '{"autoLayout": true}'
assert 'config' not in body['cardData']
Loading