From 60f0b5896a3dc1d3b4b96226257d0e138005b12b Mon Sep 17 00:00:00 2001
From: Yirou Qiu <y2qiu@uwaterloo.ca>
Date: Thu, 9 Mar 2023 11:50:40 -0500
Subject: [PATCH] add bill tests

---
 core/tests/test_bill.py    | 103 +++++++++++++++++++++++++++++++++++++
 core/urls.py               |   6 +++
 ece651_backend/settings.py |   1 +
 3 files changed, 110 insertions(+)
 create mode 100644 core/tests/test_bill.py

diff --git a/core/tests/test_bill.py b/core/tests/test_bill.py
new file mode 100644
index 0000000..438ff80
--- /dev/null
+++ b/core/tests/test_bill.py
@@ -0,0 +1,103 @@
+import requests
+import unittest
+
+# """ login user """
+
+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"]
+
+    headers = {'Authorization': f"Token {token}"}
+
+    endpoint = "http://localhost:8001/api/bill/"
+
+    data_1 = {
+        "title": "water",
+        "price": "0.85",
+        "comment": "discount",
+        "categories": 8
+    }
+
+    data_2 = {
+        "title": "mattress",
+        "price": "99.65",
+        "comment": "None",
+        "categories": 2
+    }
+
+
+class BillTests(unittest.TestCase):
+
+    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)
+
+        self.assertEqual(get_response_create_1.status_code, 201)
+        self.assertEqual(get_response_create_2.status_code, 201)
+
+    def test_get(self):
+        get_response_list_1 = requests.get(endpoint, headers=headers)
+        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())
+        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"])
+        self.assertEqual(get_response.json()["categories"],data_1["categories"])
+        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/"
+        data_updated = {
+            "title": "buttersquash",
+            "price": "0.01",
+            "comment": "juicy",
+            "categories": 4
+        }
+        get_response = requests.put(endpoint, data = data_updated, headers=headers)
+        # print(get_response.json())
+        self.assertEqual(get_response.status_code, 200)
+        self.assertEqual(get_response.json()["title"],data_updated["title"])
+        self.assertEqual(get_response.json()["price"],data_updated["price"])
+        self.assertEqual(get_response.json()["categories"],data_updated["categories"])
+        self.assertEqual(get_response.json()["comment"],data_updated["comment"])
+
+        ## partial update
+        data_updated_2 = {
+            "title": "buttersquash",
+            "price": "56.60",
+            "comment": "juicy",
+            "categories": 4
+        }
+        get_response = requests.put(endpoint, data = data_updated_2, headers=headers)
+        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()
diff --git a/core/urls.py b/core/urls.py
index f515f05..93a6b6d 100644
--- a/core/urls.py
+++ b/core/urls.py
@@ -34,3 +34,9 @@ urlpatterns += [
     path('api/bill/', bill_list_create_api, name="bill-list"),
     path('api/bill/<uuid:pk>/', bill_detail_api, name="bill-detail")
 ]
+
+
+from rest_framework.authtoken.views import obtain_auth_token
+urlpatterns += [
+    path('api/auth/', obtain_auth_token)
+]
diff --git a/ece651_backend/settings.py b/ece651_backend/settings.py
index 6a0882d..1c33262 100644
--- a/ece651_backend/settings.py
+++ b/ece651_backend/settings.py
@@ -40,6 +40,7 @@ INSTALLED_APPS = [
     'django.contrib.staticfiles',
     'django_extensions',
     'rest_framework',
+    'rest_framework.authtoken',
     'django_rest_passwordreset',
     'knox',
     'core',
-- 
GitLab