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 correctly setting limit_self_registration

Oliver Günther569e759182eb
diff --git a/app/services/users/register_user_service.rb b/app/services/users/register_user_service.rb
index b603e2a05fb..ca82c877dd3 100644
--- a/app/services/users/register_user_service.rb
+++ b/app/services/users/register_user_service.rb
@@ -116,6 +116,7 @@ module Users
# bypassing regular account registration restrictions
def register_omniauth_user
return if skip_omniauth_user?
+ return if limited_provider?(user)
user.activate
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 8e4c7c8427f..e345a483c60 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -495,7 +495,7 @@ en:
User registration is disabled on this system. Please ask an administrator to create an
account for you.
error_self_registration_limited_provider: >
- User registration is limit for the Single sign-on provider '%{name}'. Please ask an administrator to activate the
+ User registration is limited 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"
diff --git a/modules/openid_connect/app/models/openid_connect/provider.rb b/modules/openid_connect/app/models/openid_connect/provider.rb
index 82fdcd12476..df19926d187 100644
--- a/modules/openid_connect/app/models/openid_connect/provider.rb
+++ b/modules/openid_connect/app/models/openid_connect/provider.rb
@@ -63,6 +63,8 @@ module OpenIDConnect
(configuration || {}).fetch(:limit_self_registration, true)
end
+ alias_method :limit_self_registration?, :limit_self_registration
+
def to_h
return {} if omniauth_provider.nil?
diff --git a/modules/openid_connect/spec/controllers/providers_controller_spec.rb b/modules/openid_connect/spec/controllers/providers_controller_spec.rb
index 1f3427f97d2..8344d0c36d2 100644
--- a/modules/openid_connect/spec/controllers/providers_controller_spec.rb
+++ b/modules/openid_connect/spec/controllers/providers_controller_spec.rb
@@ -186,8 +186,8 @@ RSpec.describe OpenIDConnect::ProvidersController do
get :edit, params: { id: 'azure' }
end
- it 'shows limit_self_registration as unchecked' do
- expect(assigns[:provider]).not_to be_limit_self_registration
+ it 'shows limit_self_registration as checked' do
+ expect(assigns[:provider]).to be_limit_self_registration
end
end
end
@@ -216,6 +216,37 @@ RSpec.describe OpenIDConnect::ProvidersController do
provider = OpenProject::OpenIDConnect.providers.find { |item| item.name == "azure" }
expect(provider.secret).to eq("NEWSECRET")
end
+
+ context 'with limit_self_registration checked' do
+ let(:params) do
+ { id: 'azure', openid_connect_provider: valid_params.merge(limit_self_registration: 1) }
+ end
+
+ it 'sets the setting' do
+ put(:update, params:)
+
+ expect(OpenProject::Plugins::AuthPlugin)
+ .to be_limit_self_registration provider: :azure
+ provider = OpenProject::OpenIDConnect.providers.find { |item| item.name == "azure" }
+ expect(provider.limit_self_registration).to be true
+ end
+ end
+
+ context 'with limit_self_registration unchecked' do
+ let(:params) do
+ { id: :azure, openid_connect_provider: valid_params.merge(limit_self_registration: 0) }
+ end
+
+ it 'does not set the setting' do
+ put(:update, params:)
+
+ expect(OpenProject::Plugins::AuthPlugin)
+ .not_to be_limit_self_registration provider: valid_params[:name]
+
+ provider = OpenProject::OpenIDConnect.providers.find { |item| item.name == "azure" }
+ expect(provider.limit_self_registration).to be false
+ end
+ end
end
end
diff --git a/spec/requests/openid_google_provider_callback_spec.rb b/spec/requests/openid_google_provider_callback_spec.rb
index d0af8232539..5c1c54ee714 100644
--- a/spec/requests/openid_google_provider_callback_spec.rb
+++ b/spec/requests/openid_google_provider_callback_spec.rb
@@ -55,6 +55,7 @@ RSpec.describe 'OpenID Google provider callback', with_ee: %i[openid_providers]
expect(OpenProject::Plugins::AuthPlugin)
.to receive(:limit_self_registration?)
.with(provider: "google")
+ .twice
.and_return false
stub_request(:post, "https://accounts.google.com/o/oauth2/token").to_return(
diff --git a/spec/services/users/register_user_service_spec.rb b/spec/services/users/register_user_service_spec.rb
index a942a5922d3..5f6473be39a 100644
--- a/spec/services/users/register_user_service_spec.rb
+++ b/spec/services/users/register_user_service_spec.rb
@@ -123,13 +123,50 @@ RSpec.describe Users::RegisterUserService do
}
}
} 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
+ it 'registers the user, but does not activate it' do
+ call = instance.call
+ expect(call).to be_success
+ expect(call.result).to eq user
+ expect(user).to be_registered
+ expect(user).not_to have_received(:activate)
+ expect(call.message).to eq I18n.t(:notice_account_pending)
+ end
+ end
+
+ context 'with limit_self_registration enabled and self_registration email',
+ with_settings: {
+ self_registration: 1,
+ plugin_openproject_openid_connect: {
+ providers: {
+ azure: { identifier: "foo", secret: "bar", limit_self_registration: true }
+ }
+ }
+ } do
+ it 'registers the user, but does not activate it' do
+ call = instance.call
+ expect(call).to be_success
+ expect(call.result).to eq user
+ expect(user).to be_registered
+ expect(user).not_to have_received(:activate)
+ expect(call.message).to eq I18n.t(:notice_account_register_done)
+ end
+ end
+
+ context 'with limit_self_registration enabled and self_registration automatic',
+ with_settings: {
+ self_registration: 3,
+ plugin_openproject_openid_connect: {
+ providers: {
+ azure: { identifier: "foo", secret: "bar", limit_self_registration: true }
+ }
+ }
+ } do
+ it 'activates the user' do
+ call = instance.call
+ expect(call).to be_success
+ expect(call.result).to eq user
+ expect(user).to have_received(:activate)
+ expect(call.message).to eq I18n.t(:notice_account_activated)
end
end
end