From 5f8e4dbc18820fe9d9b8d5773191fb305d16efbb Mon Sep 17 00:00:00 2001 From: abeyuya Date: Fri, 14 Apr 2017 19:06:04 +0900 Subject: [PATCH 1/8] parge error-rendering to method in authenticate_{mapping}! --- lib/devise_token_auth/controllers/helpers.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/devise_token_auth/controllers/helpers.rb b/lib/devise_token_auth/controllers/helpers.rb index c10332e91..c0cdfb077 100644 --- a/lib/devise_token_auth/controllers/helpers.rb +++ b/lib/devise_token_auth/controllers/helpers.rb @@ -109,9 +109,7 @@ def self.define_helpers(mapping) #:nodoc: class_eval <<-METHODS, __FILE__, __LINE__ + 1 def authenticate_#{mapping}! unless current_#{mapping} - return render json: { - errors: ["Authorized users only."] - }, status: 401 + render_authenticate_#{mapping}_error end end @@ -126,11 +124,17 @@ def current_#{mapping} def #{mapping}_session current_#{mapping} && warden.session(:#{mapping}) end + + def render_authenticate_#{mapping}_error + return render json: { + errors: ["Authorized #{mapping} only."] + }, status: 401 + end METHODS ActiveSupport.on_load(:action_controller) do if respond_to?(:helper_method) - helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session" + helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "render_authenticate_#{mapping}_error" end end end From 6b1da15b87836a26f4ab4e0c95c2d5463d56020c Mon Sep 17 00:00:00 2001 From: abeyuya Date: Fri, 14 Apr 2017 21:07:18 +0900 Subject: [PATCH 2/8] parge group-error-rendering to method in authenticate_{group_name}! --- lib/devise_token_auth/controllers/helpers.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/devise_token_auth/controllers/helpers.rb b/lib/devise_token_auth/controllers/helpers.rb index c0cdfb077..95e5ca5df 100644 --- a/lib/devise_token_auth/controllers/helpers.rb +++ b/lib/devise_token_auth/controllers/helpers.rb @@ -38,9 +38,7 @@ def authenticate_#{group_name}!(favourite=nil, opts={}) end unless current_#{group_name} - return render json: { - errors: ["Authorized users only."] - }, status: 401 + render_authenticate_#{group_name}_error end end end @@ -67,8 +65,14 @@ def current_#{group_name.to_s.pluralize} end.compact end + def render_authenticate_#{group_name}_error + return render json: { + errors: ["Authorized #{group_name} only."] + }, status: 401 + end + if respond_to?(:helper_method) - helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?" + helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?", "render_authenticate_#{group_name}_error" end METHODS end From 82590f9411b758f5833bd3cadec04695814f68c2 Mon Sep 17 00:00:00 2001 From: abeyuya Date: Fri, 14 Apr 2017 21:08:16 +0900 Subject: [PATCH 3/8] add explain comment about new method --- lib/devise_token_auth/controllers/helpers.rb | 26 +++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/devise_token_auth/controllers/helpers.rb b/lib/devise_token_auth/controllers/helpers.rb index 95e5ca5df..766e7c1ca 100644 --- a/lib/devise_token_auth/controllers/helpers.rb +++ b/lib/devise_token_auth/controllers/helpers.rb @@ -15,10 +15,11 @@ module ClassMethods # devise_group :blogger, contains: [:user, :admin] # # Generated methods: - # authenticate_blogger! # Redirects unless user or admin are signed in - # blogger_signed_in? # Checks whether there is either a user or an admin signed in - # current_blogger # Currently signed in user or admin - # current_bloggers # Currently signed in user and admin + # authenticate_blogger! # Redirects unless user or admin are signed in + # blogger_signed_in? # Checks whether there is either a user or an admin signed in + # current_blogger # Currently signed in user or admin + # current_bloggers # Currently signed in user and admin + # render_authenticate_blogger_error # Render error unless user or admin are signed in # # Use: # before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated @@ -94,14 +95,15 @@ def log_process_action(payload) # Admin # # Generated methods: - # authenticate_user! # Signs user in or 401 - # authenticate_admin! # Signs admin in or 401 - # user_signed_in? # Checks whether there is a user signed in or not - # admin_signed_in? # Checks whether there is an admin signed in or not - # current_user # Current signed in user - # current_admin # Current signed in admin - # user_session # Session data available only to the user scope - # admin_session # Session data available only to the admin scope + # authenticate_user! # Signs user in or 401 + # authenticate_admin! # Signs admin in or 401 + # user_signed_in? # Checks whether there is a user signed in or not + # admin_signed_in? # Checks whether there is an admin signed in or not + # current_user # Current signed in user + # current_admin # Current signed in admin + # user_session # Session data available only to the user scope + # admin_session # Session data available only to the admin scope + # render_authenticate_#{mapping}_error # Render error if # # Use: # before_action :authenticate_user! # Tell devise to use :user map From fef2bf84df21f4c4e1157a6e78e64084828c7f5d Mon Sep 17 00:00:00 2001 From: abeyuya Date: Fri, 14 Apr 2017 22:11:55 +0900 Subject: [PATCH 4/8] add new method exist test --- test/controllers/demo_group_controller_test.rb | 14 +++++++++++++- test/controllers/demo_mang_controller_test.rb | 9 ++++++++- test/controllers/demo_user_controller_test.rb | 8 ++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/test/controllers/demo_group_controller_test.rb b/test/controllers/demo_group_controller_test.rb index 813f90378..e01f0aefc 100644 --- a/test/controllers/demo_group_controller_test.rb +++ b/test/controllers/demo_group_controller_test.rb @@ -74,6 +74,18 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest it 'should define member_signed_in?' do assert @controller.current_members.include? @resource end + + it 'should define render_authenticate_user_error' do + assert @controller.methods.include?(:render_authenticate_user_error) + end + + it 'should define render_authenticate_mang_error' do + assert @controller.methods.include?(:render_authenticate_mang_error) + end + + it 'should define render_authenticate_member_error' do + assert @controller.methods.include?(:render_authenticate_member_error) + end end end @@ -132,7 +144,7 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest it 'should return error: unauthorized status' do assert_equal 401, response.status - end + end end end end diff --git a/test/controllers/demo_mang_controller_test.rb b/test/controllers/demo_mang_controller_test.rb index 028de65a5..509e79119 100644 --- a/test/controllers/demo_mang_controller_test.rb +++ b/test/controllers/demo_mang_controller_test.rb @@ -46,6 +46,14 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest it 'should not define current_user' do refute_equal @resource, @controller.current_user end + + it 'should define render_authenticate_user_error' do + assert @controller.methods.include?(:render_authenticate_user_error) + end + + it 'should define render_authenticate_mang_error' do + assert @controller.methods.include?(:render_authenticate_mang_error) + end end it 'should return success status' do @@ -260,4 +268,3 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest end end end - diff --git a/test/controllers/demo_user_controller_test.rb b/test/controllers/demo_user_controller_test.rb index 226bd972f..1db037865 100644 --- a/test/controllers/demo_user_controller_test.rb +++ b/test/controllers/demo_user_controller_test.rb @@ -47,6 +47,14 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest it 'should not define current_mang' do refute_equal @resource, @controller.current_mang end + + it 'should define render_authenticate_user_error' do + assert @controller.methods.include?(:render_authenticate_user_error) + end + + it 'should define render_authenticate_mang_error' do + assert @controller.methods.include?(:render_authenticate_mang_error) + end end it 'should return success status' do From 68facd47bea6f91e44e1820e3c2f190063713fcd Mon Sep 17 00:00:00 2001 From: abeyuya Date: Thu, 20 Apr 2017 21:08:33 +0900 Subject: [PATCH 5/8] modify comment for explaining generated method --- lib/devise_token_auth/controllers/helpers.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/devise_token_auth/controllers/helpers.rb b/lib/devise_token_auth/controllers/helpers.rb index 766e7c1ca..05137cc20 100644 --- a/lib/devise_token_auth/controllers/helpers.rb +++ b/lib/devise_token_auth/controllers/helpers.rb @@ -103,7 +103,8 @@ def log_process_action(payload) # current_admin # Current signed in admin # user_session # Session data available only to the user scope # admin_session # Session data available only to the admin scope - # render_authenticate_#{mapping}_error # Render error if + # render_authenticate_user_error # Render error unless user is signed in + # render_authenticate_admin_error # Render error unless admin is signed in # # Use: # before_action :authenticate_user! # Tell devise to use :user map From 9dcc414d0e41bbc714a41991c32bcb0a954ee510 Mon Sep 17 00:00:00 2001 From: abeyuya Date: Sun, 25 Jun 2017 19:30:09 +0900 Subject: [PATCH 6/8] change error-render method name --- lib/devise_token_auth/controllers/helpers.rb | 17 ++++++++--------- test/controllers/demo_group_controller_test.rb | 6 +++--- test/controllers/demo_mang_controller_test.rb | 4 ++-- test/controllers/demo_user_controller_test.rb | 4 ++-- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/devise_token_auth/controllers/helpers.rb b/lib/devise_token_auth/controllers/helpers.rb index 4ec8652e5..c270a07b4 100644 --- a/lib/devise_token_auth/controllers/helpers.rb +++ b/lib/devise_token_auth/controllers/helpers.rb @@ -19,7 +19,7 @@ module ClassMethods # blogger_signed_in? # Checks whether there is either a user or an admin signed in # current_blogger # Currently signed in user or admin # current_bloggers # Currently signed in user and admin - # render_authenticate_blogger_error # Render error unless user or admin are signed in + # render_authenticate_error # Render error unless user or admin are signed in # # Use: # before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated @@ -39,7 +39,7 @@ def authenticate_#{group_name}!(favourite=nil, opts={}) end unless current_#{group_name} - render_authenticate_#{group_name}_error + render_authenticate_error end end end @@ -66,14 +66,14 @@ def current_#{group_name.to_s.pluralize} end.compact end - def render_authenticate_#{group_name}_error + def render_authenticate_error return render json: { errors: [I18n.t('devise.failure.unauthenticated')] }, status: 401 end if respond_to?(:helper_method) - helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?", "render_authenticate_#{group_name}_error" + helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?", "render_authenticate_error" end METHODS end @@ -103,8 +103,7 @@ def log_process_action(payload) # current_admin # Current signed in admin # user_session # Session data available only to the user scope # admin_session # Session data available only to the admin scope - # render_authenticate_user_error # Render error unless user is signed in - # render_authenticate_admin_error # Render error unless admin is signed in + # render_authenticate_error # Render error unless user or admin is signed in # # Use: # before_action :authenticate_user! # Tell devise to use :user map @@ -116,7 +115,7 @@ def self.define_helpers(mapping) #:nodoc: class_eval <<-METHODS, __FILE__, __LINE__ + 1 def authenticate_#{mapping}! unless current_#{mapping} - render_authenticate_#{mapping}_error + render_authenticate_error end end @@ -132,7 +131,7 @@ def #{mapping}_session current_#{mapping} && warden.session(:#{mapping}) end - def render_authenticate_#{mapping}_error + def render_authenticate_error return render json: { errors: [I18n.t('devise.failure.unauthenticated')] }, status: 401 @@ -141,7 +140,7 @@ def render_authenticate_#{mapping}_error ActiveSupport.on_load(:action_controller) do if respond_to?(:helper_method) - helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "render_authenticate_#{mapping}_error" + helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "render_authenticate_error" end end end diff --git a/test/controllers/demo_group_controller_test.rb b/test/controllers/demo_group_controller_test.rb index e01f0aefc..6324e3f53 100644 --- a/test/controllers/demo_group_controller_test.rb +++ b/test/controllers/demo_group_controller_test.rb @@ -76,15 +76,15 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest end it 'should define render_authenticate_user_error' do - assert @controller.methods.include?(:render_authenticate_user_error) + assert @controller.methods.include?(:render_authenticate_error) end it 'should define render_authenticate_mang_error' do - assert @controller.methods.include?(:render_authenticate_mang_error) + assert @controller.methods.include?(:render_authenticate_error) end it 'should define render_authenticate_member_error' do - assert @controller.methods.include?(:render_authenticate_member_error) + assert @controller.methods.include?(:render_authenticate_error) end end end diff --git a/test/controllers/demo_mang_controller_test.rb b/test/controllers/demo_mang_controller_test.rb index 509e79119..0d1950e6b 100644 --- a/test/controllers/demo_mang_controller_test.rb +++ b/test/controllers/demo_mang_controller_test.rb @@ -48,11 +48,11 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest end it 'should define render_authenticate_user_error' do - assert @controller.methods.include?(:render_authenticate_user_error) + assert @controller.methods.include?(:render_authenticate_error) end it 'should define render_authenticate_mang_error' do - assert @controller.methods.include?(:render_authenticate_mang_error) + assert @controller.methods.include?(:render_authenticate_error) end end diff --git a/test/controllers/demo_user_controller_test.rb b/test/controllers/demo_user_controller_test.rb index 1db037865..ae154b58c 100644 --- a/test/controllers/demo_user_controller_test.rb +++ b/test/controllers/demo_user_controller_test.rb @@ -49,11 +49,11 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest end it 'should define render_authenticate_user_error' do - assert @controller.methods.include?(:render_authenticate_user_error) + assert @controller.methods.include?(:render_authenticate_error) end it 'should define render_authenticate_mang_error' do - assert @controller.methods.include?(:render_authenticate_mang_error) + assert @controller.methods.include?(:render_authenticate_error) end end From 5ae32a840863efcf3910918b71095753e2640fe8 Mon Sep 17 00:00:00 2001 From: abeyuya Date: Sun, 25 Jun 2017 19:58:10 +0900 Subject: [PATCH 7/8] remove duplicate tests and modify bad test name --- test/controllers/demo_group_controller_test.rb | 10 +--------- test/controllers/demo_mang_controller_test.rb | 6 +----- test/controllers/demo_user_controller_test.rb | 6 +----- 3 files changed, 3 insertions(+), 19 deletions(-) diff --git a/test/controllers/demo_group_controller_test.rb b/test/controllers/demo_group_controller_test.rb index 6324e3f53..086d9fb5e 100644 --- a/test/controllers/demo_group_controller_test.rb +++ b/test/controllers/demo_group_controller_test.rb @@ -75,15 +75,7 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest assert @controller.current_members.include? @resource end - it 'should define render_authenticate_user_error' do - assert @controller.methods.include?(:render_authenticate_error) - end - - it 'should define render_authenticate_mang_error' do - assert @controller.methods.include?(:render_authenticate_error) - end - - it 'should define render_authenticate_member_error' do + it 'should define render_authenticate_error' do assert @controller.methods.include?(:render_authenticate_error) end end diff --git a/test/controllers/demo_mang_controller_test.rb b/test/controllers/demo_mang_controller_test.rb index 0d1950e6b..50b5bda94 100644 --- a/test/controllers/demo_mang_controller_test.rb +++ b/test/controllers/demo_mang_controller_test.rb @@ -47,11 +47,7 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest refute_equal @resource, @controller.current_user end - it 'should define render_authenticate_user_error' do - assert @controller.methods.include?(:render_authenticate_error) - end - - it 'should define render_authenticate_mang_error' do + it 'should define render_authenticate_error' do assert @controller.methods.include?(:render_authenticate_error) end end diff --git a/test/controllers/demo_user_controller_test.rb b/test/controllers/demo_user_controller_test.rb index ae154b58c..96126a219 100644 --- a/test/controllers/demo_user_controller_test.rb +++ b/test/controllers/demo_user_controller_test.rb @@ -48,11 +48,7 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest refute_equal @resource, @controller.current_mang end - it 'should define render_authenticate_user_error' do - assert @controller.methods.include?(:render_authenticate_error) - end - - it 'should define render_authenticate_mang_error' do + it 'should define render_authenticate_error' do assert @controller.methods.include?(:render_authenticate_error) end end From 1b141ce2480ad938ac86c3deb5aa8c0483826fe9 Mon Sep 17 00:00:00 2001 From: abeyuya Date: Sun, 25 Jun 2017 20:42:36 +0900 Subject: [PATCH 8/8] add test --- test/controllers/demo_group_controller_test.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/controllers/demo_group_controller_test.rb b/test/controllers/demo_group_controller_test.rb index 086d9fb5e..52ad7baab 100644 --- a/test/controllers/demo_group_controller_test.rb +++ b/test/controllers/demo_group_controller_test.rb @@ -122,6 +122,10 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest it 'should define member_signed_in?' do assert @controller.current_members.include? @mang end + + it 'should define render_authenticate_error' do + assert @controller.methods.include?(:render_authenticate_error) + end end end