예제 #1
0
파일: test_user.py 프로젝트: syin/sbc-auth
def test_delete_user_where_org_has_affiliations(session, auth_mock,
                                                keycloak_mock):  # pylint:disable=unused-argument
    """Assert that a user can be deleted."""
    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 = contact_link.flush()
    contact_link.commit()

    org = OrgService.create_org(TestOrgInfo.org1,
                                user_id=user_model.id).as_dict()
    org_id = org['id']

    entity = factory_entity_model(entity_info=TestEntityInfo.entity_lear_mock)

    affiliation = AffiliationModel(org_id=org_id, entity_id=entity.id)
    affiliation.save()
    with pytest.raises(BusinessException) as exception:
        UserService.delete_user(TestJwtClaims.user_test)
        assert exception.code == Error.DELETE_FAILED_ONLY_OWNER

    updated_user = UserModel.find_by_jwt_token(TestJwtClaims.user_test)
    contacts = UserService.get_contacts(TestJwtClaims.user_test)
    assert len(contacts) == 1

    user_orgs = MembershipModel.find_orgs_for_user(updated_user.id)
    for org in user_orgs:
        assert org.status_code == 'ACTIVE'
예제 #2
0
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()

    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()

    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()

    claims = copy.deepcopy(TestJwtClaims.public_user_role.value)
    claims['sub'] = str(user_model2.keycloak_guid)

    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
예제 #3
0
파일: test_user.py 프로젝트: syin/sbc-auth
def test_delete_user_as_only_admin_returns_400(client, jwt, session,
                                               keycloak_mock):  # pylint:disable=unused-argument
    """Test if the user is the only owner of a team assert status is 400."""
    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()

    claims = copy.deepcopy(TestJwtClaims.edit_role.value)
    claims['sub'] = str(user_model.keycloak_guid)

    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_400_BAD_REQUEST
예제 #4
0
def test_delete_user_where_org_has_another_owner(session, auth_mock,
                                                 keycloak_mock, monkeypatch):  # 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()

    patch_token_info(TestJwtClaims.get_test_user(user_model.keycloak_guid),
                     monkeypatch)
    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='ADMIN',
                                 membership_type_status=Status.ACTIVE.value)
    membership.save()
    membership.commit()

    # with pytest.raises(BusinessException) as exception:
    patch_token_info(TestJwtClaims.get_test_user(user_model2.keycloak_guid),
                     monkeypatch)
    UserService.delete_user()

    updated_user = UserModel.find_by_jwt_token()
    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'
예제 #5
0
def factory_affiliation_model(entity_id, org_id):
    """Produce a templated affiliation model."""
    affiliation = AffiliationModel(entity_id=entity_id, org_id=org_id)
    affiliation.save()
    return affiliation
예제 #6
0
def factory_affiliation_service(entity_id, org_id):
    """Produce a templated affiliation service."""
    affiliation = AffiliationModel(entity=entity_id, org=org_id)
    affiliation.save()
    affiliation_service = AffiliationService(affiliation)
    return affiliation_service
예제 #7
0
async def process_name_events(event_message: Dict[str, any]):
    """Process name events.

    1. Check if the NR already exists in entities table, if yes apply changes. If not create entity record.
    2. Check if new status is DRAFT, if yes call pay-api and get the account details for the payments against the NR.
    3. If an account is found, affiliate to that account.

    Args:
        event_message (object): cloud event message, sample below.
            {
                'specversion': '1.0.1',
                'type': 'bc.registry.names.events',
                'source': '/requests/6724165',
                'id': id,
                'time': '',
                'datacontenttype': 'application/json',
                'identifier': '781020202',
                'data': {
                    'request': {
                        'nrNum': 'NR 5659951',
                        'newState': 'APPROVED',
                        'previousState': 'DRAFT'
                    }
                }
            }
    """
    logger.debug('>>>>>>>process_name_events>>>>>')
    request_data = event_message.get('data').get('request')
    nr_number = request_data['nrNum']
    nr_status = request_data['newState']
    nr_entity = EntityModel.find_by_business_identifier(nr_number)
    if nr_entity is None:
        logger.info('Entity doesn' 't exist, creating a new entity.')
        nr_entity = EntityModel(business_identifier=nr_number,
                                corp_type_code=CorpType.NR.value)

    nr_entity.status = nr_status
    nr_entity.name = request_data.get(
        'name',
        '')  # its not part of event now, this is to handle if they include it.
    nr_entity.last_modified_by = None  # TODO not present in event message.
    nr_entity.last_modified = parser.parse(event_message.get('time'))

    if nr_status == 'DRAFT' and AffiliationModel.find_affiliations_by_business_identifier(
            nr_number) is None:
        logger.info('Status is DRAFT, getting invoices for account')
        # Find account details for the NR.
        invoices = RestService.get(
            f'{APP_CONFIG.PAY_API_URL}/payment-requests?businessIdentifier={nr_number}',
            token=RestService.get_service_account_token()).json()

        # Ideally there should be only one or two (priority fees) payment request for the NR.
        if invoices and (auth_account_id := invoices['invoices'][0].get('paymentAccount').get('accountId')) \
                and str(auth_account_id).isnumeric():
            logger.info('Account ID received : %s', auth_account_id)
            # Auth account id can be service account value too, so doing a query lookup than find_by_id
            org: OrgModel = db.session.query(OrgModel).filter(
                OrgModel.id == auth_account_id).one_or_none()
            if org:
                nr_entity.pass_code_claimed = True
                # Create an affiliation.
                logger.info(
                    'Creating affiliation between Entity : %s and Org : %s',
                    nr_entity, org)
                affiliation: AffiliationModel = AffiliationModel(
                    entity=nr_entity, org=org)
                affiliation.flush()
예제 #8
0
async def test_events_listener_queue(app, session, stan_server, event_loop,
                                     client_id, events_stan, future,
                                     monkeypatch):
    """Assert that events can be retrieved and decoded from the Queue."""
    # Call back for the subscription
    from business_events_listener.worker import cb_nr_subscription_handler

    # 1. Create an Org
    # 2. Mock the rest service to return the invoices with the org created.
    # 3. Publish NR event and assert it's affiliated to the org.
    org = OrgModel(name='Test',
                   org_type=OrgTypeModel.get_default_type(),
                   org_status=OrgStatusModel.get_default_status()).save()
    org_id = org.id

    events_subject = 'test_subject'
    events_queue = 'test_queue'
    events_durable_name = 'test_durable'

    nr_number = 'NR 1234'
    nr_state = 'DRAFT'

    # register the handler to test it
    await subscribe_to_queue(events_stan, events_subject, events_queue,
                             events_durable_name, cb_nr_subscription_handler)

    # Mock the rest service response to return the org just created.
    def get_invoices_mock(nr_number, token):
        response_content = json.dumps({
            'invoices': [{
                'businessIdentifier': nr_number,
                'paymentAccount': {
                    'accountId': org_id
                }
            }]
        })

        response = Response()
        response.status_code = 200
        response._content = str.encode(response_content)
        return response

    monkeypatch.setattr('auth_api.services.rest_service.RestService.get',
                        get_invoices_mock)
    monkeypatch.setattr(
        'auth_api.services.rest_service.RestService.get_service_account_token',
        lambda *args, **kwargs: None)

    # add an event to queue
    await helper_add_event_to_queue(events_stan, events_subject, nr_number,
                                    nr_state, 'TEST')

    # Query the affiliations and assert the org has affiliation for the NR.
    entity: EntityModel = EntityModel.find_by_business_identifier(nr_number)
    assert entity
    assert entity.pass_code_claimed
    affiliations: [AffiliationModel
                   ] = AffiliationModel.find_affiliations_by_org_id(org_id)
    assert len(affiliations) == 1
    assert affiliations[0].entity_id == entity.id
    assert affiliations[0].org_id == org_id

    # Publish message again and assert it doesn't create duplicate affiliation.
    await helper_add_event_to_queue(events_stan, events_subject, nr_number,
                                    nr_state, 'TEST')
    affiliations: [AffiliationModel
                   ] = AffiliationModel.find_affiliations_by_org_id(org_id)
    assert len(affiliations) == 1

    # Publish message for an NR which was done using a service account or staff with no user account.
    def get_invoices_mock(nr_number, token):
        response_content = json.dumps({
            'invoices': [{
                'businessIdentifier': nr_number,
                'paymentAccount': {
                    'accountId': 'SERVICE-ACCOUNT-NAME-REQUEST-SERVICE-ACCOUNT'
                }
            }]
        })

        response = Response()
        response.status_code = 200
        response._content = str.encode(response_content)
        return response

    monkeypatch.setattr('auth_api.services.rest_service.RestService.get',
                        get_invoices_mock)
    # add an event to queue
    nr_number = 'NR 000001'
    await helper_add_event_to_queue(events_stan, events_subject, nr_number,
                                    nr_state, 'TEST')

    # Query the entity and assert the entity is not affiliated.
    entity: EntityModel = EntityModel.find_by_business_identifier(nr_number)
    assert entity
    assert not entity.pass_code_claimed