def test_patch_org_status(session, monkeypatch, auth_mock): # pylint:disable=unused-argument """Assert that an Org status can be updated.""" org = factory_org_service() 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) # Validate and update org status patch_info = { 'action': PatchActions.UPDATE_STATUS.value, 'statusCode': OrgStatus.SUSPENDED.value, } with pytest.raises(BusinessException) as exception: org.patch_org(PatchActions.UPDATE_STATUS.value, patch_info) assert exception.value.code == Error.INVALID_INPUT.name patch_info['suspensionReasonCode'] = SuspensionReasonCode.OWNER_CHANGE.name with patch.object(ActivityLogPublisher, 'publish_activity', return_value=None) as mock_alp: updated_org = org.patch_org(PatchActions.UPDATE_STATUS.value, patch_info) mock_alp.assert_called_with(Activity(action=ActivityAction.ACCOUNT_SUSPENSION.value, org_id=ANY, name=ANY, id=ANY, value=SuspensionReasonCode.OWNER_CHANGE.value)) assert updated_org['status_code'] == OrgStatus.SUSPENDED.value patch_info = { 'action': PatchActions.UPDATE_STATUS.value, 'statusCode': OrgStatus.ACTIVE.value, } updated_org = org.patch_org(PatchActions.UPDATE_STATUS.value, patch_info) assert updated_org['status_code'] == OrgStatus.ACTIVE.value with patch.object(ActivityLogPublisher, 'publish_activity', return_value=None) as mock_alp: OrgService.update_login_option(org._model.id, 'BCROS') mock_alp.assert_called_with(Activity(action=ActivityAction.AUTHENTICATION_METHOD_CHANGE.value, org_id=ANY, name=ANY, id=ANY, value='BCROS'))
def test_create_org_invalid_access_type_user(session, keycloak_mock): # pylint:disable=unused-argument """Assert that an Org cannot be created by providing wrong access type.""" user = factory_user_model_with_contact() token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.BCEID.value) with pytest.raises(BusinessException) as exception: OrgService.create_org(TestOrgInfo.org_regular, user_id=user.id, token_info=token_info) assert exception.value.code == Error.USER_CANT_CREATE_REGULAR_ORG.name
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_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_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()
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 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 test_change_org_access_type(session, monkeypatch): # pylint:disable=unused-argument """Assert that an Org can be updated.""" org = factory_org_service() 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) updated_org = org.change_org_access_type(AccessType.GOVN.value) assert updated_org.as_dict()['access_type'] == AccessType.GOVN.value
def test_send_staff_review_account_reminder_exception(session, notify_org_mock, keycloak_mock): # pylint:disable=unused-argument """Send a reminder with exception.""" user = factory_user_model_with_contact(TestUserInfo.user_test) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) org_dictionary = org.as_dict() with patch.object(notification, 'send_email', return_value=False): with pytest.raises(BusinessException) as exception: OrgService.send_staff_review_account_reminder( user, org_dictionary['id'], 'localhost') assert exception.value.code == Error.FAILED_NOTIFICATION.name
def test_create_org_by_in_province_bceid_user(session, keycloak_mock): # pylint:disable=unused-argument """Assert that an Org can be created.""" user = factory_user_model_with_contact() token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.BCEID.value) with patch.object(OrgService, 'send_staff_review_account_reminder', return_value=None) as mock_notify: org = OrgService.create_org(TestOrgInfo.org_regular_bceid, user_id=user.id, token_info=token_info) assert org dictionary = org.as_dict() assert dictionary['name'] == TestOrgInfo.org1['name'] assert dictionary['org_status'] == OrgStatus.PENDING_STAFF_REVIEW.value assert dictionary['access_type'] == AccessType.REGULAR_BCEID.value mock_notify.assert_called()
def test_create_org_by_bceid_user(session, keycloak_mock, monkeypatch): # pylint:disable=unused-argument """Assert that an Org can be created.""" 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) with patch.object(OrgService, 'send_staff_review_account_reminder', return_value=None) as mock_notify: org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) assert org dictionary = org.as_dict() assert dictionary['name'] == TestOrgInfo.org1['name'] assert dictionary['org_status'] == OrgStatus.PENDING_STAFF_REVIEW.value assert dictionary['access_type'] == AccessType.EXTRA_PROVINCIAL.value mock_notify.assert_called()
def test_suspend_org(session): # pylint:disable=unused-argument """Assert that an Org can be updated.""" org = factory_org_service() user = factory_user_model_with_contact() token_info = TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.BCEID.value) updated_org = OrgService.change_org_status(org._model.id, OrgStatus.SUSPENDED.value, SuspensionReasonCode.OWNER_CHANGE.name, token_info=token_info) assert updated_org.as_dict()['status_code'] == OrgStatus.SUSPENDED.value assert updated_org.as_dict()['suspension_reason_code'] == SuspensionReasonCode.OWNER_CHANGE.name updated_org = OrgService.change_org_status(org._model.id, OrgStatus.ACTIVE.value, SuspensionReasonCode.DISPUTE.name, token_info=token_info) assert updated_org.as_dict()['status_code'] == OrgStatus.ACTIVE.value
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
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
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
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
def test_patch_org_access_type(session, monkeypatch): # pylint:disable=unused-argument """Assert that an Org access type can be updated.""" org = factory_org_service() 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) # Validate and update org status patch_info = { 'action': PatchActions.UPDATE_ACCESS_TYPE.value } with pytest.raises(BusinessException) as exception: org.patch_org(PatchActions.UPDATE_ACCESS_TYPE.value, patch_info) assert exception.value.code == Error.INVALID_INPUT.name patch_info['accessType'] = AccessType.GOVM.value with pytest.raises(BusinessException) as exception: org.patch_org(PatchActions.UPDATE_ACCESS_TYPE.value, patch_info) assert exception.value.code == Error.INVALID_INPUT.name patch_info['accessType'] = AccessType.GOVN.value updated_org = org.patch_org(PatchActions.UPDATE_ACCESS_TYPE.value, patch_info) assert updated_org['access_type'] == AccessType.GOVN.value
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