diff --git a/core/api/bill.py b/core/api/bill.py index ef0a6f1ab6cd617d811d06f2ea0eef337d5568f4..9e76a311b471007c54c40abfcde773c60e526f07 100644 --- a/core/api/bill.py +++ b/core/api/bill.py @@ -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): diff --git a/core/tests/test_bill.py b/core/tests/test_bill.py index 438ff80e016efd8680c249e8d6ae6a8deb9e1c3a..1a1946d1f8d3e450ee0d1dfee526f77135842336 100644 --- a/core/tests/test_bill.py +++ b/core/tests/test_bill.py @@ -1,58 +1,60 @@ -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()