Skip to content

Commit baefad2

Browse files
authored
Update whatsapp create template (#71)
1 parent 830ec6e commit baefad2

File tree

2 files changed

+94
-42
lines changed

2 files changed

+94
-42
lines changed

infobip_channels/whatsapp/models/body/create_template.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,36 +89,32 @@ class LanguageEnum(str, Enum):
8989
class HeaderText(CamelCaseModel):
9090
format: Literal["TEXT"]
9191
text: constr(max_length=60)
92+
example: Optional[str] = None
9293

9394

9495
class HeaderImage(CamelCaseModel):
9596
format: Literal["IMAGE"]
97+
example: Optional[AnyHttpUrl] = None
9698

9799

98100
class HeaderVideo(CamelCaseModel):
99101
format: Literal["VIDEO"]
102+
example: Optional[AnyHttpUrl] = None
100103

101104

102105
class HeaderDocument(CamelCaseModel):
103106
format: Literal["DOCUMENT"]
107+
example: Optional[AnyHttpUrl] = None
104108

105109

106110
class HeaderLocation(CamelCaseModel):
107111
format: Literal["LOCATION"]
108112

109113

110114
class CategoryEnum(str, Enum):
111-
ACCOUNT_UPDATE = "ACCOUNT_UPDATE"
112-
PAYMENT_UPDATE = "PAYMENT_UPDATE"
113-
PERSONAL_FINANCE_UPDATE = "PERSONAL_FINANCE_UPDATE"
114-
SHIPPING_UPDATE = "SHIPPING_UPDATE"
115-
RESERVATION_UPDATE = "RESERVATION_UPDATE"
116-
ISSUE_RESOLUTION = "ISSUE_RESOLUTION"
117-
APPOINTMENT_UPDATE = "APPOINTMENT_UPDATE"
118-
TRANSPORTATION_UPDATE = "TRANSPORTATION_UPDATE"
119-
TICKET_UPDATE = "TICKET_UPDATE"
120-
ALERT_UPDATE = "ALERT_UPDATE"
121-
AUTO_REPLY = "AUTO_REPLY"
115+
MARKETING = "MARKETING"
116+
TRANSACTIONAL = "TRANSACTIONAL"
117+
OTP = "OTP"
122118

123119

124120
class Button(CamelCaseModel):
@@ -143,12 +139,21 @@ class ButtonQuickReply(Button):
143139
type: Literal["QUICK_REPLY"]
144140

145141

142+
class Body(CamelCaseModel):
143+
text: str
144+
examples: Optional[List[str]] = None
145+
146+
147+
class Footer(CamelCaseModel):
148+
text: constr(max_length=60)
149+
150+
146151
class Structure(CamelCaseModel):
147152
header: Optional[
148153
Union[HeaderText, HeaderImage, HeaderVideo, HeaderDocument, HeaderLocation]
149154
] = None
150-
body: str
151-
footer: Optional[constr(max_length=60)] = None
155+
body: Body
156+
footer: Optional[Footer] = None
152157
buttons: Optional[
153158
Union[
154159
conlist(Union[ButtonPhoneNumber, ButtonUrl], max_items=2),

tests/whatsapp/models/test_create_template.py

Lines changed: 76 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ def test_when_name_is_invalid__validation_error_is_raised(name):
3232
**{
3333
"name": name,
3434
"language": "en",
35-
"category": "ACCOUNT_UPDATE",
36-
"structure": {"body": "example {{1}} body"},
35+
"category": "OTP",
36+
"structure": {
37+
"body": {"text": "body {{1}} content", "examples": ["example"]}
38+
},
3739
}
3840
)
3941

@@ -45,8 +47,10 @@ def test_when_language_is_invalid__validation_error_is_raised(language):
4547
**{
4648
"name": "examplename",
4749
"language": language,
48-
"category": "ACCOUNT_UPDATE",
49-
"structure": {"body": "example {{1}} body"},
50+
"category": "OTP",
51+
"structure": {
52+
"body": {"text": "body {{1}} content", "examples": ["example"]}
53+
},
5054
}
5155
)
5256

@@ -59,7 +63,9 @@ def test_when_category_is_invalid__validation_error_is_raised(category):
5963
"name": "examplename",
6064
"language": "en",
6165
"category": category,
62-
"structure": {"body": "example {{1}} body"},
66+
"structure": {
67+
"body": {"text": "body {{1}} content", "examples": ["example"]}
68+
},
6369
}
6470
)
6571

@@ -83,7 +89,7 @@ def test_when_structure_is_invalid__validation_error_is_raised(structure):
8389
**{
8490
"name": "examplename",
8591
"language": "en",
86-
"category": "ACCOUNT_UPDATE",
92+
"category": "OTP",
8793
"structure": structure,
8894
}
8995
)
@@ -96,10 +102,10 @@ def test_when_header_is_invalid__validation_error_is_raised(header):
96102
**{
97103
"name": "examplename",
98104
"language": "en",
99-
"category": "ACCOUNT_UPDATE",
105+
"category": "OTP",
100106
"structure": {
101107
"header": header,
102-
"body": "example {{1}} body",
108+
"body": {"text": "body {{1}} content", "examples": ["example"]},
103109
},
104110
}
105111
)
@@ -112,7 +118,7 @@ def test_when_body_is_invalid__validation_error_is_raised(body):
112118
**{
113119
"name": "examplename",
114120
"language": "en",
115-
"category": "ACCOUNT_UPDATE",
121+
"category": "OTP",
116122
"structure": {
117123
"header": {"format": "TEXT", "text": "Text example"},
118124
"body": body,
@@ -121,23 +127,58 @@ def test_when_body_is_invalid__validation_error_is_raised(body):
121127
)
122128

123129

124-
@pytest.mark.parametrize("footer", [{}, get_random_string(61)])
130+
@pytest.mark.parametrize("body_text", [None, {}])
131+
def test_when_body_text_is_invalid__validation_error_is_raised(body_text):
132+
with pytest.raises(ValidationError):
133+
CreateTemplateBodyFactory.build(
134+
**{
135+
"name": "examplename",
136+
"language": "en",
137+
"category": "OTP",
138+
"structure": {
139+
"header": {"format": "TEXT", "text": "Text example"},
140+
"body": {
141+
"text": body_text,
142+
},
143+
},
144+
}
145+
)
146+
147+
148+
@pytest.mark.parametrize("footer", [{}])
125149
def test_when_footer_is_invalid__validation_error_is_raised(footer):
126150
with pytest.raises(ValidationError):
127151
CreateTemplateBodyFactory.build(
128152
**{
129153
"name": "examplename",
130154
"language": "en",
131-
"category": "ACCOUNT_UPDATE",
155+
"category": "OTP",
132156
"structure": {
133157
"header": {"format": "TEXT", "text": "Text example"},
134-
"body": "text",
158+
"body": {"text": "body {{1}} content", "examples": ["example"]},
135159
"footer": footer,
136160
},
137161
}
138162
)
139163

140164

165+
@pytest.mark.parametrize("footer_text", [{}, get_random_string(61)])
166+
def test_when_footer_text_is_invalid__validation_error_is_raised(footer_text):
167+
with pytest.raises(ValidationError):
168+
CreateTemplateBodyFactory.build(
169+
**{
170+
"name": "examplename",
171+
"language": "en",
172+
"category": "OTP",
173+
"structure": {
174+
"header": {"format": "TEXT", "text": "Text example"},
175+
"body": {"text": "body {{1}} content", "examples": ["example"]},
176+
"footer": {"text": footer_text},
177+
},
178+
}
179+
)
180+
181+
141182
@pytest.mark.parametrize(
142183
"buttons",
143184
[
@@ -162,8 +203,11 @@ def test_when_buttons_is_invalid__validation_error_is_raised(buttons):
162203
**{
163204
"name": "examplename",
164205
"language": "en",
165-
"category": "ACCOUNT_UPDATE",
166-
"structure": {"body": "text", "buttons": buttons},
206+
"category": "OTP",
207+
"structure": {
208+
"body": {"text": "body {{1}} content", "examples": ["example"]},
209+
"buttons": buttons,
210+
},
167211
}
168212
)
169213

@@ -175,8 +219,11 @@ def test_when_button_type_is_invalid__validation_error_is_raised(button_type):
175219
**{
176220
"name": "examplename",
177221
"language": "en",
178-
"category": "ACCOUNT_UPDATE",
179-
"structure": {"body": "text", "buttons": [{"type": button_type}]},
222+
"category": "OTP",
223+
"structure": {
224+
"body": {"text": "body {{1}} content", "examples": ["example"]},
225+
"buttons": [{"type": button_type}],
226+
},
180227
}
181228
)
182229

@@ -188,10 +235,10 @@ def test_when_quick_reply_button_text_is_invalid__validation_error_is_raised(tex
188235
**{
189236
"name": "examplename",
190237
"language": "en",
191-
"category": "ACCOUNT_UPDATE",
238+
"category": "OTP",
192239
"structure": {
193240
"header": {"format": "TEXT", "text": "Text example"},
194-
"body": "example {{1}} body",
241+
"body": {"text": "body {{1}} content", "examples": ["example"]},
195242
"buttons": [{"type": "QUICK_REPLY", "text": text}],
196243
},
197244
}
@@ -205,10 +252,10 @@ def test_when_phone_number_button_text_is_invalid__validation_error_is_raised(te
205252
**{
206253
"name": "examplename",
207254
"language": "en",
208-
"category": "ACCOUNT_UPDATE",
255+
"category": "OTP",
209256
"structure": {
210257
"header": {"format": "TEXT", "text": "Text example"},
211-
"body": "example {{1}} body",
258+
"body": {"text": "body {{1}} content", "examples": ["example"]},
212259
"buttons": [
213260
{"type": "PHONE_NUMBER", "text": text, "phoneNumber": "324561"}
214261
],
@@ -224,10 +271,10 @@ def test_when_phone_number_button_number_is_invalid__validation_error_is_raised(
224271
**{
225272
"name": "examplename",
226273
"language": "en",
227-
"category": "ACCOUNT_UPDATE",
274+
"category": "OTP",
228275
"structure": {
229276
"header": {"format": "TEXT", "text": "Text example"},
230-
"body": "example {{1}} body",
277+
"body": {"text": "body {{1}} content", "examples": ["example"]},
231278
"buttons": [
232279
{"type": "PHONE_NUMBER", "text": "test", "phoneNumber": number}
233280
],
@@ -243,10 +290,10 @@ def test_when_url_button_text_is_invalid__validation_error_is_raised(text):
243290
**{
244291
"name": "examplename",
245292
"language": "en",
246-
"category": "ACCOUNT_UPDATE",
293+
"category": "OTP",
247294
"structure": {
248295
"header": {"format": "TEXT", "text": "Text example"},
249-
"body": "example {{1}} body",
296+
"body": {"text": "body {{1}} content", "examples": ["example"]},
250297
"buttons": [
251298
{"type": "URL", "text": text, "url": "https://url.com"}
252299
],
@@ -271,10 +318,10 @@ def test_when_url_button_url_is_invalid__validation_error_is_raised(url):
271318
**{
272319
"name": "examplename",
273320
"language": "en",
274-
"category": "ACCOUNT_UPDATE",
321+
"category": "OTP",
275322
"structure": {
276323
"header": {"format": "TEXT", "text": "Text example"},
277-
"body": "example {{1}} body",
324+
"body": {"text": "body {{1}} content", "examples": ["example"]},
278325
"buttons": [{"type": "URL", "text": "Text example", "url": url}],
279326
},
280327
}
@@ -287,11 +334,11 @@ def test_when_input_data_is_valid__validation_error_is_not_raised():
287334
**{
288335
"name": "exampl_ename",
289336
"language": "en",
290-
"category": "ACCOUNT_UPDATE",
337+
"category": "OTP",
291338
"structure": {
292339
"header": {"format": "TEXT", "text": "Text example"},
293-
"body": "example {{1}} body",
294-
"footer": "some footer",
340+
"body": {"text": "body {{1}} content", "examples": ["example"]},
341+
"footer": {"text": "footer content"},
295342
"buttons": [
296343
{"type": "URL", "text": "url", "url": "http://url.com"},
297344
{

0 commit comments

Comments
 (0)