2018-09-20 07:28:52 +00:00
|
|
|
import pathlib
|
|
|
|
|
2018-05-30 10:49:40 +00:00
|
|
|
import pytest
|
2019-01-19 18:19:35 +00:00
|
|
|
import requests_mock
|
2018-09-29 17:39:38 +00:00
|
|
|
from boltons.urlutils import URL
|
2022-07-19 10:27:10 +00:00
|
|
|
from flask import g
|
2018-05-30 10:49:40 +00:00
|
|
|
from pytest import raises
|
|
|
|
|
2022-07-19 10:27:10 +00:00
|
|
|
from ereuse_devicehub.client import Client, UserClient
|
2018-05-30 10:49:40 +00:00
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.devicehub import Devicehub
|
2023-03-27 09:07:30 +00:00
|
|
|
from ereuse_devicehub.ereuse_utils.session import DevicehubClient
|
2018-08-03 16:15:08 +00:00
|
|
|
from ereuse_devicehub.resources.agent.models import Organization
|
2018-09-20 09:51:25 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Desktop, Device
|
2018-06-26 13:35:13 +00:00
|
|
|
from ereuse_devicehub.resources.enums import ComputerChassis
|
2018-05-30 10:49:40 +00:00
|
|
|
from ereuse_devicehub.resources.tag import Tag
|
2022-07-19 10:27:10 +00:00
|
|
|
from ereuse_devicehub.resources.tag.view import (
|
|
|
|
CannotCreateETag,
|
|
|
|
LinkedToAnotherDevice,
|
|
|
|
TagNotLinked,
|
|
|
|
)
|
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2023-03-21 11:08:13 +00:00
|
|
|
from ereuse_devicehub.teal.db import (
|
|
|
|
DBError,
|
|
|
|
MultipleResourcesFound,
|
|
|
|
ResourceNotFound,
|
|
|
|
UniqueViolation,
|
|
|
|
)
|
|
|
|
from ereuse_devicehub.teal.marshmallow import ValidationError
|
2018-08-03 18:07:05 +00:00
|
|
|
from tests import conftest
|
2018-05-30 10:49:40 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 18:07:05 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-07-22 12:21:38 +00:00
|
|
|
def test_create_tag(user: UserClient):
|
2018-05-30 10:49:40 +00:00
|
|
|
"""Creates a tag specifying a custom organization."""
|
2018-09-30 10:29:33 +00:00
|
|
|
org = Organization(name='bar', tax_id='bartax')
|
2022-07-19 10:27:10 +00:00
|
|
|
tag = Tag(
|
|
|
|
id='bar-1', org=org, provider=URL('http://foo.bar'), owner_id=user.user['id']
|
|
|
|
)
|
2018-05-30 10:49:40 +00:00
|
|
|
db.session.add(tag)
|
|
|
|
db.session.commit()
|
2018-09-29 17:39:38 +00:00
|
|
|
tag = Tag.query.one()
|
|
|
|
assert tag.id == 'bar-1'
|
|
|
|
assert tag.provider == URL('http://foo.bar')
|
2021-03-08 11:39:17 +00:00
|
|
|
res, _ = user.get(res=Tag, item=tag.code, status=422)
|
2021-03-01 17:55:44 +00:00
|
|
|
assert res['type'] == 'TagNotLinked'
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_create_tag_with_device(user: UserClient):
|
|
|
|
"""Creates a tag specifying linked with one device."""
|
2021-10-23 11:41:26 +00:00
|
|
|
g.user = User.query.one()
|
2022-07-19 10:27:10 +00:00
|
|
|
pc = Desktop(
|
|
|
|
serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']
|
|
|
|
)
|
2021-03-01 17:55:44 +00:00
|
|
|
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'
|
2018-05-30 10:49:40 +00:00
|
|
|
|
|
|
|
|
2021-03-01 16:56:55 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_delete_tags(user: UserClient, client: Client):
|
|
|
|
"""Delete a named tag."""
|
|
|
|
# Delete Tag Named
|
2021-10-23 11:41:26 +00:00
|
|
|
g.user = User.query.one()
|
2022-07-19 10:27:10 +00:00
|
|
|
pc = Desktop(
|
|
|
|
serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']
|
|
|
|
)
|
2021-03-01 16:56:55 +00:00
|
|
|
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()
|
2021-10-23 11:41:26 +00:00
|
|
|
tag = Tag.query.all()[-1]
|
2021-03-01 16:56:55 +00:00
|
|
|
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')
|
2022-07-19 10:27:10 +00:00
|
|
|
tag = Tag(
|
|
|
|
id='bar-1', org=org, provider=URL('http://foo.bar'), owner_id=user.user['id']
|
|
|
|
)
|
2021-03-01 16:56:55 +00:00
|
|
|
db.session.add(tag)
|
|
|
|
db.session.commit()
|
2021-10-23 11:41:26 +00:00
|
|
|
tag = Tag.query.all()[-1]
|
2021-03-01 16:56:55 +00:00
|
|
|
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']
|
2021-10-23 11:41:26 +00:00
|
|
|
tag = Tag.query.all()[-1]
|
2021-03-01 16:56:55 +00:00
|
|
|
assert tag.id == 'bar-1'
|
2018-05-30 10:49:40 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 18:07:05 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-07-22 12:21:38 +00:00
|
|
|
def test_create_tag_default_org(user: UserClient):
|
2018-05-30 10:49:40 +00:00
|
|
|
"""Creates a tag using the default organization."""
|
2020-07-22 12:21:38 +00:00
|
|
|
tag = Tag(id='foo-1', owner_id=user.user['id'])
|
2022-07-19 10:27:10 +00:00
|
|
|
assert (
|
|
|
|
not tag.org_id
|
|
|
|
), 'org-id is set as default value so it should only load on flush'
|
2018-05-30 10:49:40 +00:00
|
|
|
# We don't want the organization to load, or it would make this
|
|
|
|
# object, from transient to new (added to session)
|
|
|
|
assert 'org' not in vars(tag), 'Organization should not have been loaded'
|
|
|
|
db.session.add(tag)
|
|
|
|
db.session.commit()
|
|
|
|
assert tag.org.name == 'FooOrg' # as defined in the settings
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 18:07:05 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2021-02-26 10:28:32 +00:00
|
|
|
def test_create_same_tag_default_org_two_users(user: UserClient, user2: UserClient):
|
|
|
|
"""Creates a tag using the default organization."""
|
|
|
|
tag = Tag(id='foo-1', owner_id=user.user['id'])
|
|
|
|
tag2 = Tag(id='foo-1', owner_id=user2.user['id'])
|
|
|
|
db.session.add(tag)
|
|
|
|
db.session.add(tag2)
|
|
|
|
db.session.commit()
|
|
|
|
assert tag.org.name == 'FooOrg' # as defined in the settings
|
|
|
|
assert tag2.org.name == 'FooOrg' # as defined in the settings
|
2018-05-30 10:49:40 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-08-03 18:07:05 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2020-07-22 12:21:38 +00:00
|
|
|
def test_create_two_same_tags(user: UserClient):
|
2018-05-30 10:49:40 +00:00
|
|
|
"""Ensures there cannot be two tags with the same ID and organization."""
|
2020-07-22 12:21:38 +00:00
|
|
|
|
|
|
|
db.session.add(Tag(id='foo-bar', owner_id=user.user['id']))
|
|
|
|
db.session.add(Tag(id='foo-bar', owner_id=user.user['id']))
|
2020-07-22 13:41:04 +00:00
|
|
|
|
|
|
|
with raises(DBError):
|
2018-05-30 10:49:40 +00:00
|
|
|
db.session.commit()
|
|
|
|
db.session.rollback()
|
|
|
|
# And it works if tags are in different organizations
|
2020-07-22 12:21:38 +00:00
|
|
|
db.session.add(Tag(id='foo-bar', owner_id=user.user['id']))
|
2018-05-30 10:49:40 +00:00
|
|
|
org2 = Organization(name='org 2', tax_id='tax id org 2')
|
2020-07-22 12:21:38 +00:00
|
|
|
db.session.add(Tag(id='foo-bar', org=org2, owner_id=user.user['id']))
|
2021-02-26 17:59:57 +00:00
|
|
|
with raises(DBError):
|
|
|
|
db.session.commit()
|
2018-05-30 10:49:40 +00:00
|
|
|
|
|
|
|
|
2021-02-26 10:28:32 +00:00
|
|
|
@pytest.mark.mvp
|
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
|
|
|
def test_create_tag_no_slash():
|
|
|
|
"""Checks that no tags can be created that contain a slash."""
|
|
|
|
with raises(ValidationError):
|
|
|
|
Tag('/')
|
|
|
|
|
|
|
|
with raises(ValidationError):
|
|
|
|
Tag('bar', secondary='/')
|
2018-05-30 10:49:40 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-05-30 10:49:40 +00:00
|
|
|
def test_tag_post(app: Devicehub, user: UserClient):
|
|
|
|
"""Checks the POST method of creating a tag."""
|
2018-09-20 07:28:52 +00:00
|
|
|
user.post({'id': 'foo'}, res=Tag)
|
2018-05-30 10:49:40 +00:00
|
|
|
with app.app_context():
|
|
|
|
assert Tag.query.filter_by(id='foo').one()
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-05-30 10:49:40 +00:00
|
|
|
def test_tag_post_etag(user: UserClient):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Ensures users cannot create eReuse.org tags through POST;
|
2018-05-30 10:49:40 +00:00
|
|
|
only terminal.
|
|
|
|
"""
|
2018-09-20 07:28:52 +00:00
|
|
|
user.post({'id': 'FO-123456'}, res=Tag, status=CannotCreateETag)
|
2018-05-30 10:49:40 +00:00
|
|
|
# Although similar, these are not eTags and should pass
|
2018-09-20 07:28:52 +00:00
|
|
|
user.post({'id': 'FO-0123-45'}, res=Tag)
|
|
|
|
user.post({'id': 'FOO012345678910'}, res=Tag)
|
|
|
|
user.post({'id': 'FO'}, res=Tag)
|
|
|
|
user.post({'id': 'FO-'}, res=Tag)
|
|
|
|
user.post({'id': 'FO-123'}, res=Tag)
|
|
|
|
user.post({'id': 'FOO-123456'}, res=Tag)
|
2018-05-30 10:49:40 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-05-30 10:49:40 +00:00
|
|
|
def test_tag_get_device_from_tag_endpoint(app: Devicehub, user: UserClient):
|
|
|
|
"""Checks getting a linked device from a tag endpoint"""
|
|
|
|
with app.app_context():
|
|
|
|
# Create a pc with a tag
|
2021-10-23 11:41:26 +00:00
|
|
|
g.user = User.query.one()
|
2020-07-22 12:21:38 +00:00
|
|
|
tag = Tag(id='foo-bar', owner_id=user.user['id'])
|
2022-07-19 10:27:10 +00:00
|
|
|
pc = Desktop(
|
|
|
|
serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']
|
|
|
|
)
|
2018-05-30 10:49:40 +00:00
|
|
|
pc.tags.add(tag)
|
|
|
|
db.session.add(pc)
|
|
|
|
db.session.commit()
|
|
|
|
computer, _ = user.get(res=Tag, item='foo-bar/device')
|
|
|
|
assert computer['serialNumber'] == 'sn1'
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-05-30 10:49:40 +00:00
|
|
|
def test_tag_get_device_from_tag_endpoint_no_linked(app: Devicehub, user: UserClient):
|
|
|
|
"""As above, but when the tag is not linked."""
|
|
|
|
with app.app_context():
|
2020-07-22 12:21:38 +00:00
|
|
|
db.session.add(Tag(id='foo-bar', owner_id=user.user['id']))
|
2018-05-30 10:49:40 +00:00
|
|
|
db.session.commit()
|
|
|
|
user.get(res=Tag, item='foo-bar/device', status=TagNotLinked)
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-05-30 10:49:40 +00:00
|
|
|
def test_tag_get_device_from_tag_endpoint_no_tag(user: UserClient):
|
|
|
|
"""As above, but when there is no tag with such ID."""
|
|
|
|
user.get(res=Tag, item='foo-bar/device', status=ResourceNotFound)
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2021-02-26 17:59:57 +00:00
|
|
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
2022-07-19 10:27:10 +00:00
|
|
|
def test_tag_get_device_from_tag_endpoint_multiple_tags(
|
|
|
|
app: Devicehub, user: UserClient, user2: UserClient, client: Client
|
|
|
|
):
|
2021-02-26 17:59:57 +00:00
|
|
|
"""As above, but when there are two tags with the secondary ID, the
|
2018-05-30 10:49:40 +00:00
|
|
|
system should not return any of both (to be deterministic) so
|
|
|
|
it should raise an exception.
|
|
|
|
"""
|
2021-10-23 11:41:26 +00:00
|
|
|
g.user = User.query.all()[0]
|
2021-02-26 17:59:57 +00:00
|
|
|
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):
|
2018-05-30 10:49:40 +00:00
|
|
|
db.session.commit()
|
2021-02-26 17:59:57 +00:00
|
|
|
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()
|
2022-07-19 10:27:10 +00:00
|
|
|
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']
|
|
|
|
)
|
2021-02-26 17:59:57 +00:00
|
|
|
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
|
2018-09-07 10:52:04 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-09-07 10:52:04 +00:00
|
|
|
def test_tag_create_tags_cli(app: Devicehub, user: UserClient):
|
|
|
|
"""Checks creating tags with the CLI endpoint."""
|
2020-07-23 17:20:53 +00:00
|
|
|
owner_id = user.user['id']
|
2018-09-07 10:52:04 +00:00
|
|
|
runner = app.test_cli_runner()
|
2020-07-23 17:20:53 +00:00
|
|
|
runner.invoke('tag', 'add', 'id1', '-u', owner_id)
|
2018-09-07 10:52:04 +00:00
|
|
|
with app.app_context():
|
|
|
|
tag = Tag.query.one() # type: Tag
|
|
|
|
assert tag.id == 'id1'
|
|
|
|
assert tag.org.id == Organization.get_default_org_id()
|
2018-09-20 07:28:52 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-09-29 17:39:38 +00:00
|
|
|
def test_tag_create_etags_cli(app: Devicehub, user: UserClient):
|
|
|
|
"""Creates an eTag through the CLI."""
|
|
|
|
# todo what happens to organization?
|
2020-07-23 17:20:53 +00:00
|
|
|
owner_id = user.user['id']
|
2018-09-29 17:39:38 +00:00
|
|
|
runner = app.test_cli_runner()
|
2022-07-19 10:27:10 +00:00
|
|
|
args = (
|
|
|
|
'tag',
|
|
|
|
'add',
|
|
|
|
'-p',
|
|
|
|
'https://t.ereuse.org',
|
|
|
|
'-s',
|
|
|
|
'foo',
|
|
|
|
'DT-BARBAR',
|
|
|
|
'-u',
|
|
|
|
owner_id,
|
|
|
|
)
|
2020-07-23 17:20:53 +00:00
|
|
|
runner.invoke(*args)
|
2018-09-29 17:39:38 +00:00
|
|
|
with app.app_context():
|
|
|
|
tag = Tag.query.one() # type: Tag
|
2018-09-30 10:29:33 +00:00
|
|
|
assert tag.id == 'dt-barbar'
|
2018-09-29 17:39:38 +00:00
|
|
|
assert tag.secondary == 'foo'
|
|
|
|
assert tag.provider == URL('https://t.ereuse.org')
|
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-10-18 08:09:10 +00:00
|
|
|
def test_tag_manual_link_search(app: Devicehub, user: UserClient):
|
|
|
|
"""Tests linking manually a tag through PUT /tags/<id>/device/<id>
|
|
|
|
|
|
|
|
Checks search has the term.
|
|
|
|
"""
|
2018-09-20 09:51:25 +00:00
|
|
|
with app.app_context():
|
2021-10-23 11:41:26 +00:00
|
|
|
g.user = User.query.one()
|
2020-07-22 12:21:38 +00:00
|
|
|
db.session.add(Tag('foo-bar', secondary='foo-sec', owner_id=user.user['id']))
|
2022-07-19 10:27:10 +00:00
|
|
|
desktop = Desktop(
|
|
|
|
serial_number='foo',
|
|
|
|
chassis=ComputerChassis.AllInOne,
|
|
|
|
owner_id=user.user['id'],
|
|
|
|
)
|
2018-09-20 09:51:25 +00:00
|
|
|
db.session.add(desktop)
|
|
|
|
db.session.commit()
|
|
|
|
desktop_id = desktop.id
|
2021-03-08 21:43:24 +00:00
|
|
|
devicehub_id = desktop.devicehub_id
|
2018-09-20 09:51:25 +00:00
|
|
|
user.put({}, res=Tag, item='foo-bar/device/{}'.format(desktop_id), status=204)
|
2021-03-08 21:43:24 +00:00
|
|
|
device, _ = user.get(res=Device, item=devicehub_id)
|
2021-10-23 12:44:09 +00:00
|
|
|
assert 'foo-bar' in [x['id'] for x in device['tags']]
|
2018-09-20 09:51:25 +00:00
|
|
|
|
|
|
|
# Device already linked
|
|
|
|
# Just returns an OK to conform to PUT as anything changes
|
2018-09-30 10:29:33 +00:00
|
|
|
|
2018-09-20 09:51:25 +00:00
|
|
|
user.put({}, res=Tag, item='foo-sec/device/{}'.format(desktop_id), status=204)
|
|
|
|
|
2018-09-30 10:29:33 +00:00
|
|
|
# Secondary IDs are case insensitive
|
|
|
|
user.put({}, res=Tag, item='FOO-BAR/device/{}'.format(desktop_id), status=204)
|
|
|
|
user.put({}, res=Tag, item='FOO-SEC/device/{}'.format(desktop_id), status=204)
|
|
|
|
|
2018-09-20 09:51:25 +00:00
|
|
|
# cannot link to another device when already linked
|
|
|
|
user.put({}, res=Tag, item='foo-bar/device/99', status=LinkedToAnotherDevice)
|
|
|
|
|
2018-10-18 08:09:10 +00:00
|
|
|
i, _ = user.get(res=Device, query=[('search', 'foo-bar')])
|
|
|
|
assert i['items']
|
|
|
|
i, _ = user.get(res=Device, query=[('search', 'foo-sec')])
|
|
|
|
assert i['items']
|
|
|
|
i, _ = user.get(res=Device, query=[('search', 'foo')])
|
|
|
|
assert i['items']
|
|
|
|
|
2018-09-20 09:51:25 +00:00
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2018-09-20 07:28:52 +00:00
|
|
|
def test_tag_create_tags_cli_csv(app: Devicehub, user: UserClient):
|
|
|
|
"""Checks creating tags with the CLI endpoint using a CSV."""
|
2020-07-23 17:20:53 +00:00
|
|
|
owner_id = user.user['id']
|
2018-09-20 07:28:52 +00:00
|
|
|
csv = pathlib.Path(__file__).parent / 'files' / 'tags-cli.csv'
|
|
|
|
runner = app.test_cli_runner()
|
2020-07-23 17:20:53 +00:00
|
|
|
runner.invoke('tag', 'add-csv', str(csv), '-u', owner_id)
|
2018-09-20 07:28:52 +00:00
|
|
|
with app.app_context():
|
|
|
|
t1 = Tag.from_an_id('id1').one()
|
|
|
|
t2 = Tag.from_an_id('sec1').one()
|
|
|
|
assert t1 == t2
|
|
|
|
|
|
|
|
|
|
|
|
def test_tag_multiple_secondary_org(user: UserClient):
|
|
|
|
"""Ensures two secondary ids cannot be part of the same Org."""
|
|
|
|
user.post({'id': 'foo', 'secondary': 'bar'}, res=Tag)
|
|
|
|
user.post({'id': 'foo1', 'secondary': 'bar'}, res=Tag, status=UniqueViolation)
|
2019-01-19 18:19:35 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2022-07-19 10:27:10 +00:00
|
|
|
def test_create_num_regular_tags(
|
|
|
|
user: UserClient, requests_mock: requests_mock.mocker.Mocker
|
|
|
|
):
|
2019-01-19 18:19:35 +00:00
|
|
|
"""Create regular tags. This is done using a tag provider that
|
|
|
|
returns IDs. These tags are printable.
|
|
|
|
"""
|
2022-07-19 10:27:10 +00:00
|
|
|
requests_mock.post(
|
|
|
|
'https://example.com/',
|
|
|
|
# request
|
|
|
|
request_headers={
|
|
|
|
'Authorization': 'Basic {}'.format(
|
|
|
|
DevicehubClient.encode_token('52dacef0-6bcb-4919-bfed-f10d2c96ecee')
|
|
|
|
)
|
|
|
|
},
|
|
|
|
# response
|
|
|
|
json=['tag1id', 'tag2id'],
|
|
|
|
status_code=201,
|
|
|
|
)
|
2019-01-19 18:19:35 +00:00
|
|
|
data, _ = user.post({}, res=Tag, query=[('num', 2)])
|
|
|
|
assert data['items'][0]['id'] == 'tag1id'
|
|
|
|
assert data['items'][0]['printable'], 'Tags made this way are printable'
|
|
|
|
assert data['items'][1]['id'] == 'tag2id'
|
|
|
|
assert data['items'][1]['printable']
|
2019-01-26 11:49:31 +00:00
|
|
|
|
|
|
|
|
2020-07-07 15:17:41 +00:00
|
|
|
@pytest.mark.mvp
|
2022-07-19 10:27:10 +00:00
|
|
|
def test_get_tags_endpoint(
|
|
|
|
user: UserClient, app: Devicehub, requests_mock: requests_mock.mocker.Mocker
|
|
|
|
):
|
2019-01-26 11:49:31 +00:00
|
|
|
"""Performs GET /tags after creating 3 tags, 2 printable and one
|
|
|
|
not. Only the printable ones are returned.
|
|
|
|
"""
|
|
|
|
# Prepare test
|
|
|
|
with app.app_context():
|
|
|
|
org = Organization(name='bar', tax_id='bartax')
|
2022-07-19 10:27:10 +00:00
|
|
|
tag = Tag(
|
|
|
|
id='bar-1',
|
|
|
|
org=org,
|
|
|
|
provider=URL('http://foo.bar'),
|
|
|
|
owner_id=user.user['id'],
|
|
|
|
)
|
2019-01-26 11:49:31 +00:00
|
|
|
db.session.add(tag)
|
|
|
|
db.session.commit()
|
|
|
|
assert not tag.printable
|
|
|
|
|
2022-07-19 10:27:10 +00:00
|
|
|
requests_mock.post(
|
|
|
|
'https://example.com/',
|
|
|
|
# request
|
|
|
|
request_headers={
|
|
|
|
'Authorization': 'Basic {}'.format(
|
|
|
|
DevicehubClient.encode_token('52dacef0-6bcb-4919-bfed-f10d2c96ecee')
|
|
|
|
)
|
|
|
|
},
|
|
|
|
# response
|
|
|
|
json=['tag1id', 'tag2id'],
|
|
|
|
status_code=201,
|
|
|
|
)
|
2019-01-26 11:49:31 +00:00
|
|
|
user.post({}, res=Tag, query=[('num', 2)])
|
|
|
|
|
|
|
|
# Test itself
|
|
|
|
data, _ = user.get(res=Tag)
|
|
|
|
assert len(data['items']) == 2, 'Only 2 tags are printable, thus retreived'
|
|
|
|
# Order is created descending
|
|
|
|
assert data['items'][0]['id'] == 'tag2id'
|
|
|
|
assert data['items'][0]['printable']
|
|
|
|
assert data['items'][1]['id'] == 'tag1id'
|
|
|
|
assert data['items'][1]['printable'], 'Tags made this way are printable'
|
2020-09-22 10:52:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.mvp
|
|
|
|
def test_get_tag_permissions(app: Devicehub, user: UserClient, user2: UserClient):
|
|
|
|
"""Creates a tag specifying a custom organization."""
|
|
|
|
with app.app_context():
|
|
|
|
# Create a pc with a tag
|
2021-10-23 11:41:26 +00:00
|
|
|
g.user = User.query.all()[0]
|
2020-09-22 10:52:15 +00:00
|
|
|
tag = Tag(id='foo-bar', owner_id=user.user['id'])
|
2022-07-19 10:27:10 +00:00
|
|
|
pc = Desktop(
|
|
|
|
serial_number='sn1', chassis=ComputerChassis.Tower, owner_id=user.user['id']
|
|
|
|
)
|
2020-09-22 10:52:15 +00:00
|
|
|
pc.tags.add(tag)
|
|
|
|
db.session.add(pc)
|
|
|
|
db.session.commit()
|
|
|
|
computer, res = user.get(res=Tag, item='foo-bar/device')
|
|
|
|
|
|
|
|
url = "/tags/?foo-bar/device"
|
|
|
|
computer, res = user.get(url, None)
|
|
|
|
computer2, res2 = user2.get(url, None)
|
|
|
|
assert res.status_code == 200
|
|
|
|
assert res2.status_code == 200
|
2022-07-20 12:58:32 +00:00
|
|
|
assert len(computer['items']) == 1
|
2020-09-22 10:52:15 +00:00
|
|
|
assert len(computer2['items']) == 0
|