Untitled

mail@pastecode.io avatar
unknown
python
a year ago
4.5 kB
4
Indexable
from decimal import Decimal
from unittest.mock import patch
from rest_framework.status import (
    HTTP_200_OK,
    HTTP_400_BAD_REQUEST,
    HTTP_403_FORBIDDEN,
    HTTP_500_INTERNAL_SERVER_ERROR
)
from rest_framework.test import APITestCase
from rest_framework.reverse import reverse
from api.models import User, PublicMarketplaceItem, UserTradingDetails, MarketplaceSettings
from allauth.socialaccount.models import SocialAccount


class ListingManipulationTestCase(APITestCase):
    steam_id = "1234567890"
    steam_id_2 = "7385175318353"
    asset_id = "20545131380"

    @classmethod
    def setUpTestData(cls):
        cls.user = User.objects.create_user(username='testuser', password='testpassword')
        cls.user2 = User.objects.create_user(username='testuser2', password="testpassword2")
        cls.item = PublicMarketplaceItem.objects.create(
            asset_id=cls.asset_id,
            steamid=cls.steam_id_2,
            user_price=Decimal(10.0),
            status=PublicMarketplaceItem.STATUS_PURCHASABLE,
            fee=Decimal(5.0)
        )
        cls.user_social_account = SocialAccount.objects.create(
            provider='Steam',
            uid=cls.steam_id_2,
            user=cls.user,
            extra_data={"steamid": cls.steam_id}
        )
        cls.user_trading_details = UserTradingDetails.objects.create(
            api_key="12345678901234567890123456789012",
            trade_link="https://steamcommunity.com/tradeoffer/new/?partner=123&token=123",
            steamid=cls.steam_id_2,
            phone_number="123456789",
            user=cls.user
        )

    def test_delete_listing_no_asset_id(self):
        self.client.force_authenticate(self.user)
        response = self.client.delete(path=reverse("delete_listing"))
        self.assertEqual(HTTP_400_BAD_REQUEST, response.status_code)
        self.assertIn("No asset id provided", response.data)

    def test_delete_listing_not_listed(self):
        self.client.force_authenticate(self.user)
        response = self.client.delete(path=reverse("delete_listing"), data={"asset_id": "noid"})
        self.assertEqual(HTTP_400_BAD_REQUEST, response.status_code)
        self.assertIn("The item is not listed on the marketplace", response.data)

    def test_delete_listing_success(self):
        self.client.force_authenticate(self.user)
        response = self.client.delete(
            path=reverse("delete_listing"), data={"asset_id": self.asset_id}
        )
        self.assertEqual(HTTP_200_OK, response.status_code)
        self.assertIn("Successfully deleted", response.data['message'])

    def test_edit_price_no_new_price(self):
        self.client.force_authenticate(self.user)
        response = self.client.patch(
            path=reverse("edit_listing_price"), data={"asset_id": self.asset_id}
        )
        self.assertEqual(HTTP_400_BAD_REQUEST, response.status_code)
        self.assertIn("new_price was not provided", response.data)

    def test_edit_price_marketplace_fee_missing(self):
        with patch.object(MarketplaceSettings.objects, 'first', return_value=None):
            self.client.force_authenticate(self.user)
            response = self.client.patch(
                path=reverse("edit_listing_price"),
                data={
                    "asset_id": self.asset_id,
                    "new_price": "15.0"
                }
                )
            self.assertEqual(HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)

    def test_edit_price_success(self):
        self.client.force_authenticate(self.user)
        response = self.client.patch(
            path=reverse("edit_listing_price"),
            data={"asset_id": self.asset_id, "new_price": "20.0"}
        )
        self.assertEqual(HTTP_200_OK, response.status_code)
        self.assertIn("Price updated", response.data['message'])
        updated_item = PublicMarketplaceItem.objects.get(asset_id=self.asset_id)
        self.assertEqual(updated_item.user_price, Decimal("20.0"))

    def test_edit_price_item_not_by_user(self):
        different_steam_id = "0987654321"
        UserTradingDetails.objects.create(user=self.user2, steamid=different_steam_id)
        self.client.force_authenticate(self.user2)
        response = self.client.patch(
            path=reverse("edit_listing_price"),
            data={"asset_id": self.asset_id, "new_price": "25.0"}
        )
        self.assertEqual(HTTP_403_FORBIDDEN, response.status_code)