示例#1
0
    def _make_user(role_name,
                   organisation='org',
                   organisation_is_shared=True,
                   access=None):
        name = role_name

        if organisation:
            make_organisation(organisation, is_shared=organisation_is_shared)
            name = organisation + name

        email = '{name}@rero.ch'.format(name=name)

        datastore = app.extensions['security'].datastore

        user = datastore.find_user(email=email)

        if user:
            record = UserRecord.get_user_by_email(email)
            return record

        user = datastore.create_user(email=email,
                                     password=hash_password('123456'),
                                     active=True)
        datastore.commit()

        role = datastore.find_role(role_name)
        if not role:
            role = Role(name=role_name)

        role.users.append(user)

        db.session.add(role)

        if access:
            db.session.add(ActionUsers.allow(ActionNeed(access), user=user))

        db.session.commit()

        data = {
            'pid': name,
            'email': email,
            'first_name': name[0].upper() + name[1:],
            'last_name': 'Doe',
            'role': role_name
        }

        if organisation:
            data['organisation'] = {
                '$ref':
                'https://sonar.ch/api/organisations/{organisation}'.format(
                    organisation=organisation)
            }

        record = UserRecord.create(data, dbcommit=True)
        record.reindex()
        db.session.commit()

        return record
示例#2
0
def test_user_resolver(app, organisation, roles):
    """Test user resolver."""
    UserRecord.create({
        'pid': '1',
        'full_name': 'Jules Brochu',
        'email': '*****@*****.**',
        'roles': ['user'],
        'organisation': {
            '$ref': 'https://sonar.ch/api/organisations/org'
        }
    })

    record = DepositRecord.create(
        {'user': {
            '$ref': 'https://sonar.ch/api/users/1'
        }}, with_bucket=False)

    assert record.replace_refs().get('user')['email'] == '*****@*****.**'
示例#3
0
文件: conftest.py 项目: weblate/sonar
    def _make_user(role_name, organisation='org'):
        make_organisation(organisation)

        name = role_name
        if organisation:
            name = organisation + name

        email = '{name}@rero.ch'.format(name=name)

        datastore = app.extensions['security'].datastore

        user = datastore.find_user(email=email)

        if user:
            record = UserRecord.get_user_by_email(email)
            return record

        user = datastore.create_user(email=email,
                                     password=hash_password('123456'),
                                     active=True)
        datastore.commit()

        role = datastore.find_role(role_name)
        if not role:
            role = Role(name=role_name)

        role.users.append(user)

        db.session.add(role)
        db.session.add(
            ActionUsers.allow(ActionNeed(
                '{role}-access'.format(role=role_name)),
                              user=user))
        db.session.commit()

        record = UserRecord.create(
            {
                'pid': name,
                'email': email,
                'full_name': name,
                'roles': [role_name],
                'organisation': {
                    '$ref':
                    'https://sonar.ch/api/organisations/{organisation}'.format(
                        organisation=organisation)
                }
            },
            dbcommit=True)
        record.reindex()
        db.session.commit()

        return record
示例#4
0
def test_is_role_property(organisation, roles):
    """Test if user is in a particular role."""
    user = UserRecord.create(
        {
            'full_name': 'John Doe',
            'email': '*****@*****.**',
            'roles': [UserRecord.ROLE_MODERATOR],
            'organisation': {
                '$ref': 'https://sonar.ch/api/organisations/org'
            }
        },
        dbcommit=True)

    assert user.is_user
    assert user.is_submitter
    assert user.is_moderator
    assert not user.is_admin
    assert not user.is_superuser
示例#5
0
def test_get_moderators(app, organisation, roles):
    """Test search for moderators."""
    user = UserRecord.create(
        {
            'full_name': 'John Doe',
            'email': '*****@*****.**',
            'roles': [UserRecord.ROLE_MODERATOR],
            'organisation': {
                '$ref': 'https://sonar.ch/api/organisations/org'
            }
        },
        dbcommit=True)
    user.reindex()

    moderators = UserSearch().get_moderators()
    assert list(moderators)

    moderators = UserSearch().get_moderators('not_existing_organisation')
    assert not list(moderators)
示例#6
0
def test_is_granted(app, organisation, roles):
    """Test if user is granted with a role."""
    user = UserRecord.create(
        {
            'full_name': 'John Doe',
            'email': '*****@*****.**',
            'roles': [UserRecord.ROLE_MODERATOR],
            'organisation': {
                '$ref': 'https://sonar.ch/api/organisations/org'
            }
        },
        dbcommit=True)

    assert not user.is_granted(UserRecord.ROLE_ADMIN)
    assert not user.is_granted('fake_role')
    assert user.is_granted(UserRecord.ROLE_MODERATOR)
    assert user.is_granted(UserRecord.ROLE_USER)

    del user['roles']
    assert not user.is_granted(UserRecord.ROLE_MODERATOR)
示例#7
0
文件: signals.py 项目: weblate/sonar
def user_registered_handler(app, user, confirm_token):
    """Called when a new user is registered.

    :param app: App context.
    :param user: User account.
    """
    # Add a default role to user
    role = datastore.find_role(UserRecord.ROLE_USER)
    datastore.add_role_to_user(user, role)
    datastore.commit()

    # Create user record
    user_record = UserRecord.get_user_by_email(user.email)
    if not user_record:
        user_record = UserRecord.create(
            {
                'full_name': user.email,
                'email': user.email,
                'roles': [UserRecord.ROLE_USER]
            },
            dbcommit=True)
        user_record.reindex()
示例#8
0
def test_get_moderators_emails(app, organisation, roles):
    """Test getting list of moderators emails."""
    user = UserRecord.create(
        {
            'full_name': 'John Doe',
            'email': '*****@*****.**',
            'roles': [UserRecord.ROLE_MODERATOR],
            'organisation': {
                '$ref': 'https://sonar.ch/api/organisations/org'
            }
        },
        dbcommit=True)
    user.reindex()

    emails = user.get_moderators_emails()
    assert emails
    assert '*****@*****.**' in emails

    user['organisation'] = {
        '$ref': 'https://sonar.ch/api/organisations/not-existing'
    }
    emails = user.get_moderators_emails()
    assert not emails