Browse Source

Add purchase endpoint.

feature/bike-tracking
Drew Larson 8 years ago
parent
commit
ceeeeb9fb4
  1. 74
      bikeshop_project/bike/tests.py
  2. 13
      bikeshop_project/bike/views.py

74
bikeshop_project/bike/tests.py

@ -226,3 +226,77 @@ class TestGet(TestCase):
self.assertEqual(result.status_code, status.HTTP_200_OK) self.assertEqual(result.status_code, status.HTTP_200_OK)
self.assertEqual(result.data['state'], BikeState.CLAIMED) 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)

13
bikeshop_project/bike/views.py

@ -68,3 +68,16 @@ class BikeViewSet(viewsets.ModelViewSet):
serializer = BikeSerializer(bike, context={'request': request}) serializer = BikeSerializer(bike, context={'request': request})
return Response(serializer.data, status=status.HTTP_200_OK) 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)

Loading…
Cancel
Save