Ejemplo n.º 1
0
def test_update_task(session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that a task can be updated."""
    user_with_token = TestUserInfo.user_bceid_tester
    user_with_token['keycloak_guid'] = TestJwtClaims.public_bceid_user['sub']
    user = factory_user_model_with_contact(user_with_token)

    patch_token_info(TestJwtClaims.public_bceid_user, monkeypatch)
    affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    AffidavitService.create_affidavit(affidavit_info=affidavit_info)
    org = OrgService.create_org(TestOrgInfo.org_with_mailing_address(),
                                user_id=user.id)
    org_dict = org.as_dict()
    assert org_dict['org_status'] == OrgStatus.PENDING_STAFF_REVIEW.value

    token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid,
                                             source=LoginSource.STAFF.value)
    patch_token_info(token_info, monkeypatch)

    tasks = TaskService.fetch_tasks(task_status=[TaskStatus.OPEN.value],
                                    page=1,
                                    limit=10)
    fetched_tasks = tasks['tasks']
    fetched_task = fetched_tasks[0]

    task_info = {'relationshipStatus': TaskRelationshipStatus.ACTIVE.value}
    task: TaskModel = TaskModel.find_by_task_id(fetched_task['id'])

    task = TaskService.update_task(TaskService(task), task_info=task_info)
    dictionary = task.as_dict()
    user = UserModel.find_by_id(user.id)
    assert dictionary['status'] == TaskStatus.COMPLETED.value
    assert dictionary[
        'relationship_status'] == TaskRelationshipStatus.ACTIVE.value
    assert user.verified
Ejemplo n.º 2
0
    def _update_relationship(self, origin_url: str = None):
        """Retrieve the relationship record and update the status."""
        task_model: TaskModel = self._model
        current_app.logger.debug('<update_task_relationship ')
        is_approved: bool = task_model.relationship_status == TaskRelationshipStatus.ACTIVE.value
        is_hold: bool = task_model.status == TaskStatus.HOLD.value

        if task_model.relationship_type == TaskRelationshipType.ORG.value:
            # Update Org relationship
            org_id = task_model.relationship_id
            if not is_hold:
                self._update_org(is_approved=is_approved, org_id=org_id,
                                 origin_url=origin_url, task_action=task_model.action)
            else:
                # Task with ACCOUNT_REVIEW action cannot be put on hold
                if task_model.action != TaskAction.AFFIDAVIT_REVIEW.value:
                    raise BusinessException(Error.INVALID_INPUT, None)

                # no updates on org yet.put the task on hold and send mail to user
                org: OrgModel = OrgModel.find_by_org_id(org_id)
                # send on-hold mail notification
                Task._notify_admin_about_hold(task_model, org=org)
            # TODO Update user.verified flag
        elif task_model.relationship_type == TaskRelationshipType.PRODUCT.value:
            # Update Product relationship
            product_subscription_id = task_model.relationship_id
            account_id = task_model.account_id
            self._update_product_subscription(is_approved=is_approved, product_subscription_id=product_subscription_id,
                                              org_id=account_id)

        elif task_model.relationship_type == TaskRelationshipType.USER.value:
            user_id = task_model.relationship_id
            if not is_hold:
                self._update_bceid_admin(is_approved=is_approved, user_id=user_id)
            else:
                user: UserModel = UserModel.find_by_id(user_id)
                membership = MembershipModel.find_membership_by_userid(user_id)
                # Send mail to admin about hold with reasons
                Task._notify_admin_about_hold(user=user, task_model=task_model, is_new_bceid_admin_request=True,
                                              membership_id=membership.id)

        # If action is affidavit review, mark the user as verified.
        if is_approved and task_model.action == TaskAction.AFFIDAVIT_REVIEW.value and task_model.user:
            task_model.user.verified = True
            task_model.user.save()

        current_app.logger.debug('>update_task_relationship ')
Ejemplo n.º 3
0
    def _update_bceid_admin(is_approved: bool, user_id: int):
        """Approve/Reject BCeId Admin User and Affidavit."""
        from auth_api.services import Affidavit  # pylint:disable=cyclic-import, import-outside-toplevel
        current_app.logger.debug('<update_bceid_admin_to_org ')

        # Update user
        user: UserModel = UserModel.find_by_id(user_id)
        user.status = Status.ACTIVE.value if is_approved else Status.INACTIVE.value

        # Update membership
        membership = MembershipModel.find_membership_by_userid(user_id)
        membership.status = Status.ACTIVE.value if is_approved else Status.REJECTED.value

        # Update affidavit
        Affidavit.approve_or_reject_bceid_admin(admin_user_id=user_id, is_approved=is_approved, user=user)

        current_app.logger.debug('>update_bceid_admin_to_org ')
Ejemplo n.º 4
0
    def delete_org(org_id):
        """Soft-Deletes an Org.

        Only admin can perform this.
        1 - All businesses gets unaffiliated.
        2 - All team members removed.
        3 - If there is any credit on the account then cannot be deleted.

        Premium:
        1 - If there is any active PAD transactions going on, then cannot be deleted.

        """
        current_app.logger.debug(f'<Delete Org {org_id}')
        # Affiliation uses OrgService, adding as local import
        # pylint:disable=import-outside-toplevel, cyclic-import
        from auth_api.services.affiliation import Affiliation as AffiliationService

        check_auth(one_of_roles=(ADMIN, STAFF), org_id=org_id)

        org: OrgModel = OrgModel.find_by_org_id(org_id)
        if not org:
            raise BusinessException(Error.DATA_NOT_FOUND, None)
        if org.status_code not in (OrgStatus.ACTIVE.value,
                                   OrgStatus.PENDING_INVITE_ACCEPT.value):
            raise BusinessException(Error.NOT_ACTIVE_ACCOUNT, None)

        # Deactivate pay account
        Org._delete_pay_account(org_id)

        # Find all active affiliations and remove them.
        entities = AffiliationService.find_affiliations_by_org_id(org_id)
        for entity in entities:
            AffiliationService.delete_affiliation(
                org_id=org_id,
                business_identifier=entity['business_identifier'],
                reset_passcode=True)

        # Deactivate all members.
        members = MembershipModel.find_members_by_org_id(org_id)
        for member in members:
            member.status = Status.INACTIVE.value
            member.flush()

            user: UserModel = UserModel.find_by_id(member.user_id)
            # Remove user from keycloak group if they are not part of any orgs
            if len(MembershipModel.find_orgs_for_user(member.user_id)) == 0:
                KeycloakService.remove_from_account_holders_group(
                    user.keycloak_guid)

            # If the admin is BCeID user, mark the affidavit INACTIVE.
            if user.login_source == LoginSource.BCEID.value and member.membership_type_code == ADMIN:
                affidavit: AffidavitModel = AffidavitModel.find_approved_by_user_id(
                    user.id)
                if affidavit:
                    affidavit.status_code = AffidavitStatus.INACTIVE.value
                    affidavit.flush()

        # Set the account as INACTIVE
        org.status_code = OrgStatus.INACTIVE.value
        org.save()

        current_app.logger.debug('org Inactivated>')