def test_booking_price_three_days_package(self): started_at = self._next_weekday(0) ended_at = started_at + timedelta(days=3) response = self.client.get( reverse('booking_price', args=[ 'rent/bebe/mobilier-bebe/lits/', 'perceuse-visseuse-philips', '1' ]), { '0-started_at_0': started_at.strftime("%d/%m/%Y"), '0-started_at_1': '08:00:00', '0-ended_at_0': ended_at.strftime("%d/%m/%Y"), '0-ended_at_1': '08:00:00', '0-quantity': 1 }, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEquals(response.status_code, 200) json_res = json.loads(response.content) self.assertTrue('duration' in json_res) self.assertTrue('total_price' in json_res) self.assertEquals(json_res['unit_name'], '3jours', json_res) self.assertEquals( json_res['total_price'], json.loads(json.dumps(smart_str(currency(D('48.00'))))), json_res) self.assertEquals(json_res['duration'], '3 ' + "jours", json_res)
def test_product_creation(self): f = open(local_path('../fixtures/bentley.jpg')) post_data = { 'summary': 'Tondeuse', 'description': 'Merveilleuse tondeuse', 'quantity': 1, 'price': "15.2", 'deposit_amount': 150, 'picture': encodestring(f.read()), 'category': '/api/1.0/category/1/', 'address': '/api/1.0/address/1/' } request = self._get_request(method='POST') response = self.client.post(self._resource_url('product'), data=json.dumps(post_data), content_type='application/json', **self._get_headers(request)) self.assertEquals(response.status_code, 201) self.assertTrue('Location' in response) num = Product.objects.count() product = Product.objects.get(pk=num) self.assertTrue(response["Location"].endswith( product.get_absolute_url())) self.assertEquals(product.summary, 'Tondeuse') self.assertEquals(product.description, 'Merveilleuse tondeuse') self.assertEquals(product.quantity, 1) self.assertEquals(product.category.id, 1) self.assertEquals(product.address.id, 1) self.assertEquals(product.pictures.count(), 1)
def test_customer_list(self): login_success = self.client.login(username='******', password='******') self.assertTrue(login_success) users_data = [{ 'username': '******', 'password': '******', 'email': '*****@*****.**' }, { 'username': '******', 'password': '******', 'email': '*****@*****.**' }] for data in users_data: headers = self._get_headers(self._get_request(method='POST')) response_post = self.client.post(self._resource_url('customer'), data=json.dumps(data), content_type='application/json', **headers) headers = self._get_headers(self._get_request(method='GET')) response = self.client.get(self._resource_url('customer'), **headers) content = json.loads(response.content) self.assertEquals(content["meta"]["total_count"], 2)
def test_user_modification(self): login_success = self.client.login(username='******', password='******') self.assertTrue(login_success) headers = self._get_headers(self._get_request(method='PUT')) myid = Patron.objects.get(email='*****@*****.**').id response = self.client.put(self._resource_url('user', myid), data=json.dumps({'username': '******'}), content_type='application/json', **headers) self.assertEquals(response.status_code, 204) patron = Patron.objects.get(email='*****@*****.**') self.assertEquals(patron.username, 'trololol')
def test_account_creation_with_duplicate_email(self): post_data = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } request = self._get_request(method='POST') response = self.client.post(self._resource_url('user'), data=json.dumps(post_data), content_type='application/json', **self._get_headers(request)) self.assertEquals(response.status_code, 201) self.assertTrue('Location' in response) post_data = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } request = self._get_request(method='POST') response = self.client.post(self._resource_url('user'), data=json.dumps(post_data), content_type='application/json', **self._get_headers(request)) self.assertEquals(response.status_code, 400)
def post_list(self, request, **kwargs): post_data = self.deserialize(request, request.body, format=request.META.get( 'CONTENT_TYPE', 'application/json')) bundle = self.build_bundle(data=dict_strip_unicode_keys(post_data), request=request) self.is_valid(bundle) updated_bundle = self.obj_create(bundle) if updated_bundle.obj.sender is updated_bundle.obj.recipient: error_message = "You can't send message to yourself" cont_dic = {"error": error_message} content = json.dumps(cont_dic) return HttpResponseBadRequest(content_type='application/json', content=content) updated_bundle.obj.save() return HttpCreated(location=updated_bundle.obj.get_absolute_url())
def test_customer_creation(self): login_success = self.client.login(username='******', password='******') self.assertTrue(login_success) post_data = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } headers = self._get_headers(self._get_request(method='POST')) response = self.client.post(self._resource_url('customer'), data=json.dumps(post_data), content_type='application/json', **headers) customer = Patron.objects.get(username='******') renter = Patron.objects.get(email='*****@*****.**') rcust = renter.customers.get(username='******') self.assertEquals(customer, rcust)
def test_account_creation(self): post_data = { 'username': '******', 'password': '******', 'email': '*****@*****.**' } request = self._get_request(method='POST') response = self.client.post(self._resource_url('user'), data=json.dumps(post_data), content_type='application/json', **self._get_headers(request)) self.assertEquals(response.status_code, 201) self.assertTrue('Location' in response) patron = Patron.objects.get( pk=int(response['Location'].split('/')[-2])) self.assertEquals(patron.username, 'chuck') self.assertEquals(patron.email, '*****@*****.**') self.assertEquals(patron.is_active, True)
def test_message_creation(self): post_data = { "body": "test body", "product": "/api/1.0/product/6/", "subject": "test subject", "parent_msg": "/api/1.0/message/2/", "thread": 1, "recipient": "/api/1.0/user/4/" } request = self._get_request(method='POST', use_token=True) response = self.client.post(reverse("api_dispatch_list", args=['1.0', 'message']), data=json.dumps(post_data), content_type='application/json', **self._get_headers(request)) self.assertEquals(response.status_code, 201) self.assertTrue('Location' in response) message = ProductRelatedMessage.objects.get( pk=int(response['Location'].split('/')[-2])) self.assertEquals(message.sender.id, 1) self.assertEquals(message.thread.id, 1) self.assertEquals(message.body, 'test body') self.assertEquals(message.subject, 'test subject')
def error(error_str): raise ImmediateHttpResponse(response=HttpResponseBadRequest( content_type='application/json', content=json.dumps({"error": error_str})))