Beispiel #1
0
def test_game_update_state(app, patch_geventws):  # pylint: disable=unused-argument
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}?state' page is requested (PUT)
    THEN check that the response is valid
    """
    game.send_game_state_message = MagicMock()

    # Create a new game
    game_id = test_utils.create_game(0)

    # Verify the database agrees.
    db_response = game.read_one(game_id)
    initial_state = db_response.get("state")
    assert initial_state == 0
    state = initial_state + 1

    with app.test_client() as test_client:
        # Attempt to access the advance state game api
        response = test_client.put(f"/api/game/{game_id}?state=true")
        assert response.status == "200 OK"

    # Make sure the WS message went out
    game.send_game_state_message.assert_called_with(game_id, state)
    # Verify the database agrees.
    db_response = game.read_one(game_id)
    assert db_response is not None
    assert game_id == db_response.get("game_id")
    assert state == db_response.get("state")
Beispiel #2
0
def test_game_update_state_no_change(app, patch_geventws):  # pylint: disable=unused-argument
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}?state' page is requested (PUT)
    THEN check that the response is valid
    """
    game_id, round_id, team_ids, player_ids = test_utils.setup_complete_game(4)

    # Set state to the last one available.
    test_utils.set_game_state(game_id, len(GameModes.modes))

    # Verify the database agrees.
    db_response = game.read_one(game_id)
    initial_state = db_response.get("state")
    assert len(GameModes.modes) == initial_state

    with app.test_client() as test_client:
        # Attempt to access the delete game api
        response = test_client.put(f"/api/game/{game_id}?state=false")
        assert response.status == "200 OK"

    # Verify the database agrees.
    db_response = game.read_one(game_id)
    assert db_response is not None
    assert game_id == db_response.get("game_id")
    assert db_response.get("state") == initial_state
Beispiel #3
0
def test_game_read_one(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}' page is requested (GET)
    THEN check that the response is a UUID and contains the expected information
    """
    # Create a new game
    db_response, status = game.create(4)
    assert status == 201
    assert db_response is not None
    game_id = db_response.get("game_id")

    with app.test_client() as test_client:
        # Attempt to access the create round api
        response = test_client.get(f"/api/game/{game_id}")
        assert response.status == "200 OK"
        assert response.get_data(as_text=True) is not None
        # This is a JSON formatted STRING
        response_str = response.get_data(as_text=True)
        response_data = json.loads(response_str)
        r_game_id = response_data.get("game_id")
        assert r_game_id != ""
        assert r_game_id == game_id
        assert test_utils.UUID_REGEX.match(r_game_id)

    # Verify the database agrees.
    db_response = game.read_one(game_id)
    assert db_response is not None
    assert game_id == db_response.get("game_id")
Beispiel #4
0
def test_game_create(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game' page is requested (POST)
    THEN check that the response is a UUID
    """
    with app.test_client() as test_client:
        # Attempt to access the create game api
        response = test_client.post("/api/game?kitty_size=4")
        assert response.status == "201 CREATED"
        # This is a JSON formatted STRING
        response_str = response.get_data(as_text=True)
        assert response_str is not None
        response_data = json.loads(response_str)

        game_id = response_data["game_id"]
        assert game_id != ""
        assert test_utils.UUID_REGEX.match(game_id)

    # Verify the database agrees.
    db_response = game.read_one(game_id)
    assert db_response is not None
    assert game_id == db_response.get("game_id")
    assert db_response.get("kitty_size") == 4
    assert db_response.get("state") == 0
Beispiel #5
0
def test_game_update_invalid_game(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}' page is requested (PUT)
    THEN check that the response is a UUID
    """
    # Create a new game
    game_id = str(uuid.uuid4())

    with app.test_client() as test_client:
        new_kitty = 12
        # Attempt to access the set kitty size game api
        response = test_client.put(f"/api/game/{game_id}?kitty_size={new_kitty}")
        assert response.status == "404 NOT FOUND"

    # Verify the database agrees.
    with pytest.raises(exceptions.NotFound):
        game.read_one(game_id)
Beispiel #6
0
def test_game_read_one_missing(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}' page is requested (GET) with non-existent game_id
    THEN check that the response is correct
    """
    game_id = str(uuid.uuid4())

    with app.test_client() as test_client:
        # Attempt to access the create round api
        response = test_client.get(f"/api/game/{game_id}")
        assert response.status == "404 NOT FOUND"

    # Verify the database agrees.
    with pytest.raises(exceptions.NotFound):
        db_response = game.read_one(game_id)
        assert db_response is not None
Beispiel #7
0
def test_game_update_kitty_size(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game/{game_id}' page is requested (PUT)
    THEN check that the response is a UUID
    """
    new_kitty = 10
    # Create a new game
    game_id = test_utils.create_game(0)

    with app.test_client() as test_client:
        # Attempt to access the delete game api
        response = test_client.put(f"/api/game/{game_id}?kitty_size={new_kitty}")
        assert response.status == "200 OK"

    # Verify the database agrees.
    db_response = game.read_one(game_id)
    assert db_response is not None
    assert game_id == db_response.get("game_id")
    assert new_kitty == db_response.get("kitty_size")
Beispiel #8
0
def test_game_delete(app):
    """
    GIVEN a Flask application configured for testing
    WHEN the '/api/game' page is requested (DELETE)
    THEN check that the response is a UUID
    """
    # Create a new game
    game_id = test_utils.create_game(0)

    with app.test_client() as test_client:
        # Attempt to access the delete game api
        response = test_client.delete(f"/api/game/{game_id}")
        assert response.status == "200 OK"

        # Attempt to retrieve the now-deleted game id
        response = test_client.get(f"/api/game/{game_id}")
        assert response.status == "404 NOT FOUND"

    # Verify the database agrees.
    with pytest.raises(exceptions.NotFound):
        db_response = game.read_one(game_id)
        assert db_response is not None