from django.test import TestCase, Client from django.urls import reverse from django.contrib.auth import get_user_model from .models import Seller, Game, Set, Card, Bounty from django.conf import settings User = get_user_model() class BountyCreateTest(TestCase): def setUp(self): # Ensure feature is on (might need to mock settings if not settable directly, # but usually tests run with default settings. We need ensuring it's True) # Note: In some setups, modifying settings in test needs override_settings self.user = User.objects.create_user(username='bounty_seller', password='password') self.seller = Seller.objects.create( user=self.user, store_name='Bounty Store', slug='bounty-store' ) self.game = Game.objects.create(name='Bounty Game', slug='bounty-game') self.set = Set.objects.create(game=self.game, name='Bounty Set') self.card = Card.objects.create(set=self.set, name='Bounty Card') self.client = Client() self.client.force_login(self.user) self.url = reverse('store:bounty_create') def test_create_bounty_with_card_id(self): # Simulate selecting a card via autocomplete data = { 'card_name': 'Bounty Card', 'card_id': self.card.id, 'target_price': '100.00', 'quantity_wanted': 1, # Title is optional if card picked? Form logic says "if no card and no title raise error" # Our view logic: if card_id -> get card -> set bounty.card. # Form clean: if not card_id and not title and not card_name -> Error. } with self.settings(FEATURE_BOUNTY_BOARD=True): response = self.client.post(self.url, data) self.assertRedirects(response, reverse('store:bounty_list')) # Verify bounty = Bounty.objects.first() self.assertIsNotNone(bounty) self.assertEqual(bounty.card, self.card) self.assertEqual(bounty.seller, self.seller) # Title should be auto-set in save() if empty self.assertEqual(bounty.title, "Buying Bounty Card") def test_create_bounty_with_name_fallback(self): # Simulate typing a name but not selecting a card (no ID) data = { 'card_name': 'Generic Card', 'card_id': '', 'target_price': '50.00', 'quantity_wanted': 1 } with self.settings(FEATURE_BOUNTY_BOARD=True): response = self.client.post(self.url, data) self.assertRedirects(response, reverse('store:bounty_list')) bounty = Bounty.objects.last() self.assertIsNone(bounty.card) self.assertEqual(bounty.title, 'Generic Card') def test_create_bounty_validation_error(self): # Empty everything data = { 'card_name': '', 'card_id': '', 'target_price': '50.00', 'quantity_wanted': 1 } with self.settings(FEATURE_BOUNTY_BOARD=True): response = self.client.post(self.url, data) self.assertEqual(response.status_code, 200) # Form errors, no redirect form = response.context['form'] self.assertIn("You must either select a Card or provide a Title.", form.non_field_errors())