From 93e70631020548969ebc5faea72fee027e671269 Mon Sep 17 00:00:00 2001 From: Mikkel Malmberg Date: Fri, 10 Nov 2023 21:58:32 +0100 Subject: [PATCH] Fix test helpers (#181) * Fix test helpers * .. --- .../passwordless/sessions_controller.rb | 2 +- lib/passwordless/context.rb | 3 +- lib/passwordless/test_helpers.rb | 33 ++++++++++++--- test/passwordless/test_helpers_test.rb | 41 ++++++++++++++++--- 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/app/controllers/passwordless/sessions_controller.rb b/app/controllers/passwordless/sessions_controller.rb index 24ed626..c114e00 100644 --- a/app/controllers/passwordless/sessions_controller.rb +++ b/app/controllers/passwordless/sessions_controller.rb @@ -37,7 +37,7 @@ def create end redirect_to( - Passwordless.context.url_for( + Passwordless.context.path_for( @session, id: @session.to_param, action: "show" diff --git a/lib/passwordless/context.rb b/lib/passwordless/context.rb index e1da6f9..83812eb 100644 --- a/lib/passwordless/context.rb +++ b/lib/passwordless/context.rb @@ -40,8 +40,7 @@ def url_for(session_or_authenticatable, **options) end Rails.application.routes.url_helpers.url_for( - **resource.defaults, - **options + resource.defaults.merge(options) ) end diff --git a/lib/passwordless/test_helpers.rb b/lib/passwordless/test_helpers.rb index 0174d58..1be852b 100644 --- a/lib/passwordless/test_helpers.rb +++ b/lib/passwordless/test_helpers.rb @@ -1,22 +1,42 @@ module Passwordless module TestHelpers - module TestCase + module ControllerTestCase + class H + extend ControllerHelpers + end + + def passwordless_sign_out(cls = nil) + cls ||= "User".constantize + @request.session.delete(H.session_key(cls)) + end + + def passwordless_sign_in(resource) + session = Passwordless::Session.create!(authenticatable: resource) + @request.session[H.session_key(resource.class)] = session.id + end + end + + module RequestTestCase def passwordless_sign_out(cls = nil) cls ||= "User".constantize resource = cls.model_name.to_s.tableize + dest = Passwordless.context.path_for(resource, action: "destroy") delete(dest) + follow_redirect! end def passwordless_sign_in(resource) session = Passwordless::Session.create!(authenticatable: resource) + magic_link = Passwordless.context.path_for( session, action: "confirm", id: session.to_param, token: session.token ) + get(magic_link) follow_redirect! end @@ -26,17 +46,20 @@ module SystemTestCase def passwordless_sign_out(cls = nil) cls ||= "User".constantize resource = cls.model_name.to_s.tableize + visit(Passwordless.context.url_for(resource, action: "destroy")) end def passwordless_sign_in(resource) session = Passwordless::Session.create!(authenticatable: resource) + magic_link = Passwordless.context.url_for( session, action: "confirm", - id: session.id, + id: session.to_param, token: session.token ) + visit(magic_link) end end @@ -44,7 +67,7 @@ def passwordless_sign_in(resource) end if defined?(ActiveSupport::TestCase) - ActiveSupport::TestCase.send(:include, ::Passwordless::TestHelpers::TestCase) + ActiveSupport::TestCase.send(:include, ::Passwordless::TestHelpers::ControllerTestCase) end if defined?(ActionDispatch::SystemTestCase) @@ -53,8 +76,8 @@ def passwordless_sign_in(resource) if defined?(RSpec) RSpec.configure do |config| - config.include(::Passwordless::TestHelpers::TestCase, type: :request) - config.include(::Passwordless::TestHelpers::TestCase, type: :controller) + config.include(::Passwordless::TestHelpers::ControllerTestCase, type: :controller) + config.include(::Passwordless::TestHelpers::RequestTestCase, type: :request) config.include(::Passwordless::TestHelpers::SystemTestCase, type: :system) end end diff --git a/test/passwordless/test_helpers_test.rb b/test/passwordless/test_helpers_test.rb index 0697b29..26a5d8d 100644 --- a/test/passwordless/test_helpers_test.rb +++ b/test/passwordless/test_helpers_test.rb @@ -12,8 +12,19 @@ def initialize attr_reader :actions end - class MockUnitTest < MockTest - include Passwordless::TestHelpers::TestCase + class MockControllerTest < MockTest + include Passwordless::TestHelpers::ControllerTestCase + + def initialize + super + @request = OpenStruct.new(session: {}) + end + + attr_reader :request + end + + class MockRequestTest < MockTest + include Passwordless::TestHelpers::RequestTestCase def get(*args) @actions << [:get, args] @@ -37,16 +48,20 @@ def visit(*args) end class PasswordlessTestHelpersTest < ActiveSupport::TestCase - test("unit test") do + class H + extend ControllerHelpers + end + + test("request test") do alice = users(:alice) - controller = MockUnitTest.new + controller = MockRequestTest.new controller.passwordless_sign_in(alice) assert 1, Session.count assert alice, Session.last!.authenticatable assert_match( - %r{/users/sign_in/[a-z0-9-]+/[a-z0-9]+}i, + %r{/users/sign_in/[a-z0-9-]{36}/[a-z0-9]+}i, controller.actions.first.last.first ) @@ -58,6 +73,20 @@ class PasswordlessTestHelpersTest < ActiveSupport::TestCase ) end + test("controller test") do + alice = users(:alice) + controller = MockControllerTest.new + + controller.passwordless_sign_in(alice) + + assert 1, Session.count + assert alice, Session.last!.authenticatable + assert_equal Session.last!.id, controller.request.session[H.session_key(User)] + + controller.passwordless_sign_out(User) + assert_nil controller.request.session[H.session_key(User)] + end + test("system test") do alice = users(:alice) controller = MockSystemTest.new @@ -67,7 +96,7 @@ class PasswordlessTestHelpersTest < ActiveSupport::TestCase assert 1, Session.count assert alice, Session.last!.authenticatable assert_match( - %r{^http://.*/users/sign_in/[a-z0-9]+/[a-z0-9]+}i, + %r{^http://.*/users/sign_in/[a-z0-9-]{36}/[a-z0-9]+}i, controller.actions.last.last.first )