88from django .utils import timezone
99from rest_framework import status
1010from rest_framework .test import APITestCase
11+ from rest_framework_simplejwt .tokens import RefreshToken
1112
1213from coupons .models import Coupon
1314from orders .models import Order , OrderItem
1718
1819
1920class OrderAPITests (APITestCase ):
21+
2022 def setUp (self ):
2123 self .admin = User .objects .create_superuser (
2224 username = 'admin' ,
password = 'pass' ,
email = '[email protected] ' 2325 )
24- self .user = User .objects .create_user (username = 'user' , password = 'pass' )
26+ self .
user = User .
objects .
create_user (
username = 'user' ,
password = 'pass' , email = '[email protected] ' )
2527 self .category = Category .objects .create (name = 'Test Category' )
2628 self .product1 = Product .objects .create (
2729 user = self .admin ,
@@ -41,8 +43,11 @@ def setUp(self):
4143 self .url = reverse ('api-v1:order-list' )
4244 self .cart_session_id = getattr (settings , 'CART_SESSION_ID' , 'cart' )
4345
46+ # Set JWT token for admin by default
47+ refresh = RefreshToken .for_user (self .admin )
48+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
49+
4450 def prepare_cart_session (self , products ):
45- """Helper method to prepare cart session data"""
4651 session = self .client .session
4752 session [self .cart_session_id ] = {}
4853 for product , quantity in products :
@@ -54,7 +59,8 @@ def prepare_cart_session(self, products):
5459 session .save ()
5560
5661 def test_create_order_empty_cart (self ):
57- self .client .login (username = 'admin' , password = 'pass' )
62+ refresh = RefreshToken .for_user (self .admin )
63+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
5864 session = self .client .session
5965 if self .cart_session_id in session :
6066 del session [self .cart_session_id ]
@@ -65,7 +71,8 @@ def test_create_order_empty_cart(self):
6571 self .assertEqual (response .data ['error' ], "You cannot place an order with an empty cart." )
6672
6773 def test_create_order_with_cart (self ):
68- self .client .login (username = 'admin' , password = 'pass' )
74+ refresh = RefreshToken .for_user (self .admin )
75+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
6976 self .prepare_cart_session ([(self .product1 , 2 )])
7077
7178 with patch ('orders.views.send_order_confirmation_email.delay' ) as mock_task :
@@ -82,34 +89,35 @@ def test_create_order_with_cart(self):
8289 mock_task .assert_called_once_with (order .order_id )
8390
8491 def test_create_order_with_multiple_products (self ):
85- self .client .login (username = 'admin' , password = 'pass' )
92+ refresh = RefreshToken .for_user (self .admin )
93+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
8694 self .prepare_cart_session ([(self .product1 , 1 ), (self .product2 , 3 )])
8795
8896 response = self .client .post (self .url )
8997 self .assertEqual (response .status_code , status .HTTP_201_CREATED )
9098
9199 order = Order .objects .get (order_id = response .data ['order_id' ])
92100 self .assertEqual (order .items .count (), 2 )
93- self .assertEqual (order .quantity , 4 ) # Number of distinct products
94- self .assertEqual (order .get_total_cost_before_discount (), decimal .Decimal ('140.00' )) # 50 + (30*3)
95-
101+ self .assertEqual (order .quantity , 4 )
102+ self .assertEqual (order .get_total_cost_before_discount (), decimal .Decimal ('140.00' ))
96103
97104 def test_order_list_filtered_by_user (self ):
98- # Create orders for both users
99105 order1 = Order .objects .create (user = self .admin , quantity = 1 )
100106 OrderItem .objects .create (order = order1 , product = self .product1 , quantity = 1 )
101107
102108 order2 = Order .objects .create (user = self .user , quantity = 1 )
103109 OrderItem .objects .create (order = order2 , product = self .product2 , quantity = 1 )
104110
105- # Test admin can see all orders
106- self .client .login (username = 'admin' , password = 'pass' )
111+ # Admin can see all orders
112+ refresh = RefreshToken .for_user (self .admin )
113+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
107114 response = self .client .get (self .url )
108115 self .assertEqual (response .status_code , status .HTTP_200_OK )
109116 self .assertEqual (len (response .data ['data' ]), 2 )
110117
111- # Test regular user can only see their own orders
112- self .client .login (username = 'user' , password = 'pass' )
118+ # Regular user can only see their own orders
119+ refresh = RefreshToken .for_user (self .user )
120+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
113121 response = self .client .get (self .url )
114122 self .assertEqual (response .status_code , status .HTTP_200_OK )
115123 self .assertEqual (len (response .data ['data' ]), 1 )
@@ -120,30 +128,34 @@ def test_order_retrieve(self):
120128 OrderItem .objects .create (order = order , product = self .product1 , quantity = 2 )
121129 detail_url = reverse ('api-v1:order-detail' , kwargs = {'pk' : order .order_id })
122130
123- # Test admin can retrieve any order
124- self .client .login (username = 'admin' , password = 'pass' )
131+ # Admin can retrieve any order
132+ refresh = RefreshToken .for_user (self .admin )
133+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
125134 response = self .client .get (detail_url )
126135 self .assertEqual (response .status_code , status .HTTP_200_OK )
127136 self .assertEqual (response .data ['order_id' ], str (order .order_id ))
128137
129- # Test regular user cannot retrieve other user's order
130- self .client .login (username = 'user' , password = 'pass' )
138+ # Regular user cannot retrieve other user's order
139+ refresh = RefreshToken .for_user (self .user )
140+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
131141 response = self .client .get (detail_url )
132142 self .assertEqual (response .status_code , status .HTTP_404_NOT_FOUND )
133143
134144 def test_order_update_permissions (self ):
135145 order = Order .objects .create (user = self .admin , quantity = 1 )
136146 OrderItem .objects .create (order = order , product = self .product1 , quantity = 1 )
137147 detail_url = reverse ('api-v1:order-detail' , kwargs = {'pk' : order .order_id })
138- data = {'status' : 'CO' } # Completed
148+ data = {'status' : 'CO' }
139149
140- # Test regular user cannot update
141- self .client .login (username = 'user' , password = 'pass' )
150+ # Regular user cannot update
151+ refresh = RefreshToken .for_user (self .user )
152+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
142153 response = self .client .patch (detail_url , data )
143154 self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
144155
145- # Test admin can update
146- self .client .login (username = 'admin' , password = 'pass' )
156+ # Admin can update
157+ refresh = RefreshToken .for_user (self .admin )
158+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
147159 response = self .client .patch (detail_url , data )
148160 self .assertEqual (response .status_code , status .HTTP_200_OK )
149161 order .refresh_from_db ()
@@ -154,13 +166,15 @@ def test_order_delete_permissions(self):
154166 OrderItem .objects .create (order = order , product = self .product1 , quantity = 1 )
155167 detail_url = reverse ('api-v1:order-detail' , kwargs = {'pk' : order .order_id })
156168
157- # Test regular user cannot delete
158- self .client .login (username = 'user' , password = 'pass' )
169+ # Regular user cannot delete
170+ refresh = RefreshToken .for_user (self .user )
171+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
159172 response = self .client .delete (detail_url )
160173 self .assertEqual (response .status_code , status .HTTP_403_FORBIDDEN )
161174
162- # Test admin can delete
163- self .client .login (username = 'admin' , password = 'pass' )
175+ # Admin can delete
176+ refresh = RefreshToken .for_user (self .admin )
177+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
164178 response = self .client .delete (detail_url )
165179 self .assertEqual (response .status_code , status .HTTP_204_NO_CONTENT )
166180 self .assertFalse (Order .objects .filter (pk = order .order_id ).exists ())
@@ -170,7 +184,8 @@ def test_order_serializer_fields(self):
170184 OrderItem .objects .create (order = order , product = self .product1 , quantity = 2 )
171185 detail_url = reverse ('api-v1:order-detail' , kwargs = {'pk' : order .order_id })
172186
173- self .client .login (username = 'admin' , password = 'pass' )
187+ refresh = RefreshToken .for_user (self .admin )
188+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
174189 response = self .client .get (detail_url )
175190
176191 self .assertEqual (response .status_code , status .HTTP_200_OK )
@@ -183,7 +198,7 @@ def test_order_serializer_fields(self):
183198 self .assertIn ('original_price' , data )
184199 self .assertEqual (data ['original_price' ], '100.00' )
185200 self .assertIn ('total_price' , data )
186- self .assertEqual (data ['total_price' ], '90.00' ) # With 10% coupon
201+ self .assertEqual (data ['total_price' ], '90.00' )
187202 self .assertIn ('coupon' , data )
188203 self .assertEqual (data ['coupon' ], 'TEST10' )
189204 self .assertIn ('discount' , data )
@@ -193,22 +208,25 @@ def test_order_status_choices(self):
193208 order = Order .objects .create (user = self .admin , quantity = 1 )
194209 detail_url = reverse ('api-v1:order-detail' , kwargs = {'pk' : order .order_id })
195210
196- self .client .login (username = 'admin' , password = 'pass' )
211+ refresh = RefreshToken .for_user (self .admin )
212+ self .client .credentials (HTTP_AUTHORIZATION = f'Bearer { str (refresh .access_token )} ' )
197213 response = self .client .get (detail_url )
198- self .assertEqual (response .data ['status' ], 'PE' ) # Pending by default
214+ self .assertEqual (response .data ['status' ], 'PE' )
199215
200- # Test valid status update
201- data = {'status' : 'CO' } # Completed
216+ # Valid status update
217+ data = {'status' : 'CO' }
202218 response = self .client .patch (detail_url , data )
203219 self .assertEqual (response .status_code , status .HTTP_200_OK )
204220 self .assertEqual (response .data ['status' ], 'CO' )
205221
206- # Test invalid status
222+ # Invalid status
207223 data = {'status' : 'INVALID' }
208224 response = self .client .patch (detail_url , data )
209225 self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
210226
211227 def test_unauthenticated_access (self ):
228+ self .client .credentials () # Remove authentication
229+
212230 # List
213231 response = self .client .get (self .url )
214232 self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
0 commit comments