Skip to content

Commit

Permalink
Update test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mikker committed Nov 7, 2023
1 parent 588821c commit 6e89a62
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 12 deletions.
55 changes: 43 additions & 12 deletions lib/passwordless/test_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,65 @@
module Passwordless
module TestHelpers
module TestCase
def passwordless_sign_out
delete(Passwordless::Engine.routes.url_helpers.sign_out_path)
def passwordless_sign_out(cls = nil)
cls ||= "User".constantize
dest = url_for(
{
controller: "passwordless/sessions",
action: "destroy",
authenticatable: cls.model_name.singular,
resource: cls.model_name.plural
}
)
delete(dest)
follow_redirect!
end

def passwordless_sign_in(resource)
cls = resource.class
session = Passwordless::Session.create!(authenticatable: resource)
magic_link = Passwordless::Engine.routes.url_helpers.send(
:"confirm_#{session.authenticatable_type.tableize}_sign_in_url",
session,
session.token
magic_link = url_for(
{
controller: "passwordless/sessions",
action: "confirm",
id: session.id,
token: session.token,
authenticatable: cls.model_name.singular,
resource: cls.model_name.plural
}
)
get(magic_link)
follow_redirect!
end
end

module SystemTestCase
def passwordless_sign_out
visit(Passwordless::Engine.routes.url_helpers.sign_out_path)
def passwordless_sign_out(cls = nil)
cls ||= "User".constantize
visit(
url_for(
{
controller: "passwordless/sessions",
action: "destroy",
authenticatable: cls.model_name.singular,
resource: cls.model_name.plural
}
)
)
end

def passwordless_sign_in(resource)
cls = resource.class
session = Passwordless::Session.create!(authenticatable: resource)
magic_link = Passwordless::Engine.routes.url_helpers.send(
:"confirm_#{session.authenticatable_type.tableize}_sign_in_url",
session,
session.token
magic_link = url_for(
{
controller: "passwordless/sessions",
action: "confirm",
id: session.id,
token: session.token,
authenticatable: cls.model_name.singular,
resource: cls.model_name.plural
}
)
visit(magic_link)
end
Expand Down
82 changes: 82 additions & 0 deletions test/passwordless/test_helpers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require "test_helper"
require "passwordless/test_helpers"

module Passwordless
class MockTest
include Rails.application.routes.url_helpers

def initialize
@actions = []
end

attr_reader :actions
end

class MockUnitTest < MockTest
include Passwordless::TestHelpers::TestCase

def get(*args)
@actions << [:get, args]
end

def delete(*args)
@actions << [:delete, args]
end

def follow_redirect!
@actions << [:follow_redirect!]
end
end

class MockSystemTest < MockTest
include Passwordless::TestHelpers::SystemTestCase

def visit(*args)
@actions << [:visit, args]
end
end

class PasswordlessTestHelpersTest < ActiveSupport::TestCase
test("unit test") do
alice = users(:alice)
controller = MockUnitTest.new

controller.passwordless_sign_in(alice)

assert 1, Session.count
assert alice, Session.last!.authenticatable
assert_match(
%r{^http://.*/users/sign_in/[a-z0-9]+/[a-z0-9]+}i,
controller.actions.first.last.first
)

controller.passwordless_sign_out

assert_match(
%r{^http://.*/users/sign_out},
controller.actions[-2].last.first
)
end

test("system test") do
alice = users(:alice)
controller = MockSystemTest.new

controller.passwordless_sign_in(alice)

assert 1, Session.count
assert alice, Session.last!.authenticatable
assert_match(
%r{^http://.*/users/sign_in/[a-z0-9]+/[a-z0-9]+}i,
controller.actions.last.last.first
)

controller.passwordless_sign_out

assert_match(
%r{^http://.*/users/sign_out},
controller.actions.last.last.first
)
end
end
end

0 comments on commit 6e89a62

Please sign in to comment.