From dce46086f49c1a16d2bbf9e5b090b2d7477ee8ed Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Fri, 17 Apr 2020 14:23:33 -0300 Subject: [PATCH] FIX: Do not raise an error if the post action type is nil (#9458) --- .../post_action_users_controller.rb | 2 +- .../post_action_users_controller_spec.rb | 23 +++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/controllers/post_action_users_controller.rb b/app/controllers/post_action_users_controller.rb index 73efa06a3e4..fe42fd3b9ca 100644 --- a/app/controllers/post_action_users_controller.rb +++ b/app/controllers/post_action_users_controller.rb @@ -27,7 +27,7 @@ class PostActionUsersController < ApplicationController end action_type = PostActionType.types.key(post_action_type_id) - total_count = post["#{action_type}_count"] + total_count = post["#{action_type}_count"].to_i data = { post_action_users: serialize_data(post_actions.to_a, PostActionUserSerializer) } diff --git a/spec/requests/post_action_users_controller_spec.rb b/spec/requests/post_action_users_controller_spec.rb index 4b6f6375d48..bc053524fe9 100644 --- a/spec/requests/post_action_users_controller_spec.rb +++ b/spec/requests/post_action_users_controller_spec.rb @@ -15,8 +15,8 @@ describe PostActionUsersController do get "/post_action_users.json", params: { id: post.id, post_action_type_id: notify_mod } expect(response.status).to eq(200) - json = JSON.parse(response.body) - users = json["post_action_users"] + + users = response.parsed_body["post_action_users"] expect(users.length).to eq(1) expect(users[0]["id"]).to eq(post.user.id) @@ -69,14 +69,27 @@ describe PostActionUsersController do get "/post_action_users.json", params: { id: post.id, post_action_type_id: PostActionType.types[:like], page: 1, limit: 2 } - json = JSON.parse(response.body) - users = json["post_action_users"] - total = json["total_rows_post_action_users"] + users = response.parsed_body["post_action_users"] + total = response.parsed_body["total_rows_post_action_users"] expect(users.length).to eq(2) expect(users.map { |u| u["id"] }).to eq(user_ids[2..3]) expect(total).to eq(5) end + + 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 end