def test_get_event(self): """ Ensure we can get an existing game. """ # Seed the database with a game event = Event() event.description = "Test" event.date = "2021-02-14" event.time = "12:30:00" event.organizer = Gamer.objects.get(pk=1) event.game = Game.objects.get(pk=1) event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.id}") # Parse the JSON in the response body json_response = json.loads(response.content) # Assert that the game was retrieved self.assertEqual(response.status_code, status.HTTP_200_OK) # Assert that the values are correct self.assertEqual(json_response["description"], "Test") self.assertEqual(json_response["date"], "2021-02-14") self.assertEqual(json_response["time"], "12:30:00") self.assertEqual(json_response["organizer"]["id"], 1) self.assertEqual(json_response["game"]["id"], 1)
def create(self, request): gamer = Gamer.objects.get(user=request.auth.user) event = Event() event.time = request.data["time"] event.date = request.data["date"] event.description = request.data["description"] event.organizer = gamer game = Game.objects.get(pk=request.data["gameId"]) event.game = game try: event.save() serializer = EventSerializer(event, context={'request': request}) return Response(serializer.data) except ValidationError as ex: return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)
def test_change_event(self): """ Ensure we can change an existing event. """ event = Event() event.time = "13:00" event.date = "11-04-2020" event.description = "Party Night" gamer = Gamer.objects.get(pk=1) event.organizer = gamer game = Game.objects.get(pk=1) event.game = game event.save() # DEFINE NEW PROPERTIES FOR GAME data = { "time": "15:00", "date": "12-25-2020", "description": "Xmas Party Night", "organizer": 1, "gameId": 1 } # print('GAME ID ' + str(game.id)) self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(f"/events/{event.id}", data, format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # get game again to verify changes response = self.client.get(f"/events/{event.id}") json_response = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) # assert that the properties are correct self.assertEqual(json_response["time"], "15:00") self.assertEqual(json_response["date"], "12-25-2020") self.assertEqual(json_response["description"], "Xmas Party Night")
def create(self, request): """Handle POST operations for events Returns: Response -- JSON serialized event instance """ gamer = Gamer.objects.get(user=request.auth.user) event = Event() event.start_date = request.data["startDate"] event.description = request.data["description"] event.organizer = gamer game = Game.objects.get(pk=request.data["gameId"]) event.game = game try: event.save() event.attendees.set(request.data['gamers']) serializer = EventSerializer(event, context={'request': request}) return Response(serializer.data) except ValidationError as ex: return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)
def test_delete_event(self): """ Ensure we can delete an existing event. """ event = Event() event.time = "13:00" event.date = "11-04-2020" event.description = "Party Night" gamer = Gamer.objects.get(pk=1) event.organizer = gamer game = Game.objects.get(pk=1) event.game = game event.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.delete(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET GAME AGAIN TO VERIFY 404 response response = self.client.get(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)