Untitled
unknown
python
a year ago
2.6 kB
2
Indexable
Never
from rest_framework.test import APITestCase from rest_framework.reverse import reverse from rest_framework.status import HTTP_200_OK, HTTP_400_BAD_REQUEST from api.models import UserTradingDetails from extensions.auth.models import User class AddAffiliateCodeTestCase(APITestCase): @classmethod def setUpTestData(cls): cls.user = User.objects.create_user(username='testuser', password='testpassword') # Case 1: User has trading details and sends a valid affiliate code def test_valid_affiliate_code_with_trading_details(self): UserTradingDetails.objects.create( user=self.user, steamid='12345678', api_key='api_key_123', trade_link='http://example.com' ) self.client.force_authenticate(self.user) data = { "affiliate_code": "VALIDCODE123" } response = self.client.post(path=reverse("add_affiliate_code"), data=data) self.assertEqual(HTTP_200_OK, response.status_code) self.assertTrue(response.data["success"]) self.assertEqual(response.data["message"], "Affiliate code has been saved!") user_details = UserTradingDetails.objects.get(user=self.user) self.assertEqual(user_details.affiliate_code, "VALIDCODE123") # Case 2: User has trading details and sends an invalid affiliate code def test_invalid_affiliate_code_with_trading_details(self): UserTradingDetails.objects.create( user=self.user, steamid='12345678', api_key='api_key_123', trade_link='http://example.com' ) self.client.force_authenticate(self.user) data = { "affiliate_code": "Invalid Code" # Some invalid format here } response = self.client.post(path=reverse("add_affiliate_code"), data=data) self.assertEqual(HTTP_400_BAD_REQUEST, response.status_code) self.assertFalse(response.data["success"]) self.assertEqual(response.data["message"], "Invalid format!") # Case 3: User doesn't have trading details def test_no_trading_details(self): self.client.force_authenticate(self.user) data = { "affiliate_code": "VALIDCODE123" } response = self.client.post(path=reverse("add_affiliate_code"), data=data) self.assertEqual(HTTP_400_BAD_REQUEST, response.status_code) self.assertFalse(response.data["success"]) self.assertEqual(response.data["message"], "Trading details not saved")