Skip to content

Commit

Permalink
Merge pull request #14448 from opf/chore/speed-up-specs
Browse files Browse the repository at this point in the history
Speed up specs by using shared_let.
  • Loading branch information
ba1ash committed Dec 19, 2023
2 parents ef1d5b9 + 51d7286 commit a20025d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 43 deletions.
Expand Up @@ -33,18 +33,7 @@

RSpec.describe 'API v3 file links resource' do
include 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 UserPermissionsHelper

def enable_module(project, modul)
project.enabled_module_names = project.enabled_module_names + [modul]
Expand Down
56 changes: 25 additions & 31 deletions modules/storages/spec/requests/api/v3/storages/storages_api_spec.rb
Expand Up @@ -32,20 +32,23 @@
RSpec.describe 'API v3 storages resource', :webmock, content_type: :json do
include API::V3::Utilities::PathHelper
include 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) do
create(: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) do
get path
Expand All @@ -54,7 +57,6 @@
before do
allow(connection_manager).to receive_messages(get_authorization_uri: authorize_url, authorization_state: :connected)
allow(OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
project_storage
login_as current_user
end

Expand All @@ -79,12 +81,10 @@
let(: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' do
subject { last_response.body }
Expand Down Expand Up @@ -132,7 +132,7 @@
end

context 'as admin' do
let(:current_user) { create(:admin) }
let(:current_user) { admin }

describe 'creates a storage and returns it' do
subject { last_response.body }
Expand Down Expand Up @@ -195,14 +195,13 @@
let(: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
Expand All @@ -221,7 +220,7 @@
end

context 'as admin' do
let(:current_user) { create(:admin) }
let(:current_user) { admin }

it_behaves_like 'successful storage response', as_admin: true

Expand Down Expand Up @@ -292,16 +291,14 @@
end

context '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'
end
end

context 'as admin' do
let(:current_user) { create(:admin) }
let(:current_user) { admin }

describe 'patches the storage and returns it' do
subject { last_response.body }
Expand Down Expand Up @@ -379,7 +376,7 @@
end

context 'as admin' do
let(:current_user) { create(:admin) }
let(:current_user) { admin }

it_behaves_like 'successful no content response'
end
Expand All @@ -394,9 +391,7 @@
end

context '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'

Expand All @@ -419,7 +414,7 @@
end

context 'as admin' do
let(:current_user) { create(:admin) }
let(:current_user) { admin }

it_behaves_like 'redirect response'
end
Expand All @@ -428,7 +423,8 @@
it_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
Expand Down Expand Up @@ -462,16 +458,14 @@
end

context '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'
end
end

context 'as admin' do
let(:current_user) { create(:admin) }
let(:current_user) { admin }

describe 'creates new oauth client secrets' do
subject { last_response.body }
Expand Down
43 changes: 43 additions & 0 deletions 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

0 comments on commit a20025d

Please sign in to comment.