Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make DTA respect controller overrides it doesn't know about #1435

Merged
merged 1 commit into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/devise_token_auth/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ def mount_devise_token_auth_for(resource, opts)
opts[:skip] ||= []

# check for ctrl overrides, fall back to defaults
sessions_ctrl = opts[:controllers][:sessions] || 'devise_token_auth/sessions'
registrations_ctrl = opts[:controllers][:registrations] || 'devise_token_auth/registrations'
passwords_ctrl = opts[:controllers][:passwords] || 'devise_token_auth/passwords'
confirmations_ctrl = opts[:controllers][:confirmations] || 'devise_token_auth/confirmations'
token_validations_ctrl = opts[:controllers][:token_validations] || 'devise_token_auth/token_validations'
omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || 'devise_token_auth/omniauth_callbacks'
unlocks_ctrl = opts[:controllers][:unlocks] || 'devise_token_auth/unlocks'
sessions_ctrl = opts[:controllers].delete(:sessions) || 'devise_token_auth/sessions'
registrations_ctrl = opts[:controllers].delete(:registrations) || 'devise_token_auth/registrations'
passwords_ctrl = opts[:controllers].delete(:passwords) || 'devise_token_auth/passwords'
confirmations_ctrl = opts[:controllers].delete(:confirmations) || 'devise_token_auth/confirmations'
token_validations_ctrl = opts[:controllers].delete(:token_validations) || 'devise_token_auth/token_validations'
omniauth_ctrl = opts[:controllers].delete(:omniauth_callbacks) || 'devise_token_auth/omniauth_callbacks'
unlocks_ctrl = opts[:controllers].delete(:unlocks) || 'devise_token_auth/unlocks'

# define devise controller mappings
controllers = { sessions: sessions_ctrl,
controllers = opts[:controllers].merge(
sessions: sessions_ctrl,
registrations: registrations_ctrl,
passwords: passwords_ctrl,
confirmations: confirmations_ctrl }
confirmations: confirmations_ctrl
)

controllers[:unlocks] = unlocks_ctrl if unlocks_ctrl

Expand Down
29 changes: 29 additions & 0 deletions test/lib/devise_token_auth/rails/custom_routes_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'test_helper'

class DeviseTokenAuth::CustomRoutesTest < ActiveSupport::TestCase
after do
Rails.application.reload_routes!
end
test 'custom controllers' do
class ActionDispatch::Routing::Mapper
include Mocha::ParameterMatchers
end
Rails.application.routes.draw do
self.expects(:devise_for).with(
:users,
has_entries(
controllers: has_entries(
invitations: "custom/invitations", foo: "custom/foo"
)
)
)

mount_devise_token_auth_for 'User', at: 'my_custom_users', controllers: {
invitations: 'custom/invitations',
foo: 'custom/foo'
}
end
end
end
87 changes: 87 additions & 0 deletions test/lib/devise_token_auth/rails/routes_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true

require 'test_helper'

# Needed for MiniTest to start a controller test so we can use assert_recognizes
class DeviseTokenAuth::RoutesTestController < DeviseTokenAuth::ApplicationController
end

class DeviseTokenAuth::RoutesTest < ActionController::TestCase
self.controller_class = DeviseTokenAuth::RoutesTestController
before do
Rails.application.routes.draw do
mount_devise_token_auth_for 'User', at: 'my_custom_users', controllers: {
invitations: 'custom/invitations',
foo: 'custom/foo'
}
end
end

after do
Rails.application.reload_routes!
end

test 'map new user session' do
assert_recognizes({controller: 'devise_token_auth/sessions', action: 'new'}, {path: 'my_custom_users/sign_in', method: :get})
end

test 'map create user session' do
assert_recognizes({controller: 'devise_token_auth/sessions', action: 'create'}, {path: 'my_custom_users/sign_in', method: :post})
end

test 'map destroy user session' do
assert_recognizes({controller: 'devise_token_auth/sessions', action: 'destroy'}, {path: 'my_custom_users/sign_out', method: :delete})
end

test 'map new user confirmation' do
assert_recognizes({controller: 'devise_token_auth/confirmations', action: 'new'}, 'my_custom_users/confirmation/new')
end

test 'map create user confirmation' do
assert_recognizes({controller: 'devise_token_auth/confirmations', action: 'create'}, {path: 'my_custom_users/confirmation', method: :post})
end

test 'map show user confirmation' do
assert_recognizes({controller: 'devise_token_auth/confirmations', action: 'show'}, {path: 'my_custom_users/confirmation', method: :get})
end

test 'map new user password' do
assert_recognizes({controller: 'devise_token_auth/passwords', action: 'new'}, 'my_custom_users/password/new')
end

test 'map create user password' do
assert_recognizes({controller: 'devise_token_auth/passwords', action: 'create'}, {path: 'my_custom_users/password', method: :post})
end

test 'map edit user password' do
assert_recognizes({controller: 'devise_token_auth/passwords', action: 'edit'}, 'my_custom_users/password/edit')
end

test 'map update user password' do
assert_recognizes({controller: 'devise_token_auth/passwords', action: 'update'}, {path: 'my_custom_users/password', method: :put})
end

test 'map new user registration' do
assert_recognizes({controller: 'devise_token_auth/registrations', action: 'new'}, 'my_custom_users/sign_up')
end

test 'map create user registration' do
assert_recognizes({controller: 'devise_token_auth/registrations', action: 'create'}, {path: 'my_custom_users', method: :post})
end

test 'map edit user registration' do
assert_recognizes({controller: 'devise_token_auth/registrations', action: 'edit'}, {path: 'my_custom_users/edit', method: :get})
end

test 'map update user registration' do
assert_recognizes({controller: 'devise_token_auth/registrations', action: 'update'}, {path: 'my_custom_users', method: :put})
end

test 'map destroy user registration' do
assert_recognizes({controller: 'devise_token_auth/registrations', action: 'destroy'}, {path: 'my_custom_users', method: :delete})
end

test 'map cancel user registration' do
assert_recognizes({controller: 'devise_token_auth/registrations', action: 'cancel'}, {path: 'my_custom_users/cancel', method: :get})
end
end