Skip to content

Commit

Permalink
Add tests for CommandLevelChannelPermissionChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Nov 15, 2024
1 parent 4f6ce2f commit 8589625
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.test.interactions;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.PrivilegeConfig;
import net.dv8tion.jda.api.interactions.commands.privileges.IntegrationPrivilege;
import net.dv8tion.jda.internal.entities.SelfUserImpl;
import net.dv8tion.jda.internal.utils.interactions.commands.AppLevelChannelPermissionChecks;
import net.dv8tion.jda.internal.utils.interactions.commands.AppLevelUserOrRolePermissionChecks;
import net.dv8tion.jda.internal.utils.interactions.commands.CommandLevelChannelPermissionChecks;
import net.dv8tion.jda.internal.utils.interactions.commands.CommandLevelUserOrRolePermissionChecks;
import net.dv8tion.jda.test.Constants;
import net.dv8tion.jda.test.IntegrationTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mock;
import org.mockito.MockedStatic;

import java.util.Arrays;

import static org.mockito.Mockito.*;

public abstract class AbstractPrivilegeConfigTest extends IntegrationTest
{

protected static final boolean ENABLED = true;
protected static final boolean DISABLED = false;

@Mock
protected Guild guild;
@Mock
protected GuildChannel channel;
@Mock
protected Member member;
@Mock
protected Command command;
@Mock
protected PrivilegeConfig config;

protected MockedStatic<CommandLevelChannelPermissionChecks> commandLevelChannelMock;
protected MockedStatic<AppLevelChannelPermissionChecks> appLevelChannelMock;
protected MockedStatic<CommandLevelUserOrRolePermissionChecks> commandLevelUserRoleMock;
protected MockedStatic<AppLevelUserOrRolePermissionChecks> appLevelUserRoleMock;

@BeforeEach
void setupMocks()
{
when(jda.getSelfUser()).thenReturn(new SelfUserImpl(Constants.BUTLER_USER_ID, jda));
when(guild.getIdLong()).thenReturn(Constants.GUILD_ID);
when(channel.getIdLong()).thenReturn(Constants.CHANNEL_ID);
when(config.canMemberRun(any(), any(), any())).thenCallRealMethod();

commandLevelChannelMock = mockStatic(CommandLevelChannelPermissionChecks.class);
appLevelChannelMock = mockStatic(AppLevelChannelPermissionChecks.class);
commandLevelUserRoleMock = mockStatic(CommandLevelUserOrRolePermissionChecks.class);
appLevelUserRoleMock = mockStatic(AppLevelUserOrRolePermissionChecks.class);
}

@AfterEach
void teardownMocks()
{
commandLevelChannelMock.close();
appLevelChannelMock.close();
commandLevelUserRoleMock.close();
appLevelUserRoleMock.close();
}

protected void useCommandPrivileges(IntegrationPrivilege... privileges)
{
when(config.getCommandPrivileges(command)).thenReturn(Arrays.asList(privileges));
}

protected void useAppPrivileges(IntegrationPrivilege... privileges)
{
when(config.getApplicationPrivileges()).thenReturn(Arrays.asList(privileges));
}

protected IntegrationPrivilege channelPrivilege(boolean enabled)
{
return new IntegrationPrivilege(guild, IntegrationPrivilege.Type.CHANNEL, enabled, Constants.CHANNEL_ID);
}

protected IntegrationPrivilege allChannelsPrivilege(boolean enabled)
{
return new IntegrationPrivilege(guild, IntegrationPrivilege.Type.CHANNEL, enabled, Constants.GUILD_ID - 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.test.interactions;

import net.dv8tion.jda.internal.utils.interactions.commands.AppLevelChannelPermissionChecks;
import net.dv8tion.jda.internal.utils.interactions.commands.CommandLevelChannelPermissionChecks;
import net.dv8tion.jda.internal.utils.interactions.commands.CommandLevelUserOrRolePermissionChecks;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;

public class CommandLevelChannelPrivilegeTests extends AbstractPrivilegeConfigTest
{

@Test
@DisplayName("Permission for channel exists and is disabled, " +
"command can't be run")
public void deniedChannelExists()
{
// Setup privileges
useAppPrivileges();
useCommandPrivileges(channelPrivilege(DISABLED));

// Logic that needs to actually run
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();

// Do run
assertThat(CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).isFalse();

// Verify what got called
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), never());
appLevelChannelMock.verifyNoInteractions();
commandLevelUserRoleMock.verifyNoInteractions();
appLevelUserRoleMock.verifyNoInteractions();
}

@Test
@DisplayName("Permission for channel exists and is enabled, " +
"forward to user/role checks")
public void enabledChannelExists()
{
// Setup privileges
useAppPrivileges();
useCommandPrivileges(channelPrivilege(ENABLED));

// Logic that needs to actually run
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();

// Do run
CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command); // No result assert, only test forwarding

// Verify what got called
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), never());
appLevelChannelMock.verifyNoInteractions();
commandLevelUserRoleMock.verify(() -> CommandLevelUserOrRolePermissionChecks.canMemberRun(config, channel, member, command), times(1));
appLevelUserRoleMock.verifyNoInteractions();
}

@Test
@DisplayName("Permission for channel does not exist, " +
"finds one that applies to all channels but is disabled")
public void channelDoesNotExistAndDisabledAllChannels()
{
// Setup privileges
useAppPrivileges();
useCommandPrivileges(allChannelsPrivilege(DISABLED));

// Logic that needs to actually run
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command)).thenCallRealMethod();

// Do run
assertThat(CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).isFalse();

// Verify what got called
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), times(1));
appLevelChannelMock.verifyNoInteractions();
commandLevelUserRoleMock.verifyNoInteractions();
appLevelUserRoleMock.verifyNoInteractions();
}

@Test
@DisplayName("Permission for channel does not exist, " +
"finds one that applies to all channels and is enabled, " +
"forward to user/role checks")
public void channelDoesNotExistButEnabledAllChannels()
{
// Setup privileges
useAppPrivileges();
useCommandPrivileges(allChannelsPrivilege(ENABLED));

// Logic that needs to actually run
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command)).thenCallRealMethod();

// Do run
CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command); // No result assert, only test forwarding

// Verify what got called
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), times(1));
appLevelChannelMock.verifyNoInteractions();
commandLevelUserRoleMock.verify(() -> CommandLevelUserOrRolePermissionChecks.canMemberRun(config, channel, member, command));
appLevelUserRoleMock.verifyNoInteractions();
}

@Test
@DisplayName("Permission for channel does not exist, " +
"none applies to all channels, " +
"forward to app-level channel checks")
public void channelDoesNotExistAndNoneAllChannels()
{
// Setup privileges
useAppPrivileges();
useCommandPrivileges();

// Logic that needs to actually run
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command)).thenCallRealMethod();

// Do run
CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command); // No result assert, only test forwarding

// Verify what got called
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), times(1));
appLevelChannelMock.verify(() -> AppLevelChannelPermissionChecks.canMemberRun(config, channel, member, command));
commandLevelUserRoleMock.verifyNoInteractions();
appLevelUserRoleMock.verifyNoInteractions();
}
}

0 comments on commit 8589625

Please sign in to comment.