Skip to content

Commit 8a9734e

Browse files
committed
Fix test for entity edit
1 parent ddc23c5 commit 8a9734e

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

entity/api_v2/serializers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def validate(self, attr):
142142
if "type" in attr and attr["type"] != entity_attr.type:
143143
raise ValidationError("type cannot be changed")
144144

145-
user: User = self.context["request"].user
145+
user: User = self.context.get("_user") or self.context["request"].user
146146
if attr["is_deleted"] and not user.has_permission(entity_attr, ACLType.Full):
147147
raise PermissionDenied("Does not have permission to delete")
148148
if not attr["is_deleted"] and not user.has_permission(entity_attr, ACLType.Writable):

entity/tasks.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ def create_entity_v2(self, job_id: int):
255255
job.update(Job.STATUS["ERROR"])
256256
return
257257

258-
serializer.create(serializer.validated_data)
258+
try:
259+
serializer.create(serializer.validated_data)
260+
except Exception:
261+
job.update(Job.STATUS["ERROR"])
262+
return
259263

260264
# update job status and save it
261265
job.update(Job.STATUS["DONE"])
@@ -283,7 +287,11 @@ def edit_entity_v2(self, job_id: int):
283287
job.update(Job.STATUS["ERROR"])
284288
return
285289

286-
serializer.update(entity, serializer.validated_data)
290+
try:
291+
serializer.update(entity, serializer.validated_data)
292+
except Exception:
293+
job.update(Job.STATUS["ERROR"])
294+
return
287295

288296
# update job status and save it
289297
job.update(Job.STATUS["DONE"])

entity/tests/test_api_v2.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -1283,11 +1283,11 @@ def test_create_entity_with_webhook_is_verified(self):
12831283
entity: Entity = Entity.objects.get(name=params["name"])
12841284
self.assertEqual([x.is_verified for x in entity.webhooks.all()], [True, False])
12851285

1286-
@mock.patch("custom_view.is_custom", mock.Mock(return_value=True))
1287-
@mock.patch("custom_view.call_custom")
12881286
@mock.patch(
12891287
"entity.tasks.create_entity_v2.delay", mock.Mock(side_effect=tasks.create_entity_v2)
12901288
)
1289+
@mock.patch("custom_view.is_custom", mock.Mock(return_value=True))
1290+
@mock.patch("custom_view.call_custom")
12911291
def test_create_entity_with_customview(self, mock_call_custom):
12921292
params = {"name": "hoge"}
12931293

@@ -1296,8 +1296,8 @@ def side_effect(handler_name, entity_name, user, *args):
12961296

12971297
mock_call_custom.side_effect = side_effect
12981298
resp = self.client.post("/entity/api/v2/", json.dumps(params), "application/json")
1299-
self.assertEqual(resp.status_code, 400)
1300-
self.assertEqual(resp.json(), [{"code": "AE-121000", "message": "create error"}])
1299+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
1300+
self.assertTrue(mock_call_custom.called)
13011301

13021302
def side_effect(handler_name, entity_name, user, *args):
13031303
# Check specified parameters are expected
@@ -1351,6 +1351,7 @@ def test_create_entity_with_webhook_is_disabled(self):
13511351
finally:
13521352
settings.AIRONE_FLAGS = {"WEBHOOK": True}
13531353

1354+
@mock.patch("entity.tasks.edit_entity_v2.delay", mock.Mock(side_effect=tasks.edit_entity_v2))
13541355
def test_update_entity(self):
13551356
entity: Entity = self.create_entity(
13561357
**{
@@ -1403,15 +1404,8 @@ def test_update_entity(self):
14031404
resp = self.client.put(
14041405
"/entity/api/v2/%d/" % entity.id, json.dumps(params), "application/json"
14051406
)
1406-
self.assertEqual(resp.status_code, 200)
1407+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
14071408

1408-
self.assertEqual(
1409-
resp.json(),
1410-
{
1411-
"id": entity.id,
1412-
"name": "change-entity1",
1413-
},
1414-
)
14151409
entity.refresh_from_db()
14161410
self.assertEqual(entity.name, "change-entity1")
14171411
self.assertEqual(entity.note, "change-hoge")
@@ -1567,6 +1561,7 @@ def test_update_entity_with_invalid_param(self):
15671561
{"is_toplevel": [{"code": "AE-121000", "message": "Must be a valid boolean."}]},
15681562
)
15691563

1564+
@mock.patch("entity.tasks.edit_entity_v2.delay", mock.Mock(side_effect=tasks.edit_entity_v2))
15701565
def test_update_entity_with_invalid_param_attrs(self):
15711566
params = {
15721567
"attrs": "hoge",
@@ -2088,7 +2083,7 @@ def test_update_entity_with_invalid_param_attrs(self):
20882083
resp = self.client.put(
20892084
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
20902085
)
2091-
self.assertEqual(resp.status_code, 200)
2086+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
20922087
self.assertTrue(
20932088
all(["hoge" not in x.name for x in self.entity.attrs.filter(is_active=True)])
20942089
)
@@ -2133,7 +2128,7 @@ def test_update_entity_with_invalid_param_attrs(self):
21332128
resp = self.client.put(
21342129
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
21352130
)
2136-
self.assertEqual(resp.status_code, 200)
2131+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
21372132

21382133
def test_update_entity_with_invalid_param_webhooks(self):
21392134
params = {
@@ -2429,6 +2424,7 @@ def test_update_entity_with_invalid_param_webhooks(self):
24292424
},
24302425
)
24312426

2427+
@mock.patch("entity.tasks.edit_entity_v2.delay", mock.Mock(side_effect=tasks.edit_entity_v2))
24322428
def test_update_entity_with_attrs_referral(self):
24332429
self.entity.attrs.all().delete()
24342430
params = {
@@ -2446,14 +2442,15 @@ def test_update_entity_with_attrs_referral(self):
24462442
resp = self.client.put(
24472443
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
24482444
)
2449-
self.assertEqual(resp.status_code, 200)
2445+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
24502446

24512447
for entity_attr in self.entity.attrs.all():
24522448
if entity_attr.type & AttrTypeValue["object"]:
24532449
self.assertEqual([x.id for x in entity_attr.referral.all()], [self.ref_entity.id])
24542450
else:
24552451
self.assertEqual([x.id for x in entity_attr.referral.all()], [])
24562452

2453+
@mock.patch("entity.tasks.edit_entity_v2.delay", mock.Mock(side_effect=tasks.edit_entity_v2))
24572454
def test_update_entity_with_webhook_is_verified(self):
24582455
self.entity.webhooks.all().delete()
24592456
params = {
@@ -2463,10 +2460,11 @@ def test_update_entity_with_webhook_is_verified(self):
24632460
resp = self.client.put(
24642461
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
24652462
)
2466-
self.assertEqual(resp.status_code, 200)
2463+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
24672464

24682465
self.assertEqual([x.is_verified for x in self.entity.webhooks.all()], [True, False])
24692466

2467+
@mock.patch("entity.tasks.edit_entity_v2.delay", mock.Mock(side_effect=tasks.edit_entity_v2))
24702468
@mock.patch("custom_view.is_custom", mock.Mock(return_value=True))
24712469
@mock.patch("custom_view.call_custom")
24722470
def test_update_entity_with_customview(self, mock_call_custom):
@@ -2479,8 +2477,8 @@ def side_effect(handler_name, entity_name, user, *args):
24792477
resp = self.client.put(
24802478
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
24812479
)
2482-
self.assertEqual(resp.status_code, 400)
2483-
self.assertEqual(resp.json(), [{"code": "AE-121000", "message": "update error"}])
2480+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
2481+
self.assertTrue(mock_call_custom.called)
24842482

24852483
def side_effect(handler_name, entity_name, user, *args):
24862484
# Check specified parameters are expected
@@ -2506,9 +2504,10 @@ def side_effect(handler_name, entity_name, user, *args):
25062504
resp = self.client.put(
25072505
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
25082506
)
2509-
self.assertEqual(resp.status_code, 200)
2507+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
25102508
self.assertTrue(mock_call_custom.called)
25112509

2510+
@mock.patch("entity.tasks.edit_entity_v2.delay", mock.Mock(side_effect=tasks.edit_entity_v2))
25122511
def test_update_entry_with_specified_only_param(self):
25132512
self.entity.note = "hoge"
25142513
self.entity.status = Entity.STATUS_TOP_LEVEL
@@ -2525,7 +2524,7 @@ def test_update_entry_with_specified_only_param(self):
25252524
resp = self.client.put(
25262525
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
25272526
)
2528-
self.assertEqual(resp.status_code, 200)
2527+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
25292528

25302529
self.entity.refresh_from_db()
25312530
changed_attr_count = self.entity.attrs.filter(is_active=True).count()
@@ -2536,6 +2535,7 @@ def test_update_entry_with_specified_only_param(self):
25362535
self.assertEqual(attr_count, changed_attr_count)
25372536
self.assertEqual(webhook_count, changed_webhook_count)
25382537

2538+
@mock.patch("entity.tasks.edit_entity_v2.delay", mock.Mock(side_effect=tasks.edit_entity_v2))
25392539
def test_update_entity_without_permission(self):
25402540
self.role.users.add(self.user)
25412541

@@ -2574,7 +2574,7 @@ def test_update_entity_without_permission(self):
25742574
resp = self.client.put(
25752575
"/entity/api/v2/%d/" % self.entity.id, json.dumps(paramas), "application/json"
25762576
)
2577-
self.assertEqual(resp.status_code, 200)
2577+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
25782578

25792579
# permission nothing EntityAttr update
25802580
self.entity.is_public = True
@@ -2608,7 +2608,7 @@ def test_update_entity_without_permission(self):
26082608
resp = self.client.put(
26092609
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
26102610
)
2611-
self.assertEqual(resp.status_code, 200)
2611+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
26122612

26132613
# permission writable EntityAttr delete
26142614
params = {"attrs": [{"id": entity_attr.id, "is_deleted": True}]}
@@ -2626,7 +2626,7 @@ def test_update_entity_without_permission(self):
26262626
resp = self.client.put(
26272627
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
26282628
)
2629-
self.assertEqual(resp.status_code, 200)
2629+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
26302630

26312631
def test_update_entity_with_webhook_is_disabled(self):
26322632
entity: Entity = self.create_entity(

0 commit comments

Comments
 (0)