Skip to content

Commit b3a1b36

Browse files
committed
Add tests for CommandLevelChannelPermissionChecks
1 parent 4f6ce2f commit b3a1b36

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.dv8tion.jda.test.interactions.privileges;
18+
19+
import net.dv8tion.jda.api.entities.Guild;
20+
import net.dv8tion.jda.api.entities.Member;
21+
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
22+
import net.dv8tion.jda.api.interactions.commands.Command;
23+
import net.dv8tion.jda.api.interactions.commands.PrivilegeConfig;
24+
import net.dv8tion.jda.api.interactions.commands.privileges.IntegrationPrivilege;
25+
import net.dv8tion.jda.internal.entities.SelfUserImpl;
26+
import net.dv8tion.jda.internal.utils.interactions.commands.AppLevelChannelPermissionChecks;
27+
import net.dv8tion.jda.internal.utils.interactions.commands.AppLevelUserOrRolePermissionChecks;
28+
import net.dv8tion.jda.internal.utils.interactions.commands.CommandLevelChannelPermissionChecks;
29+
import net.dv8tion.jda.internal.utils.interactions.commands.CommandLevelUserOrRolePermissionChecks;
30+
import net.dv8tion.jda.test.Constants;
31+
import net.dv8tion.jda.test.IntegrationTest;
32+
import org.junit.jupiter.api.AfterEach;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.mockito.Mock;
35+
import org.mockito.MockedStatic;
36+
37+
import java.util.Arrays;
38+
39+
import static org.mockito.Mockito.mockStatic;
40+
import static org.mockito.Mockito.when;
41+
42+
public abstract class AbstractPrivilegeConfigTest extends IntegrationTest
43+
{
44+
45+
protected static final boolean ENABLED = true;
46+
protected static final boolean DISABLED = false;
47+
48+
@Mock
49+
protected Guild guild;
50+
@Mock
51+
protected GuildChannel channel;
52+
@Mock
53+
protected Member member;
54+
@Mock
55+
protected Command command;
56+
@Mock
57+
protected PrivilegeConfig config;
58+
59+
protected MockedStatic<CommandLevelChannelPermissionChecks> commandLevelChannelMock;
60+
protected MockedStatic<AppLevelChannelPermissionChecks> appLevelChannelMock;
61+
protected MockedStatic<CommandLevelUserOrRolePermissionChecks> commandLevelUserRoleMock;
62+
protected MockedStatic<AppLevelUserOrRolePermissionChecks> appLevelUserRoleMock;
63+
64+
@BeforeEach
65+
void setupMocks()
66+
{
67+
when(jda.getSelfUser()).thenReturn(new SelfUserImpl(Constants.BUTLER_USER_ID, jda));
68+
when(guild.getIdLong()).thenReturn(Constants.GUILD_ID);
69+
when(channel.getIdLong()).thenReturn(Constants.CHANNEL_ID);
70+
71+
commandLevelChannelMock = mockStatic(CommandLevelChannelPermissionChecks.class);
72+
appLevelChannelMock = mockStatic(AppLevelChannelPermissionChecks.class);
73+
commandLevelUserRoleMock = mockStatic(CommandLevelUserOrRolePermissionChecks.class);
74+
appLevelUserRoleMock = mockStatic(AppLevelUserOrRolePermissionChecks.class);
75+
}
76+
77+
@AfterEach
78+
void teardownMocks()
79+
{
80+
commandLevelChannelMock.close();
81+
appLevelChannelMock.close();
82+
commandLevelUserRoleMock.close();
83+
appLevelUserRoleMock.close();
84+
}
85+
86+
protected void useCommandPrivileges(IntegrationPrivilege... privileges)
87+
{
88+
when(config.getCommandPrivileges(command)).thenReturn(Arrays.asList(privileges));
89+
}
90+
91+
protected void useAppPrivileges(IntegrationPrivilege... privileges)
92+
{
93+
when(config.getApplicationPrivileges()).thenReturn(Arrays.asList(privileges));
94+
}
95+
96+
protected IntegrationPrivilege channelPrivilege(boolean enabled)
97+
{
98+
return new IntegrationPrivilege(guild, IntegrationPrivilege.Type.CHANNEL, enabled, Constants.CHANNEL_ID);
99+
}
100+
101+
protected IntegrationPrivilege allChannelsPrivilege(boolean enabled)
102+
{
103+
return new IntegrationPrivilege(guild, IntegrationPrivilege.Type.CHANNEL, enabled, Constants.GUILD_ID - 1);
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.dv8tion.jda.test.interactions.privileges;
18+
19+
import net.dv8tion.jda.internal.utils.interactions.commands.AppLevelChannelPermissionChecks;
20+
import net.dv8tion.jda.internal.utils.interactions.commands.CommandLevelChannelPermissionChecks;
21+
import net.dv8tion.jda.internal.utils.interactions.commands.CommandLevelUserOrRolePermissionChecks;
22+
import org.junit.jupiter.api.DisplayName;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.mockito.Mockito.never;
27+
import static org.mockito.Mockito.times;
28+
29+
public class CommandLevelChannelPrivilegeTests extends AbstractPrivilegeConfigTest
30+
{
31+
32+
@Test
33+
@DisplayName("Permission for channel exists and is disabled, " +
34+
"command can't be run")
35+
public void deniedChannelExists()
36+
{
37+
// Setup privileges
38+
useAppPrivileges();
39+
useCommandPrivileges(channelPrivilege(DISABLED));
40+
41+
// Logic that needs to actually run
42+
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();
43+
44+
// Do run
45+
assertThat(CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).isFalse();
46+
47+
// Verify what got called
48+
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), never());
49+
appLevelChannelMock.verifyNoInteractions();
50+
commandLevelUserRoleMock.verifyNoInteractions();
51+
appLevelUserRoleMock.verifyNoInteractions();
52+
}
53+
54+
@Test
55+
@DisplayName("Permission for channel exists and is enabled, " +
56+
"forward to user/role checks")
57+
public void enabledChannelExists()
58+
{
59+
// Setup privileges
60+
useAppPrivileges();
61+
useCommandPrivileges(channelPrivilege(ENABLED));
62+
63+
// Logic that needs to actually run
64+
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();
65+
66+
// Do run
67+
CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command); // No result assert, only test forwarding
68+
69+
// Verify what got called
70+
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), never());
71+
appLevelChannelMock.verifyNoInteractions();
72+
commandLevelUserRoleMock.verify(() -> CommandLevelUserOrRolePermissionChecks.canMemberRun(config, channel, member, command), times(1));
73+
appLevelUserRoleMock.verifyNoInteractions();
74+
}
75+
76+
@Test
77+
@DisplayName("Permission for channel does not exist, " +
78+
"finds one that applies to all channels but is disabled")
79+
public void channelDoesNotExistAndDisabledAllChannels()
80+
{
81+
// Setup privileges
82+
useAppPrivileges();
83+
useCommandPrivileges(allChannelsPrivilege(DISABLED));
84+
85+
// Logic that needs to actually run
86+
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();
87+
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command)).thenCallRealMethod();
88+
89+
// Do run
90+
assertThat(CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).isFalse();
91+
92+
// Verify what got called
93+
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), times(1));
94+
appLevelChannelMock.verifyNoInteractions();
95+
commandLevelUserRoleMock.verifyNoInteractions();
96+
appLevelUserRoleMock.verifyNoInteractions();
97+
}
98+
99+
@Test
100+
@DisplayName("Permission for channel does not exist, " +
101+
"finds one that applies to all channels and is enabled, " +
102+
"forward to user/role checks")
103+
public void channelDoesNotExistButEnabledAllChannels()
104+
{
105+
// Setup privileges
106+
useAppPrivileges();
107+
useCommandPrivileges(allChannelsPrivilege(ENABLED));
108+
109+
// Logic that needs to actually run
110+
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();
111+
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command)).thenCallRealMethod();
112+
113+
// Do run
114+
CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command); // No result assert, only test forwarding
115+
116+
// Verify what got called
117+
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), times(1));
118+
appLevelChannelMock.verifyNoInteractions();
119+
commandLevelUserRoleMock.verify(() -> CommandLevelUserOrRolePermissionChecks.canMemberRun(config, channel, member, command), times(1));
120+
appLevelUserRoleMock.verifyNoInteractions();
121+
}
122+
123+
@Test
124+
@DisplayName("Permission for channel does not exist, " +
125+
"none applies to all channels, " +
126+
"forward to app-level channel checks")
127+
public void channelDoesNotExistAndNoneAllChannels()
128+
{
129+
// Setup privileges
130+
useAppPrivileges();
131+
useCommandPrivileges();
132+
133+
// Logic that needs to actually run
134+
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command)).thenCallRealMethod();
135+
commandLevelChannelMock.when(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command)).thenCallRealMethod();
136+
137+
// Do run
138+
CommandLevelChannelPermissionChecks.canMemberRun(config, channel, member, command); // No result assert, only test forwarding
139+
140+
// Verify what got called
141+
commandLevelChannelMock.verify(() -> CommandLevelChannelPermissionChecks.isAllowedInAllChannels(config, channel, member, command), times(1));
142+
appLevelChannelMock.verify(() -> AppLevelChannelPermissionChecks.canMemberRun(config, channel, member, command), times(1));
143+
commandLevelUserRoleMock.verifyNoInteractions();
144+
appLevelUserRoleMock.verifyNoInteractions();
145+
}
146+
}

0 commit comments

Comments
 (0)