Skip to content

Commit

Permalink
Test bulk-ban deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed Apr 6, 2024
1 parent c0b03db commit 5954e30
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 8 deletions.
24 changes: 24 additions & 0 deletions src/test/java/net/dv8tion/jda/test/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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;

public interface Constants
{
long GUILD_ID = 125227483518861312L;
long MINN_USER_ID = 86699011792191488L;
long BUTLER_USER_ID = 150203841827045376L;
}
9 changes: 8 additions & 1 deletion src/test/java/net/dv8tion/jda/test/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import org.mockito.Mock;

import javax.annotation.Nonnull;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;

import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.openMocks;

public class IntegrationTest
{
protected Random random = new Random(4242);
@Mock
protected JDAImpl jda;
@Mock
Expand Down Expand Up @@ -64,4 +66,9 @@ protected RestActionAssertions assertThatNextRequest()
return RestActionAssertions.assertThatNextAction(requester)
.withNormalizedBody(this::normalizeRequestBody);
}

protected void assertRequestStarted()
{
verify(requester, times(1)).request(any());
}
}
16 changes: 13 additions & 3 deletions src/test/java/net/dv8tion/jda/test/RestActionAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mockito.ThrowingConsumer;

import javax.annotation.Nonnull;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
Expand Down Expand Up @@ -71,10 +72,9 @@ public RestActionAssertions hasBodyEqualTo(@Nonnull DataObject expected)
normalizeRequestBody.accept(dataObject);
normalizeRequestBody.accept(expected);

assertThat(dataObject)
.withRepresentation(new PrettyRepresentation())
assertThat(dataObject.toPrettyString())
.as("RestAction should send request using expected request body")
.isEqualTo(expected);
.isEqualTo(expected.toPrettyString());
});
}

Expand All @@ -98,6 +98,16 @@ public RestActionAssertions hasCompiledRoute(@Nonnull String route)
);
}

@Contract("_->this")
public RestActionAssertions hasAuditReason(@Nonnull String reason)
{
return checkAssertions(request ->
assertThat(request.getHeaders())
.as("RestAction should set header")
.contains(new AbstractMap.SimpleEntry<>("X-Audit-Log-Reason", reason))
);
}

@Override
public void acceptThrows(Request<?> request) throws Throwable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.entities.guild;

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.UserSnowflake;
import net.dv8tion.jda.api.requests.Method;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.entities.MemberImpl;
import net.dv8tion.jda.internal.entities.SelfUserImpl;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.UnlockHook;
import net.dv8tion.jda.internal.utils.cache.MemberCacheViewImpl;
import net.dv8tion.jda.test.Constants;
import net.dv8tion.jda.test.IntegrationTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import java.time.Duration;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

public class GuildActionsTest extends IntegrationTest
{
@Mock
protected SelfUserImpl selfUser;
@Mock
protected MemberImpl selfMember;

protected GuildImpl guild;

@BeforeEach
void setupGuild()
{
when(selfUser.getIdLong()).thenReturn(Constants.MINN_USER_ID);
when(jda.getSelfUser()).thenReturn(selfUser);
when(jda.getCacheFlags()).thenReturn(EnumSet.allOf(CacheFlag.class));

guild = new GuildImpl(jda, Constants.GUILD_ID);

MemberCacheViewImpl members = guild.getMembersView();
try (UnlockHook ignored = members.writeLock())
{
members.getMap().put(Constants.MINN_USER_ID, selfMember);
}
}

@Test
void testBulkBanDuplicates()
{
hasPermission();

Duration duration = Duration.ofSeconds(random.nextInt(10000));
String reason = Helpers.format("User %d was banned by %d for %s", Constants.BUTLER_USER_ID, Constants.MINN_USER_ID, duration);
List<UserSnowflake> users = Arrays.asList(
User.fromId(Constants.BUTLER_USER_ID),
User.fromId(Constants.BUTLER_USER_ID)
);

assertThatNextRequest()
.hasMethod(Method.POST)
.hasCompiledRoute("guilds/" + Constants.GUILD_ID + "/bulk-ban")
.hasAuditReason(reason)
.hasBodyEqualTo(DataObject.empty()
.put("delete_message_seconds", duration.getSeconds())
.put("user_ids", DataArray.empty().add(Constants.BUTLER_USER_ID)));

guild.ban(users, duration).reason(reason).queue();

assertRequestStarted();
}

protected void hasPermission()
{
when(selfMember.hasPermission(any(Permission[].class))).thenReturn(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

import static net.dv8tion.jda.api.requests.Method.POST;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;

public class MessageCreateActionTest extends IntegrationTest
{
Expand Down Expand Up @@ -88,7 +87,7 @@ void testContentOnly()
.setContent("test content")
.queue();

verify(requester, times(1)).request(any());
assertRequestStarted();
}

@Test
Expand All @@ -107,7 +106,7 @@ void testEmbedOnly()
.build())
.queue();

verify(requester, times(1)).request(any());
assertRequestStarted();
}

@Nonnull
Expand Down

0 comments on commit 5954e30

Please sign in to comment.