2020-06-30 14:25:51 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'test_helper'
|
2023-01-10 13:09:04 +01:00
|
|
|
require 'helpers/auth_provider_helper'
|
2020-06-30 14:25:51 +02:00
|
|
|
|
|
|
|
class AuthProvidersTest < ActionDispatch::IntegrationTest
|
2023-01-10 13:09:04 +01:00
|
|
|
include AuthProviderHelper
|
|
|
|
|
2020-06-30 14:25:51 +02:00
|
|
|
def setup
|
|
|
|
@admin = User.find_by(username: 'admin')
|
|
|
|
login_as(@admin, scope: :user)
|
2023-02-24 17:26:55 +01:00
|
|
|
FabManager::Application.load_tasks if Rake::Task.tasks.empty?
|
2020-06-30 14:25:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'create an auth external provider and activate it' do
|
|
|
|
name = 'GitHub'
|
|
|
|
post '/api/auth_providers',
|
|
|
|
params: {
|
2023-01-10 13:09:04 +01:00
|
|
|
auth_provider: github_provider_params(name)
|
2020-06-30 14:25:51 +02:00
|
|
|
}.to_json,
|
|
|
|
headers: default_headers
|
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 201, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2020-06-30 14:25:51 +02:00
|
|
|
|
|
|
|
# Check the provider was correctly created
|
2022-11-24 13:02:25 +01:00
|
|
|
db_provider = OAuth2Provider.includes(:auth_provider).where('auth_providers.name': name).first&.auth_provider
|
2020-06-30 14:25:51 +02:00
|
|
|
assert_not_nil db_provider
|
|
|
|
|
|
|
|
provider = json_response(response.body)
|
|
|
|
assert_equal name, provider[:name]
|
2022-11-24 13:02:25 +01:00
|
|
|
assert_equal db_provider&.id, provider[:id]
|
2020-06-30 14:25:51 +02:00
|
|
|
assert_equal 'pending', provider[:status]
|
2022-03-28 19:50:36 +02:00
|
|
|
assert_equal 2, provider[:auth_provider_mappings_attributes].length
|
2020-06-30 14:25:51 +02:00
|
|
|
|
|
|
|
# now let's activate this new provider
|
2023-01-10 13:09:04 +01:00
|
|
|
Rake::Task['fablab:auth:switch_provider'].execute(Rake::TaskArguments.new([:provider], [name]))
|
2020-06-30 14:25:51 +02:00
|
|
|
|
2022-11-24 13:02:25 +01:00
|
|
|
db_provider&.reload
|
|
|
|
assert_equal 'active', db_provider&.status
|
|
|
|
assert_equal AuthProvider.active.id, db_provider&.id
|
2023-03-22 16:05:25 +01:00
|
|
|
User.find_each do |u|
|
2020-06-30 14:25:51 +02:00
|
|
|
assert_not_nil u.auth_token
|
|
|
|
end
|
|
|
|
end
|
2023-01-10 13:09:04 +01:00
|
|
|
|
|
|
|
test 'update an authentication provider' do
|
|
|
|
provider = AuthProvider.create!(github_provider_params('GitHub'))
|
|
|
|
patch "/api/auth_providers/#{provider.id}",
|
|
|
|
params: {
|
|
|
|
auth_provider: {
|
|
|
|
providable_type: 'OAuth2Provider',
|
|
|
|
auth_provider_mappings_attributes: [
|
|
|
|
{ api_data_type: 'json', api_endpoint: 'https://api.github.com/user',
|
|
|
|
api_field: 'avatar_url', local_field: 'avatar', local_model: 'profile' }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}.to_json,
|
|
|
|
headers: default_headers
|
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 200, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2023-01-10 13:09:04 +01:00
|
|
|
|
|
|
|
provider.reload
|
|
|
|
|
|
|
|
# Check the provider was updated
|
|
|
|
res = json_response(response.body)
|
|
|
|
assert_equal provider.id, res[:id]
|
|
|
|
assert_equal 3, provider.auth_provider_mappings.count
|
|
|
|
assert_not_nil provider.auth_provider_mappings.find_by(api_field: 'avatar_url')
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'build an oauth2 strategy name' do
|
|
|
|
get '/api/auth_providers/strategy_name?providable_type=OAuth2Provider&name=Sleede'
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_equal 'oauth2-sleede', response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'build an openid strategy name' do
|
|
|
|
get '/api/auth_providers/strategy_name?providable_type=OpenIdConnectProvider&name=Sleede'
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_equal 'openidconnect-sleede', response.body
|
|
|
|
end
|
|
|
|
|
2023-02-17 13:27:43 +01:00
|
|
|
test 'list all authentication providers' do
|
|
|
|
get '/api/auth_providers'
|
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 200, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2023-02-17 13:27:43 +01:00
|
|
|
|
|
|
|
# Check the answer
|
|
|
|
res = json_response(response.body)
|
|
|
|
assert_equal AuthProvider.count, res.length
|
|
|
|
end
|
|
|
|
|
2023-01-10 13:09:04 +01:00
|
|
|
test 'show an authentication provider' do
|
|
|
|
provider = AuthProvider.first
|
|
|
|
get "/api/auth_providers/#{provider.id}"
|
|
|
|
|
|
|
|
# Check response format & status
|
|
|
|
assert_equal 200, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2023-01-10 13:09:04 +01:00
|
|
|
|
2023-02-17 13:27:43 +01:00
|
|
|
# Check the provider
|
2023-01-10 13:09:04 +01:00
|
|
|
res = json_response(response.body)
|
|
|
|
assert_equal provider.id, res[:id]
|
|
|
|
assert_equal provider.providable_type, res[:providable_type]
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'show fields available for mapping' do
|
|
|
|
get '/api/auth_providers/mapping_fields'
|
|
|
|
|
|
|
|
assert_equal 200, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2023-01-10 13:09:04 +01:00
|
|
|
|
|
|
|
# Check the returned fields
|
|
|
|
res = json_response(response.body)
|
|
|
|
assert_not_empty res[:user]
|
|
|
|
assert_not_empty res[:profile]
|
|
|
|
assert_not res[:user].map(&:first).include?('encrypted_password')
|
|
|
|
assert(res[:user].map(&:last).all? { |type| %w[string boolean integer datetime].include?(type) })
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'get the current active provider' do
|
|
|
|
get '/api/auth_providers/active'
|
|
|
|
|
|
|
|
assert_equal 200, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2023-01-10 13:09:04 +01:00
|
|
|
|
|
|
|
# Check the returned fields
|
|
|
|
res = json_response(response.body)
|
|
|
|
assert_equal AuthProvider.active.id, res[:id]
|
|
|
|
assert_nil res[:previous_provider]
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'send auth migration token' do
|
|
|
|
# create an enable an oauth2 provider
|
|
|
|
name = 'TokenTest'
|
|
|
|
AuthProvider.create!(github_provider_params(name))
|
|
|
|
Rake::Task['fablab:auth:switch_provider'].execute(Rake::TaskArguments.new([:provider], [name]))
|
|
|
|
|
|
|
|
# send the migration token
|
|
|
|
user = User.find(10)
|
|
|
|
post '/api/auth_providers/send_code',
|
|
|
|
params: {
|
|
|
|
email: user.email
|
|
|
|
}.to_json,
|
|
|
|
headers: default_headers
|
|
|
|
|
|
|
|
assert_equal 200, response.status, response.body
|
2023-02-24 17:26:55 +01:00
|
|
|
assert_match Mime[:json].to_s, response.content_type
|
2023-01-10 13:09:04 +01:00
|
|
|
|
|
|
|
# check resulting notification
|
|
|
|
notification = Notification.find_by(
|
2023-02-16 16:08:24 +01:00
|
|
|
notification_type_id: NotificationType.find_by(name: 'notify_user_auth_migration'),
|
2023-01-10 13:09:04 +01:00
|
|
|
attached_object_type: 'User',
|
|
|
|
attached_object_id: user.id
|
|
|
|
)
|
|
|
|
assert_not_nil notification, 'user notification was not created'
|
|
|
|
end
|
2020-07-01 18:12:14 +02:00
|
|
|
end
|