Skip to content

Commit

Permalink
add Prize to V2 api (#747)
Browse files Browse the repository at this point in the history
[#184870668]
  • Loading branch information
uraniumanchor authored Dec 31, 2024
1 parent ed9b2d9 commit aecba04
Show file tree
Hide file tree
Showing 16 changed files with 727 additions and 115 deletions.
2 changes: 1 addition & 1 deletion tests/apiv2/test_donations.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_unprocessed_returns_only_after_timestamp(self):
self.event,
count=2,
state='pending',
time=date.replace(year=9999),
time=date.replace(year=9998),
)

response = self.client.get(
Expand Down
2 changes: 1 addition & 1 deletion tests/apiv2/test_milestones.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_patch(self):
with self.subTest('error cases'):
self.patch_detail(
self.public_milestone,
data={'event': self.event.id},
data={'event': self.blank_event.id},
status_code=400,
expected_error_codes=messages.EVENT_READ_ONLY_CODE,
)
Expand Down
264 changes: 264 additions & 0 deletions tests/apiv2/test_prizes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
from datetime import timedelta

from tests import randgen
from tests.util import APITestCase, today_noon
from tracker import models
from tracker.api import messages
from tracker.api.serializers import PrizeSerializer


class TestPrizes(APITestCase):
model_name = 'prize'
serializer_class = PrizeSerializer

def setUp(self):
super().setUp()
self.runs = randgen.generate_runs(
self.rand, num_runs=3, event=self.event, ordered=True
)
self.accepted_prize = randgen.generate_prize(
self.rand, start_run=self.runs[0], end_run=self.runs[1]
)
self.accepted_prize.description = 'test long description'
self.accepted_prize.shortdescription = 'test short description'
self.accepted_prize.save()
self.pending_prize = randgen.generate_prize(
self.rand, event=self.event, state='PENDING'
)
self.pending_prize.save()
self.denied_prize = randgen.generate_prize(
self.rand, event=self.event, state='DENIED'
)
self.denied_prize.save()
self.flagged_prize = randgen.generate_prize(
self.rand, event=self.event, state='FLAGGED'
)
self.flagged_prize.save()
self.locked_prize = randgen.generate_prize(
self.rand, event=self.locked_event, state='PENDING'
)
self.locked_prize.save()
# TODO
# self.view_winner_user = User.objects.create(username='view_winner_user')
# self.view_winner_user.user_permissions.add(Permission.objects.get(codename='view_prizewinner'))

def test_fetch(self):
with self.saveSnapshot():
with self.subTest('public'):
data = self.get_list()
self.assertExactV2Models([self.accepted_prize], data)

# TODO: more exhaustive?
data = self.get_list(
kwargs={'feed': 'current'}, data={'time': self.runs[0].starttime}
)
self.assertExactV2Models([self.accepted_prize], data)

data = self.get_list(
kwargs={'feed': 'current'}, data={'run': self.runs[0].pk}
)
self.assertExactV2Models([self.accepted_prize], data)

with self.subTest('searches'):
data = self.get_list(data={'q': self.accepted_prize.name})
self.assertExactV2Models([self.accepted_prize], data)

data = self.get_list(data={'q': self.accepted_prize.description})
self.assertExactV2Models([self.accepted_prize], data)

data = self.get_list(
data={'q': self.accepted_prize.shortdescription}
)
self.assertExactV2Models([self.accepted_prize], data)

data = self.get_list(data={'name': self.accepted_prize.name})
self.assertExactV2Models([self.accepted_prize], data)

data = self.get_list(data={'state': 'ACCEPTED'})
self.assertExactV2Models([self.accepted_prize], data)

data = self.get_detail(self.accepted_prize)
self.assertV2ModelPresent(self.accepted_prize, data)

data = self.get_detail(
self.accepted_prize, kwargs={'event_pk': self.event.pk}
)
self.assertV2ModelPresent(self.accepted_prize, data)

with self.subTest('private'):
data = self.get_list(user=self.view_user, kwargs={'feed': 'all'})
self.assertExactV2Models(
[
self.accepted_prize,
self.flagged_prize,
self.denied_prize,
self.pending_prize,
self.locked_prize,
],
data,
)

data = self.get_list(
user=self.view_user,
data={'state': ['PENDING', 'DENIED', 'FLAGGED']},
kwargs={'event_pk': self.event.pk},
)
self.assertExactV2Models(
[self.flagged_prize, self.denied_prize, self.pending_prize], data
)

# TODO
# data = self.get_list(user=self.view_winner_user, data={'include_winners': ''})
# self.assertExactV2Models([self.accepted_prize], data, serializer_kwargs={'include_winners': True})

with self.subTest('error cases'):
with self.subTest('private feeds'):
for feed in models.Prize.HIDDEN_FEEDS:
with self.subTest(feed):
self.get_list(user=None, kwargs={'feed': feed}, status_code=403)

with self.subTest('wrong event detail'):
self.get_detail(
self.accepted_prize,
kwargs={'event_pk': self.blank_event.pk},
status_code=404,
)

with self.subTest('private detail'):
for prize in [
self.pending_prize,
self.denied_prize,
self.flagged_prize,
]:
with self.subTest(prize.state):
self.get_detail(prize, user=None, status_code=404)

with self.subTest('private states'):
for state in models.Prize.HIDDEN_STATES:
with self.subTest(state):
self.get_list(user=None, data={'state': state}, status_code=403)

with self.subTest('combining feed and state'):
self.get_list(
data={'state': 'ACCEPTED'},
kwargs={'feed': 'public'},
status_code=400,
)

with self.subTest('combining feed and detail'):
self.get_detail(
self.accepted_prize, kwargs={'feed': 'public'}, status_code=404
)

def test_create(self):
with self.saveSnapshot(), self.assertLogsChanges(4):
with self.subTest('minimal'):
data = self.post_new(
user=self.add_user,
data={'event': self.event.pk, 'name': 'Event Wide Prize'},
)
prize = models.Prize.objects.get(pk=data['id'])
serialized = PrizeSerializer(prize)
self.assertEqual(data, serialized.data)
self.assertEqual(prize.handler, self.add_user)

data = self.post_new(
user=self.add_user,
data={
'event': self.event.pk,
'name': 'Timed Prize',
'starttime': today_noon,
'endtime': today_noon + timedelta(hours=1),
},
)
serialized = PrizeSerializer(models.Prize.objects.get(pk=data['id']))
self.assertEqual(data, serialized.data)

data = self.post_new(
user=self.add_user,
data={
'event': self.event.pk,
'name': 'Block Prize',
'startrun': self.runs[0].pk,
'endrun': self.runs[2].pk,
},
)
serialized = PrizeSerializer(models.Prize.objects.get(pk=data['id']))
self.assertEqual(data, serialized.data)

with self.subTest('full blown'):
data = self.post_new(
user=self.add_user,
kwargs={'event_pk': self.event.pk},
data={
'name': 'Earthquake Pills',
'startrun': self.runs[0].pk,
'endrun': self.runs[1].pk,
'description': 'Why wait? Make your own earthquakes - loads of fun!',
'shortdescription': 'Make your own earthquakes!',
'image': 'https://www.example.com/image.jpg',
'altimage': 'https://www.example.com/thumbnail.jpg',
'estimatedvalue': 10,
'minimumbid': 25,
'sumdonations': 1,
'provider': 'Coyote',
'creator': 'ACME',
'creatorwebsite': 'https://www.acme.com/',
},
)
serialized = PrizeSerializer(
models.Prize.objects.get(pk=data['id']), event_pk=self.event.pk
)
self.assertEqual(data, serialized.data)

with self.subTest('error cases'):
self.post_new(
user=self.add_user,
data={'event': self.locked_event.pk},
status_code=403,
expected_error_codes=messages.UNAUTHORIZED_LOCKED_EVENT_CODE,
)
self.post_new(
user=self.view_user,
status_code=403,
expected_error_codes=messages.PERMISSION_DENIED_CODE,
)
self.post_new(
user=None,
status_code=403,
expected_error_codes=messages.NOT_AUTHENTICATED_CODE,
)

def test_patch(self):
with self.saveSnapshot(), self.assertLogsChanges(1):
data = self.patch_detail(
self.pending_prize, user=self.add_user, data={'state': 'ACCEPTED'}
)
self.assertEqual(self.pending_prize.state, 'ACCEPTED')
self.assertEqual(data, PrizeSerializer(self.pending_prize).data)

with self.subTest('error cases'):
self.patch_detail(
self.accepted_prize,
data={'event': self.blank_event.pk},
status_code=400,
expected_error_codes=messages.EVENT_READ_ONLY_CODE,
)
self.patch_detail(
self.locked_prize,
user=self.add_user,
status_code=403,
expected_error_codes=messages.UNAUTHORIZED_LOCKED_EVENT_CODE,
)
self.patch_detail(
self.accepted_prize,
user=self.view_user,
status_code=403,
expected_error_codes='permission_denied',
)
self.patch_detail(
self.accepted_prize,
user=None,
status_code=403,
expected_error_codes=messages.NOT_AUTHENTICATED_CODE,
)
14 changes: 2 additions & 12 deletions tests/randgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ def generate_prize(
end_time=None,
sum_donations=None,
min_amount=Decimal('1.00'),
max_amount=Decimal('20.00'),
random_draw=True,
maxwinners=1,
state='ACCEPTED',
Expand All @@ -232,17 +231,8 @@ def generate_prize(
prize.category = category
else:
prize.category = rand.choice([None] + list(PrizeCategory.objects.all()))
if true_false_or_random(rand, sum_donations):
prize.sumdonations = True
lo = random_amount(rand, min_amount=min_amount, max_amount=max_amount)
hi = random_amount(rand, min_amount=min_amount, max_amount=max_amount)
prize.minimumbid = min(lo, hi)
prize.maximumbid = max(lo, hi)
else:
prize.sumdonations = False
prize.minimumbid = prize.maximumbid = random_amount(
rand, min_amount=min_amount, max_amount=max_amount
)
prize.sumdonations = true_false_or_random(rand, sum_donations)
prize.minimumbid = min_amount
prize.randomdraw = random_draw
if start_run:
prize.event = start_run.event
Expand Down
2 changes: 0 additions & 2 deletions tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,6 @@ def test_event_prize_report(self):
end_run=runs[0],
sum_donations=False,
min_amount=5,
max_amount=5,
)
prize.save()
donors = randgen.generate_donors(self.rand, 3)
Expand Down Expand Up @@ -781,7 +780,6 @@ def test_event_prize_report(self):
event=self.event,
sum_donations=True,
min_amount=50,
max_amount=50,
)
grandPrize.save()
# generate 2 for summation
Expand Down
2 changes: 0 additions & 2 deletions tests/test_prize.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,6 @@ def test_decline_prize_single(self):
sum_donations=False,
random_draw=False,
min_amount=amount,
max_amount=amount,
maxwinners=1,
)
targetPrize.save()
Expand Down Expand Up @@ -880,7 +879,6 @@ def test_accept_deadline_offset(self):
sum_donations=False,
random_draw=False,
min_amount=amount,
max_amount=amount,
maxwinners=1,
)
targetPrize.save()
Expand Down
Loading

0 comments on commit aecba04

Please sign in to comment.