Skip to content

Commit 0dee76b

Browse files
committed
Fix failing integration test
1 parent 3bde044 commit 0dee76b

File tree

2 files changed

+58
-60
lines changed

2 files changed

+58
-60
lines changed

flite/users/test/test_views.py

+55-57
Original file line numberDiff line numberDiff line change
@@ -14,73 +14,71 @@
1414
fake = Faker()
1515

1616

17-
# class TestUserListTestCase(APITestCase):
18-
# """
19-
# Tests /users list operations.
20-
# """
21-
22-
# def setUp(self):
23-
# self.url = reverse('user-list')
24-
# self.user_data = model_to_dict(UserFactory.build())
17+
class TestUserListTestCase(APITestCase):
18+
"""
19+
Tests /users list operations.
20+
"""
2521

26-
# def test_post_request_with_no_data_fails(self):
27-
# response = self.client.post(self.url, {})
28-
# eq_(response.status_code, status.HTTP_400_BAD_REQUEST)
22+
def setUp(self):
23+
self.url = reverse('user-list')
24+
self.user = UserFactory()
25+
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.user.auth_token}')
2926

30-
# def test_post_request_with_valid_data_succeeds(self): # failed
31-
# response = self.client.post(self.url, self.user_data)
32-
# eq_(response.status_code, status.HTTP_201_CREATED)
27+
# def test_post_request_with_valid_data_succeeds(self): # This will fail under the url "users-list" because create request is not allowed
28+
# new_User = UserFactory()
29+
# response = self.client.post(self.url, new_User)
30+
# eq_(response.status_code, status.HTTP_201_CREATED)
3331

34-
# user = User.objects.get(pk=response.data.get('id'))
35-
# eq_(user.username, self.user_data.get('username'))
36-
# ok_(check_password(self.user_data.get('password'), user.password))
32+
# user = User.objects.get(pk=response.data.get('id'))
33+
# eq_(user.username, self.user.get('username'))
34+
# ok_(check_password(self.user.get('password'), user.password))
3735

38-
# def test_post_request_with_valid_data_succeeds_and_profile_is_created(self): # failed
39-
# response = self.client.post(self.url, self.user_data)
40-
# eq_(response.status_code, status.HTTP_201_CREATED)
36+
# def test_post_request_with_valid_data_succeeds_and_profile_is_created(self): # This will fail under the url "users-list" because create request is not allowed
37+
# response = self.client.post(self.url, self.user)
38+
# eq_(response.status_code, status.HTTP_201_CREATED)
4139

42-
# eq_(UserProfile.objects.filter(user__username=self.user_data['username']).exists(),True)
40+
# eq_(UserProfile.objects.filter(user__username=self.user['username']).exists(),True)
4341

44-
# def test_post_request_with_valid_data_succeeds_referral_is_created_if_code_is_valid(self): # failed
42+
# def test_post_request_with_valid_data_succeeds_referral_is_created_if_code_is_valid(self): # This will fail under the url "users-list" because update request is not allowed
4543

46-
# referring_user = UserFactory()
47-
# self.user_data.update({"referral_code":referring_user.userprofile.referral_code})
48-
# response = self.client.post(self.url, self.user_data)
49-
# eq_(response.status_code, status.HTTP_201_CREATED)
44+
# referring_user = UserFactory()
45+
# self.user.update({"referral_code":referring_user.userprofile.referral_code})
46+
# response = self.client.post(self.url, self.user)
47+
# eq_(response.status_code, status.HTTP_201_CREATED)
5048

51-
# eq_(Referral.objects.filter(referred__username=self.user_data['username'],owner__username=referring_user.username).exists(),True)
49+
# eq_(Referral.objects.filter(referred__username=self.user['username'],owner__username=referring_user.username).exists(),True)
5250

5351

54-
# def test_post_request_with_valid_data_succeeds_referral_is_not_created_if_code_is_invalid(self): # failed
52+
# def test_post_request_with_valid_data_succeeds_referral_is_not_created_if_code_is_invalid(self): # Update Request is not allowed so this will fail
5553

56-
# self.user_data.update({"referral_code":"FAKECODE"})
57-
# response = self.client.post(self.url, self.user_data)
58-
# eq_(response.status_code, status.HTTP_400_BAD_REQUEST)
54+
# self.user.update({"referral_code":"FAKECODE"})
55+
# response = self.client.post(self.url, self.user)
56+
# eq_(response.status_code, status.HTTP_400_BAD_REQUEST)
5957

60-
# class TestUserDetailTestCase(APITestCase):
61-
# """
62-
# Tests /users detail operations.
63-
# """
64-
65-
# def setUp(self):
66-
# self.user = UserFactory()
67-
# self.url = reverse('user-detail', kwargs={'pk': self.user.pk})
68-
# self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.user.auth_token}')
69-
70-
# def test_get_request_returns_a_given_user(self):
71-
# response = self.client.get(self.url)
72-
# eq_(response.status_code, status.HTTP_200_OK)
73-
74-
# def test_put_request_updates_a_user(self): #failed
75-
# new_first_name = fake.first_name()
76-
# payload = {'first_name': new_first_name}
77-
# response = self.client.put(self.url, payload)
78-
# eq_(response.status_code, status.HTTP_200_OK)
79-
80-
# user = User.objects.get(pk=self.user.id)
81-
# eq_(user.first_name, new_first_name)
82-
83-
# Test for task endpoints
58+
class TestUserDetailTestCase(APITestCase):
59+
"""
60+
Tests /users detail operations.
61+
"""
62+
63+
def setUp(self):
64+
self.user = UserFactory()
65+
self.url = reverse('user-detail', kwargs={'pk': self.user.pk})
66+
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.user.auth_token}')
67+
68+
def test_get_request_returns_a_given_user(self):
69+
response = self.client.get(self.url)
70+
eq_(response.status_code, status.HTTP_200_OK)
71+
72+
# def test_put_request_updates_a_user(self): # Put request is not allowed
73+
# new_first_name = fake.first_name()
74+
# payload = {'first_name': new_first_name}
75+
# response = self.client.put(self.url, payload)
76+
# eq_(response.status_code, status.HTTP_200_OK)
77+
78+
# user = User.objects.get(pk=self.user.id)
79+
# eq_(user.first_name, new_first_name)
80+
81+
# Test for my task starts here
8482
class TestUserDepositView(APITestCase):
8583
"""
8684
Tests for the /user-deposit endpoint.
@@ -245,12 +243,12 @@ def test_list_transactions_no_transactions(self):
245243

246244
def test_transaction_detail_invalid_transaction_id(self):
247245
"""
248-
Test that a 400 error is returned when an invalid transaction ID format is provided.
246+
Test that a 404 error is returned when an invalid transaction ID format is provided.
249247
"""
250248
# Use an invalid transaction ID format
251249
invalid_transaction_id = uuid.uuid4()
252250
detail_url = reverse('user-transaction-detail', kwargs={'pk': self.transaction.owner.id, 'transaction_id': invalid_transaction_id})
253251

254252
response = self.client.get(detail_url)
255253
# Check if a 400 response is returned for invalid transaction ID
256-
eq_(response.status_code, status.HTTP_400_BAD_REQUEST)
254+
eq_(response.status_code, status.HTTP_404_NOT_FOUND)

flite/users/views.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,16 @@ def transaction_detail(self, request, pk=None, transaction_id=None):
312312
Retrieve details of a specific transaction.
313313
"""
314314
user = self.get_object() # Get the user account using pk
315-
315+
316316
try:
317317
# Ensure the transaction_id is a valid UUID
318318
transaction_id = uuid.UUID(transaction_id)
319319
except ValueError:
320-
return Response({"error": "Invalid transaction ID."}, status=status.HTTP_400_BAD_REQUEST)
320+
return Response({"error": "Invalid transaction ID."}, status=status.HTTP_404_NOT_FOUND)
321321

322322
transaction = Transaction.objects.filter(owner=user, id=transaction_id).first()
323323
if not transaction:
324-
return Response({"error": "Transaction not found."}, status=status.HTTP_400_BAD_REQUEST)
324+
return Response({"error": "Transaction not found."}, status=status.HTTP_404_NOT_FOUND)
325325

326326
serializer = TransactionSerializer(transaction)
327327
return Response(serializer.data)

0 commit comments

Comments
 (0)