示例#1
0
def test_delete_contact_user_link(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that a contact can not be deleted if contact link exists."""
    user_with_token = TestUserInfo.user_test
    user_with_token['keycloak_guid'] = TestJwtClaims.edit_role['sub']
    user_model = factory_user_model(user_info=user_with_token)
    user = UserService(user_model)

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

    contact = factory_contact_model()

    contact_link = ContactLinkModel()
    contact_link.contact = contact
    contact_link.user = user_model
    contact_link.org = org._model  # pylint:disable=protected-access
    contact_link = contact_link.flush()
    contact_link.commit()

    deleted_contact = UserService.delete_contact(TestJwtClaims.edit_role)

    assert deleted_contact is None

    delete_contact_link = ContactLinkModel.find_by_user_id(user.identifier)
    assert not delete_contact_link

    exist_contact_link = ContactLinkModel.find_by_org_id(org_id)
    assert exist_contact_link
示例#2
0
def test_as_dict(session):  # pylint: disable=unused-argument
    """Assert that a user is rendered correctly as a dictionary."""
    user_model = factory_user_model()
    user = UserService(user_model)

    dictionary = user.as_dict()
    assert dictionary['username'] == TestUserInfo.user1['username']
示例#3
0
def test_delete_contact_user_link(session, auth_mock):  # pylint:disable=unused-argument
    """Assert that a contact can not be deleted if contact link exists."""
    user_model = factory_user_model(user_info=TestUserInfo.user_test)
    user = UserService(user_model)

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

    contact = factory_contact_model()

    contact_link = ContactLinkModel()
    contact_link.contact = contact
    contact_link.user = user_model
    contact_link.org = org._model  # pylint:disable=protected-access
    contact_link.commit()

    updated_user = user.delete_contact(TestJwtClaims.user_test)

    dictionary = None
    dictionary = updated_user.as_dict()
    assert len(dictionary['contacts']) == 0

    delete_contact_link = ContactLinkModel.find_by_user_id(user.identifier)
    assert not delete_contact_link

    exist_contact_link = ContactLinkModel.find_by_org_id(org_id)
    assert exist_contact_link
示例#4
0
def test_user_settings(session, auth_mock, keycloak_mock, monkeypatch):  # pylint:disable=unused-argument
    """Assert that a contact can not be deleted if contact link exists."""
    user_with_token = TestUserInfo.user_test
    user_with_token['keycloak_guid'] = TestJwtClaims.public_user_role['sub']
    user_model = factory_user_model(user_info=user_with_token)
    user = UserService(user_model)

    patch_token_info(TestJwtClaims.public_user_role, monkeypatch)
    org = OrgService.create_org(TestOrgInfo.org1, user_id=user.identifier)
    org_dictionary = org.as_dict()
    org_id = org_dictionary['id']

    usersettings = UserSettingsService.fetch_user_settings(user.identifier)
    assert usersettings is not None
    org = [x for x in usersettings if x.type == 'ACCOUNT']
    assert len(usersettings) == 3
    assert org[0].label == TestOrgInfo.org1['name']
    assert org[0].id == org_id
    assert org[0].additional_label == '', 'no additional label'

    # add an org with branch name and assert additonal label
    org = OrgService.create_org(TestOrgInfo.org_branch_name, user_id=user.identifier)
    org_with_branch_dictionary = org.as_dict()

    usersettings = UserSettingsService.fetch_user_settings(user.identifier)
    assert len(usersettings) == 4
    org = [x for x in usersettings if x.type == 'ACCOUNT' and x.label == org_with_branch_dictionary.get('name')]
    assert org[0].additional_label == org_with_branch_dictionary.get('branch_name'), 'additional label matches'
示例#5
0
def test_user_find_by_username_missing_username(session):  # pylint: disable=unused-argument
    """Assert that the business can't be found by incorrect username."""
    user_model = factory_user_model(user_info=TestUserInfo.user_test)
    user = UserService(user_model)

    user = UserService.find_by_username('foo')

    assert user is None
示例#6
0
def test_user_find_by_username_missing_username(session):  # pylint: disable=unused-argument
    """Assert that the business can't be found by incorrect username."""
    user_model = factory_user_model(
        username='******',
        roles='{edit,uma_authorization,basic}',
        keycloak_guid='1b20db59-19a0-4727-affe-c6f64309fd04')
    user = UserService(user_model)

    user = UserService.find_by_username('foo')

    assert user is None
示例#7
0
def test_bcros_user_update_by_token(session):  # pylint: disable=unused-argument
    """Assert that a user can be created by token."""
    user_model = factory_user_model(TestUserInfo.user_bcros)
    user = UserService(user_model)
    dictionary = user.as_dict()
    assert dictionary.get('keycloak_guid', None) is None

    user = UserService.save_from_jwt_token(TestJwtClaims.anonymous_bcros_role)
    assert user is not None
    dictionary = user.as_dict()
    assert dictionary['username'] == TestJwtClaims.anonymous_bcros_role['preferred_username']
    assert dictionary['keycloak_guid'] == TestJwtClaims.anonymous_bcros_role['sub']
示例#8
0
def test_user_find_by_username(session):  # pylint: disable=unused-argument
    """Assert that a user can be found by username."""
    user_model = factory_user_model()
    user = UserService(user_model)

    user = UserService.find_by_username(None)
    assert user is None

    user = UserService.find_by_username(TestUserInfo.user1['username'])
    assert user is not None
    dictionary = user.as_dict()
    assert dictionary['username'] == TestUserInfo.user1['username']
示例#9
0
def test_as_dict(session):  # pylint: disable=unused-argument
    """Assert that a user is rendered correctly as a dictionary."""
    user_model = factory_user_model(
        username='******',
        roles='{edit,uma_authorization,basic}',
        keycloak_guid='1b20db59-19a0-4727-affe-c6f64309fd04')
    user = UserService(user_model)

    dictionary = user.as_dict()
    assert dictionary['username'] == 'testuser'
    assert dictionary['roles'] == '{edit,uma_authorization,basic}'
    assert dictionary[
        'keycloak_guid'] == '1b20db59-19a0-4727-affe-c6f64309fd04'
示例#10
0
def test_user_find_by_username(session):  # pylint: disable=unused-argument
    """Assert that a user can be found by username."""
    user_model = factory_user_model(
        username='******',
        roles='{edit,uma_authorization,basic}',
        keycloak_guid='1b20db59-19a0-4727-affe-c6f64309fd04')
    user = UserService(user_model)

    user = UserService.find_by_username('testuser')

    assert user is not None
    dictionary = user.as_dict()
    assert dictionary['username'] == 'testuser'
示例#11
0
def test_get_invitations(session, auth_mock):  # pylint:disable=unused-argument
    """Assert that invitations for an org can be retrieved."""
    with patch.object(InvitationService, 'send_invitation', return_value=None):
        user = factory_user_model()
        org = OrgService.create_org(TestOrgInfo.org1, user.id)

        invitation_info = factory_invitation(org.as_dict()['id'])

        invitation = InvitationService.create_invitation(invitation_info, UserService(user), {}, '')

        response = org.get_invitations()
        assert response
        assert len(response['invitations']) == 1
        assert response['invitations'][0]['recipientEmail'] == invitation.as_dict()['recipientEmail']
示例#12
0
def test_get_orgs(session):  # pylint:disable=unused-argument
    """Assert that orgs for a user can be retrieved."""
    user_model = factory_user_model(
        username='******',
        roles='{edit,uma_authorization,basic}',
        keycloak_guid='1b20db59-19a0-4727-affe-c6f64309fd04')
    user = UserService(user_model)

    OrgService.create_org(TEST_ORG_INFO, user_id=user.identifier)

    response = user.get_orgs()
    assert response['orgs']
    assert len(response['orgs']) == 1
    assert response['orgs'][0]['name'] == TEST_ORG_INFO['name']
示例#13
0
def test_user_settings(session, auth_mock, keycloak_mock):  # pylint:disable=unused-argument
    """Assert that a contact can not be deleted if contact link exists."""
    user_with_token = TestUserInfo.user_test
    user_with_token['keycloak_guid'] = TestJwtClaims.edit_role['sub']
    user_model = factory_user_model(user_info=user_with_token)
    user = UserService(user_model)

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

    usersettings = UserSettingsService.fetch_user_settings(user.identifier)
    assert usersettings is not None
    org = [x for x in usersettings if x.type == 'ACCOUNT']
    assert len(usersettings) == 3
    assert org[0].label == TestOrgInfo.org1['name']
    assert org[0].id == org_id
示例#14
0
def test_get_invitations(session, auth_mock):  # 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.edit_role['sub']
        user = factory_user_model(user_info=user_with_token)
        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), {}, '')

        response = InvitationService.get_invitations_for_org(org_dictionary['id'], 'PENDING', TestJwtClaims.edit_role)
        assert response
        assert len(response) == 1
        assert response[0].recipient_email == invitation.as_dict()['recipientEmail']