예제 #1
0
    def ensure_grant(self, grantee_slug, priv_slug, dry_run=False):
        """
        Adds a parameterless grant between grantee and priv, looked up by slug.
        """

        if dry_run:
            grants = Grant.objects.filter(from_role__slug=grantee_slug,
                                          to_role__slug=priv_slug)
            if not grants:
                logger.info('[DRY RUN] Granting privilege: %s => %s',
                            grantee_slug, priv_slug)
        else:
            grantee = Role.objects.get(slug=grantee_slug)
            priv = Role.objects.get(slug=priv_slug)

            Role.get_cache().clear()
            if grantee.has_privilege(priv):
                if self.verbose:
                    logger.info('Privilege already granted: %s => %s',
                                grantee.slug, priv.slug)
            else:
                if self.verbose:
                    logger.info('Granting privilege: %s => %s', grantee.slug,
                                priv.slug)
                Grant.objects.create(
                    from_role=grantee,
                    to_role=priv,
                )
예제 #2
0
    def setUpClass(cls):
        super(APIResourceTest, cls).setUpClass()

        Role.get_cache().clear()
        cls.domain = Domain.get_or_create_with_name('qwerty', is_active=True)
        cls.list_endpoint = cls._get_list_endpoint()
        cls.username = '******'
        cls.password = '******'
        cls.user = WebUser.create(cls.domain.name,
                                  cls.username,
                                  cls.password,
                                  None,
                                  None,
                                  email='*****@*****.**',
                                  first_name='rudolph',
                                  last_name='commcare')
        cls.user.set_role(cls.domain.name, 'admin')
        cls.user.save()

        cls.account = BillingAccount.get_or_create_account_by_domain(
            cls.domain.name, created_by="automated-test")[0]
        plan = DefaultProductPlan.get_default_plan_version(
            edition=SoftwarePlanEdition.ADVANCED)
        cls.subscription = Subscription.new_domain_subscription(
            cls.account, cls.domain.name, plan)
        cls.subscription.is_active = True
        cls.subscription.save()

        cls.api_key, _ = HQApiKey.objects.get_or_create(
            user=WebUser.get_django_user(cls.user))
예제 #3
0
    def setUp(self):
        self.domain_name = 'test'
        self.password = "******"

        username = "******"
        email = "*****@*****.**"

        self.client = Client()
        self.user = WebUser.create(self.domain_name, username, self.password, email, is_admin=True)
        self.url = '/a/{}/data/edit/archive_forms/'.format(self.domain_name)

        django_user = self.user.get_django_user()
        try:
            self.user_role = UserRole.objects.get(user=django_user)
        except UserRole.DoesNotExist:
            user_privs = Role.objects.get_or_create(
                name="Privileges for %s" % django_user.username,
                slug="%s_privileges" % django_user.username,
            )[0]
            self.user_role = UserRole.objects.create(
                user=django_user,
                role=user_privs,
            )

        # Setup default roles and plans
        generator.instantiate_accounting_for_tests()

        self.bulk_role = Role.objects.filter(slug=privileges.BULK_CASE_MANAGEMENT)[0]
        Grant.objects.create(from_role=self.user_role.role, to_role=self.bulk_role)
        Role.get_cache().clear()

        self.client.login(username=self.user.username, password=self.password)

        toggles.BULK_ARCHIVE_FORMS.set(self.user.username, True)
예제 #4
0
    def ensure_grant(self, grantee_slug, priv_slug, dry_run=False):
        """
        Adds a parameterless grant between grantee and priv, looked up by slug.
        """

        if dry_run:
            grants = Grant.objects.filter(from_role__slug=grantee_slug,
                                          to_role__slug=priv_slug)
            if not grants:
                logger.info('[DRY RUN] Granting privilege: %s => %s', grantee_slug, priv_slug)
        else:
            grantee = Role.objects.get(slug=grantee_slug)
            priv = Role.objects.get(slug=priv_slug)

            Role.get_cache().clear()
            if grantee.has_privilege(priv):
                if self.verbose:
                    logger.info('Privilege already granted: %s => %s', grantee.slug, priv.slug)
            else:
                if self.verbose:
                    logger.info('Granting privilege: %s => %s', grantee.slug, priv.slug)
                Grant.objects.create(
                    from_role=grantee,
                    to_role=priv,
                )
예제 #5
0
    def setUp(self):
        self.client = Client()
        self.url = '/a/{}/data/edit/archive_forms/'.format(DOMAIN_NAME)

        Grant.objects.get_or_create(from_role=self.user_role.role, to_role=self.bulk_role)
        Role.get_cache().clear()

        self.client.login(username=self.user.username, password=self.password)
예제 #6
0
    def setUp(self):
        self.client = Client()
        self.url = '/a/{}/data/edit/archive_forms/'.format(DOMAIN_NAME)

        Grant.objects.get_or_create(from_role=self.user_role.role, to_role=self.bulk_role)
        Role.get_cache().clear()

        self.client.login(username=self.user.username, password=self.password)
예제 #7
0
    def setUpClass(cls):
        super(RegistrationAPITestCase, cls).setUpClass()
        Role.get_cache().clear()

        cls.domain1, cls.account1, cls.subscription1 = cls.setup_domain('reg-api-test-1')
        cls.domain2, cls.account2, cls.subscription2 = cls.setup_domain('reg-api-test-2')

        cls.admin_user1 = cls.setup_webuser('admin@reg-api-test-1', cls.domain1, 'admin')
        cls.read_only_user1 = cls.setup_webuser('readonly@reg-api-test-1', cls.domain1, 'read-only')
        cls.admin_user2 = cls.setup_webuser('admin@reg-api-test-2', cls.domain2, 'admin')
예제 #8
0
    def setUpClass(cls):
        Role.get_cache().clear()
        generator.instantiate_accounting_for_tests()
        cls.domain = Domain.get_or_create_with_name("qwerty", is_active=True)
        cls.username = "******"
        cls.password = "******"
        cls.admin_user = WebUser.create(cls.domain.name, cls.username, cls.password)
        cls.admin_user.set_role(cls.domain.name, "admin")
        cls.admin_user.save()

        cls.fake_user_es = FakeUserES()
        v0_5.MOCK_BULK_USER_ES = cls.mock_es_wrapper
        cls.make_users()
        set_up_subscription(cls)
        cls.domain = Domain.get(cls.domain._id)
예제 #9
0
 def setUpClass(cls):
     Role.get_cache().clear()
     generator.instantiate_accounting_for_tests()
     cls.domain = Domain.get_or_create_with_name("qwerty", is_active=True)
     cls.list_endpoint = reverse(
         "api_dispatch_list",
         kwargs=dict(domain=cls.domain.name, api_name=cls.api_name, resource_name=cls.resource.Meta.resource_name),
     )
     cls.username = "******"
     cls.password = "******"
     cls.user = WebUser.create(cls.domain.name, cls.username, cls.password)
     cls.user.set_role(cls.domain.name, "admin")
     cls.user.save()
     set_up_subscription(cls)
     cls.domain = Domain.get(cls.domain._id)
예제 #10
0
파일: tests.py 프로젝트: dimagi/commcare-hq
    def test_bulk_archive_get_form(self):

        # Logged in
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.context["bulk_upload"]["download_url"], "/static/data_interfaces/files/forms_bulk_example.xlsx"
        )

        grant = Grant.objects.get(from_role=self.user_role.role, to_role=self.bulk_role)
        grant.delete()
        Role.get_cache().clear()

        # Revoked privileges should not render form
        response = self.client.get(self.url)
        self.assertFalse("bulk_upload" in response.context)
예제 #11
0
    def test_bulk_archive_get_form(self):

        # Logged in
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.context['bulk_upload']['download_url'],
            '/static/data_interfaces/xlsx/forms_bulk_example.xlsx')

        grant = Grant.objects.get(from_role=self.user_role.role,
                                  to_role=self.bulk_role)
        grant.delete()
        Role.get_cache().clear()

        # Revoked privileges should not render form
        response = self.client.get(self.url)
        self.assertFalse('bulk_upload' in response.context)
예제 #12
0
def ensure_grants(grants_to_privs,
                  dry_run=False,
                  verbose=False,
                  roles_by_slug=None):
    """
    Adds a parameterless grant between grantee and priv, looked up by slug.

    :param grants_to_privs: An iterable of two-tuples:
    `(grantee_slug, priv_slugs)`. Will only be iterated once.
    """
    dry_run_tag = "[DRY RUN] " if dry_run else ""
    if roles_by_slug is None:
        roles_by_slug = {role.slug: role for role in Role.objects.all()}

    granted = defaultdict(set)
    for grant in Grant.objects.select_related('from_role', 'to_role').all():
        granted[grant.from_role.slug].add(grant.to_role.slug)

    grants_to_create = []
    for grantee_slug, priv_slugs in grants_to_privs:
        if grantee_slug not in roles_by_slug:
            logger.info('grantee %s does not exist.', grantee_slug)
            continue

        for priv_slug in priv_slugs:
            if priv_slug not in roles_by_slug:
                logger.info('privilege %s does not exist.', priv_slug)
                continue

            if priv_slug in granted[grantee_slug]:
                if verbose or dry_run:
                    logger.info('%sPrivilege already granted: %s => %s',
                                dry_run_tag, grantee_slug, priv_slug)
            else:
                granted[grantee_slug].add(priv_slug)
                if verbose or dry_run:
                    logger.info('%sGranting privilege: %s => %s', dry_run_tag,
                                grantee_slug, priv_slug)
                if not dry_run:
                    grants_to_create.append(
                        Grant(from_role=roles_by_slug[grantee_slug],
                              to_role=roles_by_slug[priv_slug]))
    if grants_to_create:
        Role.get_cache().clear()
        Grant.objects.bulk_create(grants_to_create)
예제 #13
0
파일: utils.py 프로젝트: dimagi/commcare-hq
def ensure_grants(grants_to_privs, dry_run=False, verbose=False, roles_by_slug=None):
    """
    Adds a parameterless grant between grantee and priv, looked up by slug.

    :param grants_to_privs: An iterable of two-tuples:
    `(grantee_slug, priv_slugs)`. Will only be iterated once.
    """
    dry_run_tag = "[DRY RUN] " if dry_run else ""
    if roles_by_slug is None:
        roles_by_slug = {role.slug: role for role in Role.objects.all()}

    granted = defaultdict(set)
    for grant in Grant.objects.select_related('from_role', 'to_role').all():
        granted[grant.from_role.slug].add(grant.to_role.slug)

    grants_to_create = []
    for grantee_slug, priv_slugs in grants_to_privs:
        if grantee_slug not in roles_by_slug:
            logger.info('grantee %s does not exist.', grantee_slug)
            continue

        for priv_slug in priv_slugs:
            if priv_slug not in roles_by_slug:
                logger.info('privilege %s does not exist.', priv_slug)
                continue

            if priv_slug in granted[grantee_slug]:
                if verbose or dry_run:
                    logger.info('%sPrivilege already granted: %s => %s',
                        dry_run_tag, grantee_slug, priv_slug)
            else:
                granted[grantee_slug].add(priv_slug)
                if verbose or dry_run:
                    logger.info('%sGranting privilege: %s => %s',
                        dry_run_tag, grantee_slug, priv_slug)
                if not dry_run:
                    grants_to_create.append(Grant(
                        from_role=roles_by_slug[grantee_slug],
                        to_role=roles_by_slug[priv_slug]
                    ))
    if grants_to_create:
        Role.get_cache().clear()
        Grant.objects.bulk_create(grants_to_create)
예제 #14
0
파일: utils.py 프로젝트: dimagi/commcare-hq
    def setUpClass(cls):
        super(APIResourceTest, cls).setUpClass()

        Role.get_cache().clear()
        cls.domain = Domain.get_or_create_with_name('qwerty', is_active=True)
        cls.list_endpoint = cls._get_list_endpoint()
        cls.username = '******'
        cls.password = '******'
        cls.user = WebUser.create(cls.domain.name, cls.username, cls.password)
        cls.user.set_role(cls.domain.name, 'admin')
        cls.user.save()

        cls.account = BillingAccount.get_or_create_account_by_domain(cls.domain.name, created_by="automated-test")[0]
        plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.ADVANCED)
        cls.subscription = Subscription.new_domain_subscription(cls.account, cls.domain.name, plan)
        cls.subscription.is_active = True
        cls.subscription.save()

        cls.api_key, _ = ApiKey.objects.get_or_create(user=WebUser.get_django_user(cls.user))
예제 #15
0
def ensure_grant(grantee_slug, priv_slug, dry_run=False, verbose=False):
    """
    Adds a parameterless grant between grantee and priv, looked up by slug.
    """

    try:
        grantee = Role.objects.get(slug=grantee_slug)
    except Role.DoesNotExist:
        logger.info(
            '[DRY RUN] grantee {} does not exist.'.format(grantee_slug))
        return

    try:
        priv = Role.objects.get(slug=priv_slug)
    except Role.DoesNotExist:
        logger.info('[DRY RUN] privilege {} does not exist.'.format(priv_slug))
        return

    if dry_run:
        grants = Grant.objects.filter(from_role__slug=grantee_slug,
                                      to_role__slug=priv_slug)
        if not grants:
            logger.info('[DRY RUN] Granting privilege: %s => %s', grantee_slug,
                        priv_slug)
        if grantee.has_privilege(priv):
            if verbose:
                logger.info('[DRY RUN] Privilege already granted: %s => %s',
                            grantee.slug, priv.slug)

    else:
        Role.get_cache().clear()
        if grantee.has_privilege(priv):
            if verbose:
                logger.info('Privilege already granted: %s => %s',
                            grantee.slug, priv.slug)
        else:
            if verbose:
                logger.info('Granting privilege: %s => %s', grantee.slug,
                            priv.slug)
            Grant.objects.create(
                from_role=grantee,
                to_role=priv,
            )
예제 #16
0
def ensure_grants(privs_to_ensure_for_grantee,
                  dry_run=False,
                  verbose=False,
                  roles_by_slug=None):
    """
    Adds a parameterless grant between grantee and priv, looked up by slug.
    :param privs_to_ensure_for_grantee: An iterable of tuples: `(grantee_slug, [priv_slugs])`
    :param dry_run: if True, will not make any changes to the DB
    :param verbose: if True, will log additional output
    :param roles_by_slug: can specify your own roles to consider, otherwise will fetch all Role objects
    """
    dry_run_tag = "[DRY RUN] " if dry_run else ""
    roles_by_slug = roles_by_slug or get_all_roles_by_slug()
    granted_privs_for_grantee = get_granted_privs_for_grantee()

    grants_to_create = []
    for grantee_slug, priv_slugs in privs_to_ensure_for_grantee:
        if grantee_slug not in roles_by_slug:
            logger.info('grantee %s does not exist.', grantee_slug)
            continue

        for priv_slug in priv_slugs:
            if priv_slug not in roles_by_slug:
                logger.info('privilege %s does not exist.', priv_slug)
                continue

            if priv_slug in granted_privs_for_grantee[grantee_slug]:
                if verbose or dry_run:
                    logger.info('%sPrivilege already granted: %s => %s',
                                dry_run_tag, grantee_slug, priv_slug)
            else:
                granted_privs_for_grantee[grantee_slug].add(priv_slug)
                if verbose or dry_run:
                    logger.info('%sGranting privilege: %s => %s', dry_run_tag,
                                grantee_slug, priv_slug)
                if not dry_run:
                    grants_to_create.append(
                        Grant(from_role=roles_by_slug[grantee_slug],
                              to_role=roles_by_slug[priv_slug]))
    if grants_to_create:
        Role.get_cache().clear()
        Grant.objects.bulk_create(grants_to_create)
예제 #17
0
def ensure_grant(grantee_slug, priv_slug, dry_run=False, verbose=False):
    """
    Adds a parameterless grant between grantee and priv, looked up by slug.
    """

    try:
        grantee = Role.objects.get(slug=grantee_slug)
    except Role.DoesNotExist:
        logger.info('[DRY RUN] grantee {} does not exist.'.format(grantee_slug))
        return

    try:
        priv = Role.objects.get(slug=priv_slug)
    except Role.DoesNotExist:
        logger.info('[DRY RUN] privilege {} does not exist.'.format(priv_slug))
        return

    if dry_run:
        grants = Grant.objects.filter(from_role__slug=grantee_slug,
                                      to_role__slug=priv_slug)
        if not grants:
            logger.info('[DRY RUN] Granting privilege: %s => %s', grantee_slug, priv_slug)
        if grantee.has_privilege(priv):
            if verbose:
                logger.info('[DRY RUN] Privilege already granted: %s => %s', grantee.slug, priv.slug)

    else:
        Role.get_cache().clear()
        if grantee.has_privilege(priv):
            if verbose:
                logger.info('Privilege already granted: %s => %s', grantee.slug, priv.slug)
        else:
            if verbose:
                logger.info('Granting privilege: %s => %s', grantee.slug, priv.slug)
            Grant.objects.create(
                from_role=grantee,
                to_role=priv,
            )
예제 #18
0
 def setUp(self):
     Role.get_cache().clear()
     self.zazzle_privilege = arbitrary.role(
         slug=arbitrary.unique_slug('zazzle'), parameters=set(['domain']))
예제 #19
0
 def setUp(self):
     Role.get_cache().clear()
예제 #20
0
 def setUpClass(cls):
     super(BaseAccountingTest, cls).setUpClass()
     Role.get_cache().clear()
예제 #21
0
def delete_grants(grants):
    Role.get_cache().clear()
    for grant in grants:
        grant.delete()
예제 #22
0
 def setUpClass(cls):
     Role.get_cache().clear()
     generator.instantiate_accounting_for_tests()
예제 #23
0
 def setUp(self):
     Role.get_cache().clear()
     generator.instantiate_accounting_for_tests()
예제 #24
0
 def setUp(self):
     Role.get_cache().clear()
     self.zazzle_privilege = arbitrary.role(slug=arbitrary.unique_slug('zazzle'), parameters=set(['domain']))
예제 #25
0
 def setUpClass(cls):
     super(BaseAccountingTest, cls).setUpClass()
     Role.get_cache().clear()
     generator.instantiate_accounting()
예제 #26
0
 def setUpClass(cls):
     super(BaseAccountingTest, cls).setUpClass()
     Role.get_cache().clear()
     generator.instantiate_accounting_for_tests()