Skip to content
Snippets Groups Projects
Commit f588f02f authored by Chris Li's avatar Chris Li
Browse files

Modify format of tests/test_bill.

parent b8c78d56
No related branches found
No related tags found
No related merge requests found
......@@ -8,11 +8,7 @@ from rest_framework import permissions, authentication
class BillListCreate(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
queryset = Bill.objects.all()
serializer_class = BillSerializer
authentication_classes = [
authentication.SessionAuthentication,
authentication.TokenAuthentication
]
# permission_classes = (permissions.IsAuthenticated,)
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
......@@ -50,11 +46,7 @@ class BillDetail(mixins.RetrieveModelMixin,
generics.GenericAPIView):
queryset = Bill.objects.all()
serializer_class = BillSerializer
authentication_classes = [
authentication.SessionAuthentication,
authentication.TokenAuthentication
]
# permission_classes = (permissions.IsAuthenticated,)
permission_classes = (permissions.IsAuthenticated,)
lookup_field = "pk"
def get(self, request, *args, **kwargs):
......
import requests
import unittest
from django.contrib.auth.models import User
from rest_framework.test import APITestCase
from knox.models import AuthToken
# """ login user """
from core.models.bill import Bill
user = {
"username": "superuser",
"email": "",
"password": "1"
}
auth_endpoint = "http://localhost:8001/api/auth/"
auth_response = requests.post(auth_endpoint, json = user)
if auth_response.status_code == 200:
token = auth_response.json()["token"]
endpoint = "/api/bill/"
headers = {'Authorization': f"Token {token}"}
endpoint = "http://localhost:8001/api/bill/"
data_1 = {
"title": "water",
"price": "0.85",
"comment": "discount",
"categories": 8
}
data_1 = {
"title": "water",
"price": "0.85",
"comment": "discount",
"categories": 8
}
data_2 = {
"title": "mattress",
"price": "99.65",
"comment": "None",
"categories": 2
}
data_2 = {
"title": "mattress",
"price": "99.65",
"comment": "None",
"categories": 2
}
class BillTests(unittest.TestCase):
class BillTests(APITestCase):
def setUp(self):
username = "xjhmlcy"
email = "xjhmlcy123@gmail.com"
password = "abcdefg123"
user = User.objects.create_user(username=username, email=email, password=password)
token, token_key = AuthToken.objects.create(user)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token_key)
def test_create(self):
get_response_create_1 = requests.post(endpoint, json = data_1, headers=headers)
get_response_create_2 = requests.post(endpoint, json = data_2, headers=headers)
get_response_create_1 = self.client.post(endpoint, data_1, format='json')
get_response_create_2 = self.client.post(endpoint, data_2, format='json')
self.assertEqual(get_response_create_1.status_code, 201)
self.assertEqual(get_response_create_2.status_code, 201)
# print(get_response_create_1.data)
def test_get(self):
get_response_list_1 = requests.get(endpoint, headers=headers)
endpoint = "/api/bill/"
self.client.post(endpoint, data_1, format='json')
self.client.post(endpoint, data_2, format='json')
get_response_list_1 = self.client.get(endpoint)
self.assertEqual(get_response_list_1.status_code, 200)
# print(get_response_list_1.json())
def test_detailed_get(self):
endpoint = "http://localhost:8001/api/bill/d5b1886c-abff-44e3-b0a8-b26981d5cfe9/"
get_response = requests.get(endpoint, headers=headers)
print(get_response.json())
endpoint = "/api/bill/"
self.client.post(endpoint, data_1, format='json')
self.client.post(endpoint, data_2, format='json')
endpoint = f"/api/bill/{Bill.objects.all()[0].id}/"
get_response = self.client.get(endpoint)
# print(get_response.json())
self.assertEqual(get_response.status_code, 200)
self.assertEqual(get_response.json()["title"],data_1["title"])
self.assertEqual(get_response.json()["price"],data_1["price"])
......@@ -60,14 +62,18 @@ class BillTests(unittest.TestCase):
self.assertEqual(get_response.json()["comment"],data_1["comment"])
def test_detailed_update(self):
endpoint = "http://localhost:8001/api/bill/8bbf80af-12a0-4603-8270-79ad9feffda5/"
endpoint = "/api/bill/"
self.client.post(endpoint, data_1, format='json')
self.client.post(endpoint, data_2, format='json')
endpoint = f"/api/bill/{Bill.objects.all()[0].id}/"
data_updated = {
"title": "buttersquash",
"price": "0.01",
"comment": "juicy",
"categories": 4
}
get_response = requests.put(endpoint, data = data_updated, headers=headers)
get_response = self.client.put(endpoint, data_updated, format='json')
# print(get_response.json())
self.assertEqual(get_response.status_code, 200)
self.assertEqual(get_response.json()["title"],data_updated["title"])
......@@ -82,22 +88,9 @@ class BillTests(unittest.TestCase):
"comment": "juicy",
"categories": 4
}
get_response = requests.put(endpoint, data = data_updated_2, headers=headers)
get_response = self.client.put(endpoint, data_updated_2, format='json')
self.assertEqual(get_response.status_code, 200)
self.assertEqual(get_response.json()["title"],data_updated_2["title"])
self.assertEqual(get_response.json()["price"],data_updated_2["price"])
self.assertEqual(get_response.json()["categories"],data_updated_2["categories"])
self.assertEqual(get_response.json()["comment"],data_updated_2["comment"])
# def test_delete(self):
# endpoint = "http://localhost:8001/api/bill/cb0e20c4-5344-4130-8a29-cbd50656fb48/"
# get_response = requests.delete(endpoint, headers=headers)
# print(get_response.json())
# self.assertEqual(get_response.status_code, 204)
# get_response = requests.get(endpoint, headers=headers)
# # data not exist
# self.assertEqual(get_response.status_code, 404)
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment