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_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 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 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 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 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
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()
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()
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 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 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()
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"])
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"])
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 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)
def tearDown(self): threadlocals.pop_frame()