Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add test for added error when omniauth provided user is being limited

Oliver Günther7bdfbc5473db
diff --git a/app/services/users/register_user_service.rb b/app/services/users/register_user_service.rb
index 59cd783227c..b603e2a05fb 100644
--- a/app/services/users/register_user_service.rb
+++ b/app/services/users/register_user_service.rb
@@ -61,9 +61,9 @@ module Users
# Check whether the associated single sign-on providers
# allows for automatic activation of new users
def ensure_provider_not_limited!
- if limited_provider?(user)
+ if limited_provider?(user) && Setting::SelfRegistration.disabled?
name = provider_name(user)
- ServiceResult.failure(result: user, message: I18n.t('account.error_self_registration_disabled_provider', name:))
+ ServiceResult.failure(result: user, message: I18n.t('account.error_self_registration_limited_provider', name:))
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index ccb2fbf960a..8e4c7c8427f 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -494,9 +494,9 @@ en:
error_self_registration_disabled: >
User registration is disabled on this system. Please ask an administrator to create an
account for you.
- error_self_registration_disabled_provider: >
- User registration is disabled for the Single sign-on provider '%{name}'. Please ask an administrator to activate the
- account for you or change the self registration setting for this provider.
+ error_self_registration_limited_provider: >
+ User registration is limit for the Single sign-on provider '%{name}'. Please ask an administrator to activate the
+ account for you or change the self registration limit for this provider.
login_with_auth_provider: "or sign in with your existing account"
signup_with_auth_provider: "or sign up using"
auth_source_login: Please login as <em>%{login}</em> to activate your account.
diff --git a/modules/openid_connect/app/models/openid_connect/provider.rb b/modules/openid_connect/app/models/openid_connect/provider.rb
index c21c0edcbf2..82fdcd12476 100644
--- a/modules/openid_connect/app/models/openid_connect/provider.rb
+++ b/modules/openid_connect/app/models/openid_connect/provider.rb
@@ -22,7 +22,7 @@ module OpenIDConnect
delegate :to_h, to: :omniauth_provider, allow_nil: false
delegate :tenant, to: :omniauth_provider, allow_nil: false
- delegate :configuration, to: :omniauth_provider, allow_nil: false
+ delegate :configuration, to: :omniauth_provider, allow_nil: true
delegate :use_graph_api, to: :omniauth_provider, allow_nil: false
def initialize(omniauth_provider)
@@ -60,7 +60,7 @@ module OpenIDConnect
end
def limit_self_registration
- configuration.fetch(:limit_self_registration, true)
+ (configuration || {}).fetch(:limit_self_registration, true)
end
def to_h
diff --git a/spec/services/users/register_user_service_spec.rb b/spec/services/users/register_user_service_spec.rb
index 37ee66a6ad9..a942a5922d3 100644
--- a/spec/services/users/register_user_service_spec.rb
+++ b/spec/services/users/register_user_service_spec.rb
@@ -79,7 +79,6 @@ RSpec.describe Users::RegisterUserService do
describe '#register_omniauth_user' do
let(:user) { User.new(status: Principal.statuses[:registered], identity_url: 'azure:1234') }
let(:instance) { described_class.new(user) }
- let(:call) { instance.call }
before do
allow(user).to receive(:activate)
@@ -87,33 +86,50 @@ RSpec.describe Users::RegisterUserService do
# required so that the azure provider is visible (ee feature)
allow(EnterpriseToken).to receive(:show_banners?).and_return false
-
- with_all_registration_options do |_type|
- call
- end
end
it 'tries to activate that user regardless of settings' do
- expect(call).to be_success
- expect(call.result).to eq user
- expect(call.message).to eq I18n.t(:notice_account_registered_and_logged_in)
+ with_all_registration_options do |_type|
+ call = instance.call
+ expect(call).to be_success
+ expect(call.result).to eq user
+ expect(call.message).to eq I18n.t(:notice_account_registered_and_logged_in)
+ end
end
- context(
- 'with limit_self_registration enabled and self_registration disabled',
- with_settings: {
- self_registration: 0,
- plugin_openproject_openid_connect: {
- providers: {
- azure: { identifier: "foo", secret: "bar", limit_self_registration: true }
- }
- }
- }
- ) do
+ context 'with limit_self_registration enabled and self_registration disabled',
+ with_settings: {
+ self_registration: 0,
+ plugin_openproject_openid_connect: {
+ providers: {
+ azure: { identifier: "foo", secret: "bar", limit_self_registration: true }
+ }
+ }
+ } do
it 'fails to activate due to disabled self registration' do
+ call = instance.call
expect(call).not_to be_success
expect(call.result).to eq user
- expect(call.message).to eq I18n.t('account.error_self_registration_disabled_provider', name: 'azure')
+ expect(call.message).to eq I18n.t('account.error_self_registration_limited_provider', name: 'azure')
+ end
+ end
+
+ context 'with limit_self_registration enabled and self_registration manual',
+ with_settings: {
+ self_registration: 2,
+ plugin_openproject_openid_connect: {
+ providers: {
+ azure: { identifier: "foo", secret: "bar", limit_self_registration: true }
+ }
+ }
+ } do
+ it 'creates the user but does not activate' do
+ with_all_registration_options(except: :disabled) do
+ call = instance.call
+ expect(call).to be_success
+ expect(call.result).to eq user
+ expect(call.result).not_to be_active
+ end
end
end
end