def test_tag_get_device_from_tag_endpoint_multiple_tags(app: Devicehub, user: UserClient, user2: UserClient, client: Client): """As above, but when there are two tags with the secondary ID, the system should not return any of both (to be deterministic) so it should raise an exception. """ g.user = User.query.all()[0] db.session.add(Tag(id='foo', secondary='bar', owner_id=user.user['id'])) db.session.commit() db.session.add(Tag(id='foo', secondary='bar', owner_id=user2.user['id'])) db.session.commit() db.session.add(Tag(id='foo2', secondary='bar', owner_id=user.user['id'])) with raises(DBError): db.session.commit() db.session.rollback() tag1 = Tag.from_an_id('foo').filter_by(owner_id=user.user['id']).one() tag2 = Tag.from_an_id('foo').filter_by(owner_id=user2.user['id']).one() pc1 = Desktop(serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']) pc2 = Desktop(serial_number='sn2', chassis=ComputerChassis.Tower, owner_id=user2.user['id']) pc1.tags.add(tag1) pc2.tags.add(tag2) db.session.add(pc1) db.session.add(pc2) db.session.commit() computer, _ = user.get(res=Tag, item='foo/device') assert computer['serialNumber'] == 'sn1' computer, _ = user2.get(res=Tag, item='foo/device') assert computer['serialNumber'] == 'sn2' _, status = client.get(res=Tag, item='foo/device', status=MultipleResourcesFound) assert status.status_code == 422
def test_tag_create_tags_cli_csv(app: Devicehub, user: UserClient): """Checks creating tags with the CLI endpoint using a CSV.""" owner_id = user.user['id'] csv = pathlib.Path(__file__).parent / 'files' / 'tags-cli.csv' runner = app.test_cli_runner() runner.invoke('tag', 'add-csv', str(csv), '-u', owner_id) with app.app_context(): t1 = Tag.from_an_id('id1').one() t2 = Tag.from_an_id('sec1').one() assert t1 == t2
def one(self, id): """Gets the device from the tag.""" tag = Tag.from_an_id(id).one() # type: Tag if not tag.device: raise TagNotLinked(tag.id) if not request.authorization: return redirect(location=url_for_resource(Device, tag.device.id)) return app.resources[Device.t].schema.jsonify(tag.device)
def put(self, tag_id: str, device_id: str): """Links an existing tag with a device.""" tag = Tag.from_an_id(tag_id).one() # type: Tag if tag.device_id: if tag.device_id == device_id: return Response(status=204) else: raise LinkedToAnotherDevice(tag.device_id) else: tag.device_id = device_id db.session().final_flush() db.session.commit() return Response(status=204)
def test_tag_secondary_workbench_link_find(user: UserClient): """Creates and consumes tags with a secondary id, linking them through Workbench to a device and getting them through search.""" t = Tag('foo', secondary='bar', owner_id=user.user['id']) db.session.add(t) db.session.flush() assert Tag.from_an_id('bar').one() == Tag.from_an_id('foo').one() with pytest.raises(ResourceNotFound): Tag.from_an_id('nope').one() s = yaml2json('basic.snapshot') s['device']['tags'] = [{'id': 'foo', 'secondary': 'bar', 'type': 'Tag'}] snapshot, _ = user.post(json_encode(s), res=Snapshot) device, _ = user.get(res=Device, item=snapshot['device']['devicehubID']) assert 'foo' in [x['id'] for x in device['tags']] assert 'bar' in [x.get('secondary') for x in device['tags']] r, _ = user.get(res=Device, query=[('search', 'foo'), ('filter', {'type': ['Computer']})]) assert len(r['items']) == 1 r, _ = user.get(res=Device, query=[('search', 'bar'), ('filter', {'type': ['Computer']})]) assert len(r['items']) == 1
def delete(self, tag_id: str, device_id: str): tag = Tag.from_an_id(tag_id).filter_by(owner=g.user).one() # type: Tag device = Device.query.filter_by(owner=g.user).filter_by( id=device_id).one() if tag.provider: # if is an unamed tag not do nothing return Response(status=204) if tag.device == device: tag.device_id = None db.session().final_flush() db.session.commit() return Response(status=204)
def put(self, tag_id: str, device_id: int): """Links an existing tag with a device.""" device_id = int(device_id) tag = Tag.from_an_id(tag_id).filter_by(owner=g.user).one() # type: Tag if tag.device_id: if tag.device_id == device_id: return Response(status=204) else: raise LinkedToAnotherDevice(tag.device_id) else: # Check if this device exist for this owner Device.query.filter_by(owner=g.user).filter_by(id=device_id).one() tag.device_id = device_id db.session().final_flush() db.session.commit() return Response(status=204)
def one_authorization(self, id): tag = Tag.from_an_id(id).filter_by(owner=g.user).one() # type: Tag if not tag.device: raise TagNotLinked(tag.id) return app.resources[Device.t].schema.jsonify(tag.device)
def delete(self, id): tag = Tag.from_an_id(id).filter_by(owner=g.user).one() tag.delete() db.session().final_flush() db.session.commit() return Response(status=204)