|
| 1 | +package io.getstream; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import io.getstream.models.*; |
| 6 | +import io.getstream.services.ModerationImpl; |
| 7 | +import java.util.*; |
| 8 | +import org.junit.jupiter.api.AfterAll; |
| 9 | +import org.junit.jupiter.api.MethodOrderer; |
| 10 | +import org.junit.jupiter.api.Order; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.TestInstance; |
| 13 | +import org.junit.jupiter.api.TestMethodOrder; |
| 14 | + |
| 15 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 16 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 17 | +class ModerationIntegrationTest extends ChatTestBase { |
| 18 | + |
| 19 | + private final List<String> createdUserIds = new ArrayList<>(); |
| 20 | + private final List<String> createdChannelIds = new ArrayList<>(); |
| 21 | + |
| 22 | + @AfterAll |
| 23 | + void cleanup() { |
| 24 | + for (String channelId : createdChannelIds) { |
| 25 | + try { |
| 26 | + chat.deleteChannel( |
| 27 | + "messaging", channelId, DeleteChannelRequest.builder().HardDelete(true).build()) |
| 28 | + .execute(); |
| 29 | + } catch (Exception ignored) { |
| 30 | + } |
| 31 | + } |
| 32 | + if (!createdUserIds.isEmpty()) { |
| 33 | + deleteUsersWithRetry(createdUserIds); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + @Order(1) |
| 39 | + void testBanUnbanUser() throws Exception { |
| 40 | + List<String> userIds = createTestUsers(2); |
| 41 | + createdUserIds.addAll(userIds); |
| 42 | + String adminId = userIds.get(0); |
| 43 | + String targetId = userIds.get(1); |
| 44 | + |
| 45 | + // Create a channel with both users as members |
| 46 | + String channelId = createTestChannelWithMembers(adminId, userIds); |
| 47 | + createdChannelIds.add(channelId); |
| 48 | + String cid = "messaging:" + channelId; |
| 49 | + |
| 50 | + ModerationImpl moderation = new ModerationImpl(client.getHttpClient()); |
| 51 | + |
| 52 | + // Ban target user from channel |
| 53 | + var banResp = |
| 54 | + moderation |
| 55 | + .ban( |
| 56 | + BanRequest.builder() |
| 57 | + .targetUserID(targetId) |
| 58 | + .bannedByID(adminId) |
| 59 | + .channelCid(cid) |
| 60 | + .reason("moderation test ban") |
| 61 | + .build()) |
| 62 | + .execute(); |
| 63 | + assertNotNull(banResp.getData()); |
| 64 | + |
| 65 | + // Verify ban via queryBannedUsers |
| 66 | + var bansResp = |
| 67 | + client |
| 68 | + .chat() |
| 69 | + .queryBannedUsers( |
| 70 | + QueryBannedUsersRequest.builder() |
| 71 | + .Payload( |
| 72 | + QueryBannedUsersPayload.builder() |
| 73 | + .filterConditions(Map.of("channel_cid", Map.of("$eq", cid))) |
| 74 | + .build()) |
| 75 | + .build()) |
| 76 | + .execute(); |
| 77 | + assertNotNull(bansResp.getData().getBans()); |
| 78 | + boolean banFound = |
| 79 | + bansResp.getData().getBans().stream() |
| 80 | + .anyMatch(b -> b.getUser() != null && targetId.equals(b.getUser().getId())); |
| 81 | + assertTrue(banFound, "Target user should appear in banned list"); |
| 82 | + |
| 83 | + // Unban the user from the channel |
| 84 | + moderation |
| 85 | + .unban(UnbanRequest.builder().TargetUserID(targetId).ChannelCid(cid).build()) |
| 86 | + .execute(); |
| 87 | + |
| 88 | + // Verify ban is removed after unban |
| 89 | + var bansAfter = |
| 90 | + client |
| 91 | + .chat() |
| 92 | + .queryBannedUsers( |
| 93 | + QueryBannedUsersRequest.builder() |
| 94 | + .Payload( |
| 95 | + QueryBannedUsersPayload.builder() |
| 96 | + .filterConditions(Map.of("channel_cid", Map.of("$eq", cid))) |
| 97 | + .build()) |
| 98 | + .build()) |
| 99 | + .execute(); |
| 100 | + boolean stillBanned = |
| 101 | + bansAfter.getData().getBans() != null |
| 102 | + && bansAfter.getData().getBans().stream() |
| 103 | + .anyMatch(b -> b.getUser() != null && targetId.equals(b.getUser().getId())); |
| 104 | + assertFalse(stillBanned, "Target user should not appear in banned list after unban"); |
| 105 | + } |
| 106 | +} |
0 commit comments