<commit_before>"""
:Copyright: 2006-2018 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from byceps.services.authorization import service as authorization_service

from tests.base import AbstractAppTestCase


class AuthorizationServiceTestCase(AbstractAppTestCase):

    def test_get_permission_ids_for_user(self):
        board_moderator_role = create_role_with_permissions('board_moderator', [
            'board_topic_hide',
            'board_topic_pin',
        ])
        news_editor_role = create_role_with_permissions('news_editor', [
            'news_item_create',
        ])

        user = self.create_user()

        permissions_before = authorization_service.get_permission_ids_for_user(user.id)
        assert permissions_before == frozenset()

        assign_roles_to_user(user.id, {board_moderator_role, news_editor_role})

        permissions_after = authorization_service.get_permission_ids_for_user(user.id)
        assert permissions_after == {
            'board_topic_hide',
            'board_topic_pin',
            'news_item_create',
        }


def create_role_with_permissions(role_id, permission_ids):
    role = authorization_service.create_role(role_id, role_id)

    for permission_id in permission_ids:
        permission = authorization_service.create_permission(permission_id,
                                                             permission_id)
        authorization_service.assign_permission_to_role(permission.id, role.id)

    return role


def assign_roles_to_user(user_id, roles):
    for role in roles:
        authorization_service.assign_role_to_user(user_id, role.id)
<commit_msg>Use existing test helper to create and assign permissions and roles<commit_after>"""
:Copyright: 2006-2018 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from byceps.services.authorization import service as authorization_service

from tests.base import AbstractAppTestCase
from tests.helpers import assign_permissions_to_user


class AuthorizationServiceTestCase(AbstractAppTestCase):

    def test_get_permission_ids_for_user(self):
        user = self.create_user()

        permissions_before = authorization_service.get_permission_ids_for_user(user.id)
        assert permissions_before == frozenset()

        assign_permissions_to_user(user.id, 'board_moderator', {
            'board_topic_hide',
            'board_topic_pin',
        })
        assign_permissions_to_user(user.id, 'news_editor', {
            'news_item_create',
        })

        permissions_after = authorization_service.get_permission_ids_for_user(user.id)
        assert permissions_after == {
            'board_topic_hide',
            'board_topic_pin',
            'news_item_create',
        }

{'fraction_non_alphanumeric': 0.07549234135667396, 'fraction_numerical': 0.005835156819839533, 'mean_word_length': 3.29264475743349, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3621753', 'n_tokens_mistral': 793, 'n_tokens_neox': 758, 'n_words': 135}