From ceeeeb9fb4567f4251073330dcbcd76168d542cc Mon Sep 17 00:00:00 2001 From: Drew Larson Date: Sat, 7 Jan 2017 19:20:00 -0600 Subject: [PATCH] Add purchase endpoint. --- bikeshop_project/bike/tests.py | 74 ++++++++++++++++++++++++++++++++++ bikeshop_project/bike/views.py | 13 ++++++ 2 files changed, 87 insertions(+) diff --git a/bikeshop_project/bike/tests.py b/bikeshop_project/bike/tests.py index 9c3840b..11fe5fd 100644 --- a/bikeshop_project/bike/tests.py +++ b/bikeshop_project/bike/tests.py @@ -226,3 +226,77 @@ class TestGet(TestCase): self.assertEqual(result.status_code, status.HTTP_200_OK) self.assertEqual(result.data['state'], BikeState.CLAIMED) + + def test_purchase_can_transition_from_available(self): + member = mommy.make(Member) + + data = { + "colour": "black", + "make": "Miyata", + "serial_number": "12345676", + "source": Bike.COS_BIKE_DIVERSION_PILOT, + "donated_by": "Greg", + "donated_at": "2017-01-01", + "size": Bike.SMALL, + "price": Decimal('68.00'), + "state": BikeState.AVAILABLE, + "stolen": False, + "cpic_searched_at": timezone.now(), + } + + bike = Bike.objects.create(**data) + self.assertEqual(bike.state, BikeState.AVAILABLE) + client = APIClient() + client.force_authenticate(user=self.user, token='blah') + result = client.put(f'/api/v1/bikes/{bike.id}/purchase/', data={'member': member.id}, format='json') + + self.assertEqual(result.status_code, status.HTTP_200_OK) + self.assertEqual(result.data['state'], BikeState.PURCHASED) + + def test_purchase_cannot_transition_wrong_state(self): + member = mommy.make(Member) + + data = { + "colour": "black", + "make": "Miyata", + "serial_number": "12345676", + "source": Bike.COS_BIKE_DIVERSION_PILOT, + "donated_by": "Greg", + "donated_at": "2017-01-01", + "size": Bike.SMALL, + "price": Decimal('68.00'), + "state": BikeState.ASSESSED, + "stolen": False, + "cpic_searched_at": timezone.now(), + } + bike = Bike.objects.create(**data) + client = APIClient() + client.force_authenticate(user=self.user, token='blah') + result = client.put(f'/api/v1/bikes/{bike.id}/purchase/', data={'member': member.id}, format='json') + + self.assertEqual(result.status_code, status.HTTP_400_BAD_REQUEST) + + def test_purchase_cannot_transition_when_claimed(self): + member = mommy.make(Member) + + data = { + "colour": "black", + "make": "Miyata", + "serial_number": "12345676", + "source": Bike.COS_BIKE_DIVERSION_PILOT, + "donated_by": "Greg", + "donated_at": "2017-01-01", + "size": Bike.SMALL, + "price": Decimal('68.00'), + "state": BikeState.CLAIMED, + "stolen": False, + "cpic_searched_at": timezone.now(), + "claimed_by": member, + "last_worked_on": timezone.now() + } + bike = Bike.objects.create(**data) + client = APIClient() + client.force_authenticate(user=self.user, token='blah') + result = client.put('/api/v1/bikes/{bike_id}/purchase/'.format(bike_id=bike.id), data={'member': member.id}, format='json') + + self.assertEqual(result.status_code, status.HTTP_400_BAD_REQUEST) diff --git a/bikeshop_project/bike/views.py b/bikeshop_project/bike/views.py index 7c67631..0cadc20 100644 --- a/bikeshop_project/bike/views.py +++ b/bikeshop_project/bike/views.py @@ -68,3 +68,16 @@ class BikeViewSet(viewsets.ModelViewSet): serializer = BikeSerializer(bike, context={'request': request}) return Response(serializer.data, status=status.HTTP_200_OK) + @detail_route(methods=['put']) + def purchase(self, request, pk): + bike = get_object_or_404(Bike, pk=pk) + member = get_object_or_404(Member, id=request.data.get('member')) + state = BikeState.CLAIMED + if not can_proceed(bike.purchase): + raise ValidationError(detail=f'Transition from {bike.state} to {state}') + + bike.purchase(member) + bike.save() + + serializer = BikeSerializer(bike, context={'request': request}) + return Response(serializer.data, status=status.HTTP_200_OK)