diff --git a/env.example b/env.example index ee5bd9e79..9ca5e451c 100644 --- a/env.example +++ b/env.example @@ -12,6 +12,10 @@ SECRET_KEY_BASE=83daf5e7b80d990f037407bab78dff9904aaf3c195a50f84fa8695a22287e707 STRIPE_API_KEY= STRIPE_PUBLISHABLE_KEY= +# oAuth SSO keys for tests +OAUTH_CLIENT_ID=github-oauth-app-id +OAUTH_CLIENT_SECRET=github-oauth-app-secret + # Configure carefully! DEFAULT_HOST=localhost:5000 DEFAULT_PROTOCOL=http diff --git a/lib/tasks/fablab/auth.rake b/lib/tasks/fablab/auth.rake index ff27dd7c2..c6ea43ac9 100644 --- a/lib/tasks/fablab/auth.rake +++ b/lib/tasks/fablab/auth.rake @@ -36,6 +36,8 @@ namespace :fablab do end # ask the user to restart the application + next if Rails.env.test? + puts "\nActivation successful" puts "\n/!\\ WARNING: Please consider the following, otherwise the authentication will be bogus:" diff --git a/test/integration/auth_providers_test.rb b/test/integration/auth_providers_test.rb new file mode 100644 index 000000000..1f230e0b4 --- /dev/null +++ b/test/integration/auth_providers_test.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'test_helper' + +class AuthProvidersTest < ActionDispatch::IntegrationTest + def setup + @admin = User.find_by(username: 'admin') + login_as(@admin, scope: :user) + end + + + test 'create an auth external provider and activate it' do + name = 'GitHub' + post '/api/auth_providers', + params: { + auth_provider: { + name: name, + providable_type: 'OAuth2Provider', + providable_attributes: { + authorization_endpoint: 'authorize', + token_endpoint: 'access_token', + base_url: 'https://github.com/login/oauth/', + profile_url: 'https://github.com/settings/profile', + client_id: ENV.fetch('OAUTH_CLIENT_ID') { 'github-oauth-app-id' }, + client_secret: ENV.fetch('OAUTH_CLIENT_SECRET') { 'github-oauth-app-secret' }, + o_auth2_mappings_attributes: [ + { + api_data_type: 'json', + api_endpoint: 'https://api.github.com/user', + api_field: 'id', + local_field: 'uid', + local_model: 'user' + }, + { + api_data_type: 'json', + api_endpoint: 'https://api.github.com/user', + api_field: 'html_url', + local_field: 'github', + local_model: 'profile' + } + ] + } + } + }.to_json, + headers: default_headers + + # Check response format & status + assert_equal 201, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the provider was correctly created + db_provider = OAuth2Provider.includes(:auth_provider).where('auth_providers.name': name).first.auth_provider + assert_not_nil db_provider + + provider = json_response(response.body) + assert_equal name, provider[:name] + assert_equal db_provider.id, provider[:id] + assert_equal 'pending', provider[:status] + assert_equal 2, provider[:providable_attributes][:o_auth2_mappings_attributes].length + + # now let's activate this new provider + Fablab::Application.load_tasks + Rake::Task['fablab:auth:switch_provider'].invoke(name) + + db_provider.reload + assert_equal 'active', db_provider.status + assert_equal AuthProvider.active.id, db_provider.id + User.all.each do |u| + assert_not_nil u.auth_token + end + end +end \ No newline at end of file