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
def put(task_id): """Update a task.""" request_json = request.get_json() token = g.jwt_oidc_token_info valid_format, errors = schema_utils.validate(request_json, 'task_request') if not valid_format: return {'message': schema_utils.serialize(errors)}, http_status.HTTP_400_BAD_REQUEST try: task = TaskService(TaskModel.find_by_task_id(task_id)) if task: # Update task and its relationships origin = request.environ.get('HTTP_ORIGIN', 'localhost') response, status = task.update_task(task_info=request_json, token_info=token, origin_url=origin).as_dict(), http_status.HTTP_200_OK else: response, status = {'message': 'The requested task could not be found.'}, \ http_status.HTTP_404_NOT_FOUND except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status
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
def get(task_id): """Fetch task by id.""" try: task = TaskService(TaskModel.find_by_task_id(task_id=task_id)) response, status = task.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
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
def test_create_task_product(session, keycloak_mock): # pylint:disable=unused-argument """Assert that a task can be created.""" user = factory_user_model() test_org = factory_org_model() test_product = factory_product_model(org_id=test_org.id) product: ProductCodeModel = ProductCodeModel.find_by_code( test_product.product_code) test_task_info = { 'name': test_org.name, 'relationshipId': test_product.id, 'relatedTo': user.id, 'dateSubmitted': datetime.today(), 'relationshipType': TaskRelationshipType.PRODUCT.value, 'type': product.description, 'status': [TaskStatus.OPEN.value], 'accountId': test_org.id, 'relationship_status': TaskRelationshipStatus.PENDING_STAFF_REVIEW.value } task = TaskService.create_task(test_task_info) assert task dictionary = task.as_dict() assert dictionary['name'] == test_task_info['name'] assert dictionary['account_id'] == test_org.id assert dictionary[ 'relationship_type'] == TaskRelationshipType.PRODUCT.value
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
def test_create_task_govm(session, keycloak_mock): # pylint:disable=unused-argument """Assert that a task can be created when updating a GOVM account.""" user = factory_user_model() token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.STAFF.value, roles=['create_accounts']) user2 = factory_user_model(TestUserInfo.user2) public_token_info = TestJwtClaims.get_test_user( sub=user2.keycloak_guid, source=LoginSource.STAFF.value, roles=['gov_account_user']) org: OrgService = OrgService.create_org(TestOrgInfo.org_govm, user_id=user.id, token_info=token_info) assert org with patch.object(RestService, 'put') as mock_post: payment_details = TestPaymentMethodInfo.get_payment_method_input_with_revenue( ) org_body = { 'mailingAddress': TestOrgInfo.get_mailing_address(), **payment_details } org = OrgService.update_org(org, org_body, token_info=public_token_info) assert org dictionary = org.as_dict() assert dictionary['name'] == TestOrgInfo.org_govm['name'] mock_post.assert_called() actual_data = mock_post.call_args.kwargs.get('data') expected_data = { 'accountId': dictionary.get('id'), 'accountName': dictionary.get('name') + '-' + dictionary.get('branch_name'), 'paymentInfo': { 'methodOfPayment': 'EJV', 'billable': False, 'revenueAccount': payment_details.get('paymentInfo').get('revenueAccount') }, 'contactInfo': TestOrgInfo.get_mailing_address() } assert expected_data == actual_data # Assert the task that is created fetched_task = TaskService.fetch_tasks( task_status=TaskStatus.OPEN.value, page=1, limit=10) for item in fetched_task['tasks']: assert item['name'] == dictionary['name'] assert item['type'] == TaskTypePrefix.GOVM_REVIEW.value assert item['status'] == TaskStatus.OPEN.value assert item['relationship_id'] == dictionary['id']
def test_fetch_tasks(session, auth_mock): # pylint:disable=unused-argument """Assert that tasks can be fetched.""" user = factory_user_model() task = factory_task_service(user.id) dictionary = task.as_dict() name = dictionary['name'] fetched_task = TaskService.fetch_tasks(task_status=[TaskStatus.OPEN.value], page=1, limit=10) assert fetched_task['tasks'] for item in fetched_task['tasks']: assert item['name'] == name
def get(): """Fetch tasks.""" try: # Search based on request arguments task_type = request.args.get('type', None) task_relationship_status = request.args.get('relationshipStatus', None) task_status = request.args.get('status', TaskStatus.OPEN.value) page = request.args.get('page', 1) limit = request.args.get('limit', 10) response, status = TaskService.fetch_tasks(task_relationship_status=task_relationship_status, task_type=task_type, task_status=task_status, page=page, limit=limit), http_status.HTTP_200_OK except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status
def test_create_task_org(session, keycloak_mock): # pylint:disable=unused-argument """Assert that a task can be created.""" user = factory_user_model() test_org = factory_org_model() task_type_new_account = TaskTypePrefix.NEW_ACCOUNT_STAFF_REVIEW.value test_task_info = { 'name': test_org.name, 'relationshipId': test_org.id, 'relatedTo': user.id, 'dateSubmitted': datetime.today(), 'relationshipType': TaskRelationshipType.ORG.value, 'type': task_type_new_account, 'status': TaskStatus.OPEN.value, 'relationship_status': TaskRelationshipStatus.PENDING_STAFF_REVIEW.value } task = TaskService.create_task(test_task_info) assert task dictionary = task.as_dict() assert dictionary['name'] == test_org.name
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