From 01007a019f05ec699e244f2a334ad86c40ae7693 Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Tue, 12 Nov 2024 12:18:04 +0100 Subject: [PATCH 1/6] add perform_delete --- lib/rails_edge_test/dsl/edge.rb | 6 ++++++ spec/rails_edge_test/dsl_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/rails_edge_test/dsl/edge.rb b/lib/rails_edge_test/dsl/edge.rb index 046d098..0fb1baf 100644 --- a/lib/rails_edge_test/dsl/edge.rb +++ b/lib/rails_edge_test/dsl/edge.rb @@ -38,6 +38,12 @@ def perform_post(parameters={}) process(parameters) end + def perform_delete(parameters={}) + request.instance_variable_set(:@method, "DELETE") + request.env['REQUEST_METHOD'] = "DELETE" + process(parameters) + end + def process(parameters={}) request.assign_parameters( ::Rails.application.routes, diff --git a/spec/rails_edge_test/dsl_spec.rb b/spec/rails_edge_test/dsl_spec.rb index 06b7baa..3246aeb 100644 --- a/spec/rails_edge_test/dsl_spec.rb +++ b/spec/rails_edge_test/dsl_spec.rb @@ -16,6 +16,10 @@ def complex def post_action render json: {hello: 'world'} end + + def delete_action + render json: {this: 'deletes'} + end end AnotherController = Class.new ActionController::Base do @@ -30,6 +34,7 @@ def another get 'test/simple' => 'my#simple' get 'test/complex' => 'my#complex' get 'test/post_action' => 'my#post_action' + delete 'test/delete_action' => 'my#delete_action' get 'test/another' => 'another#another' end @@ -152,6 +157,29 @@ def another expect(test_value[2].body).to eq({hello: 'world'}.to_json) end + it "can perform a delete request" do + test_value = nil + + Module.new do + extend RailsEdgeTest::Dsl + + controller MyController do + action :delete_action do + edge "delete :delete_action" do + test_value = perform_delete + end + end + end + end + + RailsEdgeTest::Dsl.execute! + + expect(test_value[0]).to eq 200 + expect(test_value[1]).to be_a Hash + expect(test_value[2]).to be_a ActionDispatch::Response::RackBody + expect(test_value[2].body).to eq({this: 'deletes'}.to_json) + end + it "can incorporate request, session, and params when making a request" do test_value = nil expected_value = { From 92a09017c1e03650b2869e3440a6d5c81a3b8765 Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Tue, 12 Nov 2024 12:18:26 +0100 Subject: [PATCH 2/6] route to post action is a post --- spec/rails_edge_test/dsl_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/rails_edge_test/dsl_spec.rb b/spec/rails_edge_test/dsl_spec.rb index 3246aeb..4a1f402 100644 --- a/spec/rails_edge_test/dsl_spec.rb +++ b/spec/rails_edge_test/dsl_spec.rb @@ -33,7 +33,7 @@ def another Rails.application.routes.draw do get 'test/simple' => 'my#simple' get 'test/complex' => 'my#complex' - get 'test/post_action' => 'my#post_action' + post 'test/post_action' => 'my#post_action' delete 'test/delete_action' => 'my#delete_action' get 'test/another' => 'another#another' From c59eddf80bba29dfe4c782a5a2c41ae475748980 Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Tue, 12 Nov 2024 12:18:51 +0100 Subject: [PATCH 3/6] add set_authenticity_token helper --- lib/rails_edge_test/dsl/edge.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/rails_edge_test/dsl/edge.rb b/lib/rails_edge_test/dsl/edge.rb index 0fb1baf..b2bf5bc 100644 --- a/lib/rails_edge_test/dsl/edge.rb +++ b/lib/rails_edge_test/dsl/edge.rb @@ -44,6 +44,12 @@ def perform_delete(parameters={}) process(parameters) end + def set_authenticity_token + r = request + controller.instance_eval { @_request = r } + request.headers['X-CSRF-Token'] = controller.send :form_authenticity_token + end + def process(parameters={}) request.assign_parameters( ::Rails.application.routes, From 01ab61bee91e4fefeb6b2c049b06e177ae29e84b Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Tue, 12 Nov 2024 12:19:56 +0100 Subject: [PATCH 4/6] update version & changelog --- CHANGELOG.md | 4 ++++ lib/rails_edge_test/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c8ebf7..ef2c815 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # The Changelog +## Version 2.1.0: August 13, 2024 +- adds `delete` method for simulating post requests +- adds `set_authenticity_token` method for setting the authenticity token in the header & session + ## Version 2.0.0: August 13, 2024 - drops support for Ruby 2.* - adds `post` method for simulating post requests diff --git a/lib/rails_edge_test/version.rb b/lib/rails_edge_test/version.rb index 892ba55..c056d94 100644 --- a/lib/rails_edge_test/version.rb +++ b/lib/rails_edge_test/version.rb @@ -1,3 +1,3 @@ module RailsEdgeTest - VERSION = "2.0.0" + VERSION = "2.1.0" end From 648da52a1f28ff49d664e4d69127022cc217aefd Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Tue, 12 Nov 2024 14:52:14 +0100 Subject: [PATCH 5/6] Update CHANGELOG.md Co-authored-by: Mfon Eti-mfon <50708034+mfonism@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef2c815..d6f812a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # The Changelog ## Version 2.1.0: August 13, 2024 -- adds `delete` method for simulating post requests +- adds `delete` method for simulating delete requests - adds `set_authenticity_token` method for setting the authenticity token in the header & session ## Version 2.0.0: August 13, 2024 From 4bfdc922e9e9b79fef0821df58a4bbc4e2a86878 Mon Sep 17 00:00:00 2001 From: Michael Glass Date: Tue, 12 Nov 2024 15:35:10 +0100 Subject: [PATCH 6/6] add @mfonism's tests (thanks) --- spec/rails_edge_test/dsl_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/rails_edge_test/dsl_spec.rb b/spec/rails_edge_test/dsl_spec.rb index 4a1f402..0c78dbe 100644 --- a/spec/rails_edge_test/dsl_spec.rb +++ b/spec/rails_edge_test/dsl_spec.rb @@ -180,6 +180,29 @@ def another expect(test_value[2].body).to eq({this: 'deletes'}.to_json) end + it "can set authenticity token for the request" do + test_request = nil + + Module.new do + extend RailsEdgeTest::Dsl + + controller MyController do + action :simple do + edge "set the authenticity token" do + test_request = request + + set_authenticity_token + end + end + end + end + + allow_any_instance_of(MyController).to receive(:form_authenticity_token).and_return('a_test_token') + RailsEdgeTest::Dsl.execute! + + expect(test_request.headers['X-CSRF-Token']).to eq 'a_test_token' + end + it "can incorporate request, session, and params when making a request" do test_value = nil expected_value = {