<commit_before>from django.test import TestCase
from corehq.apps.users.models import Permissions


class PermissionsTest(TestCase):

    def test_OR(self):
        p1 = Permissions(
            edit_web_users=True,
            view_reports=True,
            view_report_list=['report1'],
        )
        p2 = Permissions(
            edit_apps=True,
            view_reports=True,
            view_report_list=['report2'],
        )
        self.assertEqual(dict(p1 | p2), dict(Permissions(
            edit_apps=True,
            edit_web_users=True,
            view_reports=True,
            view_report_list=['report1', 'report2'],
        )))<commit_msg>add permission test for user_can_view_deid_exports<commit_after>from dimagi.utils.decorators.memoized import Memoized
from django.test import TestCase, SimpleTestCase
import mock
from corehq.apps.export.views import user_can_view_deid_exports
from corehq.apps.users.decorators import get_permission_name
from corehq.apps.users.models import Permissions, WebUser, UserRole, DomainMembership
from corehq.apps.users.permissions import DEID_EXPORT_PERMISSION


class PermissionsTest(TestCase):

    def test_OR(self):
        p1 = Permissions(
            edit_web_users=True,
            view_reports=True,
            view_report_list=['report1'],
        )
        p2 = Permissions(
            edit_apps=True,
            view_reports=True,
            view_report_list=['report2'],
        )
        self.assertEqual(dict(p1 | p2), dict(Permissions(
            edit_apps=True,
            edit_web_users=True,
            view_reports=True,
            view_report_list=['report1', 'report2'],
        )))


@mock.patch('corehq.apps.export.views.domain_has_privilege',
            lambda domain, privilege: True)
class ExportPermissionsTest(SimpleTestCase):
    @classmethod
    def setUpClass(cls):
        cls.domain = 'export-permissions-test'
        cls.web_user = WebUser(username='<EMAIL>', domains=[cls.domain])
        cls.web_user.domain_memberships = [DomainMembership(domain=cls.domain, role_id='MYROLE')]
        cls.permissions = Permissions()

    def setUp(self):
        test_self = self

        def get_role(self):
            return UserRole(
                domain=test_self.domain,
                permissions=test_self.permissions
            )

        patches = [
            mock.patch.object(DomainMembership, 'role', property(get_role)),
            mock.patch.object(Memoized, '__call__',
                              lambda self, *args, **kwargs: self.func(*args, **kwargs))
        ]
        for patch in patches:
            patch.start()
            self.addCleanup(patch.stop)

    def tearDown(self):
        self.permissions = Permissions()

    def test_deid_permission(self):
        self.assertFalse(user_can_view_deid_exports(self.domain, self.web_user))
        self.permissions = Permissions(view_report_list=[DEID_EXPORT_PERMISSION])
        self.assertTrue(
            self.permissions.has(get_permission_name(Permissions.view_report),
                                 data=DEID_EXPORT_PERMISSION))
        self.assertTrue(
            self.web_user.has_permission(
                self.domain, get_permission_name(Permissions.view_report),
                data=DEID_EXPORT_PERMISSION)
        )

        self.assertTrue(user_can_view_deid_exports(self.domain, self.web_user))
{'fraction_non_alphanumeric': 0.09423929098966026, 'fraction_numerical': 0.004726735598227474, 'mean_word_length': 2.4515800203873597, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6704298', 'n_tokens_mistral': 1001, 'n_tokens_neox': 983, 'n_words': 154}