|
| 1 | +from decimal import Decimal |
| 2 | + |
| 3 | +from django.contrib.auth.models import User |
| 4 | +from django.test import Client, TestCase |
| 5 | +from django.urls import reverse |
| 6 | + |
| 7 | +from website.models import Bid |
| 8 | + |
| 9 | + |
| 10 | +class BiddingTestCase(TestCase): |
| 11 | + """Test cases for the bidding functionality.""" |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + """Set up test data.""" |
| 15 | + # Create a test user |
| 16 | + self.user = User.objects.create_user(username="testuser", email="test@example.com", password="testpassword") |
| 17 | + |
| 18 | + # Create a test client |
| 19 | + self.client = Client() |
| 20 | + |
| 21 | + # Define test data |
| 22 | + self.valid_github_issue_url = "https://github.com/OWASP-BLT/BLT-Website/issues/123" |
| 23 | + self.invalid_github_issue_url = "https://example.com/not-a-github-issue" |
| 24 | + self.bid_amount = "5.0" |
| 25 | + |
| 26 | + def test_add_bid_authenticated_user(self): |
| 27 | + """Test adding a bid with an authenticated user.""" |
| 28 | + # Log in the user |
| 29 | + self.client.login(username="testuser", password="testpassword") |
| 30 | + |
| 31 | + # Make a POST request to the bidding endpoint |
| 32 | + response = self.client.post( |
| 33 | + reverse("BiddingData"), |
| 34 | + {"issue_url": self.valid_github_issue_url, "bid_amount": self.bid_amount, "user": self.user.username}, |
| 35 | + follow=True, # Follow redirects |
| 36 | + ) |
| 37 | + |
| 38 | + # Check that the response is successful after redirect |
| 39 | + self.assertEqual(response.status_code, 200) |
| 40 | + |
| 41 | + # Check that a bid was created in the database |
| 42 | + self.assertEqual(Bid.objects.count(), 1) |
| 43 | + |
| 44 | + # Check that the bid has the correct data |
| 45 | + bid = Bid.objects.first() |
| 46 | + self.assertEqual(bid.user, self.user) |
| 47 | + self.assertEqual(bid.issue_url, self.valid_github_issue_url) |
| 48 | + self.assertEqual(bid.amount_bch, Decimal(self.bid_amount)) |
| 49 | + self.assertEqual(bid.status, "Open") |
| 50 | + |
| 51 | + def test_add_bid_unauthenticated_user(self): |
| 52 | + """Test adding a bid with an unauthenticated user.""" |
| 53 | + # Make a POST request to the bidding endpoint without logging in |
| 54 | + response = self.client.post( |
| 55 | + reverse("BiddingData"), |
| 56 | + {"issue_url": self.valid_github_issue_url, "bid_amount": self.bid_amount, "user": self.user.username}, |
| 57 | + follow=True, # Follow redirects |
| 58 | + ) |
| 59 | + |
| 60 | + # Check that the user is redirected to the login page |
| 61 | + self.assertEqual(response.status_code, 200) # After following redirects |
| 62 | + self.assertIn("/accounts/login/", response.redirect_chain[0][0]) # Check redirect URL |
| 63 | + |
| 64 | + # Check that no bid was created |
| 65 | + self.assertEqual(Bid.objects.count(), 0) |
| 66 | + |
| 67 | + def test_add_bid_with_github_username(self): |
| 68 | + """Test adding a bid with a GitHub username that doesn't match a user in the system.""" |
| 69 | + # Log in the user |
| 70 | + self.client.login(username="testuser", password="testpassword") |
| 71 | + |
| 72 | + # Make a POST request with a different GitHub username |
| 73 | + github_username = "github_user" |
| 74 | + response = self.client.post( |
| 75 | + reverse("BiddingData"), |
| 76 | + {"issue_url": self.valid_github_issue_url, "bid_amount": self.bid_amount, "user": github_username}, |
| 77 | + follow=True, # Follow redirects |
| 78 | + ) |
| 79 | + |
| 80 | + # Check that the response is successful after redirect |
| 81 | + self.assertEqual(response.status_code, 200) |
| 82 | + |
| 83 | + # Check that a bid was created |
| 84 | + self.assertEqual(Bid.objects.count(), 1) |
| 85 | + |
| 86 | + # Check that the bid has the correct data |
| 87 | + bid = Bid.objects.first() |
| 88 | + self.assertEqual(bid.user, self.user) # The authenticated user should be set as the fallback |
| 89 | + self.assertEqual(bid.github_username, github_username) |
| 90 | + self.assertEqual(bid.issue_url, self.valid_github_issue_url) |
| 91 | + self.assertEqual(bid.amount_bch, Decimal(self.bid_amount)) |
| 92 | + |
| 93 | + def test_view_bidding_page(self): |
| 94 | + """Test that the bidding page can be viewed successfully.""" |
| 95 | + self.client.login(username="testuser", password="testpassword") |
| 96 | + response = self.client.get(reverse("BiddingData")) |
| 97 | + self.assertEqual(response.status_code, 200) |
| 98 | + self.assertTemplateUsed(response, "bidding.html") |
| 99 | + |
| 100 | + def test_add_bid_without_authentication(self): |
| 101 | + """Test that a bid can be added without authentication, using just a GitHub username.""" |
| 102 | + # Logout to ensure we're testing as an unauthenticated user |
| 103 | + self.client.logout() |
| 104 | + |
| 105 | + # Initial count of bids |
| 106 | + initial_bid_count = Bid.objects.count() |
| 107 | + |
| 108 | + # Post data for creating a bid |
| 109 | + response = self.client.post( |
| 110 | + reverse("BiddingData"), |
| 111 | + { |
| 112 | + "user": "github_user_123", |
| 113 | + "issue_url": self.valid_github_issue_url, |
| 114 | + "bid_amount": self.bid_amount, |
| 115 | + }, |
| 116 | + follow=True, |
| 117 | + ) |
| 118 | + |
| 119 | + # Check response |
| 120 | + self.assertEqual(response.status_code, 200) |
| 121 | + |
| 122 | + # Check that we were redirected to the login page |
| 123 | + self.assertIn("/accounts/login/", response.redirect_chain[0][0]) |
| 124 | + |
| 125 | + # Verify no new bid was created (since unauthenticated users are redirected to login) |
| 126 | + self.assertEqual(Bid.objects.count(), initial_bid_count) |
| 127 | + |
| 128 | + def test_add_bid_with_github_url_in_profile(self): |
| 129 | + """Test that a bid can be added using the GitHub username from the user's profile.""" |
| 130 | + # Login the user |
| 131 | + self.client.login(username="testuser", password="testpassword") |
| 132 | + |
| 133 | + # Set GitHub URL in the user's profile |
| 134 | + user = User.objects.get(username="testuser") |
| 135 | + user.userprofile.github_url = "https://github.com/profile_github_user" |
| 136 | + user.userprofile.save() |
| 137 | + |
| 138 | + # Initial count of bids |
| 139 | + initial_bid_count = Bid.objects.count() |
| 140 | + |
| 141 | + # Post data for creating a bid without specifying a username |
| 142 | + response = self.client.post( |
| 143 | + reverse("BiddingData"), |
| 144 | + { |
| 145 | + "user": "", # Empty username to test extraction from profile |
| 146 | + "issue_url": self.valid_github_issue_url, |
| 147 | + "bid_amount": self.bid_amount, |
| 148 | + }, |
| 149 | + follow=True, |
| 150 | + ) |
| 151 | + |
| 152 | + # Check response |
| 153 | + self.assertEqual(response.status_code, 200) |
| 154 | + |
| 155 | + # Verify a new bid was created |
| 156 | + self.assertEqual(Bid.objects.count(), initial_bid_count + 1) |
| 157 | + |
| 158 | + # Get the newly created bid |
| 159 | + new_bid = Bid.objects.latest("created") |
| 160 | + |
| 161 | + # Verify bid details |
| 162 | + self.assertEqual(new_bid.user, user) # User should be set to the authenticated user |
| 163 | + self.assertEqual(new_bid.github_username, "profile_github_user") # GitHub username extracted from profile URL |
| 164 | + self.assertEqual(new_bid.issue_url, self.valid_github_issue_url) |
| 165 | + self.assertEqual(float(new_bid.amount_bch), float(self.bid_amount)) |
| 166 | + self.assertEqual(new_bid.status, "Open") |
0 commit comments