Skip to content

Commit

Permalink
fix namespaced mapping name
Browse files Browse the repository at this point in the history
  • Loading branch information
phfts committed Jan 3, 2016
1 parent 57cb7f9 commit 359eafc
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/devise_token_auth/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def mount_devise_token_auth_for(resource, opts)
# get full url path as if it were namespaced
full_path = "#{@scope[:path]}/#{opts[:at]}"

# get namespace name
namespace_name = @scope[:as]

# clear scope so controller routes aren't namespaced
@scope = ActionDispatch::Routing::Mapper::Scope.new(
path: "",
Expand All @@ -43,7 +46,10 @@ def mount_devise_token_auth_for(resource, opts)
parent: nil
)

devise_scope resource.underscore.gsub('/', '_').to_sym do
mapping_name = resource.underscore.gsub('/', '_')
mapping_name = "#{namespace_name}_#{mapping_name}" if namespace_name

devise_scope mapping_name.to_sym do
# path to verify token validity
get "#{full_path}/validate_token", controller: "#{token_validations_ctrl}", action: "validate_token"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,29 @@ class DeviseTokenAuth::TokenValidationsControllerTest < ActionDispatch::Integrat
end

end

describe 'using namespaces with unused resource' do

before do
@resource = scoped_users(:confirmed_email_user)
@resource.skip_confirmation!
@resource.save!

@auth_headers = @resource.create_new_auth_token

@token = @auth_headers['access-token']
@client_id = @auth_headers['client']
@expiry = @auth_headers['expiry']

# ensure that request is not treated as batch request
age_token(@resource, @client_id)
end

test "should be successful" do
get '/api_v2/auth/validate_token', {}, @auth_headers
assert_equal 200, response.status
end

end

end
7 changes: 7 additions & 0 deletions test/dummy/app/models/scoped_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ScopedUser < ActiveRecord::Base
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
include DeviseTokenAuth::Concerns::User
end
13 changes: 13 additions & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
end
end

# test namespacing with not created devise mapping
namespace :api_v2, defaults: { format: :json } do
mount_devise_token_auth_for "ScopedUser",
at: "auth",
controllers: {
omniauth_callbacks: "api_v2/omniauth_callbacks",
sessions: "api_v2/sessions",
registrations: "api_v2/registrations",
confirmations: "api_v2/confirmations",
passwords: "api_v2/passwords"
}
end

# this route will authorize visitors using the User class
get 'demo/members_only', to: 'demo_user#members_only'

Expand Down
29 changes: 29 additions & 0 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@
add_index "nice_users", ["reset_password_token"], name: "index_nice_users_on_reset_password_token", unique: true
add_index "nice_users", ["uid", "provider"], name: "index_nice_users_on_uid_and_provider", unique: true

create_table "scoped_users", force: :cascade do |t|
t.string "provider", null: false
t.string "uid", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.string "name"
t.string "nickname"
t.string "image"
t.string "email"
t.text "tokens"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "scoped_users", ["email"], name: "index_scoped_users_on_email"
add_index "scoped_users", ["reset_password_token"], name: "index_scoped_users_on_reset_password_token", unique: true
add_index "scoped_users", ["uid", "provider"], name: "index_scoped_users_on_uid_and_provider", unique: true

create_table "only_email_users", force: :cascade do |t|
t.string "provider", null: false
t.string "uid", default: "", null: false
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/scoped_users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% timestamp = DateTime.parse(2.weeks.ago.to_s).to_time.strftime("%F %T") %>
<% @email = Faker::Internet.email %>
confirmed_email_user:
uid: "<%= @email %>"
email: "<%= @email %>"
provider: 'email'
confirmed_at: '<%= timestamp %>'
created_at: '<%= timestamp %>'
updated_at: '<%= timestamp %>'
encrypted_password: <%= User.new.send(:password_digest, 'secret123') %>

0 comments on commit 359eafc

Please sign in to comment.