Skip to content

Commit

Permalink
feat: add oauth test section
Browse files Browse the repository at this point in the history
  • Loading branch information
syl-p committed Feb 23, 2024
1 parent 3b2ece2 commit 120c0f5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/controllers/oauth_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 0 additions & 12 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 120c0f5

Please sign in to comment.