def test_instance_data_pizza(self): s = Pizza(name='S1', for_sale=True) s.save() response, json = self.do_get('/slumber_examples/Pizza/data/%s/' % s.pk) self.maxDiff = None expected = dict( _meta={'message': 'OK', 'status': 200, 'username': '******'}, type=self.url('/slumber_examples/Pizza/'), identity=self.url('/slumber_examples/Pizza/data/1/'), display='S1', operations=dict( data=self.url('/slumber_examples/Pizza/data/1/'), delete=self.url('/slumber_examples/Pizza/delete/1/'), order=self.url('/slumber_examples/Pizza/order/1/'), update=self.url('/slumber_examples/Pizza/update/1/')), fields=dict( id=dict(data=s.pk, kind='value', type='django.db.models.fields.AutoField'), for_sale=dict(data=s.for_sale, kind='value', type='django.db.models.fields.BooleanField'), max_extra_toppings=dict(data=s.max_extra_toppings, kind='value', type='django.db.models.fields.IntegerField'), name=dict(data=s.name, kind='value', type='django.db.models.fields.CharField'), exclusive_to={'data': None, 'kind': 'object', 'type': self.url('/slumber_examples/Shop/')}), data_arrays=dict( prices=self.url('/slumber_examples/Pizza/data/%s/prices/' % s.pk))) if get_version() >= "1.8": expected['fields']['exclusive_to_id'] = expected['fields']['exclusive_to'] elif get_version() >= "1.7": expected['data_arrays']['exclusive_to_id'] = self.url( '/slumber_examples/Pizza/data/1/exclusive_to_id/') self.assertEquals(json, expected)
def test_instance_data_pizzaprice(self): s = Pizza(name='p1', for_sale=True) s.save() p = PizzaPrice(pizza=s, date='2010-01-01') p.save() response, json = self.do_get('/slumber_examples/PizzaPrice/data/%s/' % p.pk) self.assertEquals(response.status_code, 200) expected = dict( _meta={'message': 'OK', 'status': 200, 'username': '******'}, type=self.url('/slumber_examples/PizzaPrice/'), identity=self.url('/slumber_examples/PizzaPrice/data/1/'), display="p1 2010-01-01", operations=dict( data=self.url('/slumber_examples/PizzaPrice/data/1/'), delete=self.url('/slumber_examples/PizzaPrice/delete/1/'), update=self.url('/slumber_examples/PizzaPrice/update/1/')), fields=dict( id={'data': 1, 'kind': 'value', 'type': 'django.db.models.fields.AutoField'}, pizza={'data': { 'type': self.url('/slumber_examples/Pizza/'), 'display':'p1', 'data': self.url('/slumber_examples/Pizza/data/1/')}, 'kind': 'object', 'type': self.url('/slumber_examples/Pizza/')}, date={'data': '2010-01-01', 'kind': 'value', 'type': 'django.db.models.fields.DateField'}, ), data_arrays={'amounts': self.url('/slumber_examples/PizzaPrice/data/1/amounts/')}) if get_version() >= "1.7": expected['data_arrays']['pizza_id'] = self.url( '/slumber_examples/PizzaPrice/data/1/pizza_id/') self.assertEquals(json, expected)
def test_instance_data_pizzaprice(self): s = Pizza(name='p1', for_sale=True) s.save() p = PizzaPrice(pizza=s, date='2010-01-01') p.save() response, json = self.do_get('/slumber_examples/PizzaPrice/data/%s/' % p.pk) self.assertEquals(response.status_code, 200) expected = dict( _meta={'message': 'OK', 'status': 200, 'username': '******'}, type=self.url('/slumber_examples/PizzaPrice/'), identity=self.url('/slumber_examples/PizzaPrice/data/1/'), display="PizzaPrice object", operations=dict( data=self.url('/slumber_examples/PizzaPrice/data/1/'), delete=self.url('/slumber_examples/PizzaPrice/delete/1/'), update=self.url('/slumber_examples/PizzaPrice/update/1/')), fields=dict( id={'data': 1, 'kind': 'value', 'type': 'django.db.models.fields.AutoField'}, pizza={'data': { 'type': self.url('/slumber_examples/Pizza/'), 'display':'p1', 'data': self.url('/slumber_examples/Pizza/data/1/')}, 'kind': 'object', 'type': self.url('/slumber_examples/Pizza/')}, date={'data': '2010-01-01', 'kind': 'value', 'type': 'django.db.models.fields.DateField'}, ), data_arrays={'amounts': self.url('/slumber_examples/PizzaPrice/data/1/amounts/')}) if get_version() >= "1.7": expected['data_arrays']['pizza_id'] = self.url( '/slumber_examples/PizzaPrice/data/1/pizza_id/') self.assertEquals(json, expected)
def test_instance_data_array(self): s = Pizza(name='P', for_sale=True) s.save() for p in range(15): PizzaPrice(pizza=s, date='2011-04-%s' % (p + 1)).save() response, json = self.do_get( '/slumber_examples/Pizza/data/%s/prices/' % s.pk) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 10, json) self.assertTrue(json.has_key('next_page'), json) self.assertEquals( json['next_page'], self.url('/slumber_examples/Pizza/data/1/prices/?start_after=6'), json['next_page']) response, json = self.do_get('/slumber_examples/Pizza/data/1/prices/', {'start_after': '6'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 5) self.assertEquals( json['page'][0], { 'type': self.url('/slumber_examples/PizzaPrice/'), 'pk': 5, 'data': self.url('/slumber_examples/PizzaPrice/data/5/'), 'display': 'PizzaPrice object' }) self.assertFalse(json.has_key('next_page'), json.keys())
class OrderTests(ConfigureUser, TestCase): def setUp(self): super(OrderTests, self).setUp() self.pizza = Pizza(name="S1", for_sale=True) self.pizza.save() self.cnx = Client() def test_order_get(self): with patch("slumber.connector._get_slumber_authn_name", lambda: "service"): pizza = self.cnx.slumber_examples.Pizza.get(id=self.pizza.id) self.assertEquals( pizza._operations["order"], "http://localhost:8000/slumber/slumber_examples/Pizza/order/1/" ) # Do an unsigned GET so there will be no user logged in response = self.client.get("/slumber/slumber_examples/Pizza/order/1/") self.assertEquals(response.status_code, 200, response.content) json = loads(response.content) self.assertEquals(json, dict(_meta=dict(status=200, message="OK"), form=dict(quantity="integer"))) def test_order_post(self): pizza = self.cnx.slumber_examples.Pizza.get(id=self.pizza.id) response = self.client.post("/slumber/slumber_examples/Pizza/order/1/", dict(quantity=3)) self.assertEquals(response.status_code, 501, response.content) def test_order_create_with_pk(self): pizza2 = self.cnx.slumber_examples.Pizza.create(id=2, name="S2", for_sale=True) pizza3 = Pizza.objects.create(name="S3", for_sale=True) self.assertEqual(Pizza.objects.count(), 3)
class OrderTests(ConfigureUser, TestCase): def setUp(self): super(OrderTests, self).setUp() self.pizza = Pizza(name='S1', for_sale=True) self.pizza.save() self.cnx = Client() def test_order_get(self): with patch('slumber.connector._get_slumber_authn_name', lambda: 'service'): pizza = self.cnx.slumber_examples.Pizza.get(id=self.pizza.id) self.assertEquals(pizza._operations['order'], 'http://localhost:8000/slumber/slumber_examples/Pizza/order/1/') # Do an unsigned GET so there will be no user logged in response = self.client.get('/slumber/slumber_examples/Pizza/order/1/') self.assertEquals(response.status_code, 200, response.content) json = loads(response.content) self.assertEquals(json, dict( _meta = dict(status=200, message="OK"), form = dict(quantity='integer'), )) def test_order_post(self): pizza = self.cnx.slumber_examples.Pizza.get(id=self.pizza.id) response = self.client.post('/slumber/slumber_examples/Pizza/order/1/', dict(quantity=3)) self.assertEquals(response.status_code, 501, response.content) def test_order_create_with_pk(self): pizza2 = self.cnx.slumber_examples.Pizza.create(id=2, name='S2', for_sale=True) pizza3 = Pizza.objects.create(name='S3', for_sale=True) self.assertEqual(Pizza.objects.count(), 3)
def test_update_instance(self): self.user.is_superuser = True self.user.save() s = Pizza(name='S1', for_sale=True) s.save() response, json = self.do_post('/slumber_examples/Pizza/update/1/', { 'name': 'New pizza'}) self.assertEquals(response.status_code, 200) n = Pizza.objects.get(pk=1) self.assertEquals(n.name, "New pizza")
def test_update_instance(self): self.user.is_superuser = True self.user.save() s = Pizza(name='S1', for_sale=True) s.save() response, json = self.do_post('/slumber_examples/Pizza/update/1/', {'name': 'New pizza'}) self.assertEquals(response.status_code, 200) n = Pizza.objects.get(pk=1) self.assertEquals(n.name, "New pizza")
def test_delete_instance(self): self.user.is_superuser = True self.user.save() s = Pizza(name='P') s.save() response, json = self.do_get('/slumber_examples/Pizza/data/%s/' % s.pk) self.assertEquals(response.status_code, 200) self.assertTrue(json['operations'].has_key('delete'), json['operations']) response, json = self.do_post(json['operations']['delete'], {}) self.assertEquals(response.status_code, 200) with self.assertRaises(Pizza.DoesNotExist): Pizza.objects.get(pk=s.pk)
def test_delete_instance_with_post(self): self.user.is_superuser = True self.user.save() s = Pizza(name='P') s.save() response, json = self.do_get('/slumber_examples/Pizza/data/%s/' % s.pk) self.assertEquals(response.status_code, 200) self.assertTrue(json['operations'].has_key('delete'), json['operations']) response, json = self.do_post(json['operations']['delete'], {}) self.assertEquals(response.status_code, 200) with self.assertRaises(Pizza.DoesNotExist): Pizza.objects.get(pk=s.pk)
def test_get_instance(self): s = Pizza(name='S1', for_sale=True) s.save() response, json = self.do_get('/slumber_examples/Pizza/') get_url = json['operations']['get'] self.assertEquals(get_url, self.url('/slumber_examples/Pizza/get/')) def check_query(query): response, json = self.do_get(get_url, query) self.assertEquals(response.status_code, 200, response) self.assertTrue(json['identity'].endswith( '/slumber_examples/Pizza/data/%s/' % s.pk), response) check_query({'pk': s.pk}) check_query({'id': s.pk}) check_query({'name': s.name})
def test_instance_data_array(self): s = Pizza(name='P', for_sale=True) s.save() for p in range(15): PizzaPrice(pizza=s, date='2011-04-%s' % (p+1)).save() response, json = self.do_get('/slumber_examples/Pizza/data/%s/prices/' % s.pk) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 10, json) self.assertTrue(json.has_key('next_page'), json) self.assertEquals(json['next_page'], self.url('/slumber_examples/Pizza/data/1/prices/?start_after=6'), json['next_page']) response, json = self.do_get('/slumber_examples/Pizza/data/1/prices/', {'start_after': '6'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 5) self.assertEquals(json['page'][0], { 'type': self.url('/slumber_examples/PizzaPrice/'), 'pk': 5, 'data': self.url('/slumber_examples/PizzaPrice/data/5/'), 'display': 'PizzaPrice object'}) self.assertFalse(json.has_key('next_page'), json.keys())
def test_model_operation_instances_twelve_instances(self): for i in range(12): Pizza(name='S%s' % i, for_sale=True).save() response, json = self.do_get('/slumber_examples/Pizza/instances/') self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 10) self.assertEquals(json['next_page'], self.url('/slumber_examples/Pizza/instances/?start_after=3')) response, json = self.do_get('/slumber_examples/Pizza/instances/', {'start_after': '3'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 2) self.assertEquals(json['next_page'], self.url('/slumber_examples/Pizza/instances/?start_after=1')) response, json = self.do_get('/slumber_examples/Pizza/instances/', {'start_after': '1'}) self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 0) self.assertFalse(json.has_key('next_page'), json)
def setUp(self): super(OrderTests, self).setUp() self.pizza = Pizza(name='S1', for_sale=True) self.pizza.save() self.cnx = Client()
def test_pizza_has_instance_proxy(self): lpizza = Pizza(name='Test pizza') lpizza.save() rpizza = client.slumber_examples.Pizza.get(pk=lpizza.pk) self.assertTrue(rpizza.has_pizza_proxy())
def setUp(self): super(OrderTests, self).setUp() self.pizza = Pizza(name="S1", for_sale=True) self.pizza.save() self.cnx = Client()
def setUp(self): self.pizza = Pizza(name='Before change') self.pizza.save() super(TestAuthnRequired, self).setUp()
def test_pizza_has_instance_proxy(self): lpizza = Pizza(name='Test pizza') lpizza.save() rpizza = client.slumber_examples.Pizza.get( pk=lpizza.pk) self.assertTrue(rpizza.has_pizza_proxy())
class TestAuthnRequired(ConfigureUser, TestCase): def setUp(self): self.pizza = Pizza(name='Before change') self.pizza.save() super(TestAuthnRequired, self).setUp() def test_directory_works_when_not_authenticated(self): response = self.client.get('/slumber/') self.assertEqual(response.status_code, 200) def test_model_requires_authentication(self): response = self.client.get('/slumber/slumber_examples/Pizza/data/%s/' % self.pizza.pk) self.assertEqual(response.status_code, 401) self.assertEqual(response.get('WWW-Authenticate', None), 'FOST Realm="Slumber"') def test_authenticated(self): response = self.signed_get( None, '/slumber/slumber_examples/Pizza/data/234234/') self.assertEqual(response.status_code, 404) json = loads(response.content) self.assertTrue( json["error"].startswith("Pizza matching query does not exist."), json["error"]) def test_model_create_requires_permission(self): response = self.signed_post(self.user.username, '/slumber/slumber_examples/Pizza/create/', {'name': 'new test pizza'}) self.assertEqual(response.status_code, 403) def test_model_create_checks_correct_permission(self): permission = Permission.objects.get(codename="add_pizza") self.user.user_permissions.add(permission) response = self.signed_post(None, '/slumber/slumber_examples/Pizza/create/', {'name': 'new test pizza'}) self.assertEqual(response.status_code, 200, response.content) def test_model_update_requires_permission(self): response = self.signed_post( self.user.username, '/slumber/slumber_examples/Pizza/update/%s/' % self.pizza.pk, {'name': 'new test pizza'}) self.assertEqual(response.status_code, 403) def test_model_update_checks_correct_permission(self): permission = Permission.objects.get(codename="change_pizza") self.user.user_permissions.add(permission) response = self.signed_post( self.user.username, '/slumber/slumber_examples/Pizza/update/%s/' % self.pizza.pk, {'name': 'new test pizza'}) self.assertEqual(response.status_code, 200, response.content) def test_model_delete_requires_permission(self): response = self.signed_post( self.user.username, '/slumber/slumber_examples/Pizza/delete/%s/' % self.pizza.pk, {}) self.assertEqual(response.status_code, 403) def test_model_delete_checks_correct_permission(self): permission = Permission.objects.get(codename="delete_pizza") self.user.user_permissions.add(permission) response = self.signed_post( self.user.username, '/slumber/slumber_examples/Pizza/delete/%s/' % self.pizza.pk, {}) self.assertEqual(response.status_code, 200, response.content)
def test_model_operation_instances_one_instance(self): Pizza(name='S1', for_sale=True).save() response, json = self.do_get('/slumber_examples/Pizza/instances/') self.assertEquals(response.status_code, 200) self.assertEquals(len(json['page']), 1)
class TestsWithPizza(ConfigureUser, TestCase): def setUp(self): super(TestsWithPizza, self).setUp() self.s = Pizza(name='S1', for_sale=True) self.s.save() self.pizza = client.slumber_examples.Pizza.get(pk=self.s.pk) def test_instance_type(self): self.assertEqual(self.s.pk, self.pizza.id) self.assertEqual(type(self.pizza).__name__, 'http://localhost:8000/slumber/slumber_examples/Pizza/data/1/') pizza_type = str(type(self.pizza)) self.assertTrue(pizza_type.endswith("slumber_examples/Pizza/data/1/'>"), pizza_type) def test_cache_ttl(self): self.assertEqual(self.pizza._CACHE_TTL, 0) def test_instance_data(self): self.assertEqual('S1', self.pizza.name) prices = self.pizza.prices self.assertEqual(len(prices), 0) self.assertTrue(self.pizza.exclusive_to is None, self.pizza.exclusive_to) try: self.pizza.not_a_field self.fail("This should have thrown an AttributeError") except AttributeError: pass def test_instance_data_with_data_array(self): for p in range(15): PizzaPrice(pizza=self.s, date='2011-04-%s' % (p+1)).save() self.assertEqual('S1', self.pizza.name) prices = self.pizza.prices self.assertEquals(len(prices), 15) first_price = prices[0] self.assertEquals(unicode(first_price), "PizzaPrice object") self.assertEquals(first_price.pizza.for_sale, True) def test_instance_data_with_nested_data_array(self): p = PizzaPrice(pizza=self.s, date='2010-06-20') p.save() PizzaSizePrice(price=p, size='s', amount='13.95').save() PizzaSizePrice(price=p, size='m', amount='15.95').save() PizzaSizePrice(price=p, size='l', amount='19.95').save() self.assertEqual('S1', self.pizza.name) self.assertEqual(len(self.pizza.prices), 1) self.assertEqual(len(self.pizza.prices[0].amounts), 3) for a in self.pizza.prices[0].amounts: self.assertTrue(a.size in ['s', 'm', 'l'], a.size) def test_instance_no_pk(self): with self.assertRaises(AssertionError): pizza = client.slumber_examples.Pizza.get() def test_2nd_pizza_comes_from_cache(self): # Force a cache read self.assertEqual(unicode(self.pizza), u"S1") # Make a 2nd alias to the same object fail = lambda *a, **f: self.fail("_InstanceConnector.__init__ called again %s, %s" % (a, f)) with patch('slumber.connector.api._InstanceConnector.__init__', fail): pizza2 = client.slumber_examples.Pizza.get(pk=self.s.pk) self.assertEqual(unicode(pizza2), u"S1") def test_pizza_not_found(self): with self.assertRaises(AssertionError): p2 = client.slumber_examples.Pizza.get(pk=2)
def setUp(self): super(TestsWithPizza, self).setUp() self.s = Pizza(name='S1', for_sale=True) self.s.save() self.pizza = client.slumber_examples.Pizza.get(pk=self.s.pk)
class TestAuthnRequired(ConfigureUser, TestCase): def setUp(self): self.pizza = Pizza(name='Before change') self.pizza.save() super(TestAuthnRequired, self).setUp() def test_directory_works_when_not_authenticated(self): response = self.client.get('/slumber/') self.assertEqual(response.status_code, 200) def test_model_requires_authentication(self): response = self.client.get('/slumber/slumber_examples/Pizza/data/%s/' % self.pizza.pk) self.assertEqual(response.status_code, 401) self.assertEqual(response.get('WWW-Authenticate', None), 'FOST Realm="Slumber"') def test_authenticated(self): response = self.signed_get(None, '/slumber/slumber_examples/Pizza/data/234234/') self.assertEqual(response.status_code, 404) json = loads(response.content) self.assertEqual(json["error"], "Pizza matching query does not exist.") def test_model_create_requires_permission(self): response = self.signed_post(self.user.username, '/slumber/slumber_examples/Pizza/create/', {'name': 'new test pizza'}) self.assertEqual(response.status_code, 403) def test_model_create_checks_correct_permission(self): permission = Permission.objects.get(codename="add_pizza") self.user.user_permissions.add(permission) response = self.signed_post(None, '/slumber/slumber_examples/Pizza/create/', {'name': 'new test pizza'}) self.assertEqual(response.status_code, 200, response.content) def test_model_update_requires_permission(self): response = self.signed_post(self.user.username, '/slumber/slumber_examples/Pizza/update/%s/' % self.pizza.pk, {'name': 'new test pizza'}) self.assertEqual(response.status_code, 403) def test_model_update_checks_correct_permission(self): permission = Permission.objects.get(codename="change_pizza") self.user.user_permissions.add(permission) response = self.signed_post(self.user.username, '/slumber/slumber_examples/Pizza/update/%s/' % self.pizza.pk, {'name': 'new test pizza'}) self.assertEqual(response.status_code, 200, response.content) def test_model_delete_requires_permission(self): response = self.signed_post(self.user.username, '/slumber/slumber_examples/Pizza/delete/%s/' % self.pizza.pk, {}) self.assertEqual(response.status_code, 403) def test_model_delete_checks_correct_permission(self): permission = Permission.objects.get(codename="delete_pizza") self.user.user_permissions.add(permission) response = self.signed_post(self.user.username, '/slumber/slumber_examples/Pizza/delete/%s/' % self.pizza.pk, {}) self.assertEqual(response.status_code, 200, response.content)