2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe PostActionUsersController do
|
2019-05-10 18:59:31 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
let(:post) { Fabricate(:post, user: sign_in(user)) }
|
2017-06-03 02:23:56 +08:00
|
|
|
|
|
|
|
context 'with render' do
|
|
|
|
it 'always allows you to see your own actions' do
|
|
|
|
notify_mod = PostActionType.types[:notify_moderators]
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.new(post.user, post, notify_mod, message: 'well something is wrong here!').perform
|
|
|
|
PostActionCreator.new(Fabricate(:user), post, notify_mod, message: 'well something is not wrong here!').perform
|
2017-06-03 02:23:56 +08:00
|
|
|
|
2018-06-05 21:35:54 +08:00
|
|
|
get "/post_action_users.json", params: { id: post.id, post_action_type_id: notify_mod }
|
2017-06-03 02:23:56 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-04-18 01:23:33 +08:00
|
|
|
|
|
|
|
users = response.parsed_body["post_action_users"]
|
2017-06-03 02:23:56 +08:00
|
|
|
|
|
|
|
expect(users.length).to eq(1)
|
|
|
|
expect(users[0]["id"]).to eq(post.user.id)
|
|
|
|
end
|
|
|
|
end
|
2015-10-01 00:28:13 +08:00
|
|
|
|
|
|
|
it 'raises an error without an id' do
|
2018-06-05 21:35:54 +08:00
|
|
|
get "/post_action_users.json", params: { post_action_type_id: PostActionType.types[:like] }
|
|
|
|
expect(response.status).to eq(400)
|
2015-10-01 00:28:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error without a post action type' do
|
2018-06-05 21:35:54 +08:00
|
|
|
get "/post_action_users.json", params: { id: post.id }
|
|
|
|
expect(response.status).to eq(400)
|
2015-10-01 00:28:13 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails when the user doesn't have permission to see the post" do
|
2018-06-05 21:35:54 +08:00
|
|
|
post.trash!
|
|
|
|
get "/post_action_users.json", params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
id: post.id, post_action_type_id: PostActionType.types[:like]
|
2018-06-05 21:35:54 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2015-10-01 00:28:13 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
|
2017-06-03 02:23:56 +08:00
|
|
|
it 'raises an error when anon tries to look at an invalid action' do
|
2018-06-05 21:35:54 +08:00
|
|
|
get "/post_action_users.json", params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
id: Fabricate(:post).id,
|
|
|
|
post_action_type_id: PostActionType.types[:notify_moderators]
|
2018-06-05 21:35:54 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2015-10-01 00:28:13 +08:00
|
|
|
expect(response).to be_forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'succeeds' do
|
2018-06-05 21:35:54 +08:00
|
|
|
get "/post_action_users.json", params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
id: post.id, post_action_type_id: PostActionType.types[:like]
|
|
|
|
}
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-10-01 00:28:13 +08:00
|
|
|
end
|
2017-11-15 08:28:54 +08:00
|
|
|
|
2020-06-19 22:44:21 +08:00
|
|
|
it 'will return an unknown attribute for muted users' do
|
|
|
|
ignored_user = Fabricate(:user)
|
|
|
|
PostActionCreator.like(ignored_user, post)
|
|
|
|
regular_user = Fabricate(:user)
|
|
|
|
PostActionCreator.like(regular_user, post)
|
2020-11-03 20:38:54 +08:00
|
|
|
Fabricate(:ignored_user, user: user, ignored_user: ignored_user)
|
2020-06-19 22:44:21 +08:00
|
|
|
|
|
|
|
get "/post_action_users.json", params: {
|
|
|
|
id: post.id, post_action_type_id: PostActionType.types[:like]
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json_users = response.parsed_body['post_action_users']
|
|
|
|
expect(json_users.find { |u| u['id'] == regular_user.id }['unknown']).to be_blank
|
|
|
|
expect(json_users.find { |u| u['id'] == ignored_user.id }['unknown']).to eq(true)
|
|
|
|
end
|
|
|
|
|
2017-11-15 08:28:54 +08:00
|
|
|
it "paginates post actions" do
|
|
|
|
user_ids = []
|
|
|
|
5.times do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user_ids << user["id"]
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(user, post)
|
2017-11-15 08:28:54 +08:00
|
|
|
end
|
|
|
|
|
2018-06-05 21:35:54 +08:00
|
|
|
get "/post_action_users.json",
|
|
|
|
params: { id: post.id, post_action_type_id: PostActionType.types[:like], page: 1, limit: 2 }
|
2017-11-15 08:28:54 +08:00
|
|
|
|
2020-04-18 01:23:33 +08:00
|
|
|
users = response.parsed_body["post_action_users"]
|
|
|
|
total = response.parsed_body["total_rows_post_action_users"]
|
2017-11-15 08:28:54 +08:00
|
|
|
|
|
|
|
expect(users.length).to eq(2)
|
|
|
|
expect(users.map { |u| u["id"] }).to eq(user_ids[2..3])
|
|
|
|
|
|
|
|
expect(total).to eq(5)
|
|
|
|
end
|
2020-04-18 01:23:33 +08:00
|
|
|
|
|
|
|
it 'returns no users when the action type id is invalid' do
|
|
|
|
get "/post_action_users.json", params: {
|
|
|
|
id: post.id, post_action_type_id: "invalid_action_type"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
users = response.parsed_body["post_action_users"]
|
|
|
|
total = response.parsed_body["total_rows_post_action_users"]
|
|
|
|
|
|
|
|
expect(users.length).to eq(0)
|
|
|
|
expect(total).to be_nil
|
|
|
|
end
|
2015-10-01 00:28:13 +08:00
|
|
|
end
|