From 121c3de8caa0d85ee058cf62879022c767dce948 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Sun, 26 May 2024 18:52:44 -0500 Subject: [PATCH 1/6] Updated tests to get announcements_controller working Changes: 1) Rails 4 -> 5, parameters no longer inherit from HashWithIndifferentAccess 2) response.json no longer is valid in Rails 5. response's class in Rails 4 is ActionController::TestResponse in Rails 5, response's class is ActionDispatch::TestResponse 3) Starting Rails 5, responses will default to 204s unless specified otherwise. And status of 204 will return response.content_type as nil --- README.md | 2 +- app/services/concerns/talk_service.rb | 10 +++++++++- .../shared_examples_for_controller_actions.rb | 14 +++++++++----- .../shared_examples_for_controller_creating.rb | 3 ++- .../shared_examples_for_controller_rescuing.rb | 3 ++- .../shared_examples_for_controller_restricting.rb | 5 +++-- .../shared_examples_for_controller_updating.rb | 3 ++- spec/support/shared_examples_for_schedulable.rb | 2 +- 8 files changed, 29 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index cb9680db..78c4c8e2 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ Using the gem https://github.com/clio/ten_years_rails to help with the upgrade p ``` docker-compose -f docker-compose-rails-next.yml build -docker-compose -f docker-compose-rails-next.yml run --service-ports --rm talkapi bash +docker-compose -f docker-compose-rails-next.yml run --service-ports --rm -e RAILS_ENV=test talkapi bash ``` ### Install the gems via next diff --git a/app/services/concerns/talk_service.rb b/app/services/concerns/talk_service.rb index be15ff26..4aade0c7 100644 --- a/app/services/concerns/talk_service.rb +++ b/app/services/concerns/talk_service.rb @@ -101,7 +101,15 @@ def permitted_params end def rooted_params - permitted_params.slice model_class.table_name + # Parameters in Rails 5+ behave differently, because they no longer inherit from HashWithIndifferentAccess + # methods like slice will behave differently in Rails 4 than Rails 5 + # In Rails 5, we will need to first to apply to_h and then slice. + # TODO: Can Remove version comparison once Prod is on Rails 5+ + if Rails.version.starts_with?('4.2') + permitted_params.slice model_class.table_name + else + permitted_params.to_h.slice model_class.table_name + end end def unrooted_params diff --git a/spec/support/shared_examples_for_controller_actions.rb b/spec/support/shared_examples_for_controller_actions.rb index 695f36ad..6d50639c 100644 --- a/spec/support/shared_examples_for_controller_actions.rb +++ b/spec/support/shared_examples_for_controller_actions.rb @@ -18,11 +18,13 @@ end it 'should be json' do - expect(response.content_type).to eql 'application/json' + # starting from Rails 5+, if responding with 204, content-type of response is set by Rails as nil + expect(response.content_type).to eql 'application/json' unless response.status == 204 end it 'should be an object' do - expect(response.json).to be_a Hash + response_body = JSON.parse(response.body) + expect(response_body).to be_a Hash end end @@ -38,7 +40,8 @@ end it 'should respond with the correct error message' do - expect(response.json[:error]).to eql 'You are banned' + response_body = JSON.parse(response.body).with_indifferent_access + expect(response_body[:error]).to eql 'You are banned' end end @@ -47,7 +50,7 @@ let!(:banned_ip){ create :user_ip_ban } before(:each) do allow(subject).to receive(:current_user).and_return user - allow(subject.request).to receive(:remote_ip).and_return '1.2.3.4' + request.env['REMOTE_ADDR'] = '1.2.3.4' send_request end @@ -56,7 +59,8 @@ end it 'should respond with the correct error message' do - expect(response.json[:error]).to eql 'You are banned' + response_body = JSON.parse(response.body).with_indifferent_access + expect(response_body[:error]).to eql 'You are banned' end end end diff --git a/spec/support/shared_examples_for_controller_creating.rb b/spec/support/shared_examples_for_controller_creating.rb index f953a8ab..9221b408 100644 --- a/spec/support/shared_examples_for_controller_creating.rb +++ b/spec/support/shared_examples_for_controller_creating.rb @@ -49,7 +49,8 @@ end it 'should be an object' do - expect(response.json).to be_a Hash + response_body = JSON.parse(response.body) + expect(response_body).to be_a Hash end end diff --git a/spec/support/shared_examples_for_controller_rescuing.rb b/spec/support/shared_examples_for_controller_rescuing.rb index 570bbcd5..8318d627 100644 --- a/spec/support/shared_examples_for_controller_rescuing.rb +++ b/spec/support/shared_examples_for_controller_rescuing.rb @@ -15,7 +15,8 @@ it 'should report the error message' do get :index - expect(response.json[:error]).to eql exception.to_s + response_body = JSON.parse(response.body).with_indifferent_access + expect(response_body[:error]).to eql exception.to_s end end end diff --git a/spec/support/shared_examples_for_controller_restricting.rb b/spec/support/shared_examples_for_controller_restricting.rb index 5c76a345..abf3317d 100644 --- a/spec/support/shared_examples_for_controller_restricting.rb +++ b/spec/support/shared_examples_for_controller_restricting.rb @@ -7,11 +7,12 @@ it 'should respond appropriately' do send_request + response_body = JSON.parse(response.body).with_indifferent_access case expected_response when :error - expect(response.json).to have_key :error + expect(response_body).to have_key :error when :empty - expect(response.json[resource_name]).to be_empty + expect(response_body[resource_name]).to be_empty end end end diff --git a/spec/support/shared_examples_for_controller_updating.rb b/spec/support/shared_examples_for_controller_updating.rb index c02f8e64..22e829f9 100644 --- a/spec/support/shared_examples_for_controller_updating.rb +++ b/spec/support/shared_examples_for_controller_updating.rb @@ -51,7 +51,8 @@ end it 'should be an object' do - expect(response.json).to be_a Hash + response_body = JSON.parse(response.body) + expect(response_body).to be_a Hash end end diff --git a/spec/support/shared_examples_for_schedulable.rb b/spec/support/shared_examples_for_schedulable.rb index f4f151e9..c1ad2086 100644 --- a/spec/support/shared_examples_for_schedulable.rb +++ b/spec/support/shared_examples_for_schedulable.rb @@ -3,7 +3,7 @@ it 'gets queued on enqueued_times' do enqueued_times.each do |enqueued_time| - # update to formatted_enqueue_time when updating sidekiq-cron to 1.9+ (sidekiq 7 support) + # TODO: Once on sidekiq-cron v1.9 can updated method to "formatted_enqueue_time" job_enqueue_time = Time.at(job.formated_enqueue_time.to_f).utc expect(job_enqueue_time).to eq(enqueued_time) end From 2c3520298d031915d9a997dc9c4ee4255c36137b Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Tue, 28 May 2024 10:49:06 -0500 Subject: [PATCH 2/6] update .json method responses in test no longer an instance of ActionController::TestResponses --- spec/support/api_helpers.rb | 9 +++++++++ spec/support/shared_examples_for_controller_actions.rb | 9 +++------ spec/support/shared_examples_for_controller_creating.rb | 3 +-- spec/support/shared_examples_for_controller_rescuing.rb | 3 +-- .../shared_examples_for_controller_restricting.rb | 5 ++--- spec/support/shared_examples_for_controller_updating.rb | 3 +-- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/spec/support/api_helpers.rb b/spec/support/api_helpers.rb index 53d68ee0..3e013319 100644 --- a/spec/support/api_helpers.rb +++ b/spec/support/api_helpers.rb @@ -1,5 +1,14 @@ +# TODO: Can Remove Lines 2-6 once on Rails 5 class ActionController::TestResponse def json @_json ||= JSON.parse(body).with_indifferent_access end end + +# On Rails 5, Test responses do not inherit from ctionController::TestResponse + +class ActionDispatch::TestResponse + def json + @_json ||= JSON.parse(body).with_indifferent_access + end +end diff --git a/spec/support/shared_examples_for_controller_actions.rb b/spec/support/shared_examples_for_controller_actions.rb index 6d50639c..c064bc25 100644 --- a/spec/support/shared_examples_for_controller_actions.rb +++ b/spec/support/shared_examples_for_controller_actions.rb @@ -23,8 +23,7 @@ end it 'should be an object' do - response_body = JSON.parse(response.body) - expect(response_body).to be_a Hash + expect(response.json).to be_a Hash end end @@ -40,8 +39,7 @@ end it 'should respond with the correct error message' do - response_body = JSON.parse(response.body).with_indifferent_access - expect(response_body[:error]).to eql 'You are banned' + expect(response.json[:error]).to eql 'You are banned' end end @@ -59,8 +57,7 @@ end it 'should respond with the correct error message' do - response_body = JSON.parse(response.body).with_indifferent_access - expect(response_body[:error]).to eql 'You are banned' + expect(response.json[:error]).to eql 'You are banned' end end end diff --git a/spec/support/shared_examples_for_controller_creating.rb b/spec/support/shared_examples_for_controller_creating.rb index 9221b408..f953a8ab 100644 --- a/spec/support/shared_examples_for_controller_creating.rb +++ b/spec/support/shared_examples_for_controller_creating.rb @@ -49,8 +49,7 @@ end it 'should be an object' do - response_body = JSON.parse(response.body) - expect(response_body).to be_a Hash + expect(response.json).to be_a Hash end end diff --git a/spec/support/shared_examples_for_controller_rescuing.rb b/spec/support/shared_examples_for_controller_rescuing.rb index 8318d627..570bbcd5 100644 --- a/spec/support/shared_examples_for_controller_rescuing.rb +++ b/spec/support/shared_examples_for_controller_rescuing.rb @@ -15,8 +15,7 @@ it 'should report the error message' do get :index - response_body = JSON.parse(response.body).with_indifferent_access - expect(response_body[:error]).to eql exception.to_s + expect(response.json[:error]).to eql exception.to_s end end end diff --git a/spec/support/shared_examples_for_controller_restricting.rb b/spec/support/shared_examples_for_controller_restricting.rb index abf3317d..cc22d3e0 100644 --- a/spec/support/shared_examples_for_controller_restricting.rb +++ b/spec/support/shared_examples_for_controller_restricting.rb @@ -7,12 +7,11 @@ it 'should respond appropriately' do send_request - response_body = JSON.parse(response.body).with_indifferent_access case expected_response when :error - expect(response_body).to have_key :error + expect(response.json).to have_key :error when :empty - expect(response_body[resource_name]).to be_empty + expect(respons.json[resource_name]).to be_empty end end end diff --git a/spec/support/shared_examples_for_controller_updating.rb b/spec/support/shared_examples_for_controller_updating.rb index 22e829f9..c02f8e64 100644 --- a/spec/support/shared_examples_for_controller_updating.rb +++ b/spec/support/shared_examples_for_controller_updating.rb @@ -51,8 +51,7 @@ end it 'should be an object' do - response_body = JSON.parse(response.body) - expect(response_body).to be_a Hash + expect(response.json).to be_a Hash end end From 9ec880ce0fc9b843a36e9c349430f670960e9434 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Tue, 28 May 2024 10:49:38 -0500 Subject: [PATCH 3/6] update typo --- spec/support/shared_examples_for_controller_restricting.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/shared_examples_for_controller_restricting.rb b/spec/support/shared_examples_for_controller_restricting.rb index cc22d3e0..5c76a345 100644 --- a/spec/support/shared_examples_for_controller_restricting.rb +++ b/spec/support/shared_examples_for_controller_restricting.rb @@ -11,7 +11,7 @@ when :error expect(response.json).to have_key :error when :empty - expect(respons.json[resource_name]).to be_empty + expect(response.json[resource_name]).to be_empty end end end From 9464a9f91f770edd2917ac8dc48653667a6959a4 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Tue, 28 May 2024 10:50:34 -0500 Subject: [PATCH 4/6] update comment --- spec/support/api_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/api_helpers.rb b/spec/support/api_helpers.rb index 3e013319..1c1af20e 100644 --- a/spec/support/api_helpers.rb +++ b/spec/support/api_helpers.rb @@ -1,4 +1,4 @@ -# TODO: Can Remove Lines 2-6 once on Rails 5 +# TODO: Can Remove Lines 2-6 (definition of ActionController::TestResponse#json) once on Rails 5 class ActionController::TestResponse def json @_json ||= JSON.parse(body).with_indifferent_access From 1efa8267ee9985d35ca03823ad9ac2bcde4ed84b Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Tue, 28 May 2024 11:34:57 -0500 Subject: [PATCH 5/6] ActionController::TestResponse injerits from ActionDispatch::TestResponse in Rails 4. Rails 5 test responses are instances of ActionDispatch::TestResponse --- spec/support/api_helpers.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/spec/support/api_helpers.rb b/spec/support/api_helpers.rb index 1c1af20e..542f10c0 100644 --- a/spec/support/api_helpers.rb +++ b/spec/support/api_helpers.rb @@ -1,12 +1,3 @@ -# TODO: Can Remove Lines 2-6 (definition of ActionController::TestResponse#json) once on Rails 5 -class ActionController::TestResponse - def json - @_json ||= JSON.parse(body).with_indifferent_access - end -end - -# On Rails 5, Test responses do not inherit from ctionController::TestResponse - class ActionDispatch::TestResponse def json @_json ||= JSON.parse(body).with_indifferent_access From b30d055ba3997b4bcfb10ff8c855503425eb6832 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Tue, 28 May 2024 14:02:50 -0500 Subject: [PATCH 6/6] update talk_service update_resource to_h --- app/services/concerns/talk_service.rb | 2 +- spec/support/shared_examples_for_services.rb | 16 +++++----------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/app/services/concerns/talk_service.rb b/app/services/concerns/talk_service.rb index be15ff26..611521a4 100644 --- a/app/services/concerns/talk_service.rb +++ b/app/services/concerns/talk_service.rb @@ -33,7 +33,7 @@ def find_resource end def update_resource - resource.assign_attributes unrooted_params + resource.assign_attributes unrooted_params.to_h rescue ArgumentError => e reraise_enum_errors e rescue NameError => e diff --git a/spec/support/shared_examples_for_services.rb b/spec/support/shared_examples_for_services.rb index e738ee5c..8302c2d9 100644 --- a/spec/support/shared_examples_for_services.rb +++ b/spec/support/shared_examples_for_services.rb @@ -69,7 +69,7 @@ describe '#unrooted_params' do let(:create_params){ { resource.table_name => { foo: 1 } } } - subject{ service.unrooted_params } + subject{ service.unrooted_params.to_h } it{ is_expected.to include 'foo' => 1 } it 'should permit the params' do @@ -98,19 +98,13 @@ end it 'should set the user id' do - expect{ - service.set_user - }.to change{ - service.unrooted_params[:user_id] - }.from(nil).to current_user.id + service.set_user + expect(service.unrooted_params[:user_id]).to eq(current_user.id) end it 'should accept a block' do - expect{ - service.set_user{ unrooted_params[:foo] = 123 } - }.to change{ - service.unrooted_params[:foo] - }.from(nil).to 123 + service.set_user{ unrooted_params[:foo] = 123 } + expect(service.unrooted_params[:foo]).to eq(123) end end end