Skip to content

Commit

Permalink
Fix bug, 2 SAWarnings for non_private_roles (see note)
Browse files Browse the repository at this point in the history
Same bug as in:
143bb98
285bb51
  • Loading branch information
jdavcs committed Oct 9, 2021
1 parent 4fdc1da commit da0e9ee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
8 changes: 8 additions & 0 deletions lib/tool_shed/webapp/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class User(Base, Dictifiable, _HasTable):
dict_collection_visible_keys = ['id', 'username']
dict_element_visible_keys = ['id', 'username']
bootstrap_admin_user = False
non_private_roles = relationship(
'UserRoleAssociation',
viewonly=True,
primaryjoin=(lambda:
(User.id == UserRoleAssociation.user_id) # type: ignore
& (UserRoleAssociation.role_id == Role.id) # type: ignore
& not_(Role.name == User.email)) # type: ignore
)

def __init__(self, email=None, password=None):
self.email = email
Expand Down
5 changes: 1 addition & 4 deletions lib/tool_shed/webapp/model/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
import logging

from sqlalchemy import Boolean, Column, DateTime, desc, false, ForeignKey, Integer, not_, Table, TEXT, true
from sqlalchemy import Boolean, Column, DateTime, desc, false, ForeignKey, Integer, Table, TEXT, true
from sqlalchemy.orm import relation

import tool_shed.webapp.model
Expand Down Expand Up @@ -81,9 +81,6 @@
mapper_registry.map_imperatively(UserRoleAssociation, UserRoleAssociation.table,
properties=dict(
user=relation(User, backref="roles"),
non_private_roles=relation(User,
backref="non_private_roles",
primaryjoin=((User.id == UserRoleAssociation.table.c.user_id) & (UserRoleAssociation.table.c.role_id == Role.id) & not_(Role.name == User.email))),
role=relation(Role, back_populates='users')))

mapper_registry.map_imperatively(Repository, Repository.table,
Expand Down
28 changes: 26 additions & 2 deletions test/unit/shed_unit/model/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,8 @@ def test_relationships(
password_reset_token,
user_group_association,
user_role_association,
role_factory,
user_role_association_factory
):
obj = cls_()
obj.email = get_unique_value()
Expand All @@ -760,18 +762,26 @@ def test_relationships(
obj.api_keys.append(api_keys)
obj.reset_tokens.append(password_reset_token)
obj.groups.append(user_group_association)
obj.roles.append(user_role_association)
obj.repository_reviews.append(repository_review)

_private_role = role_factory(name=obj.email)
private_user_role = user_role_association_factory(obj, _private_role)
obj.roles.append(private_user_role)

_non_private_role = role_factory(name='a')
non_private_user_role = user_role_association_factory(obj, _non_private_role)
obj.roles.append(non_private_user_role)

with dbcleanup(session, obj) as obj_id:
stored_obj = get_stored_obj(session, cls_, obj_id)
assert stored_obj.active_repositories == [repository]
assert stored_obj.galaxy_sessions == [galaxy_session]
assert stored_obj.api_keys == [api_keys]
assert stored_obj.reset_tokens == [password_reset_token]
assert stored_obj.groups == [user_group_association]
assert stored_obj.roles == [user_role_association]
assert stored_obj.repository_reviews == [repository_review]
assert are_same_entity_collections(stored_obj.roles, [private_user_role, non_private_user_role])
assert stored_obj.non_private_roles == [non_private_user_role]


class TestUserGroupAssociation(BaseTest):
Expand Down Expand Up @@ -975,6 +985,13 @@ def make_instance(*args, **kwds):
return make_instance


@pytest.fixture
def role_factory(model):
def make_instance(*args, **kwds):
return model.Role(*args, **kwds)
return make_instance


@pytest.fixture
def user_factory(model):
def make_instance(*args, **kwds):
Expand All @@ -985,6 +1002,13 @@ def make_instance(*args, **kwds):
return make_instance


@pytest.fixture
def user_role_association_factory(model):
def make_instance(*args, **kwds):
return model.UserRoleAssociation(*args, **kwds)
return make_instance


# Test utilities

def dbcleanup_wrapper(session, obj, where_clause=None):
Expand Down

0 comments on commit da0e9ee

Please sign in to comment.