From 951d067dd6ec39bcecf326e0fdfdb18b5013030e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 30 Apr 2021 17:54:03 +0200 Subject: [PATCH] adding revoke and fixing ConfigRevike --- ereuse_devicehub/resources/action/__init__.py | 6 ++++++ ereuse_devicehub/resources/action/models.py | 5 +++++ ereuse_devicehub/resources/action/schemas.py | 21 +++++++++++++++++++ tests/test_action.py | 14 ++++++++++--- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/resources/action/__init__.py b/ereuse_devicehub/resources/action/__init__.py index 40338d97..93716d44 100644 --- a/ereuse_devicehub/resources/action/__init__.py +++ b/ereuse_devicehub/resources/action/__init__.py @@ -249,6 +249,7 @@ class MakeAvailable(ActionDef): VIEW = None SCHEMA = schemas.MakeAvailable + class ConfirmDef(ActionDef): VIEW = None SCHEMA = schemas.Confirm @@ -259,6 +260,11 @@ class ConfirmRevokeDef(ActionDef): SCHEMA = schemas.ConfirmRevoke +class RevokeDef(ActionDef): + VIEW = None + SCHEMA = schemas.Revoke + + class TradeDef(ActionDef): VIEW = None SCHEMA = schemas.Trade diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 1db7087e..71b8d2f9 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1459,6 +1459,11 @@ class Confirm(JoinedTableMixin, ActionWithMultipleDevices): origin = 'From' return '<{0.t} {0.id} accepted by {1}>'.format(self, origin) + +class Revoke(Confirm): + pass + + class ConfirmRevoke(Confirm): pass diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 8ff82bbf..c84e25dc 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -474,6 +474,27 @@ class Confirm(ActionWithMultipleDevices): raise ValidationError(txt) +class Revoke(ActionWithMultipleDevices): + __doc__ = m.Revoke.__doc__ + action = NestedOn('Action', only_query='id') + + @validates_schema + def validate_revoke(self, data: dict): + acceptances = copy.copy(data['action'].acceptances) + acceptances.reverse() + # import pdb; pdb.set_trace() + for ac in acceptances: + if ac.user == g.user and not ac.t == 'ConfirmRevoke': + return data + + if ac.user == g.user and ac.t == 'ConfirmRevoke': + txt = "you are revoke this action before" + raise ValidationError(txt) + + txt = "you can't revoke this action because you did not confirm ir before" + raise ValidationError(txt) + + class ConfirmRevoke(ActionWithMultipleDevices): __doc__ = m.ConfirmRevoke.__doc__ action = NestedOn('Action', only_query='id') diff --git a/tests/test_action.py b/tests/test_action.py index d6ec8041..8ea7d716 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -1068,6 +1068,7 @@ def test_endpoint_confirm(user: UserClient, user2: UserClient): user2.post(res=models.Action, data=request_confirm, status=422) assert len(trade.acceptances) == 2 + @pytest.mark.mvp @pytest.mark.usefixtures(conftest.app_context.__name__) def test_confirm_revoke(user: UserClient, user2: UserClient): @@ -1094,16 +1095,23 @@ def test_confirm_revoke(user: UserClient, user2: UserClient): 'devices': [] } - user2.post(res=models.Action, data=request_confirm) - request_revoke = { - 'type': 'ConfirmRevoke', + 'type': 'Revoke', 'action': trade.id, 'devices': [], } + + # Normal confirmation + user2.post(res=models.Action, data=request_confirm) + + # Normal revoke user2.post(res=models.Action, data=request_revoke) + + # Error for try duplicate revoke user2.post(res=models.Action, data=request_revoke, status=422) assert len(trade.acceptances) == 3 + + # You can to do one confirmation next of one revoke user2.post(res=models.Action, data=request_confirm) assert len(trade.acceptances) == 4