示例#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
示例#2
0
def test_create_org_by_rejected_bceid_user(session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Org can be created."""
    # Steps
    # 1. Create a pending affidavit
    # 2. Create org
    # 3. Reject Org, which will mark the affidavit as rejected
    # 4. Same user create new org, which should be PENDING_STAFF_REVIEW.
    user = factory_user_model_with_contact()
    token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.BCEID.value)
    monkeypatch.setattr('auth_api.utils.user_context._get_token_info', lambda: token_info)
    affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    AffidavitService.create_affidavit(token_info=token_info, affidavit_info=affidavit_info)

    with patch.object(OrgService, 'send_staff_review_account_reminder', return_value=None) as mock_notify:
        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
        org = OrgService.approve_or_reject(org_dict['id'], is_approved=False, token_info=token_info)
        org_dict = org.as_dict()
        assert org_dict['org_status'] == OrgStatus.REJECTED.value

        org = OrgService.create_org(TestOrgInfo.org_with_mailing_address(name='Test 123'), user_id=user.id)
        org_dict = org.as_dict()
        assert org_dict['org_status'] == OrgStatus.PENDING_STAFF_REVIEW.value
        mock_notify.assert_called()
示例#3
0
def test_create_org_by_verified_bceid_user(session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Org can be created."""
    # Steps
    # 1. Create a pending affidavit
    # 2. Create org
    # 3. Approve Org, which will mark the affidavit as approved
    # 4. Same user create new org, which should be ACTIVE.
    user = factory_user_model_with_contact(user_info=TestUserInfo.user_bceid_tester)
    token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.BCEID.value)
    patch_token_info(token_info, 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

    task_model = TaskModel.find_by_task_for_account(org_dict['id'], status=TaskStatus.OPEN.value)
    assert task_model.relationship_id == org_dict['id']
    assert task_model.action == TaskAction.AFFIDAVIT_REVIEW.value

    task_info = {
        'status': TaskStatus.OPEN.value,
        'relationshipStatus': TaskRelationshipStatus.ACTIVE.value,
    }
    TaskService.update_task(TaskService(task_model), task_info)
    org_result: OrgModel = OrgModel.find_by_org_id(org_dict['id'])
    assert org_result.status_code == OrgStatus.ACTIVE.value
示例#4
0
def test_put_task_org_on_hold(client, jwt, session, keycloak_mock,
                              monkeypatch):  # pylint:disable=unused-argument
    """Assert that the task can be updated."""
    # 1. Create User
    # 2. Get document signed link
    # 3. Create affidavit
    # 4. Create Org
    # 5. Update the created task and the relationship
    monkeypatch.setattr('auth_api.utils.user_context._get_token_info',
                        lambda: TestJwtClaims.public_bceid_user)
    user_with_token = TestUserInfo.user_staff_admin
    user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub']
    user = factory_user_model_with_contact(user_with_token)

    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
    org_id = org_dict['id']

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

    task_type_new_account = TaskTypePrefix.NEW_ACCOUNT_STAFF_REVIEW.value
    assert fetched_task['type'] == task_type_new_account

    update_task_payload = {
        'status': TaskStatus.HOLD.value,
        'relationshipStatus':
        TaskRelationshipStatus.PENDING_STAFF_REVIEW.value,
        'remark': 'AFFIDAVIT SEAL MISSING'
    }

    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.staff_role)
    rv = client.put('/api/v1/tasks/{}'.format(fetched_task['id']),
                    data=json.dumps(update_task_payload),
                    headers=headers,
                    content_type='application/json')

    dictionary = json.loads(rv.data)
    assert rv.status_code == http_status.HTTP_200_OK
    assert dictionary['status'] == TaskStatus.HOLD.value
    assert dictionary[
        'relationshipStatus'] == TaskRelationshipStatus.PENDING_STAFF_REVIEW.value

    headers = factory_auth_header(jwt=jwt,
                                  claims=TestJwtClaims.public_user_role)
    rv = client.get('/api/v1/orgs/{}'.format(org_id),
                    headers=headers,
                    content_type='application/json')
    assert rv.status_code == http_status.HTTP_200_OK
    dictionary = json.loads(rv.data)
    assert dictionary['id'] == org_id
    assert rv.json.get('orgStatus') == OrgStatus.PENDING_STAFF_REVIEW.value
示例#5
0
def test_approve_org(session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Affidavit can be approved."""
    user = factory_user_model_with_contact(
        user_info=TestUserInfo.user_bceid_tester)
    token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid,
                                             source=LoginSource.BCEID.value)
    patch_token_info(token_info, 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
    task_model = TaskModel.find_by_task_for_account(
        org_dict['id'], status=TaskStatus.OPEN.value)
    assert task_model.relationship_id == org_dict['id']
    assert task_model.action == TaskAction.AFFIDAVIT_REVIEW.value
    task_info = {
        'status': TaskStatus.OPEN.value,
        'relationshipStatus': TaskRelationshipStatus.ACTIVE.value,
        'remarks': ['Test Remark']
    }
    task = TaskService.update_task(TaskService(task_model), task_info)
    task_dict = task.as_dict()
    affidavit = AffidavitService.find_affidavit_by_org_id(
        task_dict['relationship_id'])
    assert affidavit['status'] == AffidavitStatus.APPROVED.value
示例#6
0
def test_create_affidavit_duplicate(session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that duplicate Affidavit cannot be created."""
    user = factory_user_model()
    token_info = TestJwtClaims.get_test_real_user(user.keycloak_guid)
    affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    affidavit = AffidavitService.create_affidavit(token_info=token_info, affidavit_info=affidavit_info)

    assert affidavit.as_dict().get('status', None) == AffidavitStatus.PENDING.value
    new_affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    AffidavitService.create_affidavit(token_info=token_info, affidavit_info=new_affidavit_info)
示例#7
0
def test_create_affidavit_duplicate(session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that duplicate Affidavit cannot be created."""
    user = factory_user_model()
    token_info = TestJwtClaims.get_test_real_user(user.keycloak_guid)
    affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    affidavit = AffidavitService.create_affidavit(token_info=token_info, affidavit_info=affidavit_info)
    assert affidavit
    assert affidavit.as_dict().get('status', None) == AffidavitStatus.PENDING.value
    with pytest.raises(BusinessException) as exception:
        AffidavitService.create_affidavit(token_info=token_info, affidavit_info=affidavit_info)
    assert exception.value.code == Error.ACTIVE_AFFIDAVIT_EXISTS.name
示例#8
0
def test_reject_org(session, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that an Affidavit can be rejected."""
    user = factory_user_model_with_contact()
    token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.BCEID.value)

    affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    AffidavitService.create_affidavit(token_info=token_info, affidavit_info=affidavit_info)

    org = OrgService.create_org(TestOrgInfo.org_with_mailing_address(), user_id=user.id, token_info=token_info)
    org_dict = org.as_dict()
    assert org_dict['org_status'] == OrgStatus.PENDING_STAFF_REVIEW.value
    org = OrgService.approve_or_reject(org_dict['id'], is_approved=False, token_info=token_info)
    org_dict = org.as_dict()
    assert org_dict['org_status'] == OrgStatus.REJECTED.value
    affidavit = AffidavitService.find_affidavit_by_org_id(org_dict['id'])
    assert affidavit['status'] == AffidavitStatus.REJECTED.value
示例#9
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 ')
示例#10
0
    def get(org_id):
        """Get the affidavit for the admin who created the account."""
        try:
            response, status = AffidavitService.find_affidavit_by_org_id(org_id=org_id), \
                               http_status.HTTP_200_OK

        except BusinessException as exception:
            response, status = {'code': exception.code, 'message': exception.message}, exception.status_code

        return response, status
示例#11
0
def test_create_affidavit(session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Affidavit can be created."""
    user = factory_user_model()
    token_info = TestJwtClaims.get_test_real_user(user.keycloak_guid)
    patch_token_info(token_info, monkeypatch)
    affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    affidavit = AffidavitService.create_affidavit(
        affidavit_info=affidavit_info)

    assert affidavit
    assert affidavit.as_dict().get('status',
                                   None) == AffidavitStatus.PENDING.value
示例#12
0
def test_approve_org(session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that an Affidavit can be approved."""
    user = factory_user_model_with_contact()
    token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid,
                                             source=LoginSource.BCEID.value)

    affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    AffidavitService.create_affidavit(token_info=token_info,
                                      affidavit_info=affidavit_info)
    monkeypatch.setattr('auth_api.utils.user_context._get_token_info',
                        lambda: token_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
    org = OrgService.approve_or_reject(org_dict['id'],
                                       is_approved=True,
                                       token_info=token_info)
    org_dict = org.as_dict()
    assert org_dict['org_status'] == OrgStatus.ACTIVE.value
    affidavit = AffidavitService.find_affidavit_by_org_id(org_dict['id'])
    assert affidavit['status'] == AffidavitStatus.APPROVED.value
示例#13
0
def test_tasks_on_account_creation(client, jwt, session, keycloak_mock,  # pylint:disable=unused-argument
                                   monkeypatch, user_token, access_type, expected_task_action):
    """Assert that tasks are created."""
    # 1. Create User
    # 2. Get document signed link
    # 3. Create affidavit
    # 4. Create Org
    # 5. Assert correct task is created

    monkeypatch.setattr('auth_api.utils.user_context._get_token_info', lambda: user_token)
    user = factory_user_model_with_contact(user_token, keycloak_guid=user_token['sub'])

    affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    AffidavitService.create_affidavit(affidavit_info=affidavit_info)

    org_info = TestOrgInfo.org_with_mailing_address()
    org_info['accessType'] = access_type
    OrgService.create_org(org_info, user_id=user.id)

    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.staff_role)
    rv = client.get('/api/v1/tasks', headers=headers, content_type='application/json')
    assert rv.json['tasks'][0]['action'] == expected_task_action
示例#14
0
def test_task_creation(session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that affidavit reupload creates new task."""
    user = factory_user_model_with_contact()
    token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid,
                                             source=LoginSource.BCEID.value)
    patch_token_info(token_info, 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_id = org.as_dict().get('id')
    task_model: TaskModel = TaskModel.find_by_task_for_account(
        org_id, TaskStatus.OPEN.value)
    assert task_model is not None, 'New Open should be generated'
    task_model.status = TaskStatus.HOLD.value  # set current task to hold.Its a staff action
    new_affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    AffidavitService.create_affidavit(affidavit_info=new_affidavit_info)
    assert TaskModel.find_by_id(
        task_model.id).status == TaskStatus.CLOSED.value
    assert TaskModel.find_by_task_for_account(
        org_id, TaskStatus.OPEN.value) is not None
示例#15
0
    def post(user_guid):
        """Create affidavit record for the user."""
        token = g.jwt_oidc_token_info
        request_json = request.get_json()

        if token.get('sub', None) != user_guid:
            abort(403)
        valid_format, errors = schema_utils.validate(request_json, 'affidavit')
        if not valid_format:
            return {'message': schema_utils.serialize(errors)}, http_status.HTTP_400_BAD_REQUEST

        try:
            response, status = AffidavitService.create_affidavit(token, request_json).as_dict(), http_status.HTTP_200_OK
        except BusinessException as exception:
            response, status = {'code': exception.code, 'message': exception.message}, exception.status_code
        return response, status
示例#16
0
    def get(user_guid):
        """Return pending/active affidavit for the user."""
        token = g.jwt_oidc_token_info
        affidavit_status = request.args.get('status', None)

        if Role.STAFF.value not in token['realm_access'][
                'roles'] and token.get('sub', None) != user_guid:
            abort(403)

        try:
            response, status = AffidavitService.find_affidavit_by_user_guid(user_guid, status=affidavit_status), \
                               http_status.HTTP_200_OK
        except BusinessException as exception:
            response, status = {
                'code': exception.code,
                'message': exception.message
            }, exception.status_code
        return response, status
示例#17
0
def test_put_task_product(client, jwt, session, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that the task can be updated."""
    # 1. Create User
    # 4. Create Product subscription
    # 5. Update the created task and the relationship

    # Post user, org and product subscription
    headers = factory_auth_header(jwt=jwt,
                                  claims=TestJwtClaims.staff_admin_role)
    user_with_token = TestUserInfo.user_staff_admin
    user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub']
    user = factory_user_model_with_contact(user_with_token)

    patch_token_info(
        {
            'sub': str(user_with_token['keycloak_guid']),
            'username': '******',
            'realm_access': {
                'roles': ['edit']
            }
        }, monkeypatch)

    affidavit_info = TestAffidavit.get_test_affidavit_with_contact()
    AffidavitService.create_affidavit(affidavit_info=affidavit_info)

    patch_token_info(TestJwtClaims.public_bceid_user, monkeypatch)
    org = OrgService.create_org(TestOrgInfo.org_with_mailing_address(),
                                user_id=user.id)
    org_dict = org.as_dict()

    product_which_doesnt_need_approval = TestOrgProductsInfo.org_products1
    rv_products = client.post(
        f"/api/v1/orgs/{org_dict.get('id')}/products",
        data=json.dumps(product_which_doesnt_need_approval),
        headers=headers,
        content_type='application/json')
    assert rv_products.status_code == http_status.HTTP_201_CREATED
    assert schema_utils.validate(rv_products.json,
                                 'org_product_subscriptions_response')[0]

    tasks = TaskService.fetch_tasks(task_status=[TaskStatus.OPEN.value],
                                    page=1,
                                    limit=10)
    assert len(tasks['tasks']) == 1

    product_which_needs_approval = TestOrgProductsInfo.org_products_vs
    rv_products = client.post(f"/api/v1/orgs/{org_dict.get('id')}/products",
                              data=json.dumps(product_which_needs_approval),
                              headers=headers,
                              content_type='application/json')
    assert rv_products.status_code == http_status.HTTP_201_CREATED
    assert schema_utils.validate(rv_products.json,
                                 'org_product_subscriptions_response')[0]

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

    # Assert task name
    product: ProductCodeModel = ProductCodeModel.find_by_code(
        product_which_needs_approval['subscriptions'][0].get('productCode'))
    org_name = org_dict['name']
    assert fetched_task['name'] == org_name
    assert fetched_task['type'] == product.description

    # Assert the task can be updated and the product status is changed to active
    update_task_payload = {
        'relationshipStatus': ProductSubscriptionStatus.ACTIVE.value
    }

    headers = factory_auth_header(jwt=jwt, claims=TestJwtClaims.staff_role)
    rv = client.put('/api/v1/tasks/{}'.format(fetched_task['id']),
                    data=json.dumps(update_task_payload),
                    headers=headers,
                    content_type='application/json')

    dictionary = json.loads(rv.data)
    assert rv.status_code == http_status.HTTP_200_OK
    assert dictionary['status'] == TaskStatus.COMPLETED.value
    assert dictionary[
        'relationshipStatus'] == TaskRelationshipStatus.ACTIVE.value