Skip to content

Commit

Permalink
fix being unable to create bids in any of the hidden states (#751)
Browse files Browse the repository at this point in the history
[#188743567]
  • Loading branch information
uraniumanchor authored Jan 4, 2025
1 parent b7d9f44 commit e1eb633
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
46 changes: 45 additions & 1 deletion tests/apiv2/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def test_fetch(self):
)

def test_create(self):
with self.saveSnapshot(), self.assertLogsChanges(4):
with self.saveSnapshot(), self.assertLogsChanges(7):
# TODO: natural key tests
with self.subTest('attach to event'):
data = self.post_new(
Expand Down Expand Up @@ -245,6 +245,24 @@ def test_create(self):
serialized = BidSerializer(models.Bid.objects.get(pk=data['id']))
self.assertEqual(data, serialized.data)

with self.suppressSnapshot(), self.subTest('create hidden states'):
for state in models.Bid.HIDDEN_STATES:
with self.subTest(state):
data = {'name': state.capitalize(), 'state': state}
if state == 'HIDDEN':
data['event'] = self.event.pk
data['goal'] = 50
else:
data['parent'] = self.opened_parent_bid.pk
data['istarget'] = True
data = self.post_new(data=data)
serialized = BidSerializer(
models.Bid.objects.get(pk=data['id']),
include_hidden=True,
with_permissions=('tracker.view_bid'),
)
self.assertEqual(data, serialized.data)

with self.subTest('attach to locked event with permission'):
data = self.post_new(
data={'name': 'New Locked Event Bid', 'event': self.locked_event.pk},
Expand Down Expand Up @@ -355,6 +373,32 @@ def test_patch(self):
data = self.patch_detail(self.challenge, data={'name': 'Challenge Updated'})
self.assertV2ModelPresent(self.challenge, data)

with self.assertLogsChanges(3), self.subTest('changing to hidden states'):
data = self.patch_detail(self.challenge, data={'state': 'HIDDEN'})
self.assertV2ModelPresent(
self.challenge,
data,
serializer_kwargs=(
dict(include_hidden=True, with_permissions=('tracker.view_bid'))
),
)
data = self.patch_detail(self.opened_bid, data={'state': 'DENIED'})
self.assertV2ModelPresent(
self.opened_bid,
data,
serializer_kwargs=(
dict(include_hidden=True, with_permissions=('tracker.view_bid'))
),
)
data = self.patch_detail(self.opened_bid, data={'state': 'PENDING'})
self.assertV2ModelPresent(
self.opened_bid,
data,
serializer_kwargs=(
dict(include_hidden=True, with_permissions=('tracker.view_bid'))
),
)

with self.subTest(
'can edit locked bid with permission'
), self.assertLogsChanges(1):
Expand Down
10 changes: 6 additions & 4 deletions tracker/api/views/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ class BidViewSet(
permission_classes = [BidFeedPermission, BidStatePermission]
filter_backends = [BidFilter]

def _include_hidden(self, instance=None):
def _include_hidden(self, instance=None, data=None):
# include hidden bids if we're asking for one hidden bid, or if we're asking for one of the hidden feeds
return (
isinstance(instance, Bid) and instance.state not in ['OPENED', 'CLOSED']
) or self.get_feed() in ('pending', 'all')
(isinstance(instance, Bid) and instance.state not in Bid.PUBLIC_STATES)
or self.get_feed() in ('pending', 'all')
or (data and data.get('state', None) in Bid.HIDDEN_STATES)
)

def get_feed(self):
return self.kwargs.get('feed', None)
Expand All @@ -61,7 +63,7 @@ def get_serializer(self, instance=None, *args, **kwargs):
return super().get_serializer(
instance,
*args,
include_hidden=self._include_hidden(instance),
include_hidden=self._include_hidden(instance, kwargs.get('data', None)),
feed=self.get_feed(),
**kwargs,
)
Expand Down

0 comments on commit e1eb633

Please sign in to comment.