2019-08-27 20:09:00 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe PostReadersController do
|
2019-08-27 20:09:00 +08:00
|
|
|
describe '#index' do
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:reader) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
@group = Fabricate(:group)
|
|
|
|
@group_message = Fabricate(:private_message_topic, allowed_groups: [@group])
|
|
|
|
@post = Fabricate(:post, topic: @group_message, post_number: 3)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context 'when the user has access to readers data' do
|
2019-08-27 20:09:00 +08:00
|
|
|
before do
|
|
|
|
@group.update!(publish_read_state: true)
|
|
|
|
@group.add(admin)
|
|
|
|
@group.add(reader)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty list when nobody has read the topic' do
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
readers = response.parsed_body['post_readers']
|
2019-08-27 20:09:00 +08:00
|
|
|
|
|
|
|
expect(readers).to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an user who read until that post' do
|
|
|
|
TopicUser.create!(user: reader, topic: @group_message, last_read_post_number: 3)
|
|
|
|
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
2020-05-07 23:04:12 +08:00
|
|
|
reader_data = response.parsed_body['post_readers'].first
|
2019-08-27 20:09:00 +08:00
|
|
|
|
|
|
|
assert_reader_is_correctly_serialized(reader_data, reader, @post)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an user who read pass that post' do
|
|
|
|
TopicUser.create!(user: reader, topic: @group_message, last_read_post_number: 4)
|
|
|
|
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
2020-05-07 23:04:12 +08:00
|
|
|
reader_data = response.parsed_body['post_readers'].first
|
2019-08-27 20:09:00 +08:00
|
|
|
|
|
|
|
assert_reader_is_correctly_serialized(reader_data, reader, @post)
|
|
|
|
end
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
it 'return an empty list when nodobody read until that post' do
|
2019-08-27 20:09:00 +08:00
|
|
|
TopicUser.create!(user: reader, topic: @group_message, last_read_post_number: 1)
|
|
|
|
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
2020-05-07 23:04:12 +08:00
|
|
|
readers = response.parsed_body['post_readers']
|
2019-08-27 20:09:00 +08:00
|
|
|
|
|
|
|
expect(readers).to be_empty
|
|
|
|
end
|
|
|
|
|
2019-08-27 23:13:38 +08:00
|
|
|
it "doesn't include users without reading progress on first post" do
|
|
|
|
@post.update(post_number: 1)
|
|
|
|
TopicUser.create!(user: reader, topic: @group_message, last_read_post_number: nil)
|
|
|
|
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
2020-05-07 23:04:12 +08:00
|
|
|
readers = response.parsed_body['post_readers']
|
2019-08-27 23:13:38 +08:00
|
|
|
|
|
|
|
expect(readers).to be_empty
|
|
|
|
end
|
2019-09-11 01:21:34 +08:00
|
|
|
|
|
|
|
it "doesn't include staged users" do
|
|
|
|
TopicUser.create!(user: reader, topic: @group_message, last_read_post_number: 4)
|
|
|
|
reader.update(staged: true)
|
|
|
|
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
2020-05-07 23:04:12 +08:00
|
|
|
readers = response.parsed_body['post_readers']
|
2019-09-11 01:21:34 +08:00
|
|
|
|
|
|
|
expect(readers).to be_empty
|
|
|
|
end
|
2019-09-14 04:14:31 +08:00
|
|
|
|
2019-12-03 21:49:25 +08:00
|
|
|
it "doesn't include non-staff users when the post is a whisper" do
|
2019-09-14 04:14:31 +08:00
|
|
|
@post.update(post_type: Post.types[:whisper])
|
2019-12-03 21:49:25 +08:00
|
|
|
non_staff_user = Fabricate(:user)
|
|
|
|
TopicUser.create!(user: non_staff_user, topic: @group_message, last_read_post_number: 4)
|
2019-09-14 04:14:31 +08:00
|
|
|
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
2020-05-07 23:04:12 +08:00
|
|
|
readers = response.parsed_body['post_readers']
|
2019-09-14 04:14:31 +08:00
|
|
|
|
|
|
|
expect(readers).to be_empty
|
|
|
|
end
|
2019-11-15 04:53:52 +08:00
|
|
|
|
2019-12-03 21:49:25 +08:00
|
|
|
it "includes staff users when the post is a whisper" do
|
|
|
|
@post.update(post_type: Post.types[:whisper])
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
TopicUser.create!(user: admin, topic: @group_message, last_read_post_number: 4)
|
|
|
|
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
2020-05-07 23:04:12 +08:00
|
|
|
reader_data = response.parsed_body['post_readers'].first
|
2019-12-03 21:49:25 +08:00
|
|
|
|
|
|
|
assert_reader_is_correctly_serialized(reader_data, admin, @post)
|
|
|
|
end
|
|
|
|
|
2019-11-15 04:53:52 +08:00
|
|
|
it "doesn't include bots" do
|
|
|
|
TopicUser.create!(user: Discourse.system_user, topic: @group_message, last_read_post_number: 4)
|
|
|
|
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
2020-05-07 23:04:12 +08:00
|
|
|
readers = response.parsed_body['post_readers']
|
2019-11-15 04:53:52 +08:00
|
|
|
|
|
|
|
expect(readers).to be_empty
|
|
|
|
end
|
2019-08-27 20:09:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_reader_is_correctly_serialized(reader_data, reader, post)
|
|
|
|
expect(reader_data['avatar_template']).to eq reader.avatar_template
|
|
|
|
expect(reader_data['username']).to eq reader.username
|
|
|
|
expect(reader_data['username_lower']).to eq reader.username_lower
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns forbidden if no group has publish_read_state enabled' do
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
|
|
|
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns forbidden if current_user is not a member of a group with publish_read_state enabled' do
|
|
|
|
@group.update!(publish_read_state: true)
|
|
|
|
|
|
|
|
get '/post_readers.json', params: { id: @post.id }
|
|
|
|
|
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|