def test_create_pizza(self): response1, json1 = post('/slumber/slumber_examples/Pizza/create/', { 'id': 1, 'name': 'Test P', 'for_sale': True}) self.assertEqual(response1.status_code, 200) response2, json2 = post('/slumber/slumber_examples/Pizza/create/', { 'id': 1, 'name': 'Test P', 'for_sale': True}) self.assertEqual(response2.status_code, 200) self.assertEqual(Pizza.objects.all().count(), 1)
def test_create_pizza_twice(self): response1, json1 = post('/slumber/slumber_examples/Pizza/create/', { 'id': 1, 'name': 'Test P', 'for_sale': True}) self.assertEqual(response1.status_code, 200) self.assertTrue(json1['created'], json1) response2, json2 = post('/slumber/slumber_examples/Pizza/create/', { 'id': 1, 'name': 'Test P', 'for_sale': True}) self.assertEqual(response2.status_code, 200) self.assertFalse(json2['created'], json2) self.assertEqual(Pizza.objects.all().count(), 1)
def test_create_pizza_twice(self): response1, json1 = post( "/slumber/slumber_examples/Pizza/create/", {"id": 1, "name": "Test P", "for_sale": True} ) self.assertEqual(response1.status_code, 200) self.assertTrue(json1["created"], json1) response2, json2 = post( "/slumber/slumber_examples/Pizza/create/", {"id": 1, "name": "Test P", "for_sale": True} ) self.assertEqual(response2.status_code, 200) self.assertFalse(json2["created"], json2) self.assertEqual(Pizza.objects.all().count(), 1)
def test_create_pizza_crust(self): response1, json1 = post( "/slumber/slumber_examples/PizzaCrust/create/", {"code": "org", "full_name": "original crust"} ) self.assertEqual(response1.status_code, 200) self.assertTrue(json1["created"], json1) self.assertEqual(PizzaCrust.objects.all().count(), 1)
def test_fake(self): def _post(self, url, data, **kw): return _response_fake() with patch('slumber.connector.ua.FakeClient.post', _post): response, json = post('/local/', {}) self.assertEqual(json, 123)
def test_real(self): def _request(_self, url, method, body, headers={}): self.assertEqual(body, '{"data": 23}') return _response_httplib2(), "123" with patch('slumber.connector.ua.Http.request', _request): response, json = post('http://example.com/', { 'data': 23}) self.assertEqual(json, 123)
def test_real(self): def _request(_self, url, method, body, headers={}): self.assertEqual(body, '{"data": 23}') return _response_httplib2(), "123" with patch('slumber.connector.ua.Http.request', _request): response, json = post('http://example.com/', {'data': 23}) self.assertEqual(json, 123)
def test_create_pizza(self): response, json = post("/slumber/slumber_examples/Pizza/create/", {"id": 1, "name": "Test P", "for_sale": True}) self.assertEqual(response.status_code, 200) self.assertTrue(json.has_key("pk"), json) self.assertEqual(json["pk"], 1, json) self.assertTrue(json.has_key("created"), json) self.assertTrue(json["created"], json) self.assertEqual(Pizza.objects.all().count(), 1)
def test_model_operation_with_mock_ua(self, expect): expect.get('http://pizzas/app/Model/test-op/', {'test': 'item'}) expect.post('http://pizzas/app/Model/test-op/', {'test': 'item'}, {'item': 'test'}) self.assertEqual(len(expect.expectations), 2) response1, json1 = get(client.pizzas.app.Model._operations['test-op']) self.assertEqual(json1, dict(test='item')) response2, json2 = post(client.pizzas.app.Model._operations['test-op'], json1) self.assertEqual(json2, dict(item='test'))
def test_create_pizza(self): response, json = post('/slumber/slumber_examples/Pizza/create/', { 'id': 1, 'name': 'Test P', 'for_sale': True}) self.assertEqual(response.status_code, 200) self.assertTrue(json.has_key('pk'), json) self.assertEqual(json['pk'], 1, json) self.assertTrue(json.has_key('created'), json) self.assertTrue(json['created'], json) self.assertEqual(Pizza.objects.all().count(), 1)
def test_model_operation_with_mock_ua_no_post_data_match(self, expect): expect.get('http://pizzas/app/Model/test-op/', {'test': 'item'}) expect.post('http://pizzas/app/Model/test-op/', None, {'item': 'test'}) self.assertEqual(len(expect.expectations), 2) response1, json1 = get(client.pizzas.app.Model._operations['test-op']) self.assertEqual(json1, dict(test='item')) response2, json2 = post(client.pizzas.app.Model._operations['test-op'], json1) self.assertEqual(json2, dict(item='test'))
def test_create_pizza_crust(self): response1, json1 = post('/slumber/slumber_examples/PizzaCrust/create/', { 'code': 'org', 'full_name': 'original crust' }) self.assertEqual(response1.status_code, 200) self.assertTrue(json1['created'], json1) self.assertEqual(PizzaCrust.objects.all().count(), 1)
def test_update_pizza(self): self.cnx = Client() p_id, p_sale = 1 , False response1, json1 = post('/slumber/slumber_examples/Pizza/create/',{ 'id':p_id, 'name':'Test P', 'for_sale': True}) self.assertEqual(response1.status_code, 200) pizza = self.cnx.slumber_examples.Pizza.get(pk = p_id) self.cnx.slumber_examples.Pizza.update(pizza, for_sale = p_sale) pizza = self.cnx.slumber_examples.Pizza.get(pk = p_id) self.assertEqual(pizza.for_sale, p_sale)
def test_update_pizza(self): self.cnx = Client() p_id, p_sale = 1, False response1, json1 = post( "/slumber/slumber_examples/Pizza/create/", {"id": p_id, "name": "Test P", "for_sale": True} ) self.assertEqual(response1.status_code, 200) pizza = self.cnx.slumber_examples.Pizza.get(pk=p_id) self.cnx.slumber_examples.Pizza.update(pizza, for_sale=p_sale) pizza = self.cnx.slumber_examples.Pizza.get(pk=p_id) self.assertEqual(pizza.for_sale, p_sale)
def authenticate(self, **kwargs): """Allow a forwarded request for authentication. """ # We're accessing attributes that are provided by the other types # pylint: disable = E1101 _, json = post(self._operations['authenticate'], kwargs) if json['authenticated']: # Pylint can't see the __call__ implemented in another base class # pylint: disable = E1102 remote_user = self(urljoin(self._url, json['user']['url']), json['user']['display_name']) return attach_to_local_user(remote_user)
def create(self, **kwargs): """Implements the client side for the model `create` operator. """ url = urljoin(self._url, self._operations['create']) _, json = post(url, kwargs) return get_instance_from_data(url, json)
def test_no_post_expectation(self, expect): post('/', {})
def test_no_expectated_get_got_post(self, expect): expect.get('/', {}) post('/', {})
def test_404_allowed_for_fake(self): response, json = post('/slumber/does-not-exist/', {}, [404]) self.assertEqual(response.status_code, 404)
def test_expect_wrong_post_data(self, expect): expect.post('/', {'data': True}, {}) post('/', {'data': False})
def do_post(): with patch('slumber.connector.ua._calculate_signature', self.signature_with_username): post('/slumber/', {})
def update(self, instance_connector, **kwargs): """Implements the client side for the model 'update' operator. """ url = urljoin(self._url, instance_connector._operations['update']) _, json = post(url, kwargs) return json
def test_create_pizza_crust(self): response1, json1 = post('/slumber/slumber_examples/PizzaCrust/create/', { 'code': 'org', 'full_name': 'original crust'}) self.assertEqual(response1.status_code, 200) self.assertTrue(json1['created'], json1) self.assertEqual(PizzaCrust.objects.all().count(), 1)