From 2790a7756c4f5e8ea4bc8f1d08319eae90f8e2db Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 1 Oct 2021 12:56:12 +0200 Subject: [PATCH 1/7] test action trade without devices and documents --- tests/test_action.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_action.py b/tests/test_action.py index 4d2520c0..11e98560 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -771,6 +771,29 @@ def test_trade_endpoint(user: UserClient, user2: UserClient): device2, _ = user2.get(res=Device, item=device['id']) assert device2['id'] == device['id'] + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_without_device(user: UserClient): + """Test one offer with automatic confirmation and without user to""" + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'lot': lot['id'], + 'confirms': False, + 'code': 'MAX' + } + user.post(res=models.Action, data=request_post) + + trade = models.Trade.query.one() + assert str(trade.lot.id) == lot['id'] + + @pytest.mark.mvp @pytest.mark.usefixtures(conftest.app_context.__name__) def test_offer_without_to(user: UserClient): From 2d305f5a4326185c2ee0677cf8cba473bfa107ad Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 1 Oct 2021 13:18:56 +0200 Subject: [PATCH 2/7] adding trade without devices --- tests/test_action.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/test_action.py b/tests/test_action.py index 11e98560..fe94d518 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -774,20 +774,18 @@ def test_trade_endpoint(user: UserClient, user2: UserClient): @pytest.mark.mvp @pytest.mark.usefixtures(conftest.app_context.__name__) -def test_trade_without_device(user: UserClient): +def test_trade_without_device(user: UserClient, user2: UserClient): """Test one offer with automatic confirmation and without user to""" lot, _ = user.post({'name': 'MyLot'}, res=Lot) request_post = { 'type': 'Trade', - 'devices': [], 'userFromEmail': user.email, - 'price': 10, - 'date': "2020-12-01T02:00:00+00:00", + 'userToEmail': user2.email, 'lot': lot['id'], - 'confirms': False, - 'code': 'MAX' + 'confirms': True, } + user.post(res=models.Action, data=request_post) trade = models.Trade.query.one() From 920937a51f778839e1d30d5bca07f89d0d49fe3b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 1 Oct 2021 13:30:39 +0200 Subject: [PATCH 3/7] fixing bug of trade without devices --- ereuse_devicehub/resources/action/schemas.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index ec11dd8f..5a1bb38c 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -707,6 +707,11 @@ class Trade(ActionWithMultipleDevices): required=True, only_query='id') + @pre_load + def adding_devices(self, data: dict): + if not 'devices' in data.keys(): + data['devices'] = [] + @validates_schema def validate_lot(self, data: dict): if not g.user.email in [data['user_from_email'], data['user_to_email']]: From b83bbe25a0212fbe2198bb0324e8467e4bc45706 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 1 Oct 2021 18:01:39 +0200 Subject: [PATCH 4/7] test bug 168 --- tests/test_action.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/test_action.py b/tests/test_action.py index fe94d518..a1363d84 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -2521,3 +2521,59 @@ def test_moveOnDocument(user: UserClient, user2: UserClient): assert description == mvs.description tradedocument_to, _ = user.post(res=TradeDocument, data=request_post2) user.post(res=models.Action, data=request_moveOn, status=422) + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_moveOnDocument_bug168(user: UserClient, user2: UserClient): + """If you use one moveOnDocument in a trade Document. Next you can not drop this document.""" + lotIn, _ = user.post({'name': 'MyLotIn'}, res=Lot) + lotOut, _ = user.post({'name': 'MyLotOut'}, res=Lot) + url = 'http://www.ereuse.org/apapaapaapaapaapaapaapaapaapaapapaapaapaapaapaapaapaapaapaapapaapaapaapaapaapaapaapaapaapaaaa', + request_post1 = { + 'filename': 'test.pdf', + 'hash': 'bbbbbbbb', + 'url': url, + 'weight': 150, + 'lot': lotIn['id'] + } + tradedocument_from, _ = user.post(res=TradeDocument, data=request_post1) + id_hash = 'aaaaaaaaa' + request_post2 = { + 'filename': 'test.pdf', + 'hash': id_hash, + 'url': url, + 'weight': 0, + 'lot': lotOut['id'] + } + tradedocument_to, _ = user.post(res=TradeDocument, data=request_post2) + + request_trade = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'lot': lotIn['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_trade) + + description = 'This is a good description' + request_moveOn = { + 'type': 'MoveOnDocument', + 'weight': 15, + 'container_from': tradedocument_from['id'], + 'container_to': id_hash, + 'description': description + } + doc, _ = user.post(res=models.Action, data=request_moveOn) + + assert doc['weight'] == request_moveOn['weight'] + assert doc['container_from']['id'] == tradedocument_from['id'] + assert doc['container_to']['id'] == tradedocument_to['id'] + + import pdb; pdb.set_trace() + tradedocument, _ = user.delete(res=TradeDocument, item=tradedocument_to['id']) From b02f9dfac5b8a9c3c054b0db28fa7a56df34768f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 4 Oct 2021 13:34:49 +0200 Subject: [PATCH 5/7] fixing test bug #168 --- tests/test_action.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_action.py b/tests/test_action.py index a1363d84..34cb246d 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -2564,16 +2564,18 @@ def test_moveOnDocument_bug168(user: UserClient, user2: UserClient): description = 'This is a good description' request_moveOn = { 'type': 'MoveOnDocument', - 'weight': 15, + 'weight': 4, 'container_from': tradedocument_from['id'], 'container_to': id_hash, 'description': description } doc, _ = user.post(res=models.Action, data=request_moveOn) + trade = models.Trade.query.one() + trade_document1 = TradeDocument.query.filter_by(id=tradedocument_from['id']).one() + trade_document2 = TradeDocument.query.filter_by(id=tradedocument_to['id']).one() + assert trade_document1.total_weight == 150.0 + assert trade_document2.total_weight == 4.0 + assert trade_document1.trading == 'Confirm' + assert trade_document2.trading == None - assert doc['weight'] == request_moveOn['weight'] - assert doc['container_from']['id'] == tradedocument_from['id'] - assert doc['container_to']['id'] == tradedocument_to['id'] - - import pdb; pdb.set_trace() tradedocument, _ = user.delete(res=TradeDocument, item=tradedocument_to['id']) From ab74240d6c3963d9de4c4dfd267f0f6a3cea7c9d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 4 Oct 2021 13:35:51 +0200 Subject: [PATCH 6/7] fixing 3 bugs, #168 --- ereuse_devicehub/resources/action/models.py | 8 +++++++- ereuse_devicehub/resources/device/models.py | 7 ------- .../resources/tradedocument/models.py | 18 ++++++++++++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 603462b8..18cc4902 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1664,7 +1664,10 @@ class MoveOnDocument(JoinedTableMixin, ActionWithMultipleTradeDocuments): ) container_from = db.relationship( 'TradeDocument', - primaryjoin='MoveOnDocument.container_from_id == TradeDocument.id', + backref=backref('containers_from', + lazy=True, + cascade=CASCADE_OWN), + primaryjoin='MoveOnDocument.container_from_id == TradeDocument.id' ) container_from_id.comment = """This is the trade document used as container in a incoming lot""" @@ -1675,6 +1678,9 @@ class MoveOnDocument(JoinedTableMixin, ActionWithMultipleTradeDocuments): ) container_to = db.relationship( 'TradeDocument', + backref=backref('containers_to', + lazy=True, + cascade=CASCADE_OWN), primaryjoin='MoveOnDocument.container_to_id == TradeDocument.id', ) container_to_id.comment = """This is the trade document used as container in a outgoing lot""" diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index a0917bef..f27a97d8 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -342,13 +342,6 @@ class Device(Thing): if action.type == 'Revoke': return action.id - @property - def confirm_status(self): - """The actual state of confirmation of one Trade, or None if no Trade action - has ever been performed to this device.""" - # TODO @cayop we need implement this functionality - return None - @property def physical(self): """The actual physical state, None otherwise.""" diff --git a/ereuse_devicehub/resources/tradedocument/models.py b/ereuse_devicehub/resources/tradedocument/models.py index 36ae370a..177b2bd3 100644 --- a/ereuse_devicehub/resources/tradedocument/models.py +++ b/ereuse_devicehub/resources/tradedocument/models.py @@ -1,3 +1,5 @@ +import copy +from contextlib import suppress from citext import CIText from flask import g @@ -101,10 +103,10 @@ class TradeDocument(Thing): revoke = 'Revoke' revoke_pending = 'Revoke Pending' confirm_revoke = 'Document Revoked' - if not self.actions: + ac = self.last_action_trading() + if not ac: return - ac = self.actions[-1] if ac.type == 'ConfirmRevokeDocument': # can to do revoke_confirmed @@ -143,6 +145,18 @@ class TradeDocument(Thing): return weight + def last_action_trading(self): + """which is the last action trading""" + with suppress(StopIteration, ValueError): + actions = copy.copy(self.actions) + actions.sort(key=lambda x: x.created) + t_trades = ['Trade', + 'Confirm', + 'ConfirmRevokeDocument', + 'RevokeDocument', + 'ConfirmDocument'] + return next(e for e in reversed(actions) if e.t in t_trades) + def _warning_actions(self, actions): """Show warning actions""" return sorted(ev for ev in actions if ev.severity >= Severity.Warning) From 406d3d644996a701ee2a19e375d92a86070fb179 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 6 Oct 2021 12:48:03 +0200 Subject: [PATCH 7/7] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 298f5c8a..c4072b01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ml). [1.0.10-beta] ## [1.0.10-beta] +- [bugfix] #168 can to do a trade without devices. ## [1.0.9-beta] - [addend] #159 external document as proof of erase of disk