async def test_add_duplicate_management_role(): # Given guild_id = 1001 duplicate_role = 6000 # The role is already a management role configs_repository = FakeConfigsRepository( guild_id=guild_id, management_roles=[duplicate_role]) # And we try to add it again event = _make_event(guild_id=guild_id, options={ 'role': duplicate_role, }) message = "Role is already a management role." message_provider = MagicMock(MessageProvider) message_provider.add_duplicate_management_role.return_value = message route = AddManagementRoleRoute( configs_repository=configs_repository, message_provider=message_provider, ) # When response = await route.call(event) # Then guild_config = configs_repository.get(guild_id) assert duplicate_role in guild_config.management_roles assert len(guild_config.management_roles) == 1 assert response.is_ephemeral assert response.content == message
async def test_admin_info(): # Given guild_id = 1001 configs_repository = FakeConfigsRepository(guild_id=guild_id) guild_config = configs_repository.get(guild_id) static_message = "admin-info" message_provider = FakeMessageProvider() message_provider.expect_channel_admin_info_call( expected_config=guild_config, message=static_message) route = GuildInfoRoute( message_provider=message_provider, configs_repository=configs_repository, ) event = _make_event(guild_id=guild_id) # When response = await route.call(event) # Then assert response.is_ephemeral assert response.content == static_message
async def test_add_management_role(): # Given guild_id = 1001 role = 6000 # We have no management roles configs_repository = FakeConfigsRepository(guild_id=guild_id) # And we add a new management role event = _make_event(guild_id=guild_id, options={'role': role}) message = "Management role added" message_provider = MagicMock(MessageProvider) message_provider.added_management_role.return_value = message route = AddManagementRoleRoute( configs_repository=configs_repository, message_provider=message_provider, ) # When response = await route.call(event) # Then guild_config = configs_repository.get(guild_id) assert role in guild_config.management_roles assert response.is_ephemeral assert response.content == message
async def test_add_duplicate_management_channel(): # Given guild_id = 1001 channel = 9500 # We have a channel that's a management channel configs_repository = FakeConfigsRepository(guild_id=guild_id, management_channels=[channel]) # And we try to add it again event = _make_event(guild_id=guild_id, options={'channel': channel}) error_message = "channel is already a management channel." message_provider = MagicMock(MessageProvider) message_provider.error_duplicate_management_channel.return_value = error_message route = AddManagementChannelRoute( configs_repository=configs_repository, message_provider=message_provider, ) # When response = await route.call(event) # Then guild_config = configs_repository.get(guild_id) assert channel in guild_config.management_channels assert len(guild_config.management_channels) == 1 assert response.is_ephemeral assert response.content == error_message
async def test_add_management_channel(): # Given guild_id = 1001 channel = 9500 # We have no management channels yet configs_repository = FakeConfigsRepository(guild_id=guild_id) message = "New management channel added." message_provider = MagicMock(MessageProvider) message_provider.added_management_channel.return_value = message # We add the new management channel event = _make_event( guild_id=guild_id, options={'channel': channel}, ) route = AddManagementChannelRoute( message_provider=message_provider, configs_repository=configs_repository, ) # When response = await route.call(event) # Then guild_config = configs_repository.get(guild_id) assert channel in guild_config.management_channels assert response.is_ephemeral assert response.content == message
async def test_remove_invalid_management_role(): # Given guild_id = 1001 management_role = 6060 role_to_remove = 6050 # We have a management role configs_repository = FakeConfigsRepository( guild_id=guild_id, management_roles=[management_role]) # And we remove another role event = _make_event(guild_id=guild_id, options={'role': role_to_remove}) route = RemoveManagementRoleRoute( message_provider=FakeMessageProvider(), configs_repository=configs_repository, ) # When response = await route.call(event) # Then guild_config = configs_repository.get(guild_id) assert role_to_remove not in guild_config.management_roles assert management_role in guild_config.management_roles assert response.is_ephemeral
async def test_remove_invalid_management_channel(): # Given guild_id = 1001 channel_to_remove = 9500 other_channel = 9510 # We have a management channel configs_repository = FakeConfigsRepository( guild_id=guild_id, management_channels=[other_channel]) # And we try to remove a channel that's not a management channel event = _make_event(guild_id=guild_id, options={'channel': channel_to_remove}) route = RemoveManagementChannelRoute( message_provider=FakeMessageProvider(), configs_repository=configs_repository, ) # When response = await route.call(event) # Then guild_config = configs_repository.get(guild_id) assert channel_to_remove not in guild_config.management_channels assert other_channel in guild_config.management_channels assert response.is_ephemeral
async def test_remove_management_channel(): # Given guild_id = 1001 management_channel = 9500 # We have a management channel configs_repository = FakeConfigsRepository( guild_id=guild_id, management_channels=[management_channel]) # And we remove it event = _make_event( guild_id=guild_id, options={'channel': management_channel}, ) route = RemoveManagementChannelRoute( configs_repository=configs_repository, message_provider=FakeMessageProvider(), ) # When response = await route.call(event) # Then guild_config = configs_repository.get(guild_id) assert management_channel not in guild_config.management_channels assert response.is_ephemeral
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
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