Skip to content

Commit acd3e10

Browse files
committed
Seperate moderation spec to a different file
1 parent cf8d70a commit acd3e10

File tree

2 files changed

+205
-117
lines changed

2 files changed

+205
-117
lines changed

spec/client_spec.rb

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -872,121 +872,4 @@ def loop_times(times)
872872
end
873873
end
874874
end
875-
876-
describe 'moderation' do
877-
before(:each) do
878-
@moderation = @client.moderation
879-
@test_user_id = SecureRandom.uuid
880-
@test_message_id = SecureRandom.uuid
881-
@test_config_key = SecureRandom.uuid
882-
end
883-
884-
it 'flagging a user and message' do
885-
msg_response = @channel.send_message({ id: @test_message_id, text: 'Test message' }, @test_user_id)
886-
expect(msg_response['message']['id']).to eq(@test_message_id)
887-
expect(msg_response['message']['user']['id']).to eq(@test_user_id)
888-
response = @moderation.flag_user(
889-
@test_user_id,
890-
'inappropriate_behavior',
891-
user_id: @random_user[:id],
892-
custom: { severity: 'high' }
893-
)
894-
expect(response['duration']).not_to be_nil
895-
response = @moderation.flag_message(
896-
@test_message_id,
897-
'inappropriate_content',
898-
user_id: @random_user[:id],
899-
custom: { category: 'spam' }
900-
)
901-
expect(response['duration']).not_to be_nil
902-
end
903-
904-
it 'mute a user and unmute a user' do
905-
@channel.send_message({ id: @test_message_id, text: 'Test message' }, @test_user_id)
906-
testuserid1 = @random_user[:id]
907-
response = @moderation.mute_user(
908-
@test_user_id,
909-
user_id: testuserid1,
910-
timeout: 60
911-
)
912-
expect(response['duration']).not_to be_nil
913-
expect(response['mutes'][0]['user']['id']).to eq(testuserid1)
914-
response = @moderation.unmute_user(
915-
@test_user_id,
916-
user_id: @random_user[:id]
917-
)
918-
expect(response['duration']).not_to be_nil
919-
920-
response = @moderation.get_user_moderation_report(
921-
@test_user_id,
922-
include_user_blocks: true,
923-
include_user_mutes: true
924-
)
925-
expect(response['duration']).not_to be_nil
926-
end
927-
928-
it 'adds custom flags to an entity' do
929-
testuserid1 = @random_user[:id]
930-
testmsgid1 = SecureRandom.uuid
931-
@channel.send_message({ id: testmsgid1, text: 'Test message' }, testuserid1)
932-
entity_type = 'stream:chat:v1:message'
933-
entity_id = testmsgid1
934-
moderation_payload = {
935-
'texts' => ['Test message'],
936-
'custom' => { 'original_message_type' => 'regular' }
937-
}
938-
flags = [{ type: 'custom_check_text', value: 'test_flag' }]
939-
940-
response = @moderation.add_custom_flags(entity_type, entity_id, moderation_payload, flags, entity_creator_id: testuserid1)
941-
expect(response['duration']).not_to be_nil
942-
response = @moderation.add_custom_message_flags(
943-
testmsgid1,
944-
[{ type: 'custom_check_text', value: 'test_flag' }]
945-
)
946-
expect(response['duration']).not_to be_nil
947-
end
948-
949-
it 'config test' do
950-
# Create moderation config
951-
moderation_config = {
952-
key: "chat:team:#{@channel.id}",
953-
block_list_config: {
954-
enabled: true,
955-
rules: [
956-
{
957-
name: 'profanity_en_2020_v1',
958-
action: 'flag'
959-
}
960-
]
961-
}
962-
}
963-
@moderation.upsert_config(moderation_config)
964-
response = @moderation.get_config("chat:team:#{@channel.id}")
965-
expect(response['config']['key']).to eq("chat:team:#{@channel.id}")
966-
967-
response = @moderation.query_configs(
968-
{ key: "chat:messaging:#{@channel.id}" },
969-
[]
970-
)
971-
expect(response).not_to be_nil
972-
973-
# Send message that should be blocked
974-
response = @channel.send_message(
975-
{ text: 'damn' },
976-
@random_user[:id],
977-
force_moderation: true
978-
)
979-
980-
# Verify message appears in review queue
981-
queue_response = @moderation.query_review_queue(
982-
{ entity_type: 'stream:chat:v1:message' },
983-
{ created_at: -1 },
984-
limit: 1
985-
)
986-
expect(queue_response['items'][0]['entity_id']).to eq(response['message']['id'])
987-
988-
response = @moderation.delete_config("chat:team:#{@channel.id}")
989-
expect(response['duration']).not_to be_nil
990-
end
991-
end
992875
end

spec/moderation_spec.rb

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# frozen_string_literal: true
2+
3+
require 'jwt'
4+
require 'securerandom'
5+
require 'stream-chat'
6+
require 'faraday'
7+
8+
describe StreamChat::Moderation do
9+
def loop_times(times)
10+
loop do
11+
begin
12+
yield()
13+
return
14+
rescue StandardError, RSpec::Expectations::ExpectationNotMetError
15+
raise if times.zero?
16+
end
17+
18+
sleep(1)
19+
times -= 1
20+
end
21+
end
22+
23+
before(:all) do
24+
@client = StreamChat::Client.from_env
25+
26+
@created_users = []
27+
28+
@fellowship_of_the_ring = [
29+
{ id: 'frodo-baggins', name: 'Frodo Baggins', race: 'Hobbit', age: 50 },
30+
{ id: 'sam-gamgee', name: 'Samwise Gamgee', race: 'Hobbit', age: 38 },
31+
{ id: 'gandalf', name: 'Gandalf the Grey', race: 'Istari' },
32+
{ id: 'legolas', name: 'Legolas', race: 'Elf', age: 500 }
33+
]
34+
@client.upsert_users(@fellowship_of_the_ring)
35+
@channel = @client.channel('team', channel_id: 'fellowship-of-the-ring',
36+
data: { members: @fellowship_of_the_ring.map { |fellow| fellow[:id] } })
37+
@channel.create('gandalf')
38+
end
39+
40+
before(:each) do
41+
@random_users = [{ id: SecureRandom.uuid }, { id: SecureRandom.uuid }]
42+
@random_user = { id: SecureRandom.uuid }
43+
users_to_insert = [@random_users[0], @random_users[1], @random_user]
44+
45+
@created_users.push(*users_to_insert.map { |u| u[:id] })
46+
47+
@client.upsert_users(users_to_insert)
48+
end
49+
50+
after(:all) do
51+
curr_idx = 0
52+
batch_size = 25
53+
54+
slice = @created_users.slice(0, batch_size)
55+
56+
while !slice.nil? && !slice.empty?
57+
@client.delete_users(slice, user: StreamChat::HARD_DELETE, messages: StreamChat::HARD_DELETE)
58+
59+
curr_idx += batch_size
60+
slice = @created_users.slice(curr_idx, batch_size)
61+
end
62+
end
63+
64+
it 'properly sets up a new client' do
65+
client = StreamChat::Client.from_env
66+
67+
client.set_http_client(Faraday.new(url: 'https://getstream.io'))
68+
expect { client.get_app_settings }.to raise_error(StreamChat::StreamAPIException)
69+
70+
client.set_http_client(Faraday.new(url: 'https://chat.stream-io-api.com'))
71+
response = client.get_app_settings
72+
expect(response).to include 'app'
73+
end
74+
75+
it 'raises ArgumentError if no api_key is provided' do
76+
expect { StreamChat::Client.new(nil, nil) }.to raise_error(TypeError)
77+
end
78+
79+
it 'properly handles stream response class' do
80+
response = @client.get_app_settings
81+
expect(response.rate_limit.limit).to be > 0
82+
expect(response.rate_limit.remaining).to be > 0
83+
expect(response.rate_limit.reset).to be_within(120).of Time.now.utc
84+
expect(response.status_code).to be 200
85+
expect(response.to_json).not_to include 'rate_limit'
86+
expect(response.to_json).not_to include 'status_code'
87+
end
88+
89+
describe 'moderation' do
90+
before(:each) do
91+
@moderation = @client.moderation
92+
@test_user_id = SecureRandom.uuid
93+
@test_message_id = SecureRandom.uuid
94+
@test_config_key = SecureRandom.uuid
95+
end
96+
97+
it 'flagging a user and message' do
98+
msg_response = @channel.send_message({ id: @test_message_id, text: 'Test message' }, @test_user_id)
99+
expect(msg_response['message']['id']).to eq(@test_message_id)
100+
expect(msg_response['message']['user']['id']).to eq(@test_user_id)
101+
response = @moderation.flag_user(
102+
@test_user_id,
103+
'inappropriate_behavior',
104+
user_id: @random_user[:id],
105+
custom: { severity: 'high' }
106+
)
107+
expect(response['duration']).not_to be_nil
108+
response = @moderation.flag_message(
109+
@test_message_id,
110+
'inappropriate_content',
111+
user_id: @random_user[:id],
112+
custom: { category: 'spam' }
113+
)
114+
expect(response['duration']).not_to be_nil
115+
end
116+
117+
it 'mute a user and unmute a user' do
118+
@channel.send_message({ id: @test_message_id, text: 'Test message' }, @test_user_id)
119+
testuserid1 = @random_user[:id]
120+
response = @moderation.mute_user(
121+
@test_user_id,
122+
user_id: testuserid1,
123+
timeout: 60
124+
)
125+
expect(response['duration']).not_to be_nil
126+
expect(response['mutes'][0]['user']['id']).to eq(testuserid1)
127+
response = @moderation.unmute_user(
128+
@test_user_id,
129+
user_id: @random_user[:id]
130+
)
131+
expect(response['duration']).not_to be_nil
132+
133+
response = @moderation.get_user_moderation_report(
134+
@test_user_id,
135+
include_user_blocks: true,
136+
include_user_mutes: true
137+
)
138+
expect(response['duration']).not_to be_nil
139+
end
140+
141+
it 'adds custom flags to an entity' do
142+
testuserid1 = @random_user[:id]
143+
testmsgid1 = SecureRandom.uuid
144+
@channel.send_message({ id: testmsgid1, text: 'Test message' }, testuserid1)
145+
entity_type = 'stream:chat:v1:message'
146+
entity_id = testmsgid1
147+
moderation_payload = {
148+
'texts' => ['Test message'],
149+
'custom' => { 'original_message_type' => 'regular' }
150+
}
151+
flags = [{ type: 'custom_check_text', value: 'test_flag' }]
152+
153+
response = @moderation.add_custom_flags(entity_type, entity_id, moderation_payload, flags, entity_creator_id: testuserid1)
154+
expect(response['duration']).not_to be_nil
155+
response = @moderation.add_custom_message_flags(
156+
testmsgid1,
157+
[{ type: 'custom_check_text', value: 'test_flag' }]
158+
)
159+
expect(response['duration']).not_to be_nil
160+
end
161+
162+
it 'config test' do
163+
# Create moderation config
164+
moderation_config = {
165+
key: "chat:team:#{@channel.id}",
166+
block_list_config: {
167+
enabled: true,
168+
rules: [
169+
{
170+
name: 'profanity_en_2020_v1',
171+
action: 'flag'
172+
}
173+
]
174+
}
175+
}
176+
@moderation.upsert_config(moderation_config)
177+
response = @moderation.get_config("chat:team:#{@channel.id}")
178+
expect(response['config']['key']).to eq("chat:team:#{@channel.id}")
179+
180+
response = @moderation.query_configs(
181+
{ key: "chat:messaging:#{@channel.id}" },
182+
[]
183+
)
184+
expect(response).not_to be_nil
185+
186+
# Send message that should be blocked
187+
response = @channel.send_message(
188+
{ text: 'damn' },
189+
@random_user[:id],
190+
force_moderation: true
191+
)
192+
193+
# Verify message appears in review queue
194+
queue_response = @moderation.query_review_queue(
195+
{ entity_type: 'stream:chat:v1:message' },
196+
{ created_at: -1 },
197+
limit: 1
198+
)
199+
expect(queue_response['items'][0]['entity_id']).to eq(response['message']['id'])
200+
201+
response = @moderation.delete_config("chat:team:#{@channel.id}")
202+
expect(response['duration']).not_to be_nil
203+
end
204+
end
205+
end

0 commit comments

Comments
 (0)