|
| 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