From 170d9c30980985f719ca79157fd1d54f0d540d76 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 19 Dec 2022 10:05:19 -0500 Subject: [PATCH] Adding tests for the user service , and closing a few logic errors. Adding support for a single wild card for matching all users. --- bpmn-js-spiffworkflow/webpack.config.js | 1 + .../models/user_group_assignment_waiting.py | 13 +++-- .../services/authorization_service.py | 1 - .../services/user_service.py | 15 ++++-- .../unit/test_user_service.py | 50 +++++++++++++++++++ 5 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_user_service.py diff --git a/bpmn-js-spiffworkflow/webpack.config.js b/bpmn-js-spiffworkflow/webpack.config.js index 0774e48e..8fe08cf5 100644 --- a/bpmn-js-spiffworkflow/webpack.config.js +++ b/bpmn-js-spiffworkflow/webpack.config.js @@ -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', diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/models/user_group_assignment_waiting.py b/spiffworkflow-backend/src/spiffworkflow_backend/models/user_group_assignment_waiting.py index 78f811a0..2d14e54c 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/models/user_group_assignment_waiting.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/models/user_group_assignment_waiting.py @@ -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 diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/authorization_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/authorization_service.py index 1ad50b1c..429a65e1 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/authorization_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/authorization_service.py @@ -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 diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/user_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/user_service.py index 720d9ff8..8003c9e8 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/user_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/user_service.py @@ -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 diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_user_service.py b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_user_service.py new file mode 100644 index 00000000..5dfaf2b0 --- /dev/null +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/unit/test_user_service.py @@ -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