Skip to content

Commit

Permalink
Fix GuildManager#setSafetyAlertsChannel (#2765)
Browse files Browse the repository at this point in the history
* Fix GuildManager#setSafetyAlertsChannel
* Add GuildManagerTest to ensure finalizeData is implemented
  • Loading branch information
MinnDevelopment authored Nov 3, 2024
1 parent edb7991 commit 53272b6
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ protected RequestBody finalizeData()
body.put("rules_channel_id", rulesChannel);
if (shouldUpdate(COMMUNITY_UPDATES_CHANNEL))
body.put("public_updates_channel_id", communityUpdatesChannel);
if (shouldUpdate(SAFETY_ALERTS_CHANNEL))
body.put("safety_alerts_channel_id", safetyAlertsChannel);
if (shouldUpdate(VERIFICATION_LEVEL))
body.put("verification_level", verificationLevel);
if (shouldUpdate(NOTIFICATION_LEVEL))
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/net/dv8tion/jda/test/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ protected RestActionAssertions assertThatRequestFrom(@Nonnull RestAction<?> acti
.withNormalizedBody(this::normalizeRequestBody);
}

protected void assertThatNoRequestsWereSent()
{
verify(requester, never()).request(any());
}

protected <T> void whenSuccess(RestActionImpl<T> action, DataArray array, Consumer<T> assertion)
{
Response response = mock();
Expand Down
147 changes: 147 additions & 0 deletions src/test/java/net/dv8tion/jda/test/managers/GuildManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.managers;

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel;
import net.dv8tion.jda.api.managers.GuildManager;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.managers.GuildManagerImpl;
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.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import static net.dv8tion.jda.api.requests.Method.PATCH;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class GuildManagerTest extends IntegrationTest
{
@Mock
private Guild guild;
@Mock
private Member selfMember;
@Mock
private TextChannel textChannel;
@Mock
private VoiceChannel voiceChannel;

@BeforeEach
void setupMocks()
{
when(guild.getJDA()).thenReturn(jda);
when(guild.getId()).thenReturn(Long.toUnsignedString(Constants.GUILD_ID));
when(guild.getSelfMember()).thenReturn(selfMember);
when(selfMember.hasPermission(any(Permission[].class))).thenReturn(true);
when(textChannel.getGuild()).thenReturn(guild);
when(textChannel.getId()).thenReturn(Long.toUnsignedString(Constants.CHANNEL_ID));
when(voiceChannel.getGuild()).thenReturn(guild);
when(voiceChannel.getId()).thenReturn(Long.toUnsignedString(Constants.CHANNEL_ID));
}

@Test
void callNoSetters()
{
GuildManagerImpl manager = new GuildManagerImpl(guild);
manager.queue();
assertThatNoRequestsWereSent();
}

@Test
void callEverySetter()
{
Set<String> features = new HashSet<>(Arrays.asList("BANNER", "VERIFIED", "INVITE_SPLASH"));
when(guild.getFeatures()).thenReturn(features);

Set<String> ignoredSetters = new HashSet<>(Arrays.asList(
"setFeatures", "setInvitesDisabled"
));

AtomicInteger calledSetters = new AtomicInteger(0);
GuildManagerImpl manager = new GuildManagerImpl(guild);
for (Method method : GuildManager.class.getDeclaredMethods())
{
if (ignoredSetters.contains(method.getName()))
continue;

if (method.getName().startsWith("set") && method.getParameterCount() == 1)
{
assertThatNoException().describedAs("call " + method.getName()).isThrownBy(() -> {
Object mocked = getParameterForSetter(method);
method.invoke(manager, mocked);
calledSetters.incrementAndGet();
});
}
}

DataObject expectedBody = DataObject.empty()
.put("afk_channel_id", "125227483518861312")
.put("afk_timeout", 0)
.put("banner", null)
.put("default_message_notifications", 0)
.put("description", "test")
.put("explicit_content_filter", 0)
.put("icon", null)
.put("mfa_level", 0)
.put("name", "test")
.put("premium_progress_bar_enabled", true)
.put("public_updates_channel_id", "125227483518861312")
.put("safety_alerts_channel_id", "125227483518861312")
.put("rules_channel_id", "125227483518861312")
.put("splash", null)
.put("system_channel_id", "125227483518861312")
.put("verification_level", 0);

assertThatRequestFrom(manager)
.hasMethod(PATCH)
.hasBodyEqualTo(expectedBody)
.whenQueueCalled();

// Every setter should result in a new key in the body, make sure you updated finalizeData
assertThat(expectedBody.keys()).hasSize(calledSetters.get());
}

private Object getParameterForSetter(Method setter)
{
Class<?> paramType = setter.getParameters()[0].getType();
if (paramType == String.class)
return "test";
if (paramType == Boolean.TYPE)
return true;
if (paramType == Integer.TYPE)
return 42;
if (TextChannel.class.isAssignableFrom(paramType))
return textChannel;
if (VoiceChannel.class.isAssignableFrom(paramType))
return voiceChannel;
return mock(paramType);
}
}

0 comments on commit 53272b6

Please sign in to comment.