@@ -1165,28 +1165,82 @@ def test_create_template_with_raw_blob(self, user, mailbox):
1165
1165
template = models .MessageTemplate .objects .get ()
1166
1166
assert template .raw_blob .get_content ().decode ("utf-8" ) == "test blob content"
1167
1167
1168
- def test_update_template_raw_blob (self , user , mailbox ):
1169
- """Test updating a template with raw_blob ."""
1168
+ def test_update_content_fields_atomic_validation (self , user , mailbox ):
1169
+ """Test that content fields must be updated together atomically ."""
1170
1170
factories .MailboxAccessFactory (
1171
1171
mailbox = mailbox ,
1172
1172
user = user ,
1173
1173
role = models .MailboxRoleChoices .ADMIN ,
1174
1174
)
1175
1175
1176
- # Create template with blob
1176
+ # Create a template
1177
1177
template = factories .MessageTemplateFactory (
1178
- name = "Blob Template" ,
1179
- html_body = "<p>Content </p>" ,
1180
- text_body = "Content " ,
1178
+ name = "Test Template" ,
1179
+ html_body = "<p>Original content </p>" ,
1180
+ text_body = "Original content " ,
1181
1181
type = models .MessageTemplateTypeChoices .SIGNATURE ,
1182
1182
mailbox = mailbox ,
1183
1183
)
1184
1184
1185
1185
client = APIClient ()
1186
1186
client .force_authenticate (user = user )
1187
1187
1188
+ # Try to update only html_body - should fail
1188
1189
data = {
1189
- "raw_blob" : "new blob content" ,
1190
+ "html_body" : "<p>Updated content</p>" ,
1191
+ "mailbox_id" : str (mailbox .id ),
1192
+ }
1193
+
1194
+ response = client .patch (
1195
+ reverse ("message-templates-detail" , kwargs = {"pk" : template .id }),
1196
+ data ,
1197
+ format = "json" ,
1198
+ )
1199
+ assert response .status_code == status .HTTP_400_BAD_REQUEST
1200
+ assert (
1201
+ "all three fields (html_body, text_body, raw_blob) must be provided together"
1202
+ in str (response .data )
1203
+ )
1204
+
1205
+ # Try to update only text_body - should fail
1206
+ data = {
1207
+ "text_body" : "Updated content" ,
1208
+ "mailbox_id" : str (mailbox .id ),
1209
+ }
1210
+
1211
+ response = client .patch (
1212
+ reverse ("message-templates-detail" , kwargs = {"pk" : template .id }),
1213
+ data ,
1214
+ format = "json" ,
1215
+ )
1216
+ assert response .status_code == status .HTTP_400_BAD_REQUEST
1217
+ assert (
1218
+ "all three fields (html_body, text_body, raw_blob) must be provided together"
1219
+ in str (response .data )
1220
+ )
1221
+
1222
+ # Try to update only raw_blob - should fail
1223
+ data = {
1224
+ "raw_blob" : "updated raw content" ,
1225
+ "mailbox_id" : str (mailbox .id ),
1226
+ }
1227
+
1228
+ response = client .patch (
1229
+ reverse ("message-templates-detail" , kwargs = {"pk" : template .id }),
1230
+ data ,
1231
+ format = "json" ,
1232
+ )
1233
+ assert response .status_code == status .HTTP_400_BAD_REQUEST
1234
+ assert (
1235
+ "all three fields (html_body, text_body, raw_blob) must be provided together"
1236
+ in str (response .data )
1237
+ )
1238
+
1239
+ # Update all three fields together - should succeed
1240
+ data = {
1241
+ "html_body" : "<p>Updated content</p>" ,
1242
+ "text_body" : "Updated content" ,
1243
+ "raw_blob" : "updated raw content" ,
1190
1244
"mailbox_id" : str (mailbox .id ),
1191
1245
}
1192
1246
@@ -1196,9 +1250,100 @@ def test_update_template_raw_blob(self, user, mailbox):
1196
1250
format = "json" ,
1197
1251
)
1198
1252
assert response .status_code == status .HTTP_200_OK
1199
- # raw_blob should contain the new content
1200
- assert response .data ["raw_blob" ] == "new blob content"
1201
1253
1202
- # check that blob was created with new content
1254
+ # Verify all fields were updated
1203
1255
template .refresh_from_db ()
1204
- assert template .raw_blob .get_content ().decode ("utf-8" ) == "new blob content"
1256
+ assert template .html_body == "<p>Updated content</p>"
1257
+ assert template .text_body == "Updated content"
1258
+ assert template .raw_blob .get_content ().decode ("utf-8" ) == "updated raw content"
1259
+
1260
+ def test_create_content_fields_atomic_validation (self , user , mailbox ):
1261
+ """Test that content fields must be created together atomically."""
1262
+ factories .MailboxAccessFactory (
1263
+ mailbox = mailbox ,
1264
+ user = user ,
1265
+ role = models .MailboxRoleChoices .ADMIN ,
1266
+ )
1267
+
1268
+ client = APIClient ()
1269
+ client .force_authenticate (user = user )
1270
+
1271
+ # Try to create with only html_body - should fail
1272
+ data = {
1273
+ "name" : "Test Template" ,
1274
+ "html_body" : "<p>Content</p>" ,
1275
+ "type" : "signature" ,
1276
+ "mailbox_id" : str (mailbox .id ),
1277
+ }
1278
+
1279
+ response = client .post (
1280
+ reverse ("message-templates-list" ),
1281
+ data ,
1282
+ format = "json" ,
1283
+ )
1284
+ assert response .status_code == status .HTTP_400_BAD_REQUEST
1285
+ assert (
1286
+ "all three fields (html_body, text_body, raw_blob) must be provided together"
1287
+ in str (response .data )
1288
+ )
1289
+
1290
+ # Try to create with only text_body - should fail
1291
+ data = {
1292
+ "name" : "Test Template" ,
1293
+ "text_body" : "Content" ,
1294
+ "type" : "signature" ,
1295
+ "mailbox_id" : str (mailbox .id ),
1296
+ }
1297
+
1298
+ response = client .post (
1299
+ reverse ("message-templates-list" ),
1300
+ data ,
1301
+ format = "json" ,
1302
+ )
1303
+ assert response .status_code == status .HTTP_400_BAD_REQUEST
1304
+ assert (
1305
+ "all three fields (html_body, text_body, raw_blob) must be provided together"
1306
+ in str (response .data )
1307
+ )
1308
+
1309
+ # Try to create with only raw_blob - should fail
1310
+ data = {
1311
+ "name" : "Test Template" ,
1312
+ "raw_blob" : "raw content" ,
1313
+ "type" : "signature" ,
1314
+ "mailbox_id" : str (mailbox .id ),
1315
+ }
1316
+
1317
+ response = client .post (
1318
+ reverse ("message-templates-list" ),
1319
+ data ,
1320
+ format = "json" ,
1321
+ )
1322
+ assert response .status_code == status .HTTP_400_BAD_REQUEST
1323
+ assert (
1324
+ "all three fields (html_body, text_body, raw_blob) must be provided together"
1325
+ in str (response .data )
1326
+ )
1327
+
1328
+ # Create with all three fields together - should succeed
1329
+ data = {
1330
+ "name" : "Test Template" ,
1331
+ "html_body" : "<p>Content</p>" ,
1332
+ "text_body" : "Content" ,
1333
+ "raw_blob" : "raw content" ,
1334
+ "type" : "signature" ,
1335
+ "mailbox_id" : str (mailbox .id ),
1336
+ }
1337
+
1338
+ response = client .post (
1339
+ reverse ("message-templates-list" ),
1340
+ data ,
1341
+ format = "json" ,
1342
+ )
1343
+ assert response .status_code == status .HTTP_201_CREATED
1344
+
1345
+ # Verify all fields were created
1346
+ template = models .MessageTemplate .objects .get (name = "Test Template" )
1347
+ assert template .html_body == "<p>Content</p>"
1348
+ assert template .text_body == "Content"
1349
+ assert template .raw_blob .get_content ().decode ("utf-8" ) == "raw content"
0 commit comments