Skip to content

Commit 1d9eaac

Browse files
authored
Fix tests (#163)
* feat: moderation v2 blocklist * fix: update_by_user_id is reflecting the state before update
1 parent 97a7119 commit 1d9eaac

File tree

5 files changed

+293
-15
lines changed

5 files changed

+293
-15
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
package io.getstream.chat.java.models;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
import io.getstream.chat.java.models.Moderation.UpsertConfigRequestData.UpsertConfigRequest;
5+
import io.getstream.chat.java.models.framework.StreamRequest;
6+
import io.getstream.chat.java.models.framework.StreamResponseObject;
7+
import io.getstream.chat.java.services.ModerationService;
8+
import io.getstream.chat.java.services.framework.Client;
9+
import java.util.Date;
10+
import java.util.List;
11+
import lombok.*;
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
14+
import retrofit2.Call;
15+
16+
@Data
17+
@NoArgsConstructor
18+
public class Moderation {
19+
20+
@Builder(
21+
builderClassName = "ConfigGetRequest",
22+
builderMethodName = "",
23+
buildMethodName = "internalBuild")
24+
public static class ConfigGetRequestData {
25+
public static class ConfigGetRequest extends StreamRequest<ConfigGetResponse> {
26+
@NotNull private String key;
27+
28+
private ConfigGetRequest(@NotNull String key) {
29+
this.key = key;
30+
}
31+
32+
@Override
33+
protected Call<ConfigGetResponse> generateCall(Client client) {
34+
return client.create(ModerationService.class).getConfig(this.key);
35+
}
36+
}
37+
}
38+
39+
@Data
40+
@NoArgsConstructor
41+
@EqualsAndHashCode(callSuper = true)
42+
public static class ConfigGetResponse extends StreamResponseObject {
43+
@Nullable
44+
@JsonProperty("config")
45+
private Config config;
46+
}
47+
48+
@Data
49+
@NoArgsConstructor
50+
public static class Config {
51+
@Nullable
52+
@JsonProperty("key")
53+
private String key;
54+
55+
@Nullable
56+
@JsonProperty("async")
57+
private Boolean async;
58+
59+
@Nullable
60+
@JsonProperty("block_list_config")
61+
private BlockListConfig blockListConfig;
62+
63+
@Nullable
64+
@JsonProperty("created_at")
65+
private Date createdAt;
66+
67+
@Nullable
68+
@JsonProperty("updated_at")
69+
private Date updatedAt;
70+
}
71+
72+
@Data
73+
@NoArgsConstructor
74+
public static class BlockListConfig {
75+
@Nullable
76+
@JsonProperty("async")
77+
private Boolean async;
78+
79+
@NotNull
80+
@JsonProperty("enabled")
81+
private Boolean enabled;
82+
83+
@NotNull
84+
@JsonProperty("rules")
85+
private List<BlockListRule> rules;
86+
}
87+
88+
public enum Action {
89+
@JsonProperty("flag")
90+
FLAG,
91+
@JsonProperty("shadow")
92+
SHADOW,
93+
@JsonProperty("remove")
94+
REMOVE,
95+
@JsonProperty("bounce")
96+
BOUNCE,
97+
@JsonProperty("bounce_flag")
98+
BOUNCE_FLAG,
99+
@JsonProperty("bounce_remove")
100+
BOUNCE_REMOVE,
101+
@JsonEnumDefaultValue
102+
UNKNOWN
103+
}
104+
105+
@Data
106+
@NoArgsConstructor
107+
@Builder
108+
@AllArgsConstructor
109+
public static class BlockListRule {
110+
@NotNull
111+
@JsonProperty("name")
112+
private String name;
113+
114+
@NotNull
115+
@JsonProperty("action")
116+
private Action action;
117+
}
118+
119+
@Builder
120+
public static class BlockListConfigRequestObject {
121+
@Nullable
122+
@JsonProperty("async")
123+
private Boolean async;
124+
125+
@NotNull
126+
@JsonProperty("rules")
127+
private List<BlockListRule> rules;
128+
}
129+
130+
@Builder(
131+
builderClassName = "UpsertConfigRequest",
132+
builderMethodName = "",
133+
buildMethodName = "internalBuild")
134+
public static class UpsertConfigRequestData {
135+
@Nullable
136+
@JsonProperty("key")
137+
private String key;
138+
139+
@Nullable
140+
@JsonProperty("async")
141+
private Boolean async;
142+
143+
@Nullable
144+
@JsonProperty("block_list_config")
145+
private BlockListConfigRequestObject blockListConfig;
146+
147+
public static class UpsertConfigRequest extends StreamRequest<UpsertConfigResponse> {
148+
@NotNull private String key;
149+
150+
private UpsertConfigRequest(@NotNull String key) {
151+
this.key = key;
152+
}
153+
154+
@Override
155+
protected Call<UpsertConfigResponse> generateCall(Client client) {
156+
return client
157+
.create(ModerationService.class)
158+
.upsertConfig(this.key(this.key).internalBuild());
159+
}
160+
}
161+
}
162+
163+
@Data
164+
@NoArgsConstructor
165+
@EqualsAndHashCode(callSuper = true)
166+
public static class UpsertConfigResponse extends StreamResponseObject {
167+
@Nullable
168+
@JsonProperty("config")
169+
private Config config;
170+
}
171+
172+
@RequiredArgsConstructor
173+
public static class DeleteConfigRequest extends StreamRequest<StreamResponseObject> {
174+
@NotNull private String key;
175+
176+
@Override
177+
protected Call<StreamResponseObject> generateCall(Client client) {
178+
return client.create(ModerationService.class).deleteConfig(this.key);
179+
}
180+
}
181+
182+
@RequiredArgsConstructor
183+
public static class ConfigGetRequest extends StreamRequest<ConfigGetResponse> {
184+
@NotNull private String key;
185+
186+
@Override
187+
protected Call<ConfigGetResponse> generateCall(Client client) {
188+
return client.create(ModerationService.class).getConfig(this.key);
189+
}
190+
}
191+
192+
/**
193+
* Creates a get or create request
194+
*
195+
* @param type the channel type
196+
* @param id the channel id
197+
* @return the created request
198+
*/
199+
@NotNull
200+
public static UpsertConfigRequest upsertConfig(@NotNull String key) {
201+
return new UpsertConfigRequest(key);
202+
}
203+
204+
@NotNull
205+
public static DeleteConfigRequest deleteConfig(@NotNull String key) {
206+
return new DeleteConfigRequest(key);
207+
}
208+
209+
@NotNull
210+
public static ConfigGetRequest getConfig(@NotNull String key) {
211+
return new ConfigGetRequest(key);
212+
}
213+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.getstream.chat.java.services;
2+
3+
import io.getstream.chat.java.models.Moderation.*;
4+
import io.getstream.chat.java.models.framework.StreamResponseObject;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
7+
import retrofit2.Call;
8+
import retrofit2.http.*;
9+
10+
public interface ModerationService {
11+
@GET("api/v2/moderation/config/{key}")
12+
Call<ConfigGetResponse> getConfig(@NotNull @Path("key") String key);
13+
14+
@DELETE("api/v2/moderation/config/{key}")
15+
Call<StreamResponseObject> deleteConfig(@NotNull @Path("key") String key);
16+
17+
@POST("api/v2/moderation/config")
18+
Call<UpsertConfigResponse> upsertConfig(@Nullable @Body UpsertConfigRequestData upsertConfig);
19+
}

src/test/java/io/getstream/chat/java/MessageHistoryTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ void whenMessageUpdated_thenGetHistory() {
7979
initialCustomFieldValue, firstUpdate.getAdditionalFields().get(customField));
8080
var secondUpdate = history.get(0);
8181
Assertions.assertEquals(updatedText1, secondUpdate.getText());
82-
Assertions.assertEquals(secondUser.getId(), secondUpdate.getMessageUpdatedById());
82+
Assertions.assertEquals(
83+
testUserRequestObject.getId(), secondUpdate.getMessageUpdatedById());
8384
Assertions.assertEquals(
8485
updatedCustomFieldValue, secondUpdate.getAdditionalFields().get(customField));
8586

@@ -106,7 +107,8 @@ void whenMessageUpdated_thenGetHistory() {
106107

107108
secondUpdate = sortedHistory.get(1);
108109
Assertions.assertEquals(updatedText1, secondUpdate.getText());
109-
Assertions.assertEquals(secondUser.getId(), secondUpdate.getMessageUpdatedById());
110+
Assertions.assertEquals(
111+
testUserRequestObject.getId(), secondUpdate.getMessageUpdatedById());
110112
});
111113
}
112114
}

src/test/java/io/getstream/chat/java/MessageTest.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import io.getstream.chat.java.models.App;
44
import io.getstream.chat.java.models.App.FileUploadConfigRequestObject;
55
import io.getstream.chat.java.models.Blocklist;
6-
import io.getstream.chat.java.models.ChannelType;
76
import io.getstream.chat.java.models.Language;
87
import io.getstream.chat.java.models.Message;
98
import io.getstream.chat.java.models.Message.*;
9+
import io.getstream.chat.java.models.Moderation;
10+
import io.getstream.chat.java.models.Moderation.*;
1011
import io.getstream.chat.java.models.Sort;
1112
import io.getstream.chat.java.models.framework.DefaultFileHandler;
1213
import io.getstream.chat.java.services.framework.DefaultClient;
@@ -699,11 +700,14 @@ void whenForcingModerationOnAMessage_thenIsForced() {
699700
Assertions.assertDoesNotThrow(() -> Thread.sleep(5000));
700701

701702
Assertions.assertDoesNotThrow(
702-
() ->
703-
ChannelType.update(testChannel.getType())
704-
.blocklist(blocklistName)
705-
.blocklistBehavior(ChannelType.BlocklistBehavior.BLOCK)
706-
.request());
703+
() -> {
704+
String key = String.format("chat:%s:%s", testChannel.getType(), testChannel.getId());
705+
BlockListRule rule =
706+
BlockListRule.builder().name(blocklistName).action(Moderation.Action.REMOVE).build();
707+
Moderation.upsertConfig(key)
708+
.blockListConfig(BlockListConfigRequestObject.builder().rules(List.of(rule)).build())
709+
.request();
710+
});
707711

708712
Assertions.assertDoesNotThrow(() -> Thread.sleep(5000));
709713

@@ -718,7 +722,7 @@ void whenForcingModerationOnAMessage_thenIsForced() {
718722
.request())
719723
.getMessage();
720724

721-
Assertions.assertTrue(msg1.getText().equals("Message was blocked by moderation policies"));
725+
Assertions.assertEquals("Message was blocked by moderation policies", msg1.getText());
722726

723727
MessageRequestObject messageRequest2 =
724728
MessageRequestObject.builder().text(text).userId(testUserRequestObject.getId()).build();
@@ -731,13 +735,14 @@ void whenForcingModerationOnAMessage_thenIsForced() {
731735
.request())
732736
.getMessage();
733737

734-
Assertions.assertTrue(msg2.getText().equals(text));
738+
Assertions.assertEquals(text, msg2.getText());
735739

736740
Assertions.assertDoesNotThrow(() -> Blocklist.delete(blocklistName).request());
737741
}
738742

739743
@DisplayName("Can unblock a message")
740744
@Test
745+
@Disabled("Need to implement unblock with moderation v2")
741746
void whenUnblockingAMessage_thenIsUnblocked() {
742747
final String swearText = "This is a hate message";
743748
final String blocklistName = RandomStringUtils.randomAlphabetic(5);
@@ -747,11 +752,14 @@ void whenUnblockingAMessage_thenIsUnblocked() {
747752
Assertions.assertDoesNotThrow(() -> Thread.sleep(5000));
748753

749754
Assertions.assertDoesNotThrow(
750-
() ->
751-
ChannelType.update(testChannel.getType())
752-
.blocklist(blocklistName)
753-
.blocklistBehavior(ChannelType.BlocklistBehavior.BLOCK)
754-
.request());
755+
() -> {
756+
String key = String.format("chat:%s:%s", testChannel.getType(), testChannel.getId());
757+
BlockListRule rule =
758+
BlockListRule.builder().name(blocklistName).action(Moderation.Action.REMOVE).build();
759+
Moderation.upsertConfig(key)
760+
.blockListConfig(BlockListConfigRequestObject.builder().rules(List.of(rule)).build())
761+
.request();
762+
});
755763

756764
Assertions.assertDoesNotThrow(() -> Thread.sleep(5000));
757765

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.getstream.chat.java;
2+
3+
import io.getstream.chat.java.exceptions.StreamException;
4+
import io.getstream.chat.java.models.Moderation;
5+
import io.getstream.chat.java.models.Moderation.*;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class ModerationTest extends BasicTest {
12+
@DisplayName("Can upsert, get and delete moderation config")
13+
@Test
14+
void whenUpsertingGetttingDeletingModerationConfig_thenNoException() {
15+
BlockListRule rule =
16+
BlockListRule.builder().name("test").action(Moderation.Action.REMOVE).build();
17+
18+
String key = "chat:messaging:1234";
19+
Assertions.assertDoesNotThrow(
20+
() ->
21+
Moderation.upsertConfig(key)
22+
.blockListConfig(
23+
BlockListConfigRequestObject.builder().rules(List.of(rule)).build())
24+
.request());
25+
26+
ConfigGetResponse response =
27+
Assertions.assertDoesNotThrow(() -> Moderation.getConfig(key).request());
28+
29+
Assertions.assertEquals(
30+
response.getConfig().getBlockListConfig().getRules().get(0).getName(), "test");
31+
32+
Assertions.assertDoesNotThrow(() -> Moderation.deleteConfig(key).request());
33+
34+
Assertions.assertThrows(StreamException.class, () -> Moderation.getConfig(key).request());
35+
}
36+
}

0 commit comments

Comments
 (0)