Skip to content

Commit fd85415

Browse files
isloated tests (#175)
* isloated tests * added random user-names * chore: linting fixes * chore: linting errors fix * added channel cleanup after test run * added more cleanup * added more cleanup * chore: changed user ids in moderation specs * linting errors * randomized the user creation
1 parent 151b7ab commit fd85415

File tree

2 files changed

+71
-48
lines changed

2 files changed

+71
-48
lines changed

spec/client_spec.rb

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,50 @@ def loop_times(times)
2626
@created_users = []
2727

2828
@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 }
29+
{ id: SecureRandom.uuid, name: 'Frodo Baggins', race: 'Hobbit', age: 50 },
30+
{ id: SecureRandom.uuid, name: 'Samwise Gamgee', race: 'Hobbit', age: 38 },
31+
{ id: SecureRandom.uuid, name: 'Gandalf the Grey', race: 'Istari' },
32+
{ id: SecureRandom.uuid, name: 'Legolas', race: 'Elf', age: 500 }
3333
]
34+
35+
@legolas = @fellowship_of_the_ring[3][:id]
36+
@gandalf = @fellowship_of_the_ring[2][:id]
37+
@frodo = @fellowship_of_the_ring[0][:id]
38+
@sam = @fellowship_of_the_ring[1][:id]
39+
3440
@client.upsert_users(@fellowship_of_the_ring)
35-
@channel = @client.channel('team', channel_id: 'fellowship-of-the-ring',
41+
42+
# Create a new channel for chat max length for channel_id is 64 characters
43+
channel_id = "fellowship-of-the-ring-chat-#{SecureRandom.alphanumeric(20)}"
44+
45+
@channel = @client.channel('team', channel_id: channel_id,
3646
data: { members: @fellowship_of_the_ring.map { |fellow| fellow[:id] } })
37-
@channel.create('gandalf')
47+
@channel.create(@gandalf)
3848
end
3949

4050
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]
51+
@random_users = [{ id: SecureRandom.uuid }, { id: SecureRandom.uuid }, { id: SecureRandom.uuid }]
52+
@random_user = @random_users[0]
4453

45-
@created_users.push(*users_to_insert.map { |u| u[:id] })
54+
@created_users.push(*@random_users.map { |u| u[:id] })
4655

47-
@client.upsert_users(users_to_insert)
56+
@client.upsert_users(@random_users)
4857
end
4958

5059
after(:all) do
60+
@channel.delete
61+
62+
@users_to_delete = @created_users.dup + @fellowship_of_the_ring.map { |fellow| fellow[:id] }
5163
curr_idx = 0
5264
batch_size = 25
5365

54-
slice = @created_users.slice(0, batch_size)
66+
slice = @users_to_delete.slice(0, batch_size)
5567

5668
while !slice.nil? && !slice.empty?
5769
@client.delete_users(slice, user: StreamChat::HARD_DELETE, messages: StreamChat::HARD_DELETE)
5870

5971
curr_idx += batch_size
60-
slice = @created_users.slice(curr_idx, batch_size)
72+
slice = @users_to_delete.slice(curr_idx, batch_size)
6173
end
6274
end
6375

@@ -220,7 +232,7 @@ def loop_times(times)
220232
end
221233

222234
it 'exports a user' do
223-
response = @client.export_user('gandalf')
235+
response = @client.export_user(@gandalf)
224236
expect(response).to include 'user'
225237
expect(response['user']['name']).to eq('Gandalf the Grey')
226238
end
@@ -446,9 +458,9 @@ def loop_times(times)
446458
end
447459

448460
it 'queries channels' do
449-
response = @client.query_channels({ 'members' => { '$in' => ['legolas'] } }, sort: { 'id' => 1 })
461+
response = @client.query_channels({ 'members' => { '$in' => [@legolas] } }, sort: { 'id' => 1 })
450462
expect(response['channels'].length).to eq 1
451-
expect(response['channels'][0]['channel']['id']).to eq 'fellowship-of-the-ring'
463+
expect(response['channels'][0]['channel']['id']).to eq @channel.id
452464
expect(response['channels'][0]['members'].length).to eq 4
453465
end
454466

@@ -505,41 +517,41 @@ def loop_times(times)
505517
describe 'search' do
506518
it 'search for messages' do
507519
text = SecureRandom.uuid
508-
@channel.send_message({ text: text }, 'legolas')
509-
resp = @client.search({ members: { '$in' => ['legolas'] } }, text)
520+
@channel.send_message({ text: text }, @fellowship_of_the_ring[2][:id])
521+
resp = @client.search({ members: { '$in' => [@fellowship_of_the_ring[2][:id]] } }, text)
510522
p resp
511523
expect(resp['results'].length).to eq(1)
512524
end
513525

514526
it 'search for messages with filter conditions' do
515527
text = SecureRandom.uuid
516-
@channel.send_message({ text: text }, 'legolas')
517-
resp = @client.search({ members: { '$in' => ['legolas'] } }, { text: { '$q': text } })
528+
@channel.send_message({ text: text }, @legolas)
529+
resp = @client.search({ members: { '$in' => [@legolas] } }, { text: { '$q': text } })
518530
expect(resp['results'].length).to eq(1)
519531
end
520532

521533
it 'offset with sort should fail' do
522534
expect do
523-
@client.search({ members: { '$in' => ['legolas'] } }, SecureRandom.uuid, sort: { created_at: -1 }, offset: 2)
535+
@client.search({ members: { '$in' => [@legolas] } }, SecureRandom.uuid, sort: { created_at: -1 }, offset: 2)
524536
end.to raise_error(/cannot use offset with next or sort parameters/)
525537
end
526538

527539
it 'offset with next should fail' do
528540
expect do
529-
@client.search({ members: { '$in' => ['legolas'] } }, SecureRandom.uuid, offset: 2, next: SecureRandom.uuid)
541+
@client.search({ members: { '$in' => [@legolas] } }, SecureRandom.uuid, offset: 2, next: SecureRandom.uuid)
530542
end.to raise_error(/cannot use offset with next or sort parameters/)
531543
end
532544

533545
xit 'search for messages with sorting' do
534546
text = SecureRandom.uuid
535547
message_ids = ["0-#{text}", "1-#{text}"]
536-
@channel.send_message({ id: message_ids[0], text: text }, 'legolas')
537-
@channel.send_message({ id: message_ids[1], text: text }, 'legolas')
538-
page1 = @client.search({ members: { '$in' => ['legolas'] } }, text, sort: [{ created_at: -1 }], limit: 1)
548+
@channel.send_message({ id: message_ids[0], text: text }, @legolas)
549+
@channel.send_message({ id: message_ids[1], text: text }, @legolas)
550+
page1 = @client.search({ members: { '$in' => [@legolas] } }, text, sort: [{ created_at: -1 }], limit: 1)
539551
expect(page1['results'].length).to eq
540552
expect(page1['results'][0]['message']['id']).to eq(message_ids[1])
541553
expect(page1['next']).not_to be_empty
542-
page2 = @client.search({ members: { '$in' => ['legolas'] } }, text, limit: 1, next: page1['next'])
554+
page2 = @client.search({ members: { '$in' => [@legolas] } }, text, limit: 1, next: page1['next'])
543555
expect(page2['results'].length).to eq(1)
544556
expect(page2['results'][0]['message']['id']).to eq(message_ids[0])
545557
expect(page2['previous']).not_to be_empty
@@ -831,7 +843,7 @@ def loop_times(times)
831843

832844
it 'check push notification test are working' do
833845
message_id = SecureRandom.uuid
834-
@channel.send_message({ id: message_id, text: SecureRandom.uuid }, 'legolas')
846+
@channel.send_message({ id: message_id, text: SecureRandom.uuid }, @legolas)
835847
resp = @client.check_push({ message_id: message_id, skip_devices: true, user_id: @random_user[:id] })
836848
expect(resp['rendered_message']).not_to be_empty
837849
end
@@ -861,7 +873,7 @@ def loop_times(times)
861873

862874
it 'can translate a message' do
863875
message_id = SecureRandom.uuid
864-
@channel.send_message({ id: message_id, text: SecureRandom.uuid }, 'legolas')
876+
@channel.send_message({ id: message_id, text: SecureRandom.uuid }, @legolas)
865877
response = @client.translate_message(message_id, 'hu')
866878
expect(response['message']).not_to be_empty
867879
end
@@ -1113,15 +1125,14 @@ def loop_times(times)
11131125
end
11141126

11151127
describe 'reminders' do
1116-
before do
1117-
@client = StreamChat::Client.from_env
1118-
@channel_id = SecureRandom.uuid
1119-
@channel = @client.channel('messaging', channel_id: @channel_id)
1120-
@channel.create('john')
1128+
before(:all) do
11211129
@channel.update_partial({ config_overrides: { user_message_reminders: true } })
1122-
@message = @channel.send_message({ 'text' => 'Hello world' }, 'john')
1130+
end
1131+
1132+
before(:each) do
1133+
@user_id = @random_user[:id]
1134+
@message = @channel.send_message({ 'text' => 'Hello world' }, @user_id)
11231135
@message_id = @message['message']['id']
1124-
@user_id = 'john'
11251136
end
11261137

11271138
describe 'create_reminder' do

spec/moderation_spec.rb

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,50 @@ def loop_times(times)
2626
@created_users = []
2727

2828
@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 }
29+
{ id: SecureRandom.uuid, name: 'Frodo Baggins', race: 'Hobbit', age: 50 },
30+
{ id: SecureRandom.uuid, name: 'Samwise Gamgee', race: 'Hobbit', age: 38 },
31+
{ id: SecureRandom.uuid, name: 'Gandalf the Grey', race: 'Istari' },
32+
{ id: SecureRandom.uuid, name: 'Legolas', race: 'Elf', age: 500 }
3333
]
34+
@gandalf = @fellowship_of_the_ring[2]
35+
@frodo = @fellowship_of_the_ring[0]
36+
@sam = @fellowship_of_the_ring[1]
37+
@legolas = @fellowship_of_the_ring[3]
38+
3439
@client.upsert_users(@fellowship_of_the_ring)
35-
@channel = @client.channel('team', channel_id: 'fellowship-of-the-ring',
40+
41+
# Create a new channel for moderation
42+
channel_id = "fellowship-of-the-ring-moderation-#{SecureRandom.alphanumeric(20)}"
43+
44+
@channel = @client.channel('team', channel_id: channel_id,
3645
data: { members: @fellowship_of_the_ring.map { |fellow| fellow[:id] } })
37-
@channel.create('gandalf')
46+
@channel.create(@fellowship_of_the_ring[2][:id])
3847
end
3948

4049
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]
50+
@random_users = [{ id: SecureRandom.uuid }, { id: SecureRandom.uuid }, { id: SecureRandom.uuid }]
51+
@random_user = @random_users[0]
4452

45-
@created_users.push(*users_to_insert.map { |u| u[:id] })
53+
@created_users.push(*@random_users.map { |u| u[:id] })
4654

47-
@client.upsert_users(users_to_insert)
55+
@client.upsert_users(@random_users)
4856
end
4957

5058
after(:all) do
59+
@channel.delete
60+
5161
curr_idx = 0
5262
batch_size = 25
5363

54-
slice = @created_users.slice(0, batch_size)
64+
@users_to_delete = @created_users.dup + @fellowship_of_the_ring.map { |fellow| fellow[:id] }
65+
66+
slice = @users_to_delete.slice(0, batch_size)
5567

5668
while !slice.nil? && !slice.empty?
5769
@client.delete_users(slice, user: StreamChat::HARD_DELETE, messages: StreamChat::HARD_DELETE)
5870

5971
curr_idx += batch_size
60-
slice = @created_users.slice(curr_idx, batch_size)
72+
slice = @users_to_delete.slice(curr_idx, batch_size)
6173
end
6274
end
6375

0 commit comments

Comments
 (0)