Esempio n. 1
0
async def test_guess_game_does_not_exist():
    # Given
    guild_id = 1001
    game_id = 'fun-game'
    user_id = 2001
    event_channel_id = 7002
    member = DiscordMember()

    fake_games_repository = FakeGamesRepository([])

    error_message = "error dm"
    message_provider = MagicMock(MessageProvider)
    message_provider.error_game_not_found.return_value = error_message

    guess_route = _route(
        games_repository=fake_games_repository,
        message_provider=message_provider,
    )

    # When
    event = _create_guess_event(
        guild_id=guild_id,
        game_id=game_id,
        user_id=user_id,
        event_channel_id=event_channel_id,
        user_nickname='nickname',
        member=member,
    )
    response = await guess_route.call(event)

    # Then
    assert len(fake_games_repository.get_all(guild_id=guild_id)) == 0

    assert response.is_ephemeral
    assert response.content == error_message
Esempio n. 2
0
async def test_admin_management_call():
    # Given
    guild_id = 1000
    management_role = 2000
    other_role = 2001
    management_channel = 3000
    other_channel = 3001

    configs_repository = FakeConfigsRepository(
        guild_id=guild_id,
        management_channels=[management_channel],
        management_roles=[management_role])

    command_authorizer = CommandAuthorizerImpl(
        configs_repository=configs_repository)

    event = DiscordEvent(
        guild_id=guild_id,
        channel_id=other_channel,
        member=DiscordMember(roles=[other_role], is_admin=True),
    )

    # When
    allowed = await command_authorizer.authorize_management_call(event=event)

    # Then
    assert allowed
Esempio n. 3
0
async def test_handle():
    # Given
    command_name = "admin"
    subcommand_name = "info"

    event = DiscordEvent(command=DiscordCommand(
        command_name=command_name,
        subcommand_name=subcommand_name,
    ),
                         member=DiscordMember())

    mock_response = DiscordResponse.ephemeral_channel_message(
        "Well handled brother!")

    mock_route_handler = AsyncMock(RouteHandler)
    mock_route_handler.handle.return_value = mock_response

    router = _router(route_handler=mock_route_handler, )

    # When
    response = await router.route(event)

    # Then
    mock_route_handler.handle.assert_called()
    arguments = mock_route_handler.handle.call_args[0]
    assert arguments[0] == event
    assert arguments[1].command == command_name
    if subcommand_name:
        assert arguments[1].subcommand == subcommand_name

    assert response.status_code == 200
    assert json.loads(response.body) == mock_response.json()
Esempio n. 4
0
def _make_event(guild_id: int = -1,
                options: typing.Dict = None) -> DiscordEvent:
    if options is None:
        options = {}

    return DiscordEvent(command=DiscordCommand(
        command_name="admin",
        subcommand_name="remove-management-role",
        options=options),
                        guild_id=guild_id,
                        member=DiscordMember())
Esempio n. 5
0
def _make_event(guild_id: int = -1,
                options: typing.Dict = None) -> DiscordEvent:
    if options is None:
        options = {}

    return DiscordEvent(command=DiscordCommand(
        command_id=-1,
        command_name="admin",
        subcommand_name="does-not-matter",
        options=options),
                        guild_id=guild_id,
                        member=DiscordMember())
Esempio n. 6
0
async def test_authorized_admin_call():
    # Given
    guild_id = 1000

    command_authorizer = CommandAuthorizerImpl(
        configs_repository=FakeConfigsRepository(guild_id))

    event = DiscordEvent(
        guild_id=guild_id,
        member=DiscordMember(is_admin=True),
    )

    # When
    allowed = await command_authorizer.authorize_admin_call(event=event)

    # Then
    assert allowed
Esempio n. 7
0
async def test_guess_duplicate_guess():
    # Given
    guild_id = 1000
    game_id = 'game-id'
    event_channel_id = 7002
    guessing_user_id = 2000
    member = DiscordMember(user_id=guessing_user_id, )

    old_guess = '100'
    new_guess = '100'
    existing_guesses = {
        guessing_user_id: GameGuess(user_id=guessing_user_id, guess=old_guess),
    }

    # We have a game
    existing_game = Game(guild_id=guild_id,
                         game_id=game_id,
                         guesses=existing_guesses)
    games_repository = FakeGamesRepository(games=[existing_game])

    duplicate_guess_message = "You already placed a guess for this game."
    message_provider = MagicMock(MessageProvider)
    message_provider.error_duplicate_guess.return_value = duplicate_guess_message

    guess_route = _route(games_repository=games_repository,
                         message_provider=message_provider)

    # When we guess '42' as the same user
    event = _create_guess_event(
        guild_id=guild_id,
        user_id=guessing_user_id,
        game_id=game_id,
        guess=new_guess,
        event_channel_id=event_channel_id,
        member=member,
    )
    response = await guess_route.call(event)

    # Then no new guess is added
    saved_game = games_repository.get(guild_id, existing_game.game_id)
    assert saved_game.guesses[guessing_user_id].guess == old_guess

    # And an ephemeral error is the response
    assert response.is_ephemeral
    assert response.content == duplicate_guess_message
Esempio n. 8
0
def _make_event(guild_id: int = -1, options: typing.Dict = None, discord_member: DiscordMember = None,
                channel_id: int = -1):
    if options is None:
        options = {}

    if discord_member is None:
        discord_member = DiscordMember()

    return DiscordEvent(
        guild_id=guild_id,
        channel_id=channel_id,
        command=DiscordCommand(
            command_name="manage",
            subcommand_name="edit-guess",
            options=options,
        ),
        member=discord_member
    )
Esempio n. 9
0
async def test_guess_closed_game():
    # Given
    guild_id = 1515
    game_id = 'closed-game'
    guessing_user_id = 3000
    other_user_id = 3050
    event_channel_id = 7775
    member = DiscordMember()

    # We have a closed game
    game = Game(game_id=game_id,
                guild_id=guild_id,
                closed=True,
                guesses={
                    other_user_id: GameGuess(),
                })
    games_repository = FakeGamesRepository([game])

    duplicate_guess_message = "This game has been closed for new guesses."
    message_provider = MagicMock(MessageProvider)
    message_provider.error_guess_on_closed_game.return_value = duplicate_guess_message

    route = _route(
        games_repository=games_repository,
        message_provider=message_provider,
    )

    # When
    event = _create_guess_event(
        guild_id=guild_id,
        game_id=game_id,
        user_id=guessing_user_id,
        event_channel_id=event_channel_id,
        member=member,
    )
    response = await route.call(event)

    # Then: no guess is added
    saved_game = games_repository.get(guild_id, game_id)
    assert list(saved_game.guesses.keys()) == [other_user_id]

    # And an ephemeral error is the response
    assert response.is_ephemeral
    assert response.content == duplicate_guess_message
Esempio n. 10
0
def _create_guess_event(guild_id: int,
                        game_id: str,
                        user_id: int = -1,
                        user_nickname: str = 'nickname',
                        guess: str = 'not-relevant',
                        event_channel_id: int = -1,
                        member: DiscordMember = None):
    if member is None:
        member = DiscordMember(user_id=user_id, nickname=user_nickname)

    event = DiscordEvent(
        channel_id=event_channel_id,
        command=DiscordCommand(command_name="guess",
                               options={
                                   'game-id': game_id,
                                   'guess': guess,
                               }),
        member=member,
        guild_id=guild_id,
    )

    return event
async def test_post_invalid_game_id_sends_dm_error():
    # Given
    guild_id = 1001
    game_id = 'game-1'
    channel_id = 50001
    event_channel_id = 7733

    # We have no games
    games_repository = FakeGamesRepository([])
    discord_messaging = FakeDiscordMessaging()

    formatted_error = "mock formatted error"
    message_provider = MagicMock(MessageProvider)
    message_provider.error_game_not_found.return_value = formatted_error

    post_route = PostRoute(
        games_repository=games_repository,
        discord_messaging=discord_messaging,
        message_provider=message_provider,
    )

    discord_member = DiscordMember()

    # When
    event = _make_event(guild_id=guild_id,
                        discord_member=discord_member,
                        channel_id=event_channel_id,
                        options={
                            'game-id': game_id,
                            'channel': channel_id,
                        })
    response = await post_route.call(event)

    # Then
    message_provider.error_game_not_found.assert_called_with(game_id)

    assert response.is_ephemeral
    assert response.content == formatted_error