Skip to content

Commit

Permalink
Store routing context when mounting engine, Fix i18n path (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Igelmund authored and mikker committed Mar 8, 2018
1 parent d1eb583 commit 3e17a8b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 18 deletions.
6 changes: 0 additions & 6 deletions app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class ExpiredSessionError < StandardError; end

include ControllerHelpers

helper_method :authenticatable_resource

# get '/sign_in'
# Assigns an email_field and new Session to be used by new view.
# renders sessions/new.html.erb.
Expand Down Expand Up @@ -87,10 +85,6 @@ def authenticatable_class
authenticatable_classname.constantize
end

def authenticatable_resource
authenticatable.pluralize
end

def email_field
authenticatable_class.passwordless_email_field
end
Expand Down
6 changes: 2 additions & 4 deletions app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ class Mailer < ActionMailer::Base
def magic_link(session)
@session = session

authenticatable_resource_name =
@session.authenticatable_type.underscore.pluralize
@magic_link =
send(authenticatable_resource_name).token_sign_in_url(session.token)
@magic_link = send(Passwordless.mounted_as)
.token_sign_in_url(session.token)

email_field = @session.authenticatable.class.passwordless_email_field
mail(
Expand Down
2 changes: 1 addition & 1 deletion app/views/passwordless/sessions/create.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p><%= I18n.t('passwordless.sessions.success.email_sent_if_record_found') %></p>
<p><%= I18n.t('passwordless.sessions.create.email_sent_if_record_found') %></p>
2 changes: 1 addition & 1 deletion app/views/passwordless/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_for @session, url: send(authenticatable_resource).sign_in_path do |f| %>
<%= form_for @session, url: send(Passwordless.mounted_as).sign_in_path do |f| %>
<% email_field_name = :"passwordless[#{@email_field}]" %>
<%= text_field_tag email_field_name, params.fetch(email_field_name, nil) %>
<%= f.submit I18n.t('passwordless.sessions.new.submit') %>
Expand Down
1 change: 1 addition & 0 deletions lib/passwordless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ module Passwordless
mattr_accessor(:default_from_address) { '[email protected]' }
mattr_accessor(:token_generator) { UrlSafeBase64Generator.new }
mattr_accessor(:redirect_back_after_sign_in) { true }
mattr_accessor(:mounted_as) { :configured_when_mounting_passwordless }
end
8 changes: 5 additions & 3 deletions lib/passwordless/router_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ module RouterHelpers
# <%= link_to 'Sign in', user_session_things.sign_in_path %>).
# (Default: resource.to_s)
def passwordless_for(resource, at: nil, as: nil)
mount_at = at || resource.to_s
mount_as = as || resource.to_s
mount(
Passwordless::Engine,
at: at || resource.to_s,
as: as || resource.to_s,
Passwordless::Engine, at: mount_at, as: mount_as,
defaults: { authenticatable: resource.to_s.singularize }
)

Passwordless.mounted_as = mount_as
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alice:
email: [email protected]
54 changes: 51 additions & 3 deletions test/integration/navigation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,55 @@
require 'test_helper'

class NavigationTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
fixtures :users

test 'failed access, sign in and redirect to protected resource' do
alice = users(:alice)

# Verify the user has no access to the /secret endpoint and
# is instead redirected to homepage. Meanwhile access to /secret is
# stored in the users session.
get '/secret'
assert_equal 302, status
assert_equal 'Not worthy!', flash['error']
follow_redirect!
assert_equal 200, status
assert_equal '/', path

# Load login form
get '/users/sign_in'
assert_equal 200, status

# Submit form
post '/users/sign_in',
params: {
passwordless: { email: alice.email }
},
headers: { 'HTTP_USER_AGENT' => 'Mosaic v.1' }
assert_equal 200, status
assert response.body.include?('If we found you in the system')

# Expect session created for alice
session = Passwordless::Session.find_by! authenticatable: alice
assert_equal 'Mosaic v.1', session.user_agent

# Expect mail for alice
assert_equal 1, ActionMailer::Base.deliveries.count
email = ActionMailer::Base.deliveries.first
assert_equal alice.email, email.to.first

# Expect mail body to include session link
token_sign_in_path = "/users/sign_in/#{session.token}"
assert email.body.to_s.include?(token_sign_in_path)

# Follow link, Expect redirect to /secret path which has been unsuccessfully
# accessed in the beginning.
get token_sign_in_path
assert_equal 302, status
follow_redirect!

assert_equal 200, status
assert_equal '/secret', path
assert_equal 'shhhh! secrets!', response.body
end
end

0 comments on commit 3e17a8b

Please sign in to comment.