Ejemplo n.º 1
0
    def test_get_game(self):
        """
        Ensure we can get an existing game.
        """
        game = Game()
        game.gametype_id = 1
        game.skill_level = 5
        game.title = "Monopoly"
        game.maker = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1

        game.save()

        # Make sure request is authenticated
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)

        # Initiate request and store response
        response = self.client.get(f"/games/{game.id}")

        # Parse the JSON in the response body
        json_response = json.loads(response.content)

        # Assert that the game was created
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        # Assert that the properties are correct
        self.assertEqual(json_response["title"], "Monopoly")
        self.assertEqual(json_response["maker"], "Milton Bradley")
        self.assertEqual(json_response["skill_level"], 5)
        self.assertEqual(json_response["number_of_players"], 4)
Ejemplo n.º 2
0
    def test_change_game(self):
        #test we can update an existing game
        game = Game()
        game.game_type_id = 1
        game.title = 'Chess'
        game.description = 'GOAT Board Game'
        game.number_of_players = 2
        game.gamer_id = 1
        game.save()
        #now define NEW properties for this shit
        data = {
            'gameTypeId': 1,
            'title': 'Clue',
            'description': 'Milton Bradley',
            'numberOfPlayers': 6,
        }

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.put(f'/games/{game.id}', data, format='json')
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        #verify the changes
        response = self.client.get(f'/games/{game.id}')
        json_response = json.loads(response.content)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        #assert that the properties on the this resource are correct
        self.assertEqual(json_response['title'], 'Clue')
        self.assertEqual(json_response['description'], 'Milton Bradley')
        self.assertEqual(json_response['number_of_players'], 6)
Ejemplo n.º 3
0
    def test_change_game(self):

        game = Game()
        game.game_type_id = 1
        game.skill_level = 1
        game.title = "Sorry"
        game.maker = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        # DEFINE NEW PROPERTIES FOR GAME
        data = {
            "game_type_id": 1,
            "title": "Sorry",
            "skill_level": 1,
            "maker": "Hasbro",
            "number_of_players": 4
        }

        self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token}')
        response = self.client.put(f"/games/{game.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"/games/{game.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["title"], "Sorry")
        self.assertEqual(json_response["skill_level"], 1)
        self.assertEqual(json_response["maker"], "Hasbro")
        self.assertEqual(json_response["number_of_players"], 4)
Ejemplo n.º 4
0
    def create(self, request):
        """Handle POST operations

        Returns:
            Response -- JSON serialized game instance
        """
        gamer = Gamer.objects.get(user=request.auth.user)

        game = Game()

        try:
            game.title = request.data["title"]
            game.maker = request.data["maker"]
            game.number_of_players = request.data["numberOfPlayers"]
            game.skill_level = request.data["skillLevel"]
        except KeyError as ex:
            return Response({'message': 'Incorrect key was sent in request'},
                            status=status.HTTP_400_BAD_REQUEST)

        game.gamer = gamer

        try:
            gametype = GameType.objects.get(pk=request.data["gameTypeId"])
            game.gametype = gametype
        except GameType.DoesNotExist as ex:
            return Response({'message': 'Game type provided is not valid'},
                            status=status.HTTP_400_BAD_REQUEST)

        try:
            game.save()
            serializer = GameSerializer(game, context={'request': request})
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        except ValidationError as ex:
            return Response({"reason": ex.message},
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 5
0
    def test_get_game(self):
        """
        Ensure we can get an existing game.
        """

        # Seed the database with a game
        game = Game()
        game.game_type = self.gametype
        game.description = "You move around the game board (a mansion), as of one of the game's six suspects, collecting clues from which to deduce which suspect murdered the game's perpetual victim: Mr. Boddy (Dr. Black, outside of U.S.), and with which weapon and in what room."
        game.title = "Monopoly"
        game.number_of_players = 4
        game.gamer_id= 1

        game.save()

        # Make sure request is authenticated
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)

        # Initiate request and store response
        response = self.client.get(f"/games/{game.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["title"], "Monopoly")
        self.assertEqual(json_response["description"], "You move around the game board (a mansion), as of one of the game's six suspects, collecting clues from which to deduce which suspect murdered the game's perpetual victim: Mr. Boddy (Dr. Black, outside of U.S.), and with which weapon and in what room.")
        self.assertEqual(json_response["number_of_players"], 4)
        self.assertEqual(json_response["game_type"]["id"], 1)
        self.assertEqual(json_response["gamer"]["id"], 1)
Ejemplo n.º 6
0
    def test_change_game(self):
        """
        Ensure we can change an existing game.
        """
        game = Game()
        game.game_type = self.gametype
        game.description = "Players move their three or four pieces around the board, attempting to get all of their pieces home before any other player"
        game.title = "Sorry"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        # DEFINE NEW PROPERTIES FOR GAME
        data = {
            "gameTypeId": 1,
            "description": "Players move their three or four pieces around the board, attempting to get all of their pieces home before any other player",
            "title": "Sorry",
            "numberOfPlayers": 4
        }

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.put(f"/games/{game.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"/games/{game.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["title"], "Sorry")
        self.assertEqual(json_response["description"], "Players move their three or four pieces around the board, attempting to get all of their pieces home before any other player")
        self.assertEqual(json_response["number_of_players"], 4)
        self.assertEqual(json_response["game_type"]["id"], 1)
        self.assertEqual(json_response["gamer"]["id"], 1)
Ejemplo n.º 7
0
    def test_change_game(self):
        """
        Ensure we can change an existing game.
        """
        game = Game()
        game.game_type_id = 1
        game.title = "Monopoly"
        game.description = "family ending board game"
        game.number_of_players = 4
        game.gamer_id = 1

        game.save()

        # DEFINE NEW PROPERTIES FOR GAME
        data = {
            "gameTypeId": 1,
            "description": "difficult game",
            "title": "Sorry",
            "number_of_players": 4
        }

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.put(f"/games/{game.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"/games/{game.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["title"], "Sorry")
        self.assertEqual(json_response["description"], "difficult game")
        self.assertEqual(json_response["number_of_players"], 4)
Ejemplo n.º 8
0
    def setUp(self):
        #create new account and sample category
        url = '/register'
        data = {
            'username': '******',
            'password': '******',
            'email': '*****@*****.**',
            'address': '100 Infinity Way',
            'phone_number': '555-1212',
            'first_name': 'Steve',
            'last_name': 'Brownlee',
            'bio': 'Love those gamez!!'
        }
        #initiate request and grab the response
        response = self.client.post(url, data, format='json')
        #parse the JSON in the resonse body
        fuckin_json_response = json.loads(response.content)
        #store teh AUTH Token
        self.token = fuckin_json_response['token']
        #assert that a user was created
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        #seed the DB wit one game type so it don't break and there's no URL endpoint for creating GTs
        gametype = GameType()
        gametype.label = 'Classic Type'
        gametype.save()
        #now make a dummy game and save it
        game = Game()
        game.game_type_id = 1
        game.title = 'Chess'
        game.description = 'GOAT Board Game'
        game.number_of_players = 2
        game.gamer_id = 1
        game.save()
Ejemplo n.º 9
0
    def test_get_game(self):
        """
        Ensure we can get an existing game.
        """

        # Seed the database with a game
        game = Game()
        game.game_type_id = 1
        game.title = "Monopoly"
        game.description = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1

        game.save()

        # Make sure request is authenticated
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)

        # Initiate request and store response
        response = self.client.get(f"/games/{game.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["title"], "Monopoly")
        self.assertEqual(json_response["description"], "Milton Bradley")
        self.assertEqual(json_response["gamer"]["id"], 1)
        self.assertEqual(json_response["game_type"]["id"], 1)
        self.assertEqual(json_response["number_of_players"], 4)
Ejemplo n.º 10
0
    def setUp(self):

        url = "/register"
        data = {
            "username": "******",
            "password": "******",
            "email": "*****@*****.**",
            "first_name": "Steve",
            "last_name": "Brownlee",
            "bio": "Love those gamez!!"
        }

        response = self.client.post(url, data, format='json')

        json_response = json.loads(response.content)

        self.token = json_response["token"]

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        
        gametype = GameType()
        gametype.label = "Board game"
        
        gametype.save()
        
        # url = "/games"
        # data = {
        #     "gamer": 1,
        #     "game_type": 1,
        #     "title": "Balderdash",
        #     "number_of_players": 2,
        #     "description": "fun"
        # }

        # response = self.client.post(url, data, format='json')

        # json_response = json.loads(response.content)

        # self.token = json_response["token"]
        # self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        
        game = Game()
        game.game_type = gametype
        game.title = "Balderdash"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()
Ejemplo n.º 11
0
    def test_delete_game(self):
        #make sure we can delete a game
        game = Game()
        game.game_type_id = 1
        game.title = 'Chess'
        game.description = 'GOAT Board Game'
        game.number_of_players = 2
        game.gamer_id = 1
        game.save()

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.delete(f'/games/{game.id}')
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        # GET GAME AGAIN TO VERIFY 404 response
        response = self.client.get(f'/games/{game.id}')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Ejemplo n.º 12
0
    def setUp(self):
        """
        Create a new account and create sample category
        """
        url = "/register"
        data = {
            "username": "******",
            "password": "******",
            "email": "*****@*****.**",
            "address": "100 Infinity Way",
            "phone_number": "555-1212",
            "first_name": "Steve",
            "last_name": "Brownlee",
            "bio": "Love those eventz!!"
        }
        # Initiate request and capture response
        response = self.client.post(url, data, format='json')

        # Parse the JSON in the response body
        json_response = json.loads(response.content)

        # Store the auth token
        self.token = json_response["token"]

        # Assert that a user was created
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # SEED DATABASE WITH ONE GAME TYPE
        # This is needed because the API does not expose a /gametypes
        # endpoint for creating game types
        gametype = GameType()
        gametype.label = "Board game"
        gametype.save()

        # SEED DATABASE WITH ONE game
        # This is needed because the API does not expose a /game
        # endpoint for creating game
        game = Game()
        game.gamer_id = 1
        game.gametype_id = gametype.id
        game.skill_level = 5
        game.number_of_players = 3
        game.maker = "Wizards of the Coast"
        game.title = "Dungeons & Dragons"
        game.save()
Ejemplo n.º 13
0
    def create(self, request):
        gamer = Gamer.objects.get(user=request.auth.user)
        game = Game()
        game.title = request.data["title"]
        game.maker = request.data["maker"]
        game.number_of_players = request.data["numberOfPlayers"]
        game.skill_level = request.data["skillLevel"]
        game.gamer = gamer
        gametype = GameType.objects.get(pk=request.data["gameTypeId"])
        game.gametype = gametype

        try:
            game.save()
            serializer = GameSerializer(game, context={'request': request})
            return Response(serializer.data)
        except ValidationError as ex:
            return Response({"reason": ex.message},
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 14
0
    def test_delete_game(self):
        """
        Ensure we can delete an existing game.
        """
        game = Game()
        game.gametype_id = 1
        game.title = "Sorry"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.delete(f"/games/{game.id}")
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        # GET GAME AGAIN TO VERIFY 404 response
        response = self.client.get(f"/games/{game.id}")
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Ejemplo n.º 15
0
    def test_delete_game(self):
        """
        Ensure we can delete an existing game.
        """
        game = Game()
        game.game_type = self.gametype
        game.description = "Players move their three or four pieces around the board, attempting to get all of their pieces home before any other player"
        game.title = "Sorry"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.delete(f"/games/{game.id}")
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        # GET GAME AGAIN TO VERIFY 404 response
        response = self.client.get(f"/games/{game.id}")
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Ejemplo n.º 16
0
    def test_delete_game(self):
        """
        Ensure we can delete an existing game.
        """
        game = Game()
        game.title = "Checkers"
        game.gametype_id = 1
        game.number_of_players = 2
        game.gamer_id = 1
        game.description = "Great game"
        game.save()

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.delete(f"/games/{game.id}")
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        # GET game again to verify 404 response
        response = self.client.get(f"/games/{game.id}")
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Ejemplo n.º 17
0
    def setUp(self):
        """
        Create a new account and create sample category
        """
        url = "/register"
        data = {
            "username": "******",
            "password": "******",
            "email": "*****@*****.**",
            "address": "100 Infinity Way",
            "phone_number": "555-1212",
            "first_name": "Jon",
            "last_name": "Shearon",
            "bio": "Love those gamez!!"
        }
        # Initiate request and capture response
        response = self.client.post(url, data, format='json')

        # Parse the JSON in the response body
        json_response = json.loads(response.content)

        # Store the auth token
        self.token = json_response["token"]

        # Assert that a user was created
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # SEED DATABASE WITH ONE GAME TYPE
        # This is needed because the API does not expose a /gametypes
        # endpoint for creating game types
        gametype = GameType()
        gametype.label = "Board game"
        gametype.save()

        game = Game()
        game.gametype = GameType.objects.get(pk=1)
        game.skill_level = 5
        game.title = "Clue"
        game.maker = "Milton Bradley"
        game.number_of_players = 6
        game.gamer = Gamer.objects.get(pk=1)
        game.save()
Ejemplo n.º 18
0
    def create(self, request):
        """Handle POST operations

        Returns:
            Response -- JSON serialized game instance
        """

        # Uses the token passed in the `Authorization` header
        gamer = Gamer.objects.get(user=request.auth.user)

        # Create a new Python instance of the Game class
        # and set its properties from what was sent in the
        # body of the request from the client.
        game = Game()
        game.title = request.data["title"]
        game.number_of_players = request.data["numberOfPlayers"]
        game.description = request.data["description"]

        game.gamer = gamer

        # Use the Django ORM to get the record from the database
        # whose `id` is what the client passed as the
        # `gameTypeId` in the body of the request.
        gametype = Game_Type.objects.get(pk=request.data["gameTypeId"])
        game.game_type = gametype

        # Try to save the new game to the database, then
        # serialize the game instance as JSON, and send the
        # JSON as a response to the client request
        try:
            game.save()
            serializer = GameSerializer(game, context={'request': request})
            # specify status code
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        # If anything went wrong, catch the exception and
        # send a response with a 400 status code to tell the
        # client that something was wrong with its request data
        except ValidationError as ex:
            return Response({"reason": ex.message},
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 19
0
 def test_get_game(self):
     #check to see if we can get an existing game, 1st seed the DB
     game = Game()
     game.game_type_id = 1
     game.title = 'Chess'
     game.description = 'GOAT Board Game'
     game.number_of_players = 2
     game.gamer_id = 1
     game.save()
     #make sure request is authenticated
     self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
     #initi8ate request and store response
     response = self.client.get(f'/games/{game.id}')
     #json rESPONSE JUST F*****G PARSE IT!!!
     json_response = json.loads(response.content)
     #assert that the game WAS RETRIEVED SUCCESSFULLY
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     #assert that the values are correct
     self.assertEqual(json_response['title'], 'Chess')
     self.assertEqual(json_response['description'], 'GOAT Board Game')
     self.assertEqual(json_response['number_of_players'], 2)
Ejemplo n.º 20
0
    def setUp(self):
        """
        Create a new account and create sample category
        """
        url = "/register"
        data = {
            "username": "******",
            "password": "******",
            "email": "*****@*****.**",
            "address": "100 Infinity Way",
            "phone_number": "555-1212",
            "first_name": "Steve",
            "last_name": "Brownlee",
            "bio": "Love those gamez!!"
        }

        # Initiate request and capture response
        response = self.client.post(url, data, format='json')

        # Parse the JSON in the response body
        json_response = json.loads(response.content)

        # Store the auth token
        self.token = json_response["token"]

        # Assert that a user was created
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        gametype = GameType()
        gametype.label = "Board game"
        gametype.save()

        game = Game()
        game.title = "title"
        game.number_of_players = 4
        game.description = "This gmae is fun"
        game.gamer_id = 1
        game_type = 1
        game.game_type_id = game_type
        game.save()
Ejemplo n.º 21
0
    def create(self, request):
        # use token passed in the 'Authorization' header
        gamer = Gamer.objects.get(user=request.auth.user)
        # create a new Python instance of the Game class con properties de REQUEST de client
        game = Game()
        game.title = request.data["title"]
        game.game_type_id = request.data["gameTypeId"]
        game.number_of_players = request.data["numberOfPlayers"]
        game.description = request.data["description"]
        game.gamer = gamer
        # now use the Djanog ORM to fetch the record from the database whose 'id' is what the client passed as gameTypeId
        game_type = GameType.objects.get(pk=request.data["gameTypeId"])
        game.game_type = game_type

        # try to save the new game to the db, then serialize it to JSON, then send that JSON back to client
        try:
            game.save()
            serializer = GameSerializer(game, context={'request': request})
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        except ValidationError as ex:
            return Response({"reason": ex.message},
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 22
0
    def test_change_game(self):
        """
        Ensure we can change an existing game.
        """
        game = Game()
        game.title = "Sorry"
        game.gametype_id = 1
        game.number_of_players = 4
        game.gamer_id = 1
        game.description = "Sucks to be you"
        game.save()

        # DEFINE NEW PROPERTIES FOR GAME
        data = {
            "title": "Sorry",
            "gameTypeId": 1,
            "numberOfPlayers": 4,
            "gamer": 1,
            "description": "Sorry suckaaa!"
        }

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.put(f"/games/{game.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"/games/{game.id}")
        json_response = json.loads(response.content)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        # print(f"(Hello){type(json_response)}")

        # Assert that the properties are correct
        self.assertEqual(json_response["title"], "Sorry")
        self.assertEqual(json_response["gametype"]["id"], 1)
        self.assertEqual(json_response["number_of_players"], 4)
        self.assertEqual(json_response["gamer"]["id"], 1)
        self.assertEqual(json_response["description"], "Sorry suckaaa!")
Ejemplo n.º 23
0
    def setUp(self):
        """
        Create a new account and set up a game
        """
        url = "/register"
        data = {
            "username": "******",
            "password": "******",
            "email": "*****@*****.**",
            "first_name": "Steve",
            "last_name": "Brownlee",
            "bio": "Love those eventz!!"
        }
        # Initiate request and capture response
        response = self.client.post(url, data, format='json')

        # Parse the JSON in the response body
        json_response = json.loads(response.content)

        # Store the auth token
        self.token = json_response["token"]

        # Assert that a user was created
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # Seed database with one game type
        gametype = GameType()
        gametype.label = "Board game"
        gametype.save()

        # Seed database with one game
        game = Game()
        game.title = "Checkers"
        game.gametype_id = 1
        game.number_of_players = 2
        game.gamer_id = 1
        game.description = "Great game"
        game.save()
Ejemplo n.º 24
0
    def create(self, request):
        gamer = Gamer.objects.get(user=request.auth.user)

        game = Game()
        game.title = request.data['title']
        game.maker = request.data['maker']
        game.number_of_players = request.data['numberOfPlayers']
        game.skill_level = request.data['skillLevel']
        game.gamer = gamer

        game_type = GameType.objects.get(pk=request.data['gameTypeId'])
        # select *
        # from gametype
        # where id=request.data['game_type_id']
        game.game_type = game_type

        try:
            game.save()
            serializer = GameSerializer(game, context={'request': request})
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        except ValidationError as ex:
            return Response({'reason': ex.message},
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 25
0
    def test_change_game(self):
        """
        Ensure we can change an existing game.
        """
        game = Game()
        game.gametype_id = 1
        game.skill_level = 5
        game.title = "Sorry"
        game.maker = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        # DEFINE NEW PROPERTIES FOR GAME
        data = {
            "gameTypeId": 1,
            "skillLevel": 2,
            "title": "Sorry",
            "maker": "Hasbro",
            "numberOfPlayers": 4
        }

        # print('GAME ID ' + str(game.id))
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.put(f"/games/{game.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"/games/{game.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["title"], "Sorry")
        self.assertEqual(json_response["maker"], "Hasbro")
        self.assertEqual(json_response["skill_level"], 2)
        self.assertEqual(json_response["number_of_players"], 4)
Ejemplo n.º 26
0
    def test_delete_game(self):
        """
        Ensure we can delete an existing game.
        """

        # Use the ORM to insert some test data into the database
        game = Game()
        game.gametype_id = 1
        game.skill_level = 5
        game.title = "Sorry"
        game.maker = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        # Authorize the request
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)

        # Use the API to delete the test game
        response = self.client.delete(f"/games/{game.id}")

        # GET GAME AGAIN TO VERIFY 404 response
        response = self.client.get(f"/games/{game.id}")
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
Ejemplo n.º 27
0
    def test_change_game(self):
        """
        Ensure we can change an existing game
        """
        game = Game()
        game.title = "Checkers"
        game.gametype_id = 1
        game.number_of_players = 2
        game.gamer_id = 1
        game.description = "Great game"
        game.save()

        # Define new properties for game
        data = {
            "title": "Checkers!",
            "gameTypeId": 1,
            "numberOfPlayers": 2,
            "gamer": 1,
            "description": "Swell boardgame"
        }

        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
        response = self.client.put(f"/games/{game.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"/games/{game.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["title"], "Checkers!")
        self.assertEqual(json_response["gametype"]["id"], 1)
        self.assertEqual(json_response["number_of_players"], 2)
        self.assertEqual(json_response["gamer"]["id"], 1)
        self.assertEqual(json_response["description"], "Swell boardgame")
Ejemplo n.º 28
0
    def test_get_all_games(self):
        """Ensure we can get all games"""

        # Seed the database with games
        game = Game()
        game.gametype_id = 1
        game.skill_level = 5
        game.title = "Monopoly"
        game.maker = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        game = Game()
        game.gametype_id = 1
        game.skill_level = 5
        game.title = "Clue"
        game.maker = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        game = Game()
        game.gametype_id = 1
        game.skill_level = 5
        game.title = "Sorry"
        game.maker = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        # Make sure request is authenticated
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)

        # Initiate request and store response
        response = self.client.get(f"/games")

        # Parse the JSON in the response body
        json_response = json.loads(response.content)

        # Assert that the games list is the correct length
        self.assertEqual(len(json_response), 3)

        # Assert that the values are correct
        self.assertEqual(json_response[0]["title"], "Monopoly")
        self.assertEqual(json_response[1]["title"], "Clue")
        self.assertEqual(json_response[2]["title"], "Sorry")
Ejemplo n.º 29
0
class GameTests(APITestCase):
    def setUp(self):
        """
        Create a new account and create sample category
        """
        url = "/register"
        data = {
            "username": "******",
            "password": "******",
            "email": "*****@*****.**",
            "address": "100 Infinity Way",
            "phone_number": "555-1212",
            "first_name": "Steve",
            "last_name": "Brownlee",
            "bio": "Love those gamez!!"
        }
        # Initiate request and capture response
        response = self.client.post(url, data, format='json')

        # Parse the JSON in the response body
        json_response = json.loads(response.content)

        # Store the auth token
        self.token = json_response["token"]

        # Assert that a user was created
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # SEED DATABASE WITH ONE GAME TYPE
        # This is needed because the API does not expose a /gametypes
        # endpoint for creating game types
        game_type = GameType()
        game_type.label = "Board game"
        game_type.save()

        self.game = Game()
        self.game.game_type_id = 1
        self.game.title = "Monopoly"
        self.game.skill_level = 1
        self.game.maker = "Milton Bradley"
        self.game.number_of_players = 4
        self.game.gamer_id = 1

        self.game.save()

    def test_create_game(self):
        """
        Ensure we can create a new game.
        """
        # DEFINE GAME PROPERTIES
        url = "/games"
        data = {
            "game_type_id": 1,
            "skill_level": 1,
            "title": "Clue",
            "maker": "Milton Bradley",
            "number_of_players": 6,
        }

        # Make sure request is authenticated
        self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token}')

        # Initiate request and store response
        response = self.client.post(url, data, format='json')

        # Assert that the game was created
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # Parse the JSON in the response body
        json_response = json.loads(response.content)

        # Assert that the properties on the created resource are correct.
        # Make sure json response matches  the data
        self.assertEqual(json_response["title"], data['title'])
        self.assertEqual(json_response["maker"], data['maker'])
        self.assertEqual(json_response["skill_level"], data['skill_level'])
        self.assertEqual(json_response["number_of_players"],
                         data['number_of_players'])

    def test_get_game(self):
        """
        Ensure we can get an existing game.
        """
        # Make sure request is authenticated
        self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token}')

        # Initiate request and store response
        response = self.client.get(f"/games/{self.game.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["title"], self.game.title)
        self.assertEqual(json_response["skill_level"], self.game.skill_level)
        self.assertEqual(json_response["maker"], self.game.maker)
        self.assertEqual(json_response["number_of_players"],
                         self.game.number_of_players)

    def test_change_game(self):

        game = Game()
        game.game_type_id = 1
        game.skill_level = 1
        game.title = "Sorry"
        game.maker = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        # DEFINE NEW PROPERTIES FOR GAME
        data = {
            "game_type_id": 1,
            "title": "Sorry",
            "skill_level": 1,
            "maker": "Hasbro",
            "number_of_players": 4
        }

        self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token}')
        response = self.client.put(f"/games/{game.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"/games/{game.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["title"], "Sorry")
        self.assertEqual(json_response["skill_level"], 1)
        self.assertEqual(json_response["maker"], "Hasbro")
        self.assertEqual(json_response["number_of_players"], 4)

    def test_delete_game(self):
        """
        Ensure we can delete an existing game.
        """
        game = Game()
        game.game_type_id = 1
        game.title = "Sorry"
        game.skill_level = 5
        game.maker = "Milton Bradley"
        game.number_of_players = 4
        game.gamer_id = 1
        game.save()

        self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token}')
        response = self.client.delete(f"/games/{game.id}")
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

        # GET GAME AGAIN TO VERIFY 404 response
        response = self.client.get(f"/games/{game.id}")
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)