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

Cover project storage openining with tests.

Pavel Balashou673e8f6cf5da
diff --git a/modules/storages/app/components/storages/open_project_storage_modal_component/body.html.erb b/modules/storages/app/components/storages/open_project_storage_modal_component/body.html.erb
index 639a7ae832d..22d9632fb5b 100644
--- a/modules/storages/app/components/storages/open_project_storage_modal_component/body.html.erb
+++ b/modules/storages/app/components/storages/open_project_storage_modal_component/body.html.erb
@@ -26,4 +26,3 @@
<% end %>
<% end %>
<% end %>
-<!-- Timeout: Permissions were not set within the expected time. Please, contact the administrator. -->
diff --git a/modules/storages/app/controllers/storages/project_storages_controller.rb b/modules/storages/app/controllers/storages/project_storages_controller.rb
index f688efd3f5c..9229c593deb 100644
--- a/modules/storages/app/controllers/storages/project_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/project_storages_controller.rb
@@ -33,70 +33,72 @@ class Storages::ProjectStoragesController < ApplicationController
model_object Storages::ProjectStorage
before_action :require_login
- before_action :find_model_object, only: %i[open]
+ before_action :find_model_object
before_action :find_project_by_project_id
+ before_action :render_403, unless: -> { User.current.allowed_in_project?(:view_file_links, @project) }
def open
- url = @object.open(current_user).match(
+ storage_open_url = @object.open(current_user).match(
on_success: ->(url) { url },
on_failure: ->(error) { raise_error(error) }
)
-
- modal_params = {
- project_storage_open_url: request.path,
- redirect_url: url
- }
- storage = @object.storage
- result = ::Storages::Peripherals::Registry
- .resolve("queries.#{storage.short_provider_type}.file_info")
- .call(storage:,
- user: current_user,
- file_id: @object.project_folder_id)
-
- redirect_to url if [email protected]_folder_automatic?
- result.match(
- on_success: ->(_) do
- respond_to do |format|
- format.turbo_stream do
- stream = OpTurbo::StreamComponent.new(
- action: :update,
- target: Storages::OpenProjectStorageModalComponent.dialog_body_id,
- template: Storages::OpenProjectStorageModalComponent::Body.new(:success).render_in(view_context)
- ).render_in(view_context)
- render turbo_stream: stream
- end
- format.html { redirect_to url }
- end
- end,
- on_failure: ->(result) do
- case result.code
- when :unauthorized
+ if @object.project_folder_automatic?
+ storage = @object.storage
+ # check if user "see" project_folder
+ result = ::Storages::Peripherals::Registry
+ .resolve("queries.#{storage.short_provider_type}.file_info")
+ .call(storage:,
+ user: current_user,
+ file_id: @object.project_folder_id)
+ result.match(
+ on_success: ->(_) do
respond_to do |format|
- format.turbo_stream { head :no_content }
- format.html do
- ensure_connection_url = oauth_clients_ensure_connection_url(
- oauth_client_id: storage.oauth_client.client_id,
- storage_id: storage.id,
- destination_url: request.url
+ format.turbo_stream do
+ render(
+ turbo_stream: OpTurbo::StreamComponent.new(
+ action: :update,
+ target: Storages::OpenProjectStorageModalComponent.dialog_body_id,
+ template: Storages::OpenProjectStorageModalComponent::Body.new(:success).render_in(view_context)
+ ).render_in(view_context)
)
- redirect_to ensure_connection_url
end
+ format.html { redirect_to storage_open_url }
end
- when :forbidden
+ end,
+ on_failure: ->(result) do
respond_to do |format|
- format.turbo_stream do
- head :no_content
- end
+ format.turbo_stream { head :no_content }
format.html do
- flash[:modal] = {
- type: 'Storages::OpenProjectStorageModalComponent',
- parameters: modal_params.merge(state: :waiting)
- }
- redirect_to project_overview_path(project_id: @project.identifier)
+ case result.code
+ when :unauthorized
+ redirect_to(
+ oauth_clients_ensure_connection_url(
+ oauth_client_id: storage.oauth_client.client_id,
+ storage_id: storage.id,
+ destination_url: request.url
+ )
+ )
+ when :forbidden
+ redirect_to(
+ project_overview_path(project_id: @project.identifier),
+ flash: {
+ modal: {
+ type: 'Storages::OpenProjectStorageModalComponent',
+ parameters: {
+ project_storage_open_url: request.path,
+ redirect_url: storage_open_url,
+ state: :waiting
+ }
+ }
+ }
+ )
+ end
end
end
end
- end
- )
+ )
+ else
+ redirect_to storage_open_url
+ end
end
end
diff --git a/modules/storages/app/models/storages/project_storage.rb b/modules/storages/app/models/storages/project_storage.rb
index c11062ab7a2..b8bca14d3c3 100644
--- a/modules/storages/app/models/storages/project_storage.rb
+++ b/modules/storages/app/models/storages/project_storage.rb
@@ -69,7 +69,9 @@ class Storages::ProjectStorage < ApplicationRecord
end
def open(user)
- if project_folder_inactive? || (project_folder_automatic? && !user.allowed_in_project?(:read_files, project))
+ if project_folder_inactive? ||
+ (project_folder_automatic? && !user.allowed_in_project?(:read_files, project)) ||
+ project_folder_id.blank?
Storages::Peripherals::Registry
.resolve("queries.#{storage.short_provider_type}.open_storage")
.call(storage:, user:)
diff --git a/modules/storages/spec/features/storages_menu_links_spec.rb b/modules/storages/spec/features/storages_menu_links_spec.rb
index 7994c264be7..eebfe258982 100644
--- a/modules/storages/spec/features/storages_menu_links_spec.rb
+++ b/modules/storages/spec/features/storages_menu_links_spec.rb
@@ -32,8 +32,6 @@ require 'spec_helper'
require_module_spec_helper
RSpec.describe 'Storage links in project menu', :js do
- include API::V3::Utilities::PathHelper
-
let!(:storage_configured_linked1) { create(:nextcloud_storage_configured, name: "Storage 1") }
let!(:project_storage1) { create(:project_storage, project:, storage: storage_configured_linked1) }
let!(:storage_configured_linked2) { create(:nextcloud_storage_configured, name: "Storage 2") }
diff --git a/modules/storages/spec/models/storages/project_storage_spec.rb b/modules/storages/spec/models/storages/project_storage_spec.rb
index 4c78fbdad06..9e372da8d2d 100644
--- a/modules/storages/spec/models/storages/project_storage_spec.rb
+++ b/modules/storages/spec/models/storages/project_storage_spec.rb
@@ -123,4 +123,73 @@ RSpec.describe Storages::ProjectStorage do
.backed_by_column_of_type(:enum)
end
end
+
+ describe '#open' do
+ let(:user) { create(:user, member_with_permissions: { project => permissions }) }
+ let(:permissions) { %i[] }
+ let(:project_storage) do
+ build(:project_storage,
+ storage:,
+ project_folder_mode:,
+ project_folder_id:,
+ project:)
+ end
+ let(:project_folder_id) { nil }
+
+ context 'when inactive' do
+ let(:project_folder_mode) { 'inactive' }
+
+ it 'opens storage' do
+ expect(project_storage.open(user).result).to eq("#{storage.host}/index.php/apps/files")
+ end
+ end
+
+ context 'when manual' do
+ let(:project_folder_mode) { 'manual' }
+
+ context 'when project_folder_id is missing' do
+ it 'opens storage' do
+ expect(project_storage.open(user).result).to eq("#{storage.host}/index.php/apps/files")
+ end
+ end
+
+ context 'when project_folder_id is present' do
+ let(:project_folder_id) { '123' }
+
+ it 'opens project_folder' do
+ expect(project_storage.open(user).result).to eq("#{storage.host}/index.php/f/123?openfile=1")
+ end
+ end
+ end
+
+ context 'when automatic' do
+ let(:project_folder_mode) { 'automatic' }
+
+ context 'when user has no permissions to read files in storage' do
+ let(:project_folder_mode) { 'automatic' }
+
+ it 'opens storage' do
+ expect(project_storage.open(user).result).to eq("#{storage.host}/index.php/apps/files")
+ end
+ end
+
+ context 'when user has permissions to read files in storage' do
+ let(:permissions) { %i[read_files] }
+
+ context 'when project_folder_id is missing' do
+ it 'opens storage' do
+ expect(project_storage.open(user).result).to eq("#{storage.host}/index.php/apps/files")
+ end
+ end
+
+ context 'when project_folder_id is present' do
+ let(:project_folder_id) { '123' }
+
+ it 'opens project_folder' do
+ expect(project_storage.open(user).result).to eq("#{storage.host}/index.php/f/123?openfile=1")
+ end
+ end
+ end
+ end
+ end
end
diff --git a/modules/storages/spec/requests/project_storages_open_spec.rb b/modules/storages/spec/requests/project_storages_open_spec.rb
new file mode 100644
index 00000000000..464157a21a5
--- /dev/null
+++ b/modules/storages/spec/requests/project_storages_open_spec.rb
@@ -0,0 +1,156 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+require 'spec_helper'
+require_module_spec_helper
+
+RSpec.describe 'projects/:project_id/project_storages/:id/open' do
+ shared_let(:project) { create(:project) }
+ shared_let(:storage) { create(:nextcloud_storage_configured) }
+ let(:project_storage) { create(:project_storage, project:, storage:) }
+ let(:route) { "projects/#{project.identifier}/project_storages/#{project_storage.id}/open" }
+
+ context 'when user is logged in' do
+ current_user { create(:user, member_with_permissions: { project => permissions }) }
+
+ context 'when user has permissions' do
+ let(:permissions) { %i[view_file_links] }
+
+ context 'when project_folder is automatic' do
+ let(:project_storage) { create(:project_storage, :as_automatically_managed, project:, storage:) }
+
+ context 'when user is able to read project_folder' do
+ before do
+ Storages::Peripherals::Registry.stub(
+ 'queries.nextcloud.file_info', ->(_) { ServiceResult.success }
+ )
+ end
+
+ context 'html' do
+ it 'redirects to storage_open_url' do
+ get route, {}, { 'HTTP_ACCEPT' => 'text/html' }
+
+ expect(last_response.status).to eq (302)
+ expect(last_response.headers["Location"]).to eq ("#{storage.host}/index.php/apps/files")
+ end
+ end
+
+ context 'turbo_stream' do
+ it 'renders an appropirate turbo_stream' do
+ get route, {}, { 'HTTP_ACCEPT' => 'text/vnd.turbo-stream.html' }
+
+ expect(last_response.status).to eq (200)
+ expect(last_response.body).to eq ("<turbo-stream action=\"update\" target=\"open-project-storage-modal-body-component\">\n <template>\n <div data-view-component=\"true\" class=\"flex-items-center d-flex flex-column\">\n <div data-view-component=\"true\"> <svg aria-hidden=\"true\" height=\"24\" viewBox=\"0 0 24 24\" version=\"1.1\" width=\"24\" data-view-component=\"true\" class=\"octicon octicon-check-circle color-fg-success\">\n <path d=\"M17.28 9.28a.75.75 0 0 0-1.06-1.06l-5.97 5.97-2.47-2.47a.75.75 0 0 0-1.06 1.06l3 3a.75.75 0 0 0 1.06 0l6.5-6.5Z\"></path><path d=\"M12 1c6.075 0 11 4.925 11 11s-4.925 11-11 11S1 18.075 1 12 5.925 1 12 1ZM2.5 12a9.5 9.5 0 0 0 9.5 9.5 9.5 9.5 0 0 0 9.5-9.5A9.5 9.5 0 0 0 12 2.5 9.5 9.5 0 0 0 2.5 12Z\"></path>\n</svg>\n</div>\n <div data-view-component=\"true\"> <span data-view-component=\"true\">Integration setup completed</span>\n</div>\n <div data-view-component=\"true\"> <span data-view-component=\"true\">You are being redirected</span>\n</div>\n</div>\n\n\n </template>\n</turbo-stream>\n\n")
+ end
+ end
+ end
+
+ context 'when user is not able to read project_folder' do
+ let(:code) { :forbidden }
+
+ before do
+ Storages::Peripherals::Registry.stub(
+ 'queries.nextcloud.file_info', ->(_) do
+ ServiceResult.failure(result: code,
+ errors: Storages::StorageError.new(code:))
+ end
+ )
+ end
+
+ context 'html' do
+ context 'when error code is unauthorized' do
+ let(:code) { :unauthorized }
+
+ it 'redirects to ensure_connection url with current request url as a destination_url' do
+ get route, {}, { 'HTTP_ACCEPT' => 'text/html' }
+
+ expect(last_response.status).to eq (302)
+ expect(last_response.headers["Location"]).to eq (
+ "http://example.org/oauth_clients/#{storage.oauth_client.client_id}/ensure_connection?destination_url=http%3A%2F%2Fexample.org%2Fprojects%2F#{project.identifier}%2Fproject_storages%2F#{project_storage.id}%2Fopen&storage_id=#{storage.id}"
+ )
+ end
+ end
+
+ context 'when error code is forbidden' do
+ it 'redirects to project overview page with modal flash set up' do
+ get route, {}, { 'HTTP_ACCEPT' => 'text/html' }
+
+ expect(last_response.status).to eq (302)
+ expect(last_response.headers["Location"]).to eq ("http://example.org/projects/#{project.identifier}")
+ expect(last_response.headers["Location"]).to eq ("http://example.org/projects/#{project.identifier}")
+ expect(last_request.session['flash']['flashes'])
+ .to eq({
+ "modal" => {
+ type: "Storages::OpenProjectStorageModalComponent",
+ parameters: { project_storage_open_url: "/projects/#{project.identifier}/project_storages/#{project_storage.id}/open",
+ redirect_url: "#{storage.host}/index.php/apps/files",
+ state: :waiting }
+ }
+ })
+ end
+ end
+ end
+
+ context 'turbo_stream' do
+ it 'responds with 204 no content' do
+ get route, {}, { 'HTTP_ACCEPT' => 'text/vnd.turbo-stream.html' }
+
+ expect(last_response.status).to eq (204)
+ expect(last_response.body).to eq ("")
+ end
+ end
+ end
+ end
+
+ context 'when project_folder is not automatic' do
+ it 'redirects to storage_open_url' do
+ get route, {}, { 'HTTP_ACCEPT' => 'text/html' }
+
+ expect(last_response.status).to eq (302)
+ expect(last_response.headers["Location"]).to eq ("#{storage.host}/index.php/apps/files")
+ end
+ end
+ end
+
+ context 'when user has no permissions' do
+ let(:permissions) { %i[] }
+
+ it 'responds with 403' do
+ get route, {}, { 'HTTP_ACCEPT' => 'text/html' }
+ expect(last_response.status).to eq(403)
+ end
+ end
+ end
+
+ context 'when user is not logged in' do
+ it 'responds with 401' do
+ get route
+ expect(last_response.status).to eq(401)
+ end
+ end
+end