diff --git a/ereuse_devicehub/resources/lot/views.py b/ereuse_devicehub/resources/lot/views.py index 6208d261..29a77e86 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -224,6 +224,7 @@ class LotDeviceView(LotBaseChildrenView): id = ma.fields.List(ma.fields.Integer()) def _post(self, lot: Lot, ids: Set[int]): + # import pdb; pdb.set_trace() # get only new devices ids -= {x.id for x in lot.devices} if not ids: @@ -233,7 +234,7 @@ class LotDeviceView(LotBaseChildrenView): if lot.trade: # all users involved in the trade action can modify the lot trade_users = [lot.trade.user_from.id, lot.trade.user_to.id] - if g.user in trade_users: + if g.user.id in trade_users: users = trade_users devices = set(Device.query.filter(Device.id.in_(ids)).filter( diff --git a/tests/test_action.py b/tests/test_action.py index 872eae81..7c22d502 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -1449,3 +1449,1009 @@ def test_confirmRevoke(user: UserClient, user2: UserClient): # assert device_10.actions[-2].t == 'Confirm' # assert device_10.actions[-2].user == trade.user_to # assert device_10.actions[-3].t == 'Revoke' + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case1(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']), + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device2 = trade.devices + + assert device1.actions[-2].t == 'Trade' + assert device1.actions[-1].t == 'Confirm' + assert device1.actions[-1].user == trade.user_to + assert device2.actions[-2].t == 'Trade' + assert device2.actions[-1].t == 'Confirm' + assert device2.actions[-1].user == trade.user_to + + lot, _ = user.delete({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices, status=200) + + assert device1.actions[-2].t == 'Revoke' + assert device1.actions[-1].t == 'ConfirmRevoke' + assert device1.actions[-1].user == trade.user_to + assert device2.actions[-2].t == 'Revoke' + assert device2.actions[-1].t == 'ConfirmRevoke' + assert device2.actions[-1].user == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case2(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']), + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device2 = trade.devices + + assert device1.actions[-2].t == 'Trade' + assert device1.actions[-1].t == 'Confirm' + assert device1.actions[-1].user == trade.user_to + assert device2.actions[-2].t == 'Trade' + assert device2.actions[-1].t == 'Confirm' + assert device2.actions[-1].user == trade.user_to + + request_revoke = { + 'type': 'Revoke', + 'action': trade.id, + 'devices': [device1.id, device2.id], + } + + # Normal revoke + user.post(res=models.Action, data=request_revoke) + + assert device1.actions[-2].t == 'Revoke' + assert device1.actions[-1].t == 'ConfirmRevoke' + assert device1.actions[-1].user == trade.user_to + assert device2.actions[-2].t == 'Revoke' + assert device2.actions[-1].t == 'ConfirmRevoke' + assert device2.actions[-1].user == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case3(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']), + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + + lot, _ = user2.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device2 = trade.devices + + assert device1.actions[-2].t == 'Trade' + assert device1.actions[-1].t == 'Confirm' + assert device1.actions[-1].user == trade.user_to + assert device2.actions[-2].t == 'Trade' + assert device2.actions[-1].t == 'Confirm' + assert device2.actions[-1].user == trade.user_from + + lot, _ = user2.delete({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:], status=200) + + assert device2.actions[-2].t == 'Revoke' + assert device2.actions[-1].t == 'ConfirmRevoke' + assert device2.actions[-1].user == trade.user_from + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case4(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']), + ] + lot1, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + + lot2, _ = user2.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device2 = trade.devices + + assert device1.actions[-2].t == 'Trade' + assert device1.actions[-1].t == 'Confirm' + assert device1.actions[-1].user == trade.user_to + assert device2.actions[-2].t == 'Trade' + assert device2.actions[-1].t == 'Confirm' + assert device2.actions[-1].user == trade.user_from + + request_revoke = { + 'type': 'Revoke', + 'action': trade.id, + 'devices': [device2.id], + } + + # Normal revoke + user2.post(res=models.Action, data=request_revoke) + + # import pdb; pdb.set_trace() + assert device1.actions[-2].t == 'Trade' + assert device1.actions[-1].t == 'Confirm' + assert device1.actions[-1].user == trade.user_to + assert device2.actions[-2].t == 'Revoke' + assert device2.actions[-1].t == 'ConfirmRevoke' + assert device2.actions[-1].user == trade.user_from + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case5(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']), + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device2 = trade.devices + + assert device1.actions[-2].t == 'Trade' + assert device1.actions[-1].t == 'Confirm' + assert device1.actions[-1].user == trade.user_to + assert device2.actions[-2].t == 'Trade' + assert device2.actions[-1].t == 'Confirm' + assert device2.actions[-1].user == trade.user_to + + lot, _ = user2.delete({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:], status=200) + + assert device2.actions[-2].t == 'Confirm' + assert device2.actions[-1].t == 'Revoke' + assert device2.actions[-1].user == trade.user_from + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device2.actions[-1].id, + 'devices': [device2.id], + } + + # Normal revoke + user.post(res=models.Action, data=request_confirm_revoke) + + assert device2.actions[-2].t == 'Revoke' + assert device2.actions[-1].t == 'ConfirmRevoke' + assert device2.actions[-1].user == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case6(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']), + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + + lot, _ = user2.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device2 = trade.devices + + assert device1.actions[-2].t == 'Trade' + assert device1.actions[-1].t == 'Confirm' + assert device1.actions[-1].user == trade.user_to + assert device2.actions[-2].t == 'Trade' + assert device2.actions[-1].t == 'Confirm' + assert device2.actions[-1].user == trade.user_from + + lot, _ = user.delete({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:], status=200) + + assert device2.actions[-2].t == 'Confirm' + assert device2.actions[-1].t == 'Revoke' + assert device2.actions[-1].user == trade.user_to + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device2.actions[-1].id, + 'devices': [device2.id], + } + + # Normal revoke + user2.post(res=models.Action, data=request_confirm_revoke) + + assert device2.actions[-2].t == 'Revoke' + assert device2.actions[-1].t == 'ConfirmRevoke' + assert device2.actions[-1].user == trade.user_from + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case7(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id'])] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + device = trade.devices[0] + + request_confirm = { + 'type': 'Confirm', + 'action': trade.id, + 'devices': [device.id], + } + + # Normal revoke + user2.post(res=models.Action, data=request_confirm) + + lot, _ = user.delete({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices, status=200) + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device.actions[-1].id, + 'devices': [device.id], + } + + user2.post(res=models.Action, data=request_confirm_revoke) + + assert device.actions[-1].t == 'ConfirmRevoke' + assert device.actions[-1].user == trade.user_from + assert device.actions[-2].t == 'Revoke' + assert device.actions[-2].user == trade.user_to + assert device.actions[-3].t == 'Confirm' + assert device.actions[-3].user == trade.user_from + assert device.actions[-4].t == 'Confirm' + assert device.actions[-4].user == trade.user_to + assert device.actions[-5].t == 'Trade' + assert device.actions[-5].author == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case8(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id'])] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + device = trade.devices[0] + + request_confirm = { + 'type': 'Confirm', + 'action': trade.id, + 'devices': [device.id], + } + + # Normal revoke + user2.post(res=models.Action, data=request_confirm) + + request_revoke = { + 'type': 'Revoke', + 'action': trade.id, + 'devices': [device.id], + } + + # Normal revoke + user.post(res=models.Action, data=request_revoke) + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device.actions[-1].id, + 'devices': [device.id], + } + + user2.post(res=models.Action, data=request_confirm_revoke) + + assert device.actions[-1].t == 'ConfirmRevoke' + assert device.actions[-1].user == trade.user_from + assert device.actions[-2].t == 'Revoke' + assert device.actions[-2].user == trade.user_to + assert device.actions[-3].t == 'Confirm' + assert device.actions[-3].user == trade.user_from + assert device.actions[-4].t == 'Confirm' + assert device.actions[-4].user == trade.user_to + assert device.actions[-5].t == 'Trade' + assert device.actions[-5].author == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case9(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']) + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + + lot, _ = user2.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device = trade.devices + + assert device.owner == trade.user_from + + request_confirm = { + 'type': 'Confirm', + 'action': trade.id, + 'devices': [device.id], + } + + # Normal revoke + user.post(res=models.Action, data=request_confirm) + + assert device.owner == trade.user_to + + lot, _ = user2.delete({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:], status=200) + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device.actions[-1].id, + 'devices': [device.id], + } + + user.post(res=models.Action, data=request_confirm_revoke) + + assert device.owner == trade.user_from + + assert device.actions[-1].t == 'ConfirmRevoke' + assert device.actions[-1].user == trade.user_to + assert device.actions[-2].t == 'Revoke' + assert device.actions[-2].user == trade.user_from + assert device.actions[-3].t == 'Confirm' + assert device.actions[-3].user == trade.user_to + assert device.actions[-4].t == 'Confirm' + assert device.actions[-4].user == trade.user_from + assert device.actions[-5].t == 'Trade' + assert device.actions[-5].author == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case10(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']) + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + + lot, _ = user2.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device = trade.devices + + assert device.owner == trade.user_from + + request_confirm = { + 'type': 'Confirm', + 'action': trade.id, + 'devices': [device.id], + } + + # Normal confirm + user.post(res=models.Action, data=request_confirm) + + assert device.owner == trade.user_to + + request_revoke = { + 'type': 'Revoke', + 'action': trade.id, + 'devices': [device.id], + } + + # Normal revoke + user2.post(res=models.Action, data=request_revoke) + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device.actions[-1].id, + 'devices': [device.id], + } + + user.post(res=models.Action, data=request_confirm_revoke) + + assert device.owner == trade.user_from + + assert device.actions[-1].t == 'ConfirmRevoke' + assert device.actions[-1].user == trade.user_to + assert device.actions[-2].t == 'Revoke' + assert device.actions[-2].user == trade.user_from + assert device.actions[-3].t == 'Confirm' + assert device.actions[-3].user == trade.user_to + assert device.actions[-4].t == 'Confirm' + assert device.actions[-4].user == trade.user_from + assert device.actions[-5].t == 'Trade' + assert device.actions[-5].author == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case11(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']) + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + # import pdb; pdb.set_trace() + + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device = trade.devices + + request_confirm = { + 'type': 'Confirm', + 'action': trade.id, + 'devices': [device.id], + } + + user2.post(res=models.Action, data=request_confirm) + + lot, _ = user2.delete({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:], status=200) + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device.actions[-1].id, + 'devices': [device.id], + } + + user.post(res=models.Action, data=request_confirm_revoke) + + assert device.actions[-1].t == 'ConfirmRevoke' + assert device.actions[-1].user == trade.user_to + assert device.actions[-2].t == 'Revoke' + assert device.actions[-2].user == trade.user_from + assert device.actions[-3].t == 'Confirm' + assert device.actions[-3].user == trade.user_from + assert device.actions[-4].t == 'Confirm' + assert device.actions[-4].user == trade.user_to + assert device.actions[-5].t == 'Trade' + assert device.actions[-5].author == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case12(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']) + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + # import pdb; pdb.set_trace() + + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device = trade.devices + + # Normal confirm + request_confirm = { + 'type': 'Confirm', + 'action': trade.id, + 'devices': [device.id], + } + + user2.post(res=models.Action, data=request_confirm) + + request_revoke = { + 'type': 'Revoke', + 'action': trade.id, + 'devices': [device.id], + } + + # Normal revoke + user2.post(res=models.Action, data=request_revoke) + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device.actions[-1].id, + 'devices': [device.id], + } + + user.post(res=models.Action, data=request_confirm_revoke) + + assert device.actions[-1].t == 'ConfirmRevoke' + assert device.actions[-1].user == trade.user_to + assert device.actions[-2].t == 'Revoke' + assert device.actions[-2].user == trade.user_from + assert device.actions[-3].t == 'Confirm' + assert device.actions[-3].user == trade.user_from + assert device.actions[-4].t == 'Confirm' + assert device.actions[-4].user == trade.user_to + assert device.actions[-5].t == 'Trade' + assert device.actions[-5].author == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case13(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']) + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + # import pdb; pdb.set_trace() + + lot, _ = user2.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device = trade.devices + + request_confirm = { + 'type': 'Confirm', + 'action': trade.id, + 'devices': [device.id], + } + + user.post(res=models.Action, data=request_confirm) + + lot, _ = user.delete({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:], status=200) + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device.actions[-1].id, + 'devices': [device.id], + } + + user2.post(res=models.Action, data=request_confirm_revoke) + + assert device.actions[-1].t == 'ConfirmRevoke' + assert device.actions[-1].user == trade.user_from + assert device.actions[-2].t == 'Revoke' + assert device.actions[-2].user == trade.user_to + assert device.actions[-3].t == 'Confirm' + assert device.actions[-3].user == trade.user_to + assert device.actions[-4].t == 'Confirm' + assert device.actions[-4].user == trade.user_from + assert device.actions[-5].t == 'Trade' + assert device.actions[-5].author == trade.user_to + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_trade_case14(user: UserClient, user2: UserClient): + # the pRp (manatest_usecase_confirmationger) creates a temporary lot + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + # The manager add 7 device into the lot + snap1, _ = user.post(file('basic.snapshot'), res=models.Snapshot) + snap2, _ = user2.post(file('acer.happy.battery.snapshot'), res=models.Snapshot) + + devices = [('id', snap1['device']['id']), + ('id', snap2['device']['id']) + ] + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[:-1]) + + # the manager shares the temporary lot with the SCRAP as an incoming lot + # for the CRAP to confirm it + request_post = { + 'type': 'Trade', + 'devices': [], + 'userFromEmail': user2.email, + 'userToEmail': user.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirms': True, + } + + user.post(res=models.Action, data=request_post) + trade = models.Trade.query.one() + # import pdb; pdb.set_trace() + + lot, _ = user2.post({}, + res=Lot, + item='{}/devices'.format(lot['id']), + query=devices[-1:]) + + device1, device = trade.devices + + # Normal confirm + request_confirm = { + 'type': 'Confirm', + 'action': trade.id, + 'devices': [device.id], + } + + user.post(res=models.Action, data=request_confirm) + + request_revoke = { + 'type': 'Revoke', + 'action': trade.id, + 'devices': [device.id], + } + + # Normal revoke + user.post(res=models.Action, data=request_revoke) + + request_confirm_revoke = { + 'type': 'ConfirmRevoke', + 'action': device.actions[-1].id, + 'devices': [device.id], + } + + user2.post(res=models.Action, data=request_confirm_revoke) + + assert device.actions[-1].t == 'ConfirmRevoke' + assert device.actions[-1].user == trade.user_from + assert device.actions[-2].t == 'Revoke' + assert device.actions[-2].user == trade.user_to + assert device.actions[-3].t == 'Confirm' + assert device.actions[-3].user == trade.user_to + assert device.actions[-4].t == 'Confirm' + assert device.actions[-4].user == trade.user_from + assert device.actions[-5].t == 'Trade' + assert device.actions[-5].author == trade.user_to