From 517c21789d761672d137e76624211a1033e08be0 Mon Sep 17 00:00:00 2001 From: Xavier Bustamante Talavera Date: Sat, 29 Sep 2018 19:39:38 +0200 Subject: [PATCH] Fix not creating tags with providers; check if default org is created --- ereuse_devicehub/resources/agent/models.py | 17 +++++++++++------ ereuse_devicehub/resources/tag/__init__.py | 4 ++-- ereuse_devicehub/resources/tag/model.py | 4 +--- requirements.txt | 2 +- setup.py | 2 +- tests/test_tag.py | 19 ++++++++++++++++++- 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/ereuse_devicehub/resources/agent/models.py b/ereuse_devicehub/resources/agent/models.py index 9727a711..aa53d6e5 100644 --- a/ereuse_devicehub/resources/agent/models.py +++ b/ereuse_devicehub/resources/agent/models.py @@ -9,9 +9,9 @@ from sqlalchemy.ext.declarative import declared_attr from sqlalchemy.orm import backref, relationship, validates from sqlalchemy_utils import EmailType, PhoneNumberType from teal import enums -from teal.db import INHERIT_COND, POLYMORPHIC_ID, \ - POLYMORPHIC_ON +from teal.db import DBError, INHERIT_COND, POLYMORPHIC_ID, POLYMORPHIC_ON from teal.marshmallow import ValidationError +from werkzeug.exceptions import NotImplemented, UnprocessableEntity from ereuse_devicehub.resources.models import STR_SIZE, STR_SM_SIZE, Thing from ereuse_devicehub.resources.user.models import User @@ -85,10 +85,15 @@ class Organization(JoinedTableMixin, Agent): @classmethod def get_default_org_id(cls) -> UUID: """Retrieves the default organization.""" - return g.setdefault('org_id', - Organization.query.filter_by( - **app.config.get_namespace('ORGANIZATION_') - ).one().id) + try: + return g.setdefault('org_id', + Organization.query.filter_by( + **app.config.get_namespace('ORGANIZATION_') + ).one().id) + except (DBError, UnprocessableEntity): + # todo test how well this works + raise NotImplemented('Error in getting the default organization. ' + 'Is the DB initialized?') class Individual(JoinedTableMixin, Agent): diff --git a/ereuse_devicehub/resources/tag/__init__.py b/ereuse_devicehub/resources/tag/__init__.py index 0483a3f3..5d9a1af5 100644 --- a/ereuse_devicehub/resources/tag/__init__.py +++ b/ereuse_devicehub/resources/tag/__init__.py @@ -19,7 +19,7 @@ class TagDef(Resource): ORG_H = 'The name of an existing organization in the DB. ' 'By default the organization operating this Devicehub.' - PROV_H = 'The Base URL of the provider. ' + PROV_H = 'The Base URL of the provider; scheme + domain. Ex: "https://foo.com". ' 'By default set to the actual Devicehub.' CLI_SCHEMA = schema.Tag(only=('id', 'provider', 'org', 'secondary')) @@ -56,7 +56,7 @@ class TagDef(Resource): org: str = None, sec: str = None, provider: str = None): - """Create TAGS and associates them to a specific PROVIDER.""" + """Create a tag with the given ID.""" db.session.add(Tag(**self.schema.load( dict(id=id, org=org, secondary=sec, provider=provider) ))) diff --git a/ereuse_devicehub/resources/tag/model.py b/ereuse_devicehub/resources/tag/model.py index 3345bac1..f7da86fb 100644 --- a/ereuse_devicehub/resources/tag/model.py +++ b/ereuse_devicehub/resources/tag/model.py @@ -40,9 +40,7 @@ class Tag(Thing): secondary = Column(Unicode()) secondary.comment = """ A secondary identifier for this tag. It has the same - constraints as the main one. - - Only needed in special cases. + constraints as the main one. Only needed in special cases. """ def __init__(self, id: str, **kwargs) -> None: diff --git a/requirements.txt b/requirements.txt index fb17b315..2939b19b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -26,6 +26,6 @@ requests==2.19.1 requests-mock==1.5.2 SQLAlchemy==1.2.11 SQLAlchemy-Utils==0.33.3 -teal==0.2.0a16 +teal==0.2.0a19 webargs==4.0.0 Werkzeug==0.14.1 diff --git a/setup.py b/setup.py index f36e0bce..c78fcd43 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ setup( long_description=long_description, long_description_content_type='text/markdown', install_requires=[ - 'teal>=0.2.0a18', # teal always first + 'teal>=0.2.0a19', # teal always first 'click', 'click-spinner', 'ereuse-rate==0.0.2', diff --git a/tests/test_tag.py b/tests/test_tag.py index 51e54531..1305aaf9 100644 --- a/tests/test_tag.py +++ b/tests/test_tag.py @@ -1,6 +1,7 @@ import pathlib import pytest +from boltons.urlutils import URL from pytest import raises from teal.db import MultipleResourcesFound, ResourceNotFound, UniqueViolation from teal.marshmallow import ValidationError @@ -21,9 +22,12 @@ from tests import conftest def test_create_tag(): """Creates a tag specifying a custom organization.""" org = Organization(name='Bar', tax_id='BarTax') - tag = Tag(id='bar-1', org=org) + tag = Tag(id='bar-1', org=org, provider=URL('http://foo.bar')) db.session.add(tag) db.session.commit() + tag = Tag.query.one() + assert tag.id == 'bar-1' + assert tag.provider == URL('http://foo.bar') @pytest.mark.usefixtures(conftest.app_context.__name__) @@ -136,6 +140,19 @@ def test_tag_create_tags_cli(app: Devicehub, user: UserClient): assert tag.org.id == Organization.get_default_org_id() +def test_tag_create_etags_cli(app: Devicehub, user: UserClient): + """Creates an eTag through the CLI.""" + # todo what happens to organization? + runner = app.test_cli_runner() + runner.invoke(args=['create-tag', '-p', 'https://t.ereuse.org', '-s', 'foo', 'DT-BARBAR'], + catch_exceptions=False) + with app.app_context(): + tag = Tag.query.one() # type: Tag + assert tag.id == 'DT-BARBAR' + assert tag.secondary == 'foo' + assert tag.provider == URL('https://t.ereuse.org') + + def test_tag_manual_link(app: Devicehub, user: UserClient): """Tests linking manually a tag through PUT /tags//device/""" with app.app_context():