adding revoke and fixing ConfigRevike

This commit is contained in:
Cayo Puigdefabregas 2021-04-30 17:54:03 +02:00
parent 34fbf0dca6
commit 951d067dd6
4 changed files with 43 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -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')

View file

@ -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