def test_find_game_returns_401_if_no_authentication(): """ When a game is requested, but no authentication is provided HTTP status code 401 should be returned. """ request = Mock(json={}, headers=Mock(get=Mock(return_value=None))) with pytest.raises(werkzeug.exceptions.Unauthorized): main.find_game(request)
def test_find_game_returns_401_if_bad_authentication(): """ When a game is requested, but incorrect authentication is provided HTTP status code 401 should be returned. """ (main.db.collection.return_value.where.return_value.stream.return_value ) = [] request = Mock(json={}, headers=Mock(get=Mock(return_value='Bearer asd'))) with pytest.raises(werkzeug.exceptions.Unauthorized): main.find_game(request)
def test_find_game_returns_waiting_if_no_gameservers_are_ready(): """ When a game is requested with no gameservers available, it should tell the user that it is waiting for a game. """ def where(field, *_, **__): """ Side effect for db.collection('users').where(...).stream() """ if field == 'token': mock = Mock() mock.stream.return_value = [MagicMock(id='asd')] return mock if field == 'status': mock = Mock() mock.stream.return_value = [MagicMock(id='sdf')] return mock raise ValueError() (main.db.collection.return_value.where.side_effect) = where request = Mock(json={}, headers=Mock(get=Mock(return_value='Bearer asd'))) response = json.loads(main.find_game(request)) assert response['status'] == 'waiting_for_game'
def test_find_game_returns_in_game_if_other_players_are_waiting(): """ When a game is requested while another user is waiting, it should tell the user they have found a game. """ def where(field, *_, **__): """ Side effect for db.collection('users').where(...).stream() """ if field == 'token': mock = Mock() mock.stream.return_value = [MagicMock(id='asd')] return mock if field == 'status': mock = Mock() mock.stream.return_value = [MagicMock(id='sdf')] return mock raise ValueError() (main.db.collection.return_value.where.side_effect) = where request = Mock(json={}, headers=Mock(get=Mock(return_value='Bearer asd'))) response = json.loads(main.find_game(request)) assert response['status'] == 'in_game'
def test_find_game_returns_in_game_if_already_in_game(): """ When a game is requested and one has been found between the this request and the last request, the 'in_game' status and the gameserver URL should be returned. """ user = Mock() user.to_dict.return_value = { 'status': 'in_game', 'gameserver': 'tcp://test_server:123' } (main.db.collection.return_value.where.return_value.stream.return_value ) = [user] request = Mock(json={}, headers=Mock(get=Mock(return_value='Bearer asd'))) response = json.loads(main.find_game(request)) assert response['status'] == 'in_game' assert response['gameserver'] == 'tcp://test_server:123'