def test_create_product_multiple_subscription(session, keycloak_mock, monkeypatch): # pylint:disable=unused-argument """Assert that an Org can be created.""" user_with_token = TestUserInfo.user_bceid_tester user_with_token['keycloak_guid'] = TestJwtClaims.public_bceid_user['sub'] user = factory_user_model(user_with_token) patch_token_info({'sub': user.keycloak_guid}, monkeypatch) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) assert org dictionary = org.as_dict() assert dictionary['name'] == TestOrgInfo.org1['name'] patch_token_info(TestJwtClaims.public_bceid_user, monkeypatch) subscriptions = ProductService.create_product_subscription(dictionary['id'], TestOrgProductsInfo.org_products2, skip_auth=True) assert next(prod for prod in subscriptions if prod.get('code') == TestOrgProductsInfo.org_products2['subscriptions'][0]['productCode']) assert next(prod for prod in subscriptions if prod.get('code') == TestOrgProductsInfo.org_products2['subscriptions'][1]['productCode'])
def test_validate_token_accepted(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Validate invalid invitation token.""" with patch.object(InvitationService, 'send_invitation', return_value=None): user = factory_user_model(TestUserInfo.user_test) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) org_dictionary = org.as_dict() user_invitee = factory_user_model(TestUserInfo.user1) invitation_info = factory_invitation(org_dictionary['id']) new_invitation = InvitationService.create_invitation( invitation_info, User(user_invitee), {}, '').as_dict() confirmation_token = InvitationService.generate_confirmation_token( new_invitation['id']) InvitationService.accept_invitation(new_invitation['id'], User(user_invitee), '') with pytest.raises(BusinessException) as exception: InvitationService.validate_token(confirmation_token) assert exception.value.code == Error.ACTIONED_INVITATION.name
def test_delete_user_where_user_is_member_on_org(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Assert that a user can be deleted.""" # Create a user and org user_model = factory_user_model(user_info=TestUserInfo.user_test) contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model contact_link.commit() org = OrgService.create_org(TestOrgInfo.org1, user_id=user_model.id) org_dictionary = org.as_dict() org_id = org_dictionary['id'] entity = factory_entity_model(entity_info=TestEntityInfo.entity_lear_mock) affiliation = AffiliationModel(org_id=org_id, entity_id=entity.id) affiliation.save() # Create another user and add membership to the above org user_model2 = factory_user_model(user_info=TestUserInfo.user2) contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model2 contact_link.commit() membership = MembershipModel(org_id=org_id, user_id=user_model2.id, membership_type_code='MEMBER', membership_type_status=Status.ACTIVE.value) membership.save() UserService.delete_user( TestJwtClaims.get_test_user(user_model2.keycloak_guid)) updated_user = UserModel.find_by_jwt_token( TestJwtClaims.get_test_user(user_model2.keycloak_guid)) assert len(updated_user.contacts) == 0 user_orgs = MembershipModel.find_orgs_for_user(updated_user.id) for org in user_orgs: assert org.status_code == 'INACTIVE'
def test_update_invitation_verify_different_tokens(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Update the specified invitation with new data.""" with patch.object(InvitationService, 'send_invitation', return_value=None): user = factory_user_model(TestUserInfo.user_test) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) org_dictionary = org.as_dict() invitation_info = factory_invitation(org_dictionary['id']) new_invitation = InvitationService.create_invitation( invitation_info, User(user), {}, '') old_token = new_invitation.as_dict().get('token') with freeze_time( lambda: datetime.now() + timedelta(seconds=1) ): # to give time difference..or else token will be same.. updated_invitation = new_invitation.update_invitation( User(user), {}, '').as_dict() new_token = updated_invitation.get('token') assert old_token != new_token assert updated_invitation['status'] == 'PENDING'
def test_send_invitation_exception(session, notify_mock, keycloak_mock): # pylint:disable=unused-argument """Send an existing invitation with exception.""" user = factory_user_model(TestUserInfo.user_test) user_dictionary = User(user).as_dict() org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) org_dictionary = org.as_dict() invitation_info = factory_invitation(org_dictionary['id']) invitation = InvitationModel.create_from_dict(invitation_info, user.id, 'STANDARD') with patch.object(notification, 'send_email', return_value=False): with pytest.raises(BusinessException) as exception: InvitationService.send_invitation(invitation, org_dictionary['name'], user_dictionary, '') assert exception.value.code == Error.FAILED_INVITATION.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_AFFIDAVIT_REVIEW.value assert dictionary['access_type'] == AccessType.REGULAR_BCEID.value mock_notify.assert_called()
def test_get_invitations_by_org_id(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Find an existing invitation with the provided org id.""" with patch.object(InvitationService, 'send_invitation', return_value=None): user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role[ 'sub'] user = factory_user_model(user_with_token) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) org_dictionary = org.as_dict() org_id = org_dictionary['id'] invitation_info = factory_invitation(org_dictionary['id']) InvitationService.create_invitation(invitation_info, User(user), {}, '').as_dict() invitations: list = InvitationService.get_invitations_for_org( org_id, status='PENDING', token_info=TestJwtClaims.public_user_role) assert invitations assert len(invitations) == 1
def test_create_product_multiple_subscription(session, keycloak_mock): # pylint:disable=unused-argument """Assert that an Org can be created.""" user_with_token = TestUserInfo.user_bceid_tester user_with_token['keycloak_guid'] = TestJwtClaims.public_bceid_user['sub'] user = factory_user_model(user_with_token) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) assert org dictionary = org.as_dict() assert dictionary['name'] == TestOrgInfo.org1['name'] subscriptions = ProductService.create_product_subscription( dictionary['id'], TestOrgProductsInfo.org_products2, skip_auth=True, token_info=TestJwtClaims.public_bceid_user) assert len(subscriptions) == 2 assert subscriptions[0].product_code == TestOrgProductsInfo.org_products2[ 'subscriptions'][0]['productCode'] assert subscriptions[1].product_code == TestOrgProductsInfo.org_products2[ 'subscriptions'][1]['productCode']
def test_accept_invitation_for_govm(session, auth_mock, keycloak_mock, monkeypatch): # pylint:disable=unused-argument """Accept the invitation and add membership from the invitation to the org.""" with patch.object(InvitationService, 'send_invitation', return_value=None): with patch.object(auth, 'check_auth', return_value=True): with patch.object(InvitationService, 'notify_admin', return_value=None): user_with_token = TestUserInfo.user_staff_admin user_with_token[ 'keycloak_guid'] = TestJwtClaims.public_user_role['sub'] user = factory_user_model(user_with_token) patch_token_info(TestJwtClaims.staff_admin_role, monkeypatch) org = OrgService.create_org(TestOrgInfo.org_govm, user_id=user.id) org_dictionary = org.as_dict() invitation_info = factory_invitation(org_dictionary['id']) user_with_token_invitee = TestUserInfo.user1 user_with_token_invitee[ 'keycloak_guid'] = TestJwtClaims.edit_role_2['sub'] user_invitee = factory_user_model(user_with_token_invitee) new_invitation = InvitationService.create_invitation( invitation_info, User(user_invitee), '') new_invitation_dict = new_invitation.as_dict() with patch.object(ActivityLogPublisher, 'publish_activity', return_value=None) as mock_alp: InvitationService.accept_invitation( new_invitation_dict['id'], User(user_invitee), '') mock_alp.assert_called_with( Activity( action=ActivityAction.APPROVE_TEAM_MEMBER.value, org_id=ANY, name=ANY, id=ANY, value=ANY)) members = MembershipService.get_members_for_org( org_dictionary['id'], 'ACTIVE') assert members assert len(members) == 1, 'user gets active membership'
def test_accept_invitation_exceptions(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Accept the invitation and add membership from the invitation to the org.""" with patch.object(InvitationService, 'send_invitation', return_value=None): with patch.object(auth, 'check_auth', return_value=True): with patch.object(InvitationService, 'notify_admin', return_value=None): user = factory_user_model(TestUserInfo.user_test) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) org_dictionary = org.as_dict() invitation_info = factory_invitation(org_dictionary['id']) user_invitee = factory_user_model(TestUserInfo.user1) with pytest.raises(BusinessException) as exception: InvitationService.accept_invitation( None, User(user_invitee), '') assert exception.value.code == Error.DATA_NOT_FOUND.name new_invitation = InvitationService.create_invitation( invitation_info, User(user_invitee), {}, '') new_invitation_dict = new_invitation.as_dict() InvitationService.accept_invitation(new_invitation_dict['id'], User(user_invitee), '') with pytest.raises(BusinessException) as exception: InvitationService.accept_invitation( new_invitation_dict['id'], User(user_invitee), '') assert exception.value.code == Error.ACTIONED_INVITATION.name with pytest.raises(BusinessException) as exception: expired_invitation: InvitationModel = InvitationModel \ .find_invitation_by_id(new_invitation_dict['id']) expired_invitation.invitation_status = InvitationStatusModel.get_status_by_code( 'EXPIRED') expired_invitation.save() InvitationService.accept_invitation( expired_invitation.id, User(user_invitee), '') assert exception.value.code == Error.EXPIRED_INVITATION.name
def test_delete_user_is_member_returns_204(client, jwt, session, keycloak_mock): # pylint:disable=unused-argument """Test if the user is the member of a team assert status is 204.""" user_model = factory_user_model(user_info=TestUserInfo.user_test) contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model contact_link.commit() user_model2 = factory_user_model(user_info=TestUserInfo.user2) contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model2 contact_link.commit() claims = copy.deepcopy(TestJwtClaims.public_user_role.value) claims['sub'] = str(user_model2.keycloak_guid) org = OrgService.create_org(TestOrgInfo.org1, user_id=user_model.id, token_info=claims) org_dictionary = org.as_dict() org_id = org_dictionary['id'] entity = factory_entity_model(entity_info=TestEntityInfo.entity_lear_mock) affiliation = AffiliationModel(org_id=org_id, entity_id=entity.id) affiliation.save() membership = MembershipModel(org_id=org_id, user_id=user_model2.id, membership_type_code='USER', membership_type_status=Status.ACTIVE.value) membership.save() headers = factory_auth_header(jwt=jwt, claims=claims) rv = client.delete('/api/v1/users/@me', headers=headers, content_type='application/json') assert rv.status_code == http_status.HTTP_204_NO_CONTENT
def test_get_invitations(session, auth_mock, keycloak_mock, monkeypatch): # pylint:disable=unused-argument """Assert that invitations for an org can be retrieved.""" with patch.object(InvitationService, 'send_invitation', return_value=None): user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub'] user = factory_user_model(user_info=user_with_token) patch_token_info({'sub': user.keycloak_guid}, monkeypatch) org = OrgService.create_org(TestOrgInfo.org1, user.id) org_dictionary = org.as_dict() invitation_info = factory_invitation(org_dictionary['id']) invitation = InvitationService.create_invitation(invitation_info, UserService(user), '') patch_token_info(TestJwtClaims.public_user_role, monkeypatch) response = InvitationService.get_invitations_for_org(org_dictionary['id'], 'PENDING') assert response assert len(response) == 1 assert response[0].recipient_email == invitation.as_dict()['recipient_email']
def post(): """Post a new org using the request body. If the org already exists, update the attributes. """ request_json = request.get_json() valid_format, errors = schema_utils.validate(request_json, 'org') if not valid_format: return {'message': schema_utils.serialize(errors)}, http_status.HTTP_400_BAD_REQUEST try: user = UserService.find_by_jwt_token() if user is None: response, status = {'message': 'Not authorized to perform this action'}, \ http_status.HTTP_401_UNAUTHORIZED return response, status response, status = OrgService.create_org(request_json, user.identifier).as_dict(), http_status.HTTP_201_CREATED except BusinessException as exception: response, status = {'code': exception.code, 'message': exception.message}, exception.status_code return response, status
def test_reset_bceid_user(session, auth_mock): # pylint: disable=unused-argument """Assert that reset data from a bceid user.""" keycloak_service = KeycloakService() request = KeycloakScenario.create_user_by_user_info( TestJwtClaims.tester_bceid_role) keycloak_service.add_user(request, return_if_exists=True) user = keycloak_service.get_user_by_username(request.user_name) assert user is not None user_id = user.id user_with_token = TestUserInfo.user_bceid_tester user_with_token['keycloak_guid'] = user_id user = factory_user_model(user_info=user_with_token) org = factory_org_model(user_id=user.id) response = ResetDataService.reset( TestJwtClaims.get_test_user(user_id, 'BCEID')) assert response is None found_org = OrgService.find_by_org_id(org.id) assert found_org is None
def test_delete_user(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Assert that a user can be deleted.""" user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.user_test['sub'] user_model = factory_user_model(user_info=user_with_token) contact = factory_contact_model() contact_link = ContactLinkModel() contact_link.contact = contact contact_link.user = user_model contact_link.commit() org = OrgService.create_org(TestOrgInfo.org1, user_id=user_model.id) UserService.delete_user(TestJwtClaims.user_test) updated_user = UserModel.find_by_jwt_token(TestJwtClaims.user_test) assert len(updated_user.contacts) == 0 user_orgs = MembershipModel.find_orgs_for_user(updated_user.id) for org in user_orgs: assert org.status_code == 'INACTIVE'
def get(): """Search orgs.""" # Search based on request arguments business_identifier = request.args.get('affiliation', None) name = request.args.get('name', None) status = request.args.get('status', None) access_type = request.args.get('access_type', None) bcol_account_id = request.args.get('bcolAccountId', None) page = request.args.get('page', 1) limit = request.args.get('limit', 10) try: token = g.jwt_oidc_token_info response, status = OrgService.search_orgs( business_identifier=business_identifier, access_type=access_type, name=name, status=status, bcol_account_id=bcol_account_id, page=page, limit=limit, token=token), http_status.HTTP_200_OK # If public user is searching , return 200 with empty results if orgs exist # Else return 204 is_public_user = Role.PUBLIC_USER.value in token.get( 'realm_access').get('roles') if is_public_user: # public user cant get the details in search.Gets only status of orgs if response and response.get('orgs'): status = http_status.HTTP_200_OK else: status = http_status.HTTP_204_NO_CONTENT response = {} # Do not return any results if searching by name except BusinessException as exception: response, status = { 'code': exception.code, 'message': exception.message }, exception.status_code return response, status
def test_create_basic_org_assert_pay_request_is_correct_online_banking(session, keycloak_mock): # pylint:disable=unused-argument """Assert that while org creation , pay-api gets called with proper data for basic accounts.""" user = factory_user_model() with patch.object(RestService, 'post') as mock_post: org = OrgService.create_org(TestOrgInfo.org_onlinebanking, user_id=user.id) assert org dictionary = org.as_dict() assert dictionary['name'] == TestOrgInfo.org1['name'] mock_post.assert_called() actual_data = mock_post.call_args.kwargs.get('data') expected_data = { 'accountId': dictionary.get('id'), 'accountName': dictionary.get('name'), 'paymentInfo': { 'methodOfPayment': PaymentMethod.ONLINE_BANKING.value, 'billable': True } } assert expected_data == actual_data
def put(org_id): """Update the org specified by the provided id with the request body.""" request_json = request.get_json() action = request.args.get('action', '').upper() valid_format, errors = schema_utils.validate(request_json, 'org') toke_info = g.jwt_oidc_token_info bearer_token = request.headers['Authorization'].replace('Bearer ', '') origin = request.environ.get('HTTP_ORIGIN', 'localhost') if not valid_format: return { 'message': schema_utils.serialize(errors) }, http_status.HTTP_400_BAD_REQUEST try: org = OrgService.find_by_org_id(org_id, g.jwt_oidc_token_info, allowed_roles=(*CLIENT_ADMIN_ROLES, STAFF)) if org and org.as_dict().get('accessType', None) == AccessType.ANONYMOUS.value and \ Role.STAFF_CREATE_ACCOUNTS.value not in toke_info.get('realm_access').get('roles'): return {'message': 'The organisation can only be updated by a staff admin.'}, \ http_status.HTTP_401_UNAUTHORIZED if org: if action in (ChangeType.DOWNGRADE.value, ChangeType.UPGRADE.value): response, status = org.change_org_ype( request_json, action, bearer_token).as_dict(), http_status.HTTP_200_OK else: response, status = org.update_org(org_info=request_json, token_info=toke_info, bearer_token=bearer_token, origin_url=origin).as_dict(), \ http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization 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_accept_invitation(session, auth_mock, keycloak_mock): # pylint:disable=unused-argument """Accept the invitation and add membership from the invitation to the org.""" with patch.object(InvitationService, 'send_invitation', return_value=None): with patch.object(auth, 'check_auth', return_value=True): with patch.object(InvitationService, 'notify_admin', return_value=None): user_with_token = TestUserInfo.user_test user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub'] user = factory_user_model(user_with_token) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) org_dictionary = org.as_dict() invitation_info = factory_invitation(org_dictionary['id']) user_with_token_invitee = TestUserInfo.user1 user_with_token_invitee['keycloak_guid'] = TestJwtClaims.edit_role_2['sub'] user_invitee = factory_user_model(user_with_token_invitee) new_invitation = InvitationService.create_invitation(invitation_info, User(user_invitee), {}, '') new_invitation_dict = new_invitation.as_dict() InvitationService.accept_invitation(new_invitation_dict['id'], User(user_invitee), '') members = MembershipService.get_members_for_org(org_dictionary['id'], 'PENDING_APPROVAL', token_info=TestJwtClaims.public_user_role) assert members assert len(members) == 1
def test_reject_org(session, keycloak_mock, monkeypatch): # pylint:disable=unused-argument """Assert that an Affidavit can be rejected.""" 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() affidavit1 = AffidavitService.create_affidavit( affidavit_info=affidavit_info) affidavit_info = TestAffidavit.get_test_affidavit_with_contact() affidavit = AffidavitService.create_affidavit( affidavit_info=affidavit_info) assert affidavit1.as_dict().get('status', None) == AffidavitStatus.INACTIVE.value assert affidavit.as_dict().get('status', None) == AffidavitStatus.PENDING.value 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.REJECTED.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.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 put(org_id): """Update the org specified by the provided id with the request body.""" request_json = request.get_json() valid_format, errors = schema_utils.validate(request_json, 'org') token_info = g.jwt_oidc_token_info if not valid_format: return {'message': schema_utils.serialize(errors)}, http_status.HTTP_400_BAD_REQUEST try: org = OrgService.find_by_org_id(org_id, allowed_roles=(*CLIENT_ADMIN_ROLES, STAFF)) if org and org.as_dict().get('accessType', None) == AccessType.ANONYMOUS.value and \ Role.STAFF_CREATE_ACCOUNTS.value not in token_info.get('realm_access').get('roles'): return {'message': 'The organisation can only be updated by a staff admin.'}, \ http_status.HTTP_401_UNAUTHORIZED if org: response, status = org.update_org(org_info=request_json).as_dict(), \ http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization 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_product_single_subscription_duplicate_error(session, keycloak_mock): # pylint:disable=unused-argument """Assert that an Org can be created.""" user_with_token = TestUserInfo.user_bceid_tester user_with_token['keycloak_guid'] = TestJwtClaims.public_bceid_user['sub'] user = factory_user_model(user_with_token) org = OrgService.create_org(TestOrgInfo.org1, user_id=user.id) assert org dictionary = org.as_dict() assert dictionary['name'] == TestOrgInfo.org1['name'] subscriptions = ProductService.create_product_subscription(dictionary['id'], TestOrgProductsInfo.org_products_business, skip_auth=True, token_info=TestJwtClaims.public_bceid_user) assert next(prod for prod in subscriptions if prod.get('code') == TestOrgProductsInfo.org_products_business['subscriptions'][0]['productCode']) with pytest.raises(BusinessException) as exception: ProductService.create_product_subscription(dictionary['id'], TestOrgProductsInfo.org_products_business, skip_auth=True, token_info=TestJwtClaims.public_bceid_user) assert exception.value.code == Error.PRODUCT_SUBSCRIPTION_EXISTS.name
def test_hold_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.PENDING_STAFF_REVIEW.value, 'status': TaskStatus.HOLD.value, 'remarks': ['Test Remark'] } task: TaskModel = TaskModel.find_by_task_id(fetched_task['id']) task = TaskService.update_task(TaskService(task), task_info=task_info) dictionary = task.as_dict() assert dictionary['status'] == TaskStatus.HOLD.value assert dictionary[ 'relationship_status'] == TaskRelationshipStatus.PENDING_STAFF_REVIEW.value assert dictionary['remarks'] == ['Test Remark']
def test_reset(session, auth_mock): # pylint: disable=unused-argument """Assert that can be reset data by the provided token.""" user_with_token = TestUserInfo.user_tester user_with_token['keycloak_guid'] = TestJwtClaims.tester_role['sub'] user = factory_user_model(user_info=user_with_token) org = factory_org_model(user_id=user.id) factory_membership_model(user.id, org.id) entity = factory_entity_model(user_id=user.id) ResetDataService.reset(TestJwtClaims.tester_role) with pytest.raises(BusinessException) as exception: UserService.find_by_jwt_token(user_with_token) assert exception.value.code == Error.DATA_NOT_FOUND.name found_org = OrgService.find_by_org_id(org.id) assert found_org is None found_entity = EntityService.find_by_entity_id(entity.id) assert found_entity is None found_memeber = MembershipService.get_members_for_org(org.id) assert found_memeber is None
def test_create_basic_org_assert_pay_request_is_govm(session, keycloak_mock, staff_user_mock): # pylint:disable=unused-argument """Assert that while org creation , pay-api gets called with proper data for basic accounts.""" user = factory_user_model() TestJwtClaims.get_test_user(sub=user.keycloak_guid, source=LoginSource.STAFF.value, roles=['create_accounts']) with patch.object(RestService, 'post') as mock_post: org = OrgService.create_org(TestOrgInfo.org_govm, user_id=user.id) 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': PaymentMethod.EJV.value, 'billable': False } } assert expected_data == actual_data
def test_create_premium_org_assert_pay_request_is_correct( session, keycloak_mock, monkeypatch): # pylint:disable=unused-argument """Assert that while org creation , pay-api gets called with proper data for basic accounts.""" bcol_response = Mock(spec=Response) bcol_response.json.return_value = { 'userId': 'PB25020', 'accountNumber': '180670', 'orgName': 'BC ONLINE TECHNICAL TEAM DEVL' } bcol_response.status_code = 200 pay_api_response = Mock(spec=Response) pay_api_response.status_code = 201 with patch.object(RestService, 'post', side_effect=[bcol_response, pay_api_response]) as mock_post: user = factory_user_model() patch_token_info({'sub': user.keycloak_guid}, monkeypatch) org = OrgService.create_org(TestOrgInfo.bcol_linked(), user_id=user.id) assert org dictionary = org.as_dict() mock_post.assert_called() actual_data = mock_post.call_args_list[1].kwargs.get('data') expected_data = { 'accountId': dictionary.get('id'), 'accountName': TestOrgInfo.bcol_linked().get('name'), 'paymentInfo': { 'methodOfPayment': PaymentMethod.BCOL.value, 'billable': True }, 'bcolAccountNumber': dictionary.get('bcol_account_id'), 'bcolUserId': dictionary.get('bcol_user_id'), 'contactInfo': TestOrgInfo.bcol_linked().get('mailingAddress') } assert actual_data == expected_data
def put(org_id): """Update an existing contact for the specified org.""" request_json = request.get_json() valid_format, errors = schema_utils.validate(request_json, 'contact') if not valid_format: return { 'message': schema_utils.serialize(errors) }, http_status.HTTP_400_BAD_REQUEST try: org = OrgService.find_by_org_id(org_id, g.jwt_oidc_token_info, allowed_roles=CLIENT_ADMIN_ROLES) if org: response, status = org.update_contact( request_json).as_dict(), http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization 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 get(org_id): """Retrieve the set of invitations for the given org.""" try: invitation_status = request.args.get( 'status').upper() if request.args.get('status') else 'ALL' org = OrgService.find_by_org_id(org_id, g.jwt_oidc_token_info, allowed_roles=(*CLIENT_ADMIN_ROLES, STAFF)) if org: response, status = jsonify(org.get_invitations(invitation_status, g.jwt_oidc_token_info)), \ http_status.HTTP_200_OK else: response, status = {'message': 'The requested organization 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 patch(org_id): """Patch an account.""" request_json = request.get_json() token = g.jwt_oidc_token_info # For now allowed is to put the status code, which will be done by bcol_staff_admin. # If this patch is going to be used by other other roles, then add proper security check try: is_approved: bool = request_json.get( 'statusCode', None) == AffidavitStatus.APPROVED.value origin = request.environ.get('HTTP_ORIGIN', 'localhost') response, status = OrgService.approve_or_reject( org_id=org_id, is_approved=is_approved, token_info=token, origin_url=origin).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