diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 05f0a2a6..b846839d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -16,7 +16,7 @@ def info # Find the user that owns the access token def current_resource_owner - User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token + User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token && doorkeeper_token.resource_owner_id end def current_user diff --git a/test/controllers/oauth_controller_test.rb b/test/controllers/oauth_controller_test.rb new file mode 100644 index 00000000..6b3606fb --- /dev/null +++ b/test/controllers/oauth_controller_test.rb @@ -0,0 +1,31 @@ +require 'test_helper' +require 'authorization_helper' + +class OauthControllerTest < ActionDispatch::IntegrationTest + include AuthorizationHelper + include Devise::Test::IntegrationHelpers + + setup do + if ENV['ENABLE_AUTHENTICATION'].present? + + end + @pia = FactoryBot.create(:pia) + @admin = FactoryBot.create(:user_admin, identifier: "admin") + @doorkeeper_token = doorkeeper_token + @auth = FactoryBot.create(:access_token) + @auth_tokens = nil + end + + test "should not access to pia because is not logged" do + get pias_url + assert_response :unauthorized + end + + test "admin authentification and get pias" do + @auth_tokens = auth_tokens_for_user(@admin, @auth) + @doorkeeper_token = @auth_tokens['access_token'] + + get pias_url, headers: { 'Authorization' => "Bearer #{@doorkeeper_token}" }, as: :json + assert_response :success + end +end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 95f2f132..acb70ed0 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -46,18 +46,6 @@ class UsersControllerTest < ActionDispatch::IntegrationTest end end - # TODO - test "test delete the only one admin" do - if ENV['ENABLE_AUTHENTICATION'].present? - user_to_delete = FactoryBot.create(:user, identifier: "technical") - delete user_url(user_to_delete), headers: { - "Authorization": "Bearer #{@auth_tokens['access_token']}" - } - - # should not return a error - end - end - test "test process to unlock user and set password" do # create a user locked by default new_user = FactoryBot.create(:user) diff --git a/test/factories/users.rb b/test/factories/users.rb index 28d9c222..d17bd046 100644 --- a/test/factories/users.rb +++ b/test/factories/users.rb @@ -6,7 +6,7 @@ firstname { 'user' } lastname { 'nothing' } - password { [*'0'..'9', *'a'..'z', *'A'..'Z', *'!'..'?'].sample(16).join } + password { [*'0'..'9', *'a'..'z', *'A'..'Z', *'!'..'?'].sample(16).join('-') } password_confirmation { password } is_user { true } email { "user+#{identifier ? identifier : 'default'}@test.com" }