write tests for delete_bid view

pull/3/head
lance 5 years ago
parent e3b6c8aee7
commit 48721a66f3

@ -264,3 +264,54 @@ class ItemBidViewsTestCase(TestCase):
self.assertTrue(response.context['form'])
self.assertIsInstance(response.context['form'], CreateItemBidForm)
new_bid.delete()
##### Delete Bid
def test_delete_bid_redirect_home_if_bid_id_missing(self):
self.client.login(username=self.buyer.username, password=self.buyer_password)
response = self.client.get(reverse('delete_bid', args=[9999]))
self.client.logout()
self.assertEqual(response.url, reverse('home'))
self.assertEqual(response.status_code, 302)
def test_delete_bid_redirect_item_if_user_not_bidder(self):
new_bid = ItemBid.objects.create(
item=self.test_item,
bidder=self.buyer,
bid_price_xmr=0.1,
return_address=self.return_address
)
self.client.login(username=self.seller.username, password=self.seller_password)
response = self.client.get(reverse('delete_bid', args=[new_bid.id]))
self.client.logout()
self.assertEqual(response.url, reverse('get_item', args=[new_bid.item.id]))
self.assertEqual(response.status_code, 302)
def test_delete_bid_redirect_item_if_bid_is_accepted(self):
new_bid = ItemBid.objects.create(
item=self.test_item,
bidder=self.buyer,
bid_price_xmr=0.1,
return_address=self.return_address,
accepted=True
)
self.client.login(username=self.buyer.username, password=self.buyer_password)
response = self.client.get(reverse('delete_bid', args=[new_bid.id]))
buyer_bid = ItemBid.objects.filter(bidder=self.buyer, item=self.test_item.id).first()
self.assertEqual(response.url, reverse('get_item', args=[self.test_item.id]))
self.assertEqual(response.status_code, 302)
self.assertTrue(buyer_bid.accepted)
def test_delete_bid_redirect_item_if_bid_is_deleted(self):
new_bid = ItemBid.objects.create(
item=self.test_item,
bidder=self.buyer,
bid_price_xmr=0.1,
return_address=self.return_address
)
self.client.login(username=self.buyer.username, password=self.buyer_password)
response = self.client.get(reverse('delete_bid', args=[new_bid.id]))
buyer_bid = ItemBid.objects.filter(bidder=self.buyer, item=self.test_item.id).first()
self.assertEqual(response.url, reverse('get_item', args=[self.test_item.id]))
self.assertEqual(response.status_code, 302)
self.assertIsNone(buyer_bid)

@ -80,7 +80,7 @@ def create_bid(request, item_id):
def edit_bid(request, bid_id):
bid = ItemBid.objects.filter(id=bid_id).first()
if bid is None:
messages.error(request, "That bid does not exist for you to edit.")
messages.error(request, "You can't edit a bid that doesn't exist.")
return HttpResponseRedirect(reverse('home'))
# Do not allow editing if current user doesn't own the bid
@ -172,7 +172,10 @@ def accept_bid(request, bid_id):
@login_required
def delete_bid(request, bid_id):
bid = ItemBid.objects.get(id=bid_id)
bid = ItemBid.objects.filter(id=bid_id).first()
if bid is None:
messages.error(request, "You can't delete a bid that doesn't exist.")
return HttpResponseRedirect(reverse('home'))
# Do not allow deleting the bid unless you own the bid
if request.user != bid.bidder: