Beispiel #1
0
    def test_obj_method_decorators(self):
        """
        Tests that the L{require_obj_permissions} and
        L{require_obj_permissions_for_user} are correct.
        """
        threadlocals.push_frame()
        d = threadlocals.get_thread_locals()

        for obj in self.objs:
            d["user_kw"] = self.u1
            self.assertEqual(obj.get_val_x2(), 2)
            d["user_kw"] = self.u2
            self.assertEqual(obj.get_val_x3_other_val(), 3)
            d["test_kw"] = self.o3
            self.assertEqual(obj.get_val_x4(), 4)
            d["user_kw"] = self.u1
            self.assertEqual(obj.get_val_x5_username(), 5)

        for obj in self.objs:
            d["user_kw"] = self.u2
            self.assertRaises(PermissionDenied, obj.get_val_x2, obj)
            d["user_kw"] = self.o3
            if obj != self.o3:
                self.assertRaises(PermissionDenied, obj.get_val_x3_other_val)
            d["test_kw"] = self.objs[1]
            if obj != self.objs[1]:
                self.assertRaises(PermissionDenied, obj.get_val_x4)
            d["user_kw"] = self.u2
            self.assertRaises(PermissionDenied, obj.get_val_x5_username, obj)
Beispiel #2
0
def stop_expired_slices():
    """Find expired slices and stop them, sending an email to the owner."""

    expired_slices = Slice.objects.filter(expiration_date__lte=datetime.now(),
                                          started=True)

    for slice in expired_slices:
        threadlocals.push_frame(user=slice.owner)
        try:
            slice.stop(slice.owner)
        except:
            logger.error("Error stopping expired slice"
                         " %s: %s" % (slice, traceback.format_exc()))
        threadlocals.pop_frame()
        try:
            send_mail(
                "Your slice %s expired." % slice,
                "Your slice %s has been stopped because it expired on %s."
                "Before you restart your slice, you will need to update the "
                "slice's expiration date." % (slice, slice.expiration_date),
                from_email=settings.DEFAULT_FROM_EMAIL,
                recipient_list=[slice.owner.email],
            )
        except:
            logger.error("Error sending expired slice "
                         "email to user: %s" % traceback.format_exc())
Beispiel #3
0
def create_test_slice(test):

    add_test_aggregate_to_project(test)

    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # create the slice
    Slice.objects.all().delete()

    expiration = datetime.now() + timedelta(days=settings.MAX_SLICE_LIFE - 5)

    test_get_and_post_form(
        client=test.client,
        url=Slice.get_create_url(proj_id=test.project.id),
        params={
            "name": "slice name",
            "description": "slice description",
            "expiration_date_0": "%s" % expiration.date(),
            "expiration_date_1": expiration.time().strftime("%H:%m:%S"),
        },
    )
    test.slice = Slice.objects.all()[0]

    test.client.logout()
    threadlocals.pop_frame()
Beispiel #4
0
def start_test_slice(test):
    """Create a test setup with aggregates, users, project, and started slice.
    
    Creates two users, test.u1 and test.u2. Gives test.u1 permission to
    create aggregates. Creates two dummy aggregates with resources using u1.
    
    Gives u2 permission to create project, creates project with u2, add u1 as
    researcher member, creates a slice, add all dummy resources to slice,
    and starts it.
    
    C{test} must be a child of L{expedient.common.tests.manager.SettingsTestCase}.
    
    """

    add_resources_to_test_slice(test)

    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # start the slice
    test_get_and_post_form(
        client=test.client,
        url=test.slice.get_start_url(),
        params={},
    )
    test.slice = Slice.objects.get(pk=test.slice.pk)
    test.assertTrue(test.slice.started)

    test.client.logout()
    threadlocals.pop_frame()
Beispiel #5
0
    def test_full(self):
        """
        Test that a project with a started slice can be deleted.
        """
        
        start_test_slice(self)
        
        slice_name = "%s" % self.slice
        
        self.client.login(
            username=self.u2.username, password="******")
        threadlocals.push_frame(user=self.u2)

        # delete the project. This should delete all the slivers
        # and resources, and delete the slice. It should also stop
        # the slice (which creates a DummySliceEvent)
        response = test_get_and_post_form(
            client=self.client,
            url=self.project.get_delete_url(),
            params={},
        )
        self.assertRedirects(response, "/")

        self.assertEqual(
            DummySliceEvent.objects.filter(
                slice=slice_name, status="stopped").count(), 2)
        self.assertEqual(Sliver.objects.count(), 0)
        self.assertEqual(Project.objects.count(), 0)
        
        self.client.logout()
        threadlocals.pop_frame()
def create_test_slice(test):
    
    add_test_aggregate_to_project(test)
    
    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # create the slice
    Slice.objects.all().delete()
    
    expiration = datetime.now() + timedelta(days=settings.MAX_SLICE_LIFE - 5)
    
    test_get_and_post_form(
        client=test.client,
        url=Slice.get_create_url(proj_id=test.project.id),
        params={
            "name": "slice name",
            "description": "slice description",
            "expiration_date_0": "%s" % expiration.date(),
            "expiration_date_1": expiration.time().strftime("%H:%m:%S"),
        },
    )
    test.slice = Slice.objects.all()[0]

    test.client.logout()
    threadlocals.pop_frame()
def stop_expired_slices():
    """Find expired slices and stop them, sending an email to the owner."""
    
    expired_slices = Slice.objects.filter(
        expiration_date__lte=datetime.now(), started=True)
    
    for slice in expired_slices:
        threadlocals.push_frame(user=slice.owner)
        try:
            slice.stop(slice.owner)
        except:
            logger.error(
                "Error stopping expired slice"
                " %s: %s" % (slice, traceback.format_exc()))
        threadlocals.pop_frame()
        try:
            send_mail(
                "Your slice %s expired." % slice,
                "Your slice %s has been stopped because it expired on %s."
                "Before you restart your slice, you will need to update the "
                "slice's expiration date." % (slice, slice.expiration_date),
                from_email=settings.DEFAULT_FROM_EMAIL,
                recipient_list=[slice.owner.email],
            )
        except:
            logger.error(
                "Error sending expired slice "
                "email to user: %s" % traceback.format_exc())
def create_test_aggregates(test):

    from expedient.clearinghouse.aggregate.tests.models import DummyAggregate
    
    give_test_permissions(test)
    
    test.client.login(username=test.u1.username, password="******")
    threadlocals.push_frame(user=test.u1)
    
    test.agg1 = DummyAggregate.objects.create(
        name="Agg1",
    )
    test.agg1.create_resources()
    
    test.agg2 = DummyAggregate.objects.create(
        name="Agg2",
    )
    test.agg2.create_resources()
    
    # give permissions to use aggregates
    give_permission_to("can_use_aggregate", test.agg1, test.u2)
    give_permission_to("can_use_aggregate", test.agg2, test.u2)
    
    test.client.logout()
    threadlocals.pop_frame()
def start_test_slice(test):
    """Create a test setup with aggregates, users, project, and started slice.
    
    Creates two users, test.u1 and test.u2. Gives test.u1 permission to
    create aggregates. Creates two dummy aggregates with resources using u1.
    
    Gives u2 permission to create project, creates project with u2, add u1 as
    researcher member, creates a slice, add all dummy resources to slice,
    and starts it.
    
    C{test} must be a child of L{expedient.common.tests.manager.SettingsTestCase}.
    
    """
    
    add_resources_to_test_slice(test)
    
    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # start the slice
    test_get_and_post_form(
        client=test.client,
        url=test.slice.get_start_url(),
        params={},
    )
    test.slice = Slice.objects.get(pk=test.slice.pk)
    test.assertTrue(test.slice.started)

    test.client.logout()
    threadlocals.pop_frame()
 def test_obj_method_decorators(self):
     """
     Tests that the L{require_obj_permissions} and
     L{require_obj_permissions_for_user} are correct.
     """
     threadlocals.push_frame()
     d = threadlocals.get_thread_locals()
     
     for obj in self.objs:
         d["user_kw"] = self.u1
         self.assertEqual(obj.get_val_x2(), 2)
         d["user_kw"] = self.u2
         self.assertEqual(obj.get_val_x3_other_val(), 3)
         d["test_kw"] = self.o3
         self.assertEqual(obj.get_val_x4(), 4)
         d["user_kw"] = self.u1
         self.assertEqual(obj.get_val_x5_username(), 5)
     
     for obj in self.objs:
         d["user_kw"] = self.u2
         self.assertRaises(PermissionDenied, obj.get_val_x2, obj)
         d["user_kw"] = self.o3
         if obj != self.o3:
             self.assertRaises(PermissionDenied, obj.get_val_x3_other_val)
         d["test_kw"] = self.objs[1]
         if obj != self.objs[1]:
             self.assertRaises(PermissionDenied, obj.get_val_x4)
         d["user_kw"] = self.u2
         self.assertRaises(PermissionDenied, obj.get_val_x5_username, obj)
Beispiel #11
0
    def test_allowed_create(self, name="test"):
        """Tests that we can create a project"""

        self.client.login(username="******", password="******")
        threadlocals.push_frame(user=self.su)
        p = Project.objects.create(
            name=name,
            description="description",
        )
        threadlocals.pop_frame()
        self.client.logout()
        return p
Beispiel #12
0
    def setUp(self):
        # Add the test application
        add_dummy_agg_to_test_settings(self)

        u = User(username="******")
        u.set_password("password")
        u.save()
        self.u = u
        self.su = User.objects.create_superuser("superuser", "*****@*****.**",
                                                "password")
        threadlocals.push_frame(user=u)
        self.client.login(username="******", password="******")
    def test_delegation(self):
        """
        Tests that permission delegation works correctly.
        """
        threadlocals.push_frame()
        d = threadlocals.get_thread_locals()
        d["user_kw"] = self.u2
        d["test_kw"] = self.u1

        # Test allowed delegation
        self.assertRaises(PermissionDenied, self.objs[0].get_val_x2,
                          self.objs[0])
        self.assertRaises(PermissionDenied, self.objs[1].get_val_x2,
                          self.objs[1])
        
        give_permission_to("can_get_x2", self.objs[0], self.u2,
                           giver=self.u1, can_delegate=True)
        
        self.assertEqual(self.objs[0].get_val_x2(), 2)
        self.assertRaises(PermissionDenied, self.objs[1].get_val_x2,
                          self.objs[1])

        give_permission_to("can_get_x2", self.objs[1], self.u2,
                           giver=self.u1, can_delegate=False)

        self.assertEqual(self.objs[1].get_val_x2(), 2)
        
        # Test disallowed delegation
        self.assertRaises(PermissionCannotBeDelegated,
                          give_permission_to,
                          "can_get_x3", self.objs[0], self.u1,
                          giver=self.u2, can_delegate=False)
        
        d["user_kw"] = self.u1
        self.assertRaises(PermissionDenied, self.objs[0].get_val_x3_other_val,
                          self.objs[0])
        self.assertRaises(PermissionDenied, self.objs[1].get_val_x3_other_val,
                          self.objs[1])
        
        # Test cross delegation between types
        give_permission_to("can_get_x4", self.objs[0], self.u1,
                           giver=self.o3, can_delegate=False)
        
        self.assertEqual(self.objs[0].get_val_x4(), 4)

        # Test delegation of delegated permission
        self.assertRaises(PermissionCannotBeDelegated,
                          give_permission_to,
                          "can_get_x4", self.objs[0], self.u2,
                          giver=self.u1, can_delegate=False)
        
        give_permission_to("can_get_x2", self.objs[0], self.o3,
                           giver=self.u2, can_delegate=True)
 def setUp(self):
     # Add the test application
     add_dummy_agg_to_test_settings(self)
     
     u = User(username="******")
     u.set_password("password")
     u.save()
     self.u = u
     self.su = User.objects.create_superuser(
         "superuser", "*****@*****.**", "password")
     threadlocals.push_frame(user=u)
     self.client.login(username="******", password="******")
Beispiel #15
0
def add_resources_to_test_slice(test):

    from expedient.clearinghouse.aggregate.tests.models import DummyResource

    add_test_aggregate_to_slice(test)

    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # add resources to the slice
    for agg in [test.agg1, test.agg2]:
        for rsc in DummyResource.objects.filter(aggregate=agg):
            Sliver.objects.create(resource=rsc, slice=test.slice)

    test.client.logout()
    threadlocals.pop_frame()
def add_resources_to_test_slice(test):
    
    from expedient.clearinghouse.aggregate.tests.models import DummyResource

    add_test_aggregate_to_slice(test)
    
    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # add resources to the slice
    for agg in [test.agg1, test.agg2]:
        for rsc in DummyResource.objects.filter(aggregate=agg):
            Sliver.objects.create(resource=rsc, slice=test.slice)

    test.client.logout()
    threadlocals.pop_frame()
def create_test_project(test):
    
    create_test_aggregates(test)
    
    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # create the project
    Project.objects.all().delete()
    test_get_and_post_form(
        client=test.client,
        url=Project.get_create_url(),
        params={"name": "project name", "description": "project description"},
    )
    test.project = Project.objects.all()[0]

    test.client.logout()
    threadlocals.pop_frame()
Beispiel #18
0
    def test_slice_expiration_form(self):
        add_test_aggregate_to_project(self)

        self.client.login(username=self.u2.username, password="******")
        threadlocals.push_frame(user=self.u2)

        expiration = datetime.now() \
            + timedelta(days=settings.MAX_SLICE_LIFE + 5)

        response = test_get_and_post_form(
            client=self.client,
            url=Slice.get_create_url(proj_id=self.project.id),
            params={
                "name": "slice name",
                "description": "slice description",
                "expiration_date_0": "%s" % expiration.date(),
                "expiration_date_1": expiration.time().strftime("%H:%m:%S"),
            },
        )

        self.assertContains(
            response,
            "The entered date is too late. Maximum is",
        )

        response = test_get_and_post_form(
            client=self.client,
            url=Slice.get_create_url(proj_id=self.project.id),
            params={
                "name": "slice name",
                "description": "slice description",
                "expiration_date_0": "xyaz",
                "expiration_date_1": "%s" % expiration.time(),
            },
        )

        self.assertContains(
            response,
            "Enter a valid date",
        )

        self.client.logout()
        threadlocals.pop_frame()
    def test_slice_expiration_form(self):
        add_test_aggregate_to_project(self)
        
        self.client.login(
            username=self.u2.username, password="******")
        threadlocals.push_frame(user=self.u2)
        
        expiration = datetime.now() \
            + timedelta(days=settings.MAX_SLICE_LIFE + 5)
        
        response = test_get_and_post_form(
            client=self.client,
            url=Slice.get_create_url(proj_id=self.project.id),
            params={
                "name": "slice name",
                "description": "slice description",
                "expiration_date_0": "%s" % expiration.date(),
                "expiration_date_1": expiration.time().strftime("%H:%m:%S"),
            },
        )
        
        self.assertContains(
            response, "The entered date is too late. Maximum is",
        )
        
        response = test_get_and_post_form(
            client=self.client,
            url=Slice.get_create_url(proj_id=self.project.id),
            params={
                "name": "slice name",
                "description": "slice description",
                "expiration_date_0": "xyaz",
                "expiration_date_1": "%s" % expiration.time(),
            },
        )
        
        self.assertContains(
            response, "Enter a valid date",
        )

        self.client.logout()
        threadlocals.pop_frame()
def add_test_project_member(test):
    
    create_test_project(test)
    
    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # add a member
    researcher = ProjectRole.objects.get(project=test.project, name="researcher")
    test_get_and_post_form(
        client=test.client,
        url=test.project.get_member_add_url(),
        params={"user": test.u3.id, "roles": researcher.id},
    )
    
    test.assertEqual(test.project.owners.count(), 1)
    test.assertEqual(test.project.members.count(), 2)

    test.client.logout()
    threadlocals.pop_frame()
Beispiel #21
0
def create_test_aggregates(test):

    from expedient.clearinghouse.aggregate.tests.models import DummyAggregate

    give_test_permissions(test)

    test.client.login(username=test.u1.username, password="******")
    threadlocals.push_frame(user=test.u1)

    test.agg1 = DummyAggregate.objects.create(name="Agg1", )
    test.agg1.create_resources()

    test.agg2 = DummyAggregate.objects.create(name="Agg2", )
    test.agg2.create_resources()

    # give permissions to use aggregates
    give_permission_to("can_use_aggregate", test.agg1, test.u2)
    give_permission_to("can_use_aggregate", test.agg2, test.u2)

    test.client.logout()
    threadlocals.pop_frame()
Beispiel #22
0
 def setUp(self):
     common_setup(self)
     
     self.su = User.objects.create_superuser(
         "superuser", "*****@*****.**", "password")
     
     self.client.login(username="******", password="******")
     threadlocals.push_frame(user=self.su)
     
     self.agg1 = DummyAggregate.objects.create(
         name="Agg1",
     )
     self.agg1.create_resources()
     
     self.agg2 = DummyAggregate.objects.create(
         name="Agg2",
     )
     self.agg2.create_resources()
     
     self.client.logout()
     threadlocals.pop_frame()
Beispiel #23
0
def create_test_project(test):

    create_test_aggregates(test)

    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # create the project
    Project.objects.all().delete()
    test_get_and_post_form(
        client=test.client,
        url=Project.get_create_url(),
        params={
            "name": "project name",
            "description": "project description"
        },
    )
    test.project = Project.objects.all()[0]

    test.client.logout()
    threadlocals.pop_frame()
def add_test_aggregate_to_slice(test):
    
    create_test_slice(test)
    
    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # add the aggregate to the slice
    test_get_and_post_form(
        client=test.client,
        url=test.slice.get_agg_add_url(),
        params={"id": "%s" % test.agg1.id},
    )
    test_get_and_post_form(
        client=test.client,
        url=test.slice.get_agg_add_url(),
        params={"id": "%s" % test.agg2.id},
    )
    test.assertEqual(test.slice.aggregates.count(), 2)

    test.client.logout()
    threadlocals.pop_frame()
Beispiel #25
0
def add_test_aggregate_to_slice(test):

    create_test_slice(test)

    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # add the aggregate to the slice
    test_get_and_post_form(
        client=test.client,
        url=test.slice.get_agg_add_url(),
        params={"id": "%s" % test.agg1.id},
    )
    test_get_and_post_form(
        client=test.client,
        url=test.slice.get_agg_add_url(),
        params={"id": "%s" % test.agg2.id},
    )
    test.assertEqual(test.slice.aggregates.count(), 2)

    test.client.logout()
    threadlocals.pop_frame()
 def test_disallowed_edit(self):
     """
     Test that we cannot edit or delete without permission.
     """
     threadlocals.push_frame(user=self.su)
     agg = DummyAggregate.objects.create(
         name="dummy agg",
         description="aggregate description",
         location="Stanford, CA",
     )
     threadlocals.pop_frame()
     
     self.assertRaises(PermissionDenied, agg.save)
     self.assertRaises(PermissionDenied, agg.delete)
     
     # Try delete using a post
     response = test_get_and_post_form(
         client=self.client,
         url=agg.get_delete_url(next="/"),
         params={},
     )
     self.assertEqual(response.status_code, 302)
     self.assertTrue("/permissions/can_edit_aggregate/" in response["location"])
Beispiel #27
0
    def test_allowed_delete_with_started_slice(self):
        '''
        Tests that we can delete an aggregate that is in a started slice.
        '''

        start_test_slice(self)

        self.client.login(username=self.u1.username, password="******")
        threadlocals.push_frame(user=self.u1)

        # delete the aggregate. This should delete all the slivers
        # and resources, and create a DummySliceEvent to that effect.
        response = test_get_and_post_form(
            client=self.client,
            url=self.agg1.get_delete_url(next="/"),
            params={},
        )
        self.assertRedirects(response, "/")

        self.assertEqual(DummyAggregate.objects.count(), 1)
        self.assertEqual(Sliver.objects.count(), 3)
        self.assertEqual(
            DummySliceEvent.objects.filter(slice="%s" % self.slice,
                                           status="stopped").count(), 1)
Beispiel #28
0
    def test_disallowed_edit(self):
        """
        Test that we cannot edit or delete without permission.
        """
        threadlocals.push_frame(user=self.su)
        agg = DummyAggregate.objects.create(
            name="dummy agg",
            description="aggregate description",
            location="Stanford, CA",
        )
        threadlocals.pop_frame()

        self.assertRaises(PermissionDenied, agg.save)
        self.assertRaises(PermissionDenied, agg.delete)

        # Try delete using a post
        response = test_get_and_post_form(
            client=self.client,
            url=agg.get_delete_url(next="/"),
            params={},
        )
        self.assertEqual(response.status_code, 302)
        self.assertTrue(
            "/permissions/can_edit_aggregate/" in response["location"])
Beispiel #29
0
def add_test_project_member(test):

    create_test_project(test)

    test.client.login(username=test.u2.username, password="******")
    threadlocals.push_frame(user=test.u2)

    # add a member
    researcher = ProjectRole.objects.get(project=test.project,
                                         name="researcher")
    test_get_and_post_form(
        client=test.client,
        url=test.project.get_member_add_url(),
        params={
            "user": test.u3.id,
            "roles": researcher.id
        },
    )

    test.assertEqual(test.project.owners.count(), 1)
    test.assertEqual(test.project.members.count(), 2)

    test.client.logout()
    threadlocals.pop_frame()
    def test_allowed_delete_with_started_slice(self):
        '''
        Tests that we can delete an aggregate that is in a started slice.
        '''
        
        start_test_slice(self)
        
        self.client.login(username=self.u1.username, password="******")
        threadlocals.push_frame(user=self.u1)

        # delete the aggregate. This should delete all the slivers
        # and resources, and create a DummySliceEvent to that effect.
        response = test_get_and_post_form(
            client=self.client,
            url=self.agg1.get_delete_url(next="/"),
            params={},
        )
        self.assertRedirects(response, "/")
        
        self.assertEqual(DummyAggregate.objects.count(), 1)
        self.assertEqual(Sliver.objects.count(), 3)
        self.assertEqual(
            DummySliceEvent.objects.filter(
                slice="%s" % self.slice, status="stopped").count(), 1)
Beispiel #31
0
    def setUp(self):
        """Create a project and test permissions and permittees"""
        self.su = User.objects.create_superuser("superuser", "*****@*****.**",
                                                "password")
        self.u1 = User.objects.create_user("user1", "*****@*****.**", "password")
        self.u2 = User.objects.create_user("user2", "*****@*****.**", "password")
        self.u3 = User.objects.create_user("user3", "*****@*****.**", "password")

        self.client.login(username="******", password="******")
        threadlocals.push_frame(user=self.su)

        self.project = Project.objects.create(name="projectX",
                                              description="blabla")
        self.projectY = Project.objects.create(name="projectY",
                                               description="blabla")

        self.client.logout()
        threadlocals.pop_frame()

        create_permission("perm1")
        create_permission("perm2")
        create_permission("perm3")
        create_permission("perm4")

        self.obj_perm1 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("perm1", self.project)[0]
        self.obj_perm2 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("perm2", self.project)[0]
        self.obj_perm3 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("perm3", self.project)[0]
        self.obj_perm4 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("perm4", self.project)[0]

        self.role1 = ProjectRole.objects.create(
            name="role1",
            project=self.project,
        )
        self.role1.obj_permissions.add(self.obj_perm1)

        self.role2 = ProjectRole.objects.create(
            name="role2",
            project=self.project,
        )
        self.role2.obj_permissions.add(self.obj_perm2)

        self.role3 = ProjectRole.objects.create(
            name="role3",
            project=self.project,
        )
        self.role3.obj_permissions.add(self.obj_perm1)
        self.role3.obj_permissions.add(self.obj_perm3)

        create_permission("permY1")
        create_permission("permY2")
        create_permission("permY3")
        create_permission("permY4")

        self.obj_permY1 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("permY1", self.projectY)[0]
        self.obj_permY2 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("permY2", self.projectY)[0]
        self.obj_permY3 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("permY3", self.projectY)[0]
        self.obj_permY4 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("permY4", self.projectY)[0]

        self.roleY1 = ProjectRole.objects.create(
            name="roleY1",
            project=self.projectY,
        )
        self.roleY1.obj_permissions.add(self.obj_permY1)

        self.roleY2 = ProjectRole.objects.create(
            name="roleY2",
            project=self.projectY,
        )
        self.roleY2.obj_permissions.add(self.obj_permY2)

        self.roleY3 = ProjectRole.objects.create(
            name="roleY3",
            project=self.projectY,
        )
        self.roleY3.obj_permissions.add(self.obj_permY1)
        self.roleY3.obj_permissions.add(self.obj_permY3)
    def setUp(self):
        """Create a project and test permissions and permittees"""
        self.su = User.objects.create_superuser(
            "superuser", "*****@*****.**", "password")
        self.u1 = User.objects.create_user(
            "user1", "*****@*****.**", "password")
        self.u2 = User.objects.create_user(
            "user2", "*****@*****.**", "password")
        self.u3 = User.objects.create_user(
            "user3", "*****@*****.**", "password")
        
        
        self.client.login(username="******", password="******")
        threadlocals.push_frame(user=self.su)

        self.project = Project.objects.create(
            name="projectX", description="blabla")
        self.projectY = Project.objects.create(
            name="projectY", description="blabla")
        
        self.client.logout()
        threadlocals.pop_frame()
        
        create_permission("perm1")
        create_permission("perm2")
        create_permission("perm3")
        create_permission("perm4")
        
        self.obj_perm1 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("perm1", self.project)[0]
        self.obj_perm2 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("perm2", self.project)[0]
        self.obj_perm3 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("perm3", self.project)[0]
        self.obj_perm4 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("perm4", self.project)[0]
        
        self.role1 = ProjectRole.objects.create(
            name="role1", project=self.project,
        )
        self.role1.obj_permissions.add(self.obj_perm1)
        
        self.role2 = ProjectRole.objects.create(
            name="role2", project=self.project,
        )
        self.role2.obj_permissions.add(self.obj_perm2)

        self.role3 = ProjectRole.objects.create(
            name="role3", project=self.project,
        )
        self.role3.obj_permissions.add(self.obj_perm1)
        self.role3.obj_permissions.add(self.obj_perm3)
        
        create_permission("permY1")
        create_permission("permY2")
        create_permission("permY3")
        create_permission("permY4")
        
        self.obj_permY1 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("permY1", self.projectY)[0]
        self.obj_permY2 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("permY2", self.projectY)[0]
        self.obj_permY3 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("permY3", self.projectY)[0]
        self.obj_permY4 = ObjectPermission.objects.\
            get_or_create_for_object_or_class("permY4", self.projectY)[0]
        
        self.roleY1 = ProjectRole.objects.create(
            name="roleY1", project=self.projectY,
        )
        self.roleY1.obj_permissions.add(self.obj_permY1)
        
        self.roleY2 = ProjectRole.objects.create(
            name="roleY2", project=self.projectY,
        )
        self.roleY2.obj_permissions.add(self.obj_permY2)

        self.roleY3 = ProjectRole.objects.create(
            name="roleY3", project=self.projectY,
        )
        self.roleY3.obj_permissions.add(self.obj_permY1)
        self.roleY3.obj_permissions.add(self.obj_permY3)
Beispiel #33
0
    def test_delegation(self):
        """
        Tests that permission delegation works correctly.
        """
        threadlocals.push_frame()
        d = threadlocals.get_thread_locals()
        d["user_kw"] = self.u2
        d["test_kw"] = self.u1

        # Test allowed delegation
        self.assertRaises(PermissionDenied, self.objs[0].get_val_x2,
                          self.objs[0])
        self.assertRaises(PermissionDenied, self.objs[1].get_val_x2,
                          self.objs[1])

        give_permission_to("can_get_x2",
                           self.objs[0],
                           self.u2,
                           giver=self.u1,
                           can_delegate=True)

        self.assertEqual(self.objs[0].get_val_x2(), 2)
        self.assertRaises(PermissionDenied, self.objs[1].get_val_x2,
                          self.objs[1])

        give_permission_to("can_get_x2",
                           self.objs[1],
                           self.u2,
                           giver=self.u1,
                           can_delegate=False)

        self.assertEqual(self.objs[1].get_val_x2(), 2)

        # Test disallowed delegation
        self.assertRaises(PermissionCannotBeDelegated,
                          give_permission_to,
                          "can_get_x3",
                          self.objs[0],
                          self.u1,
                          giver=self.u2,
                          can_delegate=False)

        d["user_kw"] = self.u1
        self.assertRaises(PermissionDenied, self.objs[0].get_val_x3_other_val,
                          self.objs[0])
        self.assertRaises(PermissionDenied, self.objs[1].get_val_x3_other_val,
                          self.objs[1])

        # Test cross delegation between types
        give_permission_to("can_get_x4",
                           self.objs[0],
                           self.u1,
                           giver=self.o3,
                           can_delegate=False)

        self.assertEqual(self.objs[0].get_val_x4(), 4)

        # Test delegation of delegated permission
        self.assertRaises(PermissionCannotBeDelegated,
                          give_permission_to,
                          "can_get_x4",
                          self.objs[0],
                          self.u2,
                          giver=self.u1,
                          can_delegate=False)

        give_permission_to("can_get_x2",
                           self.objs[0],
                           self.o3,
                           giver=self.u2,
                           can_delegate=True)
Beispiel #34
0
    def test_static_non_of_links(self):
        """
        Tests that we can add/delete openflow-to-openflow static links.
        """

        self.test_create_aggregates()

        i = 0
        url = reverse("openflow_aggregate_add_links", args=[i + 1])

        iface = OpenFlowInterface.objects.filter(aggregate__pk=i + 1)[0]

        threadlocals.push_frame(user=self.test_user)
        self.generic_agg = Aggregate.objects.create(name="TestAggregate")

        self.non_of_rsc = []
        resource = Resource.objects.create(
            name="TestResource%s" % i,
            aggregate=self.generic_agg,
        )

        self.assertEqual(
            NonOpenFlowConnection.objects.filter(of_iface=iface,
                                                 resource=resource).count(), 0)

        response = test_get_and_post_form(
            self.client,
            url,
            dict(
                of_iface=iface.pk,
                resource=resource.pk,
            ),
            del_params=["delete_links", "add_links"])
        self.assertRedirects(
            response,
            expected_url=url,
        )

        cnxn = NonOpenFlowConnection.objects.get(of_iface=iface,
                                                 resource=resource)

        # check that the new connection shows up
        response = self.client.get(url)
        d = pq(response.content, parser="html")
        cnxns = d("input[name='non_of_connections']")
        self.assertEqual(len(cnxns), 1)

        # disable an interface to see if the connection still shows
        iface.available = False
        iface.save()
        response = self.client.get(url)
        d = pq(response.content, parser="html")
        cnxns = d("input[name='non_of_connections']")
        self.assertEqual(len(cnxns), 0)

        # reenable the interface to see if the connection shows back up
        iface.available = True
        iface.save()
        response = self.client.get(url)
        d = pq(response.content, parser="html")
        cnxns = d("input[name='non_of_connections']")
        self.assertEqual(len(cnxns), 1)

        # test delete
        response = test_get_and_post_form(
            self.client,
            url,
            dict(non_of_connections=[cnxn.pk], ),
            del_params=["add_links", "add_other_links"],
        )
        print response
        self.assertRedirects(
            response,
            expected_url=url,
        )

        self.assertEqual(
            NonOpenFlowConnection.objects.filter(of_iface=iface,
                                                 resource=resource).count(), 0)
    def test_static_non_of_links(self):
        """
        Tests that we can add/delete openflow-to-openflow static links.
        """
        
        self.test_create_aggregates()
        
        i = 0
        url = reverse("openflow_aggregate_add_links", args=[i+1])
        
        iface = OpenFlowInterface.objects.filter(aggregate__pk=i+1)[0]

        threadlocals.push_frame(user=self.test_user)
        self.generic_agg = Aggregate.objects.create(
            name="TestAggregate")
        
        self.non_of_rsc = []
        resource = Resource.objects.create(
            name="TestResource%s" % i,
            aggregate=self.generic_agg,
        )
        
        self.assertEqual(
            NonOpenFlowConnection.objects.filter(
                of_iface=iface, resource=resource).count(), 0)
        
        response = test_get_and_post_form(
            self.client, url,
            dict(
                of_iface=iface.pk,
                resource=resource.pk,
            ),
            del_params=["delete_links", "add_links"]
        )
        self.assertRedirects(
            response,
            expected_url=url,
        )
        
        cnxn = NonOpenFlowConnection.objects.get(
            of_iface=iface, resource=resource)

        # check that the new connection shows up
        response = self.client.get(url)
        d = pq(response.content, parser="html")
        cnxns = d("input[name='non_of_connections']")
        self.assertEqual(len(cnxns), 1)
        
        # disable an interface to see if the connection still shows
        iface.available = False
        iface.save()
        response = self.client.get(url)
        d = pq(response.content, parser="html")
        cnxns = d("input[name='non_of_connections']")
        self.assertEqual(len(cnxns), 0)
        
        # reenable the interface to see if the connection shows back up
        iface.available = True
        iface.save()
        response = self.client.get(url)
        d = pq(response.content, parser="html")
        cnxns = d("input[name='non_of_connections']")
        self.assertEqual(len(cnxns), 1)

        # test delete
        response = test_get_and_post_form(
            self.client, url,
            dict(
                non_of_connections=[cnxn.pk],
            ),
            del_params=["add_links", "add_other_links"],
        )
        print response
        self.assertRedirects(
            response,
            expected_url=url,
        )
        
        self.assertEqual(
            NonOpenFlowConnection.objects.filter(
                of_iface=iface, resource=resource).count(), 0)