Adding tests for the user service , and closing a few logic errors. Adding support for a single wild card for matching all users.
This commit is contained in:
parent
e78f2471df
commit
170d9c3098
|
@ -35,6 +35,7 @@ module.exports = {
|
|||
new CopyWebpackPlugin({
|
||||
patterns: [
|
||||
{ from: 'assets/**', to: 'vendor/bpmn-js', context: 'node_modules/bpmn-js/dist/' },
|
||||
{ from: '*.css', to: 'vendor/bpmn-js-color-picker', context: 'node_modules/bpmn-js-color-picker/colors' },
|
||||
{
|
||||
from: 'assets/**',
|
||||
to: 'vendor/bpmn-js-properties-panel',
|
||||
|
|
|
@ -7,11 +7,11 @@ from sqlalchemy.orm import relationship
|
|||
from spiffworkflow_backend.models.group import GroupModel
|
||||
from spiffworkflow_backend.models.user import UserModel
|
||||
|
||||
|
||||
class UserGroupAssignmentWaitingModel(SpiffworkflowBaseDBModel):
|
||||
"""UserGroupAssignmentsWaitingModel - When a user is assinged to a group, but that user has not yet logged into
|
||||
the system, this caches that assignment, so it can be applied at the time the user logs in."""
|
||||
|
||||
"""UserGroupAssignmentsWaitingModel - When a user is assinged to a group, but that username does not exist,
|
||||
we cache it here to be applied in the event the user does log into the system.
|
||||
"""
|
||||
MATCH_ALL_USERS = "*"
|
||||
__tablename__ = "user_group_assignment_waiting"
|
||||
__table_args__ = (
|
||||
db.UniqueConstraint("username", "group_id", name="user_group_assignment_staged_unique"),
|
||||
|
@ -22,3 +22,8 @@ class UserGroupAssignmentWaitingModel(SpiffworkflowBaseDBModel):
|
|||
group_id = db.Column(ForeignKey(GroupModel.id), nullable=False)
|
||||
|
||||
group = relationship("GroupModel", overlaps="groups,user_group_assignment_waiting,users") # type: ignore
|
||||
|
||||
def is_match_all(self):
|
||||
if self.username == self.MATCH_ALL_USERS:
|
||||
return True
|
||||
return False
|
||||
|
|
|
@ -485,7 +485,6 @@ class AuthorizationService:
|
|||
email=email,
|
||||
display_name = display_name
|
||||
)
|
||||
UserService().apply_waiting_group_assignments(user_model)
|
||||
|
||||
else :
|
||||
# Update with the latest information
|
||||
|
|
|
@ -56,6 +56,7 @@ class UserService:
|
|||
message=f"Could not add user {username}",
|
||||
) from e
|
||||
cls.create_principal(user_model.id)
|
||||
UserService().apply_waiting_group_assignments(user_model)
|
||||
return user_model
|
||||
|
||||
else:
|
||||
|
@ -135,18 +136,26 @@ class UserService:
|
|||
|
||||
@classmethod
|
||||
def add_waiting_group_assignment(cls, username: str, group: GroupModel) -> None:
|
||||
exists = UserGroupAssignmentWaitingModel().query.filter_by(username=username).filter_by(group_id=group.id).count()
|
||||
if not exists:
|
||||
wugam = UserGroupAssignmentWaitingModel().query.filter_by(username=username).filter_by(group_id=group.id).first()
|
||||
if not wugam:
|
||||
wugam = UserGroupAssignmentWaitingModel(username=username, group_id=group.id)
|
||||
db.session.add(wugam)
|
||||
db.session.commit()
|
||||
if wugam.is_match_all():
|
||||
for user in UserModel.query.all():
|
||||
cls.add_user_to_group(user, group)
|
||||
|
||||
@classmethod
|
||||
def apply_waiting_group_assignments(cls, user: UserModel) -> None:
|
||||
waiting = UserGroupAssignmentWaitingModel().query.filter(UserGroupAssignmentWaitingModel.username == user.username).all()
|
||||
waiting = UserGroupAssignmentWaitingModel().query.\
|
||||
filter(UserGroupAssignmentWaitingModel.username == user.username).all()
|
||||
for assignment in waiting:
|
||||
cls.add_user_to_group(user, assignment.group)
|
||||
db.session.delete(assignment)
|
||||
wildcard = UserGroupAssignmentWaitingModel().query.\
|
||||
filter(UserGroupAssignmentWaitingModel.username == UserGroupAssignmentWaitingModel.MATCH_ALL_USERS).all()
|
||||
for assignment in wildcard:
|
||||
cls.add_user_to_group(user, assignment.group)
|
||||
db.session.commit()
|
||||
|
||||
@staticmethod
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
"""Process Model."""
|
||||
from flask.app import Flask
|
||||
from flask.testing import FlaskClient
|
||||
|
||||
from spiffworkflow_backend.models.user_group_assignment_waiting import UserGroupAssignmentWaitingModel
|
||||
from spiffworkflow_backend.services.group_service import GroupService
|
||||
from spiffworkflow_backend.services.user_service import UserService
|
||||
from tests.spiffworkflow_backend.helpers.base_test import BaseTest
|
||||
|
||||
from spiffworkflow_backend.services.git_service import GitService
|
||||
|
||||
|
||||
class TestUserService(BaseTest):
|
||||
"""TestUserService."""
|
||||
|
||||
def test_assigning_a_group_to_a_user_before_the_user_is_created (
|
||||
self,
|
||||
app: Flask,
|
||||
client: FlaskClient,
|
||||
with_db_and_bpmn_file_cleanup: None,
|
||||
) -> None:
|
||||
"""Test_waiting_group_assignments"""
|
||||
aTestGroup = GroupService.find_or_create_group("aTestGroup")
|
||||
UserService.add_group_assignment("initiator_user", aTestGroup)
|
||||
initiator_user = self.find_or_create_user("initiator_user")
|
||||
assert initiator_user.groups[0] == aTestGroup
|
||||
|
||||
def test_assigning_a_group_to_all_users_updates_new_users (
|
||||
self,
|
||||
app: Flask,
|
||||
client: FlaskClient,
|
||||
with_db_and_bpmn_file_cleanup: None,
|
||||
) -> None:
|
||||
"""Test_waiting_group_assignments"""
|
||||
everybody_group = GroupService.find_or_create_group("everybodyGroup")
|
||||
UserService.add_group_assignment(UserGroupAssignmentWaitingModel.MATCH_ALL_USERS, everybody_group)
|
||||
initiator_user = self.find_or_create_user("initiator_user")
|
||||
assert initiator_user.groups[0] == everybody_group
|
||||
|
||||
def test_assigning_a_group_to_all_users_updates_existing_users (
|
||||
self,
|
||||
app: Flask,
|
||||
client: FlaskClient,
|
||||
with_db_and_bpmn_file_cleanup: None,
|
||||
) -> None:
|
||||
"""Test_waiting_group_assignments"""
|
||||
initiator_user = self.find_or_create_user("initiator_user")
|
||||
everybody_group = GroupService.find_or_create_group("everybodyGroup")
|
||||
UserService.add_group_assignment(UserGroupAssignmentWaitingModel.MATCH_ALL_USERS, everybody_group)
|
||||
assert initiator_user.groups[0] == everybody_group
|
Loading…
Reference in New Issue