Esempio n. 1
0
    def test_can_execute_constitution(self):
        """Test that users with can_execute permissions can execute any constitution action and mark it as 'passed'"""
        all_actions_fail_policy = {
            **all_actions_pass_policy,
            "check": "return FAILED",
        }
        policy = Policy(
            **all_actions_fail_policy,
            kind=Policy.CONSTITUTION,
            community=self.slack_community,
            description="all actions fail",
            name="all actions fail",
        )
        policy.save()

        # create a test user with can_execute permissions for PolicykitAddCommunityDoc
        can_add = Permission.objects.get(name="Can add policykit add community doc")
        can_execute = Permission.objects.get(name="Can execute policykit add community doc")
        user_with_can_execute = SlackUser.objects.create(username="******", community=self.slack_community)
        user_with_can_execute.user_permissions.add(can_add)
        user_with_can_execute.user_permissions.add(can_execute)
        self.assertTrue(user_with_can_execute.has_perm("policyengine.add_policykitaddcommunitydoc"))
        self.assertTrue(user_with_can_execute.has_perm("policyengine.can_execute_policykitaddcommunitydoc"))

        # action initiated by user with "can_execute" should pass
        action = PolicykitAddCommunityDoc(
            name="my doc", initiator=user_with_can_execute, community=self.slack_community
        )
        action.save()
        self.assertEqual(action.proposal.status, "passed")

        # action initiated by user without "can_execute" should fail
        action = PolicykitAddCommunityDoc(name="my other doc", initiator=self.user, community=self.slack_community)
        action.save()
        self.assertEqual(action.proposal.status, "failed")
Esempio n. 2
0
def document_action_save(request):
    from policyengine.models import PolicykitAddCommunityDoc

    data = json.loads(request.body)
    user = get_user(request)

    action = PolicykitAddCommunityDoc()
    action.community = user.community
    action.initiator = user
    action.name = data['name']
    action.text = data['text']
    action.save()

    return HttpResponse()
Esempio n. 3
0
def document_action_save(request):
    from policyengine.models import CommunityDoc, PolicykitAddCommunityDoc, PolicykitChangeCommunityDoc

    data = json.loads(request.body)
    user = get_user(request)

    action = None
    if data['operation'] == 'Add':
        action = PolicykitAddCommunityDoc()
    elif data['operation'] == 'Change':
        action = PolicykitChangeCommunityDoc()
        action.doc = CommunityDoc.objects.filter(id=data['doc'])[0]
    else:
        return HttpResponseBadRequest()

    action.community = user.community
    action.initiator = user
    action.name = data['name']
    action.text = data['text']
    action.save()

    return HttpResponse()
    def test_cannot_propose_constitution(self):
        """Test that action fails when a user does not have permission to propose constitution change"""
        policy = ConstitutionPolicy(
            **all_actions_pass_policy,
            community=self.community,
            description="all actions pass",
            name="all actions pass",
        )
        policy.save()

        # action initiated by user without "can_add" should fail
        user = SlackUser.objects.create(username="******",
                                        community=self.community)
        self.assertEqual(
            user.has_perm("policyengine.add_policykitaddcommunitydoc"), False)
        action = PolicykitAddCommunityDoc(name="my doc",
                                          initiator=user,
                                          community=self.community)
        action.save()
        action.refresh_from_db(
        )  # test that it was saved to the db with correct proposal
        self.assertEqual(action.proposal.status, "failed")

        # action initiated by user with "can_add" should pass
        user = SlackUser.objects.create(username="******",
                                        community=self.community)
        can_add = Permission.objects.get(
            name="Can add policykit add community doc")
        user.user_permissions.add(can_add)
        self.assertTrue(
            user.has_perm("policyengine.add_policykitaddcommunitydoc"))
        action = PolicykitAddCommunityDoc(name="my other doc",
                                          initiator=user,
                                          community=self.community)
        action.save()
        action.refresh_from_db(
        )  # test that it was saved to the db with correct proposal
        self.assertEqual(action.proposal.status, "passed")