Package expedient :: Package clearinghouse :: Package aggregate :: Package tests :: Module tests
[hide private]
[frames] | no frames]

Source Code for Module expedient.clearinghouse.aggregate.tests.tests

  1  ''' 
  2  Created on Jun 11, 2010 
  3   
  4  @author: jnaous 
  5  ''' 
  6   
  7  from django.contrib.auth.models import User 
  8  from django.core.urlresolvers import reverse 
  9  from expedient.common.tests.manager import SettingsTestCase 
 10  from expedient.clearinghouse.aggregate.tests.models import DummyAggregate,\ 
 11      DummyResource, DummySliceEvent 
 12  from expedient.common.middleware import threadlocals 
 13  from expedient.common.permissions.shortcuts import give_permission_to 
 14  from expedient.clearinghouse.aggregate.models import Aggregate 
 15  from expedient.common.permissions.exceptions import PermissionDenied 
 16  from expedient.common.tests.client import test_get_and_post_form 
 17  from expedient.clearinghouse.resources.models import Sliver 
 18  from expedient.clearinghouse.utils import start_test_slice,\ 
 19      add_dummy_agg_to_test_settings 
 20   
21 -class Tests(SettingsTestCase):
22 - def setUp(self):
23 # Add the test application 24 add_dummy_agg_to_test_settings(self) 25 26 u = User(username="test") 27 u.set_password("password") 28 u.save() 29 self.u = u 30 self.su = User.objects.create_superuser( 31 "superuser", "su@su.su", "password") 32 threadlocals.push_frame(user=u) 33 self.client.login(username="test", password="password")
34
35 - def tearDown(self):
37
38 - def test_disallowed_create(self):
39 """ 40 Test that we cannot create an aggregate without permission. 41 """ 42 response = self.client.post( 43 reverse("tests_aggregate_create"), 44 data=dict( 45 name="dummy agg", 46 description="aggregate description", 47 location="Stanford, CA", 48 ) 49 ) 50 self.assertEqual(response.status_code, 302) 51 self.assertTrue("/permissions/can_add_aggregate/" in response["location"])
52
53 - def test_allowed_create(self):
54 """ 55 Test that we can create an aggregate. 56 """ 57 give_permission_to("can_add_aggregate", Aggregate, self.u) 58 response = self.client.post( 59 reverse("tests_aggregate_create"), 60 data=dict( 61 name="dummy agg", 62 description="aggregate description", 63 location="Stanford, CA", 64 ) 65 ) 66 self.assertRedirects(response, "/", 67 msg_prefix="response was %s" % response) 68 self.assertEqual(DummyAggregate.objects.all().count(), 1)
69
70 - def test_disallowed_edit(self):
71 """ 72 Test that we cannot edit or delete without permission. 73 """ 74 threadlocals.push_frame(user=self.su) 75 agg = DummyAggregate.objects.create( 76 name="dummy agg", 77 description="aggregate description", 78 location="Stanford, CA", 79 ) 80 threadlocals.pop_frame() 81 82 self.assertRaises(PermissionDenied, agg.save) 83 self.assertRaises(PermissionDenied, agg.delete) 84 85 # Try delete using a post 86 response = test_get_and_post_form( 87 client=self.client, 88 url=agg.get_delete_url(next="/"), 89 params={}, 90 ) 91 self.assertEqual(response.status_code, 302) 92 self.assertTrue("/permissions/can_edit_aggregate/" in response["location"])
93
94 - def test_allowed_delete(self):
95 """ 96 Tests that delete works when given permission. 97 """ 98 give_permission_to("can_add_aggregate", Aggregate, self.u) 99 agg = DummyAggregate.objects.create( 100 name="dummy agg", 101 description="aggregate description", 102 location="Stanford, CA", 103 ) 104 response = test_get_and_post_form( 105 client=self.client, 106 url=agg.get_delete_url(next="/"), 107 params={}, 108 ) 109 self.assertRedirects(response, "/")
110
112 ''' 113 Tests that we can delete an aggregate that is in a started slice. 114 ''' 115 116 start_test_slice(self) 117 118 self.client.login(username=self.u1.username, password="password") 119 threadlocals.push_frame(user=self.u1) 120 121 # delete the aggregate. This should delete all the slivers 122 # and resources, and create a DummySliceEvent to that effect. 123 response = test_get_and_post_form( 124 client=self.client, 125 url=self.agg1.get_delete_url(next="/"), 126 params={}, 127 ) 128 self.assertRedirects(response, "/") 129 130 self.assertEqual(DummyAggregate.objects.count(), 1) 131 self.assertEqual(Sliver.objects.count(), 3) 132 self.assertEqual( 133 DummySliceEvent.objects.filter( 134 slice="%s" % self.slice, status="stopped").count(), 1)
135
136 - def test_allowed_edit(self):
137 """ 138 Tests that we can edit an existing aggregate. 139 """ 140 give_permission_to("can_add_aggregate", Aggregate, self.u) 141 agg = DummyAggregate.objects.create( 142 name="dummy agg", 143 description="aggregate description", 144 location="Stanford, CA", 145 ) 146 response = self.client.post( 147 reverse("tests_aggregate_edit", kwargs={"agg_id": 1}), 148 data=dict( 149 name="Edited dummy agg", 150 description="Edited aggregate description", 151 location="Stanford, CA", 152 ) 153 ) 154 self.assertRedirects(response, "/", 155 msg_prefix="response was %s" % response)
156