diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 725d859c..f300416c 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -481,6 +481,10 @@ class Offer(ActionWithMultipleDevices): required=True, only_query='id') + @validates_schema + def validate_lot(self, data: dict): + data['devices'] = data['lot'].devices + @validates_schema def validate_user_to_id(self, data: dict): """ diff --git a/tests/test_action.py b/tests/test_action.py index 368794df..6a4761d4 100644 --- a/tests/test_action.py +++ b/tests/test_action.py @@ -742,39 +742,6 @@ def test_deallocate_bad_dates(user: UserClient): assert res['type'] == 'ValidationError' -@pytest.mark.mvp -@pytest.mark.xfail(reason='Old functionality') -@pytest.mark.parametrize('action_model_state', - (pytest.param(ams, id=ams[0].__name__) - for ams in [ - (models.MakeAvailable, states.Trading.Available), - (models.Sell, states.Trading.Sold), - (models.Donate, states.Trading.Donated), - (models.Rent, states.Trading.Renting), - (models.DisposeProduct, states.Trading.ProductDisposed) - ])) -def test_trade2(action_model_state: Tuple[Type[models.Action], states.Trading], user: UserClient): - """Tests POSTing all Trade actions.""" - # todo missing None states.Trading for after cancelling renting, for example - # import pdb; pdb.set_trace() - # Remove this test - action_model, state = action_model_state - snapshot, _ = user.post(file('basic.snapshot'), res=models.Snapshot) - action = { - 'type': action_model.t, - 'devices': [snapshot['device']['id']] - } - if issubclass(action_model, models.Trade): - action['to'] = user.user['individuals'][0]['id'] - action['shippingDate'] = '2018-06-29T12:28:54' - action['invoiceNumber'] = 'ABC' - action, _ = user.post(action, res=models.Action) - assert action['devices'][0]['id'] == snapshot['device']['id'] - device, _ = user.get(res=Device, item=snapshot['device']['id']) - assert device['actions'][-1]['id'] == action['id'] - assert device['trading'] == state.name - - @pytest.mark.mvp @pytest.mark.xfail(reason='Old functionality') def test_trade_endpoint(user: UserClient, user2: UserClient): @@ -820,7 +787,7 @@ def test_offer_without_to(user: UserClient): db.session.flush() request_post = { 'type': 'Offer', - 'devices': [device.id], + 'devices': [], 'userFrom': user.email, 'price': 10, 'date': "2020-12-01T02:00:00+00:00", @@ -846,7 +813,7 @@ def test_offer_without_to(user: UserClient): # check if the user_from is owner of the devices request_post = { 'type': 'Offer', - 'devices': [device.id], + 'devices': [], 'userFrom': user.email, 'price': 10, 'date': "2020-12-01T02:00:00+00:00", @@ -860,15 +827,15 @@ def test_offer_without_to(user: UserClient): # Check if the new phantom account is reused and not duplicated computer = file('1-device-with-components.snapshot') snapshot2, _ = user.post(computer, res=models.Snapshot) + device2 = Device.query.filter_by(id=snapshot2['device']['id']).one() lot2 = Lot('MyLot2') lot2.owner_id = user.user['id'] - lot2.devices.add(device) + lot2.devices.add(device2) db.session.add(lot2) db.session.flush() - device2 = Device.query.filter_by(id=snapshot2['device']['id']).one() request_post2 = { 'type': 'Offer', - 'devices': [device2.id], + 'devices': [], 'userFrom': user.email, 'price': 10, 'date': "2020-12-01T02:00:00+00:00", @@ -903,7 +870,7 @@ def test_offer_without_from(user: UserClient): db.session.flush() request_post = { 'type': 'Offer', - 'devices': [device.id], + 'devices': [], 'userTo': user2.email, 'price': 10, 'date': "2020-12-01T02:00:00+00:00", @@ -976,7 +943,7 @@ def test_offer(user: UserClient): db.session.flush() request_post = { 'type': 'Offer', - 'devices': [device.id], + 'devices': [], 'userFrom': user.email, 'userTo': user2.email, 'price': 10, @@ -991,6 +958,29 @@ def test_offer(user: UserClient): assert device.owner.email == user.email assert device.owner.email != user2.email +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_offer_without_devices(user: UserClient): + """Test one offer with doble confirmation""" + user2 = User(email='baz@baz.cxm', password='baz') + user2.individuals.add(Person(name='Tommy')) + db.session.add(user2) + db.session.commit() + lot, _ = user.post({'name': 'MyLot'}, res=Lot) + request_post = { + 'type': 'Offer', + 'devices': [], + 'userFrom': user.email, + 'userTo': user2.email, + 'price': 10, + 'date': "2020-12-01T02:00:00+00:00", + 'documentID': '1', + 'lot': lot['id'], + 'confirm': True, + } + + user.post(res=models.Action, data=request_post) + # no there are transfer of devices @pytest.mark.mvp @pytest.mark.usefixtures(conftest.auth_app_context.__name__) diff --git a/tests/test_lot.py b/tests/test_lot.py index 9c04b046..0e37f4e2 100644 --- a/tests/test_lot.py +++ b/tests/test_lot.py @@ -1,9 +1,13 @@ import pytest from flask import g +from pytest import raises +from json.decoder import JSONDecodeError from ereuse_devicehub.client import UserClient from ereuse_devicehub.db import db from ereuse_devicehub.devicehub import Devicehub +from ereuse_devicehub.resources.user.models import User +from ereuse_devicehub.resources.agent.models import Person from ereuse_devicehub.resources.device.models import Desktop, Device, GraphicCard from ereuse_devicehub.resources.enums import ComputerChassis from ereuse_devicehub.resources.lot.models import Lot, LotDevice @@ -383,6 +387,38 @@ def test_lot_post_add_remove_device_view(app: Devicehub, user: UserClient): assert not len(lot['devices']) +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_lot_error_add_device_from_other_user(user: UserClient): + """Tests adding a device to a lot using POST and + removing it with DELETE. + """ + # import pdb; pdb.set_trace() + user2 = User(email='baz@baz.cxm', password='baz') + user2.individuals.add(Person(name='Tommy')) + db.session.add(user2) + db.session.commit() + + device = Desktop(serial_number='foo', + model='bar', + manufacturer='foobar', + chassis=ComputerChassis.Lunchbox, + owner_id=user2.id) + db.session.add(device) + db.session.commit() + + device_id = device.id + parent, _ = user.post(({'name': 'lot'}), res=Lot) + lot, _ = user.post({}, + res=Lot, + item='{}/devices'.format(parent['id']), + query=[('id', device_id)]) + assert lot['devices'][0]['id'] == device_id, 'Lot contains device' + assert len(lot['devices']) == 1 + with raises(JSONDecodeError): + device, _ = user.get(res=Device, item=device_id) + + @pytest.mark.mvp def test_get_multiple_lots(user: UserClient): """Tests submitting and retreiving multiple lots."""