Skip to content

Commit

Permalink
Merge pull request #214 from iragm/label-changes
Browse files Browse the repository at this point in the history
tests for lot labels and fix #195
  • Loading branch information
iragm authored Sep 8, 2024
2 parents 2f55cc4 + 852974c commit 7f46479
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
7 changes: 5 additions & 2 deletions auctions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2811,8 +2811,11 @@ def reserve_and_buy_now_info(self):
def label_line_0(self):
"""Used for printed labels"""
result = f"<b>LOT: {self.lot_number_display}</b>"
# if self.quantity > 1:
result += f" QTY: {self.quantity}"
if not self.winning_price:
if self.donation:
result += " (D) "
if self.auction.advanced_lot_adding or self.quantity > 1:
result += f" QTY: {self.quantity}"
return result

@property
Expand Down
92 changes: 89 additions & 3 deletions auctions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Lot,
LotHistory,
PickupLocation,
UserLabelPrefs,
add_price_info,
)

Expand All @@ -30,11 +31,21 @@ class StandardTestCase(TestCase):
Tests are also run automatically on commit by github actions
"""

def endAuction(self):
self.online_auction.date_end = timezone.now() - datetime.timedelta(days=2)
self.online_auction.save()

def setUp(self):
time = timezone.now() - datetime.timedelta(days=2)
timeStart = timezone.now() - datetime.timedelta(days=3)
theFuture = timezone.now() + datetime.timedelta(days=3)
self.user = User.objects.create_user(username="my_lot", password="testpassword", email="[email protected]")
self.user_with_no_lots = User.objects.create_user(
username="no_lots", password="testpassword", email="[email protected]"
)
self.user_who_does_not_join = User.objects.create_user(
username="no_joins", password="testpassword", email="[email protected]"
)
self.online_auction = Auction.objects.create(
created_by=self.user,
title="This auction is online",
Expand Down Expand Up @@ -65,6 +76,9 @@ def setUp(self):
self.tosB = AuctionTOS.objects.create(
user=self.userB, auction=self.online_auction, pickup_location=self.location
)
self.tosC = AuctionTOS.objects.create(
user=self.user_with_no_lots, auction=self.online_auction, pickup_location=self.location
)
self.lot = Lot.objects.create(
lot_name="A test lot",
auction=self.online_auction,
Expand Down Expand Up @@ -104,8 +118,8 @@ def setUp(self):
auctiontos_seller=self.tos,
active=False,
)
self.invoice = Invoice.objects.create(auctiontos_user=self.tos)
self.invoiceB = Invoice.objects.create(auctiontos_user=self.tosB)
self.invoice, c = Invoice.objects.get_or_create(auctiontos_user=self.tos)
self.invoiceB, c = Invoice.objects.get_or_create(auctiontos_user=self.tosB)
self.adjustment_add = InvoiceAdjustment.objects.create(
adjustment_type="ADD", amount=10, notes="test", invoice=self.invoiceB
)
Expand Down Expand Up @@ -848,7 +862,7 @@ def test_seller_invoice_closed(self):
assert updated_lot.winning_price is None

def test_winner_invoice_closed(self):
self.invoice = Invoice.objects.create(auctiontos_user=self.bidder)
self.invoice, c = Invoice.objects.get_or_create(auctiontos_user=self.bidder)
self.invoice.status = "READY"
self.invoice.save()
self.client.login(username="testuser", password="testpassword")
Expand Down Expand Up @@ -1000,3 +1014,75 @@ def test_post_lot_refund_dialog(self):
updated_lot = Lot.objects.get(pk=self.lot.pk)
assert updated_lot.partial_refund_percent == 50
assert updated_lot.banned is False


class LotLabelViewTestCase(StandardTestCase):
"""Tests for the LotLabelView"""

def setUp(self):
super().setUp()
self.url = reverse(
"my_labels_by_username", kwargs={"slug": self.online_auction.slug, "username": self.user.username}
)

def test_user_can_print_own_labels(self):
"""Test that a regular user can print their own labels."""
self.client.login(username=self.user, password="testpassword")
self.endAuction()
response = self.client.get(self.url)
assert response.status_code == 200
assert "attachment; filename=" in response.headers["Content-Disposition"]

def test_small_labels(self):
user_label_prefs, created = UserLabelPrefs.objects.get_or_create(user=self.user)
user_label_prefs.preset = "sm"
user_label_prefs.save()
self.client.login(username=self.user, password="testpassword")
response = self.client.get(self.url)
# for message in list(response.wsgi_request._messages):
# print(str(message))
assert response.status_code == 200
assert "attachment; filename=" in response.headers["Content-Disposition"]

# The test below will fail in ci because tests do not run in the docker container
# thermal labels cause a 'Paragraph' object has no attribute 'blPara' error
# See https://github.com/virantha/pypdfocr/issues/80
# This is the reason we are using a hacked version of platypus/paragraph.py in python_file_hack.sh

# def test_thermal_labels(self):
# """Test that a regular user can print their own labels."""
# user_label_prefs, created = UserLabelPrefs.objects.get_or_create(user=self.user)
# user_label_prefs.preset = "thermal_sm"
# user_label_prefs.save()
# self.client.login(username=self.user, password="testpassword")
# response = self.client.get(self.url)
# assert response.status_code == 200
# assert "attachment; filename=" in response.headers["Content-Disposition"]

def test_non_admin_cannot_print_others_labels(self):
"""Test that a non-admin user cannot print labels for other users."""
self.client.login(username="no_tos", password="testpassword")

response = self.client.get(self.url)

assert response.status_code == 302
messages = list(response.wsgi_request._messages)
assert str(messages[0]) == "Your account doesn't have permission to view this page."

def test_cannot_print_if_not_joined_auction(self):
"""Test that a user cannot print labels if they haven't joined the auction."""
self.client.login(username=self.user_who_does_not_join.username, password="testpassword")
url = reverse("print_my_labels", kwargs={"slug": self.online_auction.slug})
response = self.client.get(url)
assert response.status_code == 302
self.assertRedirects(response, self.online_auction.get_absolute_url())
messages = list(response.wsgi_request._messages)
assert (
str(messages[0])
== "You haven't joined this auction yet. You need to join this auction and add lots before you can print labels."
)

def test_no_printable_lots(self):
self.client.login(username=self.user_with_no_lots.username, password="testpassword")
response = self.client.get(self.url)
assert response.status_code == 302
1 change: 1 addition & 0 deletions auctions/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
path(
"auctions/<slug:slug>/print/user/<str:username>/",
login_required(views.LotLabelView.as_view()),
name="my_labels_by_username",
),
path(
"auctions/<slug:slug>/print/bidder/<str:bidder_number>/",
Expand Down

0 comments on commit 7f46479

Please sign in to comment.