Skip to content

Commit 91d3d33

Browse files
committed
DEVX-10006: adding more RCS Card tests
1 parent 4f7b518 commit 91d3d33

2 files changed

Lines changed: 270 additions & 1 deletion

File tree

messages/src/vonage_messages/models/rcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ class RcsCard(BaseRcs):
299299
RcsSuggestionActionCreateCalendarEvent,
300300
]
301301
]
302-
] = Field(None, min_length=1, max_length=8)
302+
] = Field(None, min_length=1, max_length=4)
303303
rcs: Optional[RcsOptionsCard] = None
304304
message_type: MessageType = MessageType.CARD
305305

messages/tests/test_rcs_models.py

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,275 @@ def test_create_rcs_card_with_suggestions():
452452
assert card.model_dump(by_alias=True, exclude_none=True) == card_dict
453453

454454

455+
def test_create_rcs_cards_with_all_suggestion_types():
456+
card_1 = RcsCard(
457+
to='1234567890',
458+
from_='asdf1234',
459+
title='Card title',
460+
text='Card description',
461+
media_url='https://example.com/image.jpg',
462+
suggestions=[
463+
RcsSuggestionReply(
464+
text='Reply',
465+
postback_data='postback-data',
466+
),
467+
RcsSuggestionActionDial(
468+
text='Call us',
469+
postback_data='postback-data',
470+
phone_number='447900000000',
471+
),
472+
RcsSuggestionActionViewLocation(
473+
text='View location',
474+
postback_data='postback-data',
475+
latitude='51.5074',
476+
longitude='-0.1278',
477+
pin_label='London',
478+
fallback_url='https://example.com/location',
479+
),
480+
RcsSuggestionActionShareLocation(
481+
text='Share location',
482+
postback_data='postback-data',
483+
),
484+
],
485+
)
486+
card_2 = RcsCard(
487+
to='1234567890',
488+
from_='asdf1234',
489+
title='Card title',
490+
text='Card description',
491+
media_url='https://example.com/image.jpg',
492+
suggestions=[
493+
RcsSuggestionActionOpenUrl(
494+
text='Open URL',
495+
postback_data='postback-data',
496+
url='https://example.com',
497+
description='Click to open the URL',
498+
),
499+
RcsSuggestionActionOpenUrlWebview(
500+
text='Open URL in webview',
501+
postback_data='postback-data',
502+
url='https://example.com',
503+
description='Click to open the URL in a webview',
504+
view_mode='FULL',
505+
),
506+
RcsSuggestionActionCreateCalendarEvent(
507+
text='Add to calendar',
508+
postback_data='postback-data',
509+
start_time='2024-01-01T12:00:00Z',
510+
end_time='2024-01-01T13:00:00Z',
511+
title='Meeting with Bob',
512+
description='Discuss project updates',
513+
fallback_url='https://example.com/calendar-event',
514+
),
515+
],
516+
)
517+
card_dict_1 = {
518+
'to': '1234567890',
519+
'from': 'asdf1234',
520+
'title': 'Card title',
521+
'text': 'Card description',
522+
'media_url': 'https://example.com/image.jpg',
523+
'suggestions': [
524+
{
525+
'type': 'reply',
526+
'text': 'Reply',
527+
'postback_data': 'postback-data',
528+
},
529+
{
530+
'type': 'dial',
531+
'text': 'Call us',
532+
'postback_data': 'postback-data',
533+
'phone_number': '447900000000',
534+
},
535+
{
536+
'type': 'view_location',
537+
'text': 'View location',
538+
'postback_data': 'postback-data',
539+
'latitude': '51.5074',
540+
'longitude': '-0.1278',
541+
'pin_label': 'London',
542+
'fallback_url': 'https://example.com/location',
543+
},
544+
{
545+
'type': 'share_location',
546+
'text': 'Share location',
547+
'postback_data': 'postback-data',
548+
}
549+
],
550+
'channel': 'rcs',
551+
'message_type': 'card',
552+
}
553+
card_dict_2 = {
554+
'to': '1234567890',
555+
'from': 'asdf1234',
556+
'title': 'Card title',
557+
'text': 'Card description',
558+
'media_url': 'https://example.com/image.jpg',
559+
'suggestions': [
560+
{
561+
'type': 'open_url',
562+
'text': 'Open URL',
563+
'postback_data': 'postback-data',
564+
'url': 'https://example.com',
565+
'description': 'Click to open the URL',
566+
},
567+
{
568+
'type': 'open_url_in_webview',
569+
'text': 'Open URL in webview',
570+
'postback_data': 'postback-data',
571+
'url': 'https://example.com',
572+
'description': 'Click to open the URL in a webview',
573+
'view_mode': 'FULL',
574+
},
575+
{
576+
'type': 'create_calendar_event',
577+
'text': 'Add to calendar',
578+
'postback_data': 'postback-data',
579+
'start_time': '2024-01-01T12:00:00Z',
580+
'end_time': '2024-01-01T13:00:00Z',
581+
'title': 'Meeting with Bob',
582+
'description': 'Discuss project updates',
583+
'fallback_url': 'https://example.com/calendar-event',
584+
}
585+
],
586+
'channel': 'rcs',
587+
'message_type': 'card',
588+
}
589+
assert card_1.model_dump(by_alias=True, exclude_none=True) == card_dict_1
590+
assert card_2.model_dump(by_alias=True, exclude_none=True) == card_dict_2
591+
592+
593+
def test_create_rcs_card_without_title():
594+
with pytest.raises(ValidationError) as err:
595+
card = RcsCard(
596+
to='1234567890',
597+
from_='asdf1234',
598+
text='Card description',
599+
media_url='https://example.com/image.jpg',
600+
)
601+
assert "Field required" in str(err.value)
602+
603+
604+
def test_create_rcs_card_without_text():
605+
with pytest.raises(ValidationError) as err:
606+
card = RcsCard(
607+
to='1234567890',
608+
from_='asdf1234',
609+
title='Card title',
610+
media_url='https://example.com/image.jpg',
611+
)
612+
assert "Field required" in str(err.value)
613+
614+
615+
def test_create_rcs_card_without_media_url():
616+
with pytest.raises(ValidationError) as err:
617+
card = RcsCard(
618+
to='1234567890',
619+
from_='asdf1234',
620+
title='Card title',
621+
text='Card description',
622+
)
623+
assert "Field required" in str(err.value)
624+
625+
626+
def test_create_rcs_card_with_title_too_short():
627+
with pytest.raises(ValidationError) as err:
628+
card = RcsCard(
629+
to='1234567890',
630+
from_='asdf1234',
631+
title='',
632+
text='Card description',
633+
media_url='https://example.com/image.jpg',
634+
)
635+
assert "String should have at least 1 character" in str(err.value)
636+
637+
638+
def test_create_rcs_card_with_title_too_long():
639+
with pytest.raises(ValidationError) as err:
640+
card = RcsCard(
641+
to='1234567890',
642+
from_='asdf1234',
643+
title='A' * 200 + 'B',
644+
text='Card description',
645+
media_url='https://example.com/image.jpg',
646+
)
647+
assert "String should have at most 200 characters" in str(err.value)
648+
649+
650+
def test_create_rcs_card_with_text_too_short():
651+
with pytest.raises(ValidationError) as err:
652+
card = RcsCard(
653+
to='1234567890',
654+
from_='asdf1234',
655+
title='Card title',
656+
text='',
657+
media_url='https://example.com/image.jpg',
658+
)
659+
assert "String should have at least 1 character" in str(err.value)
660+
661+
662+
def test_create_rcs_card_with_text_too_long():
663+
with pytest.raises(ValidationError) as err:
664+
card = RcsCard(
665+
to='1234567890',
666+
from_='asdf1234',
667+
title='Card title',
668+
text='A' * 2000 + 'B',
669+
media_url='https://example.com/image.jpg',
670+
)
671+
assert "String should have at most 2000 characters" in str(err.value)
672+
673+
674+
def test_create_rcs_card_with_insuffient_suggestions():
675+
with pytest.raises(ValidationError) as err:
676+
card = RcsCard(
677+
to='1234567890',
678+
from_='asdf1234',
679+
title='Card title',
680+
text='Card description',
681+
media_url='https://example.com/image.jpg',
682+
suggestions=[],
683+
)
684+
assert "List should have at least 1 item" in str(err.value)
685+
686+
687+
def test_create_rcs_card_with_too_many_suggestions():
688+
with pytest.raises(ValidationError) as err:
689+
card = RcsCard(
690+
to='1234567890',
691+
from_='asdf1234',
692+
title='Card title',
693+
text='Card description',
694+
media_url='https://example.com/image.jpg',
695+
suggestions=[
696+
RcsSuggestionReply(
697+
text='Reply',
698+
postback_data='postback-data',
699+
),
700+
] * 5,
701+
)
702+
assert "List should have at most 4 items" in str(err.value)
703+
704+
705+
def test_create_rcs_card_with_inavalid_suggestion_types():
706+
with pytest.raises(ValidationError) as err:
707+
card = RcsCard(
708+
to='1234567890',
709+
from_='asdf1234',
710+
title='Card title',
711+
text='Card description',
712+
media_url='https://example.com/image.jpg',
713+
suggestions=[
714+
RcsSuggestionReply(
715+
text='Reply',
716+
postback_data='postback-data',
717+
),
718+
"Invalid suggestion type",
719+
],
720+
)
721+
assert "Input should be a valid dictionary or instance" in str(err.value)
722+
723+
455724
def test_create_rcs_custom():
456725
rcs_model = RcsCustom(
457726
to='1234567890',

0 commit comments

Comments
 (0)