Exemple #1
0
def test_lot_post_add_remove_device_view(user: UserClient):
    """Tests adding a device to a lot using POST and
    removing it with DELETE.
    """
    # todo check with components
    g.user = User.query.one()
    device = Desktop(serial_number='foo',
                     model='bar',
                     manufacturer='foobar',
                     chassis=ComputerChassis.Lunchbox,
                     owner_id=user.user['id'])
    db.session.add(device)
    db.session.commit()
    device_id = device.id
    devicehub_id = device.devicehub_id
    parent, _ = user.post(({'name': 'lot'}), res=Lot)
    lot, _ = user.post({},
                       res=Lot,
                       item='{}/devices'.format(parent['id']),
                       query=[('id', device_id)])
    lot = Lot.query.filter_by(id=lot['id']).one()
    assert list(lot.devices)[0].id == device_id, 'Lot contains device'
    device = Device.query.filter_by(devicehub_id=devicehub_id).one()
    assert len(device.lots) == 1
    # assert device['lots'][0]['id'] == lot['id'], 'Device is inside lot'
    assert list(device.lots)[0].id == lot.id, 'Device is inside lot'

    # Remove the device
    user.delete(res=Lot,
                item='{}/devices'.format(parent['id']),
                query=[('id', device_id)],
                status=200)
    assert not len(lot.devices)
Exemple #2
0
def test_lot_modify_patch_endpoint_and_delete(user: UserClient):
    """Creates and modifies lot properties through the endpoint."""
    l, _ = user.post({'name': 'foo', 'description': 'baz'}, res=Lot)
    assert l['name'] == 'foo'
    assert l['description'] == 'baz'
    user.patch({'name': 'bar', 'description': 'bax'}, res=Lot, item=l['id'], status=204)
    l_after, _ = user.get(res=Lot, item=l['id'])
    assert l_after['name'] == 'bar'
    assert l_after['description'] == 'bax'
    user.patch({'description': 'bax'}, res=Lot, item=l['id'], status=204)
    user.delete(res=Lot, item=l['id'], status=204)
    user.get(res=Lot, item=l['id'], status=404)
Exemple #3
0
def test_create_tag_with_device(user: UserClient):
    """Creates a tag specifying linked with one device."""
    g.user = User.query.one()
    pc = Desktop(serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id'])
    db.session.add(pc)
    db.session.commit()
    tag = Tag(id='bar', owner_id=user.user['id'])
    db.session.add(tag)
    db.session.commit()
    data = '{tag_id}/device/{device_id}'.format(tag_id=tag.id, device_id=pc.id)
    user.put({}, res=Tag, item=data, status=204)
    user.get(res=Tag, item='{}/device'.format(tag.id))
    user.delete({}, res=Tag, item=data, status=204)
    res, _ = user.get(res=Tag, item='{}/device'.format(tag.id), status=422)
    assert res['type'] == 'TagNotLinked'
Exemple #4
0
def test_lot_post_add_remove_device_view(app: Devicehub, user: UserClient):
    """Tests adding a device to a lot using POST and
    removing it with DELETE."""
    # todo check with components
    with app.app_context():
        device = Desktop(serial_number='foo',
                         model='bar',
                         manufacturer='foobar',
                         chassis=ComputerChassis.Lunchbox)
        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'
    device, _ = user.get(res=Device, item=device_id)
    assert len(device['lots']) == 1
    assert device['lots'][0]['id'] == lot['id'], 'Device is inside lot'

    # Remove the device
    lot, _ = user.delete(res=Lot,
                         item='{}/devices'.format(parent['id']),
                         query=[('id', device_id)],
                         status=200)
    assert not len(lot['devices'])
Exemple #5
0
def test_delete_tags(user: UserClient, client: Client):
    """Delete a named tag."""
    # Delete Tag Named
    g.user = User.query.one()
    pc = Desktop(serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id'])
    db.session.add(pc)
    db.session.commit()
    tag = Tag(id='bar', owner_id=user.user['id'], device_id=pc.id)
    db.session.add(tag)
    db.session.commit()
    tag = Tag.query.all()[-1]
    assert tag.id == 'bar'
    # Is not possible delete one tag linked to one device
    res, _ = user.delete(res=Tag, item=tag.id, status=422)
    msg = 'The tag bar is linked to device'
    assert msg in res['message'][0]

    tag.device_id = None
    db.session.add(tag)
    db.session.commit()
    # Is not possible delete one tag from an anonymous user
    client.delete(res=Tag, item=tag.id, status=401)

    # Is possible delete one normal tag
    user.delete(res=Tag, item=tag.id)
    user.get(res=Tag, item=tag.id, status=404)

    # Delete Tag UnNamed
    org = Organization(name='bar', tax_id='bartax')
    tag = Tag(id='bar-1', org=org, provider=URL('http://foo.bar'), owner_id=user.user['id'])
    db.session.add(tag)
    db.session.commit()
    tag = Tag.query.all()[-1]
    assert tag.id == 'bar-1'
    res, _ = user.delete(res=Tag, item=tag.id, status=422)
    msg = 'This tag {} is unnamed tag. It is imposible delete.'.format(tag.id)
    assert msg in res['message']
    tag = Tag.query.all()[-1]
    assert tag.id == 'bar-1'