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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Speed up specs by using shared_let.
Pavel Balashou51d7286fe99a
diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rbindex add90cf090b..0a30aca54c1 100644--- a/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rb+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_api_spec.rb@@ -33,18 +33,7 @@ require_module_spec_helperRSpec.describe 'API v3 file links resource' doinclude API::V3::Utilities::PathHelper-- def add_permissions(user, *)- role = Role.joins(members: :principal).where('users.id': user).first- role.add_permission!(*)- user.reload # clear user's project_role_cache- end-- def remove_permissions(user, *)- role = Role.joins(members: :principal).where('users.id': user).first- role.remove_permission!(*)- user.reload # clear user's project_role_cache- end+ include UserPermissionsHelperdef enable_module(project, modul)project.enabled_module_names = project.enabled_module_names + [modul]diff --git a/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rbindex ce487547501..a177f0e8955 100644--- a/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb+++ b/modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb@@ -32,20 +32,23 @@ require_module_spec_helperRSpec.describe 'API v3 storages resource', :webmock, content_type: :json doinclude API::V3::Utilities::PathHelperinclude StorageServerHelpers+ include UserPermissionsHelper- let(:permissions) { %i(view_work_packages view_file_links) }- let(:project) { create(:project) }+ shared_let(:permissions) { %i(view_work_packages view_file_links) }+ shared_let(:project) { create(:project) }- let(:current_user) do+ shared_let(:user_with_permissions) docreate(:user, member_with_permissions: { project => permissions })end-- let(:oauth_application) { create(:oauth_application) }- let(:storage) { create(:nextcloud_storage, creator: current_user, oauth_application:) }- let(:project_storage) { create(:project_storage, project:, storage:) }+ shared_let(:user_without_project) { create(:user) }+ shared_let(:admin) { create(:admin) }+ shared_let(:oauth_application) { create(:oauth_application) }+ shared_let(:storage) { create(:nextcloud_storage, creator: user_with_permissions, oauth_application:) }+ shared_let(:project_storage) { create(:project_storage, project:, storage:) }let(:authorize_url) { 'https://example.com/authorize' }let(:connection_manager) { instance_double(OAuthClients::ConnectionManager) }+ let(:current_user) { user_with_permissions }subject(:last_response) doget path@@ -54,7 +57,6 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json dobefore doallow(connection_manager).to receive_messages(get_authorization_uri: authorize_url, authorization_state: :connected)allow(OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)- project_storagelogin_as current_userend@@ -79,12 +81,10 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json dolet(:path) { api_v3_paths.storages }let!(:another_storage) { create(:nextcloud_storage) }- subject(:last_response) do- get path- end+ subject(:last_response) { get path }context 'as admin' do- let(:current_user) { create(:admin) }+ let(:current_user) { admin }describe 'gets the storage collection and returns it' dosubject { last_response.body }@@ -132,7 +132,7 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'as admin' do- let(:current_user) { create(:admin) }+ let(:current_user) { admin }describe 'creates a storage and returns it' dosubject { last_response.body }@@ -195,14 +195,13 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json dolet(:path) { api_v3_paths.storage(storage.id) }context 'if user belongs to a project using the given storage' do- let!(:project_storage) { create(:project_storage, project:, storage:) }-subject { last_response.body }it_behaves_like 'successful storage response'context 'if user is missing permission view_file_links' do- let(:permissions) { [] }+ before(:all) { remove_permissions(user_with_permissions, :view_file_links) }+ after(:all) { add_permissions(user_with_permissions, :view_file_links) }it_behaves_like 'not found'end@@ -221,7 +220,7 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'as admin' do- let(:current_user) { create(:admin) }+ let(:current_user) { admin }it_behaves_like 'successful storage response', as_admin: true@@ -292,16 +291,14 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'if user does not belong to a project using the given storage' do- let(:current_user) do- create(:user)- end+ let(:current_user) { user_without_project }it_behaves_like 'not found'endendcontext 'as admin' do- let(:current_user) { create(:admin) }+ let(:current_user) { admin }describe 'patches the storage and returns it' dosubject { last_response.body }@@ -379,7 +376,7 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'as admin' do- let(:current_user) { create(:admin) }+ let(:current_user) { admin }it_behaves_like 'successful no content response'end@@ -394,9 +391,7 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'if user does not belong to a project using the given storage' do- let(:current_user) do- create(:user)- end+ let(:current_user) { user_without_project }it_behaves_like 'not found'@@ -419,7 +414,7 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'as admin' do- let(:current_user) { create(:admin) }+ let(:current_user) { admin }it_behaves_like 'redirect response'end@@ -428,7 +423,8 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doit_behaves_like 'redirect response'context 'if user is missing permission view_file_links' do- let(:permissions) { [] }+ before(:all) { remove_permissions(user_with_permissions, :view_file_links) }+ after(:all) { add_permissions(user_with_permissions, :view_file_links) }it_behaves_like 'not found'end@@ -462,16 +458,14 @@ RSpec.describe 'API v3 storages resource', :webmock, content_type: :json doendcontext 'if user does not belong to a project using the given storage' do- let(:current_user) do- create(:user)- end+ let(:current_user) { user_without_project }it_behaves_like 'not found'endendcontext 'as admin' do- let(:current_user) { create(:admin) }+ let(:current_user) { admin }describe 'creates new oauth client secrets' dosubject { last_response.body }diff --git a/modules/storages/spec/support/user_permissions_helper.rb b/modules/storages/spec/support/user_permissions_helper.rbnew file mode 100644index 00000000000..5be79037511--- /dev/null+++ b/modules/storages/spec/support/user_permissions_helper.rb@@ -0,0 +1,43 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module UserPermissionsHelper+ def add_permissions(user, *)+ role = Role.joins(members: :principal).where('users.id': user).first+ role.add_permission!(*)+ user.reload # clear user's project_role_cache+ end++ def remove_permissions(user, *)+ role = Role.joins(members: :principal).where('users.id': user).first+ role.remove_permission!(*)+ user.reload # clear user's project_role_cache+ end+end