Skip to content

Commit

Permalink
Fix test helpers (#181)
Browse files Browse the repository at this point in the history
* Fix test helpers

* ..
  • Loading branch information
mikker authored Nov 10, 2023
1 parent 0db3f57 commit 93e7063
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def create
end

redirect_to(
Passwordless.context.url_for(
Passwordless.context.path_for(
@session,
id: @session.to_param,
action: "show"
Expand Down
3 changes: 1 addition & 2 deletions lib/passwordless/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 28 additions & 5 deletions lib/passwordless/test_helpers.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,25 +46,28 @@ 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
end
end

if defined?(ActiveSupport::TestCase)
ActiveSupport::TestCase.send(:include, ::Passwordless::TestHelpers::TestCase)
ActiveSupport::TestCase.send(:include, ::Passwordless::TestHelpers::ControllerTestCase)
end

if defined?(ActionDispatch::SystemTestCase)
Expand All @@ -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
41 changes: 35 additions & 6 deletions test/passwordless/test_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
)

Expand All @@ -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
Expand All @@ -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
)

Expand Down

0 comments on commit 93e7063

Please sign in to comment.