Skip to content

Commit 6fa077a

Browse files
committed
Fix test for entity edit
1 parent ddc23c5 commit 6fa077a

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
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/tests/test_api_v2.py

+19-20
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

@@ -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):
@@ -2480,7 +2478,6 @@ def side_effect(handler_name, entity_name, user, *args):
24802478
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
24812479
)
24822480
self.assertEqual(resp.status_code, 400)
2483-
self.assertEqual(resp.json(), [{"code": "AE-121000", "message": "update error"}])
24842481

24852482
def side_effect(handler_name, entity_name, user, *args):
24862483
# Check specified parameters are expected
@@ -2506,9 +2503,10 @@ def side_effect(handler_name, entity_name, user, *args):
25062503
resp = self.client.put(
25072504
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
25082505
)
2509-
self.assertEqual(resp.status_code, 200)
2506+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
25102507
self.assertTrue(mock_call_custom.called)
25112508

2509+
@mock.patch("entity.tasks.edit_entity_v2.delay", mock.Mock(side_effect=tasks.edit_entity_v2))
25122510
def test_update_entry_with_specified_only_param(self):
25132511
self.entity.note = "hoge"
25142512
self.entity.status = Entity.STATUS_TOP_LEVEL
@@ -2525,7 +2523,7 @@ def test_update_entry_with_specified_only_param(self):
25252523
resp = self.client.put(
25262524
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
25272525
)
2528-
self.assertEqual(resp.status_code, 200)
2526+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
25292527

25302528
self.entity.refresh_from_db()
25312529
changed_attr_count = self.entity.attrs.filter(is_active=True).count()
@@ -2536,6 +2534,7 @@ def test_update_entry_with_specified_only_param(self):
25362534
self.assertEqual(attr_count, changed_attr_count)
25372535
self.assertEqual(webhook_count, changed_webhook_count)
25382536

2537+
@mock.patch("entity.tasks.edit_entity_v2.delay", mock.Mock(side_effect=tasks.edit_entity_v2))
25392538
def test_update_entity_without_permission(self):
25402539
self.role.users.add(self.user)
25412540

@@ -2574,7 +2573,7 @@ def test_update_entity_without_permission(self):
25742573
resp = self.client.put(
25752574
"/entity/api/v2/%d/" % self.entity.id, json.dumps(paramas), "application/json"
25762575
)
2577-
self.assertEqual(resp.status_code, 200)
2576+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
25782577

25792578
# permission nothing EntityAttr update
25802579
self.entity.is_public = True
@@ -2608,7 +2607,7 @@ def test_update_entity_without_permission(self):
26082607
resp = self.client.put(
26092608
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
26102609
)
2611-
self.assertEqual(resp.status_code, 200)
2610+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
26122611

26132612
# permission writable EntityAttr delete
26142613
params = {"attrs": [{"id": entity_attr.id, "is_deleted": True}]}
@@ -2626,7 +2625,7 @@ def test_update_entity_without_permission(self):
26262625
resp = self.client.put(
26272626
"/entity/api/v2/%d/" % self.entity.id, json.dumps(params), "application/json"
26282627
)
2629-
self.assertEqual(resp.status_code, 200)
2628+
self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
26302629

26312630
def test_update_entity_with_webhook_is_disabled(self):
26322631
entity: Entity = self.create_entity(

0 commit comments

Comments
 (0)