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

fix[Op#50737]: Mend OAuthClient form for one drive, introduce feature tests

Kabiru Mwenja2f7383e3a0fc
diff --git a/modules/storages/app/components/storages/admin/forms/automatically_managed_project_folders_form_component.rb b/modules/storages/app/components/storages/admin/forms/automatically_managed_project_folders_form_component.rb
index 0736e2de889..153d95abbbb 100644
--- a/modules/storages/app/components/storages/admin/forms/automatically_managed_project_folders_form_component.rb
+++ b/modules/storages/app/components/storages/admin/forms/automatically_managed_project_folders_form_component.rb
@@ -64,12 +64,7 @@ module Storages::Admin::Forms
end
def new_record?
- if storage.provider_fields_changed?
- previous_configuration = storage.provider_fields_change.first
- previous_configuration.values_at('automatically_managed', 'password').compact.empty?
- else
- storage.automatic_management_unspecified?
- end
+ storage.automatic_management_new_record?
end
def default_form_url
diff --git a/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.html.erb b/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.html.erb
index 0455696d027..1c3897c11b4 100644
--- a/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.html.erb
+++ b/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.html.erb
@@ -3,6 +3,7 @@
primer_form_with(
model: oauth_client,
url: admin_settings_storage_oauth_client_path(storage),
+ method: form_method,
data: {
controller: 'storages--oauth-client-form',
application_target: 'dynamic',
@@ -66,8 +67,8 @@
submit_button_options: {
disabled: submit_button_disabled?,
data: {
- 'storages--oauth-client-form-target': 'submitButton'
- },
+ 'storages--oauth-client-form-target': 'submitButton',
+ }.merge(submit_button_data_options),
test_selector: 'storage-oauth-client-submit-button'
},
cancel_button_options: {
diff --git a/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.rb b/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.rb
index 682c6a3bc87..65634cd99f0 100644
--- a/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.rb
+++ b/modules/storages/app/components/storages/admin/forms/oauth_client_form_component.rb
@@ -40,16 +40,27 @@ module Storages::Admin::Forms
@storage = storage
end
+ def form_method
+ options[:form_method] || default_form_method
+ end
+
def cancel_button_path
storage.persisted? ? edit_admin_settings_storage_path(storage) : admin_settings_storages_path
end
def submit_button_disabled?
- !storage_oauth_client_configured?
+ !oauth_client_configured?
+ end
+
+ def submit_button_data_options
+ {}.tap do |data|
+ # For One Drive create action, break from Turbo Frame and follow full page redirect
+ data[:turbo] = false if one_drive_first_time_configuration?
+ end
end
def redirect_uri_or_instructions
- if storage_oauth_client_configured?
+ if oauth_client_configured?
oauth_client.redirect_uri
else
I18n.t("storages.instructions.one_drive.missing_client_id_for_redirect_uri")
@@ -72,7 +83,19 @@ module Storages::Admin::Forms
private
- def storage_oauth_client_configured?
+ def one_drive_first_time_configuration?
+ storage.provider_type_one_drive? && first_time_configuration?
+ end
+
+ def first_time_configuration?
+ storage.oauth_client.blank? || storage.oauth_client.new_record?
+ end
+
+ def default_form_method
+ first_time_configuration? ? :post : :patch
+ end
+
+ def oauth_client_configured?
oauth_client.present? && oauth_client.client_id.present? && oauth_client.client_secret.present?
end
end
diff --git a/modules/storages/app/components/storages/admin/storage_view_information.rb b/modules/storages/app/components/storages/admin/storage_view_information.rb
index 029d9d80eae..1c673f8189f 100644
--- a/modules/storages/app/components/storages/admin/storage_view_information.rb
+++ b/modules/storages/app/components/storages/admin/storage_view_information.rb
@@ -9,7 +9,7 @@ module Storages::Admin
end
def storage_description
- [storage.short_provider_type.capitalize,
+ [I18n.t("storages.provider_types.#{storage.short_provider_type}.name"),
storage.name,
storage.host].compact.join(' - ')
end
diff --git a/modules/storages/app/controllers/storages/admin/oauth_clients_controller.rb b/modules/storages/app/controllers/storages/admin/oauth_clients_controller.rb
index c92bea8cfeb..02c05533ce2 100644
--- a/modules/storages/app/controllers/storages/admin/oauth_clients_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/oauth_clients_controller.rb
@@ -37,7 +37,7 @@ class Storages::Admin::OAuthClientsController < ApplicationController
before_action :require_admin
before_action :find_storage
- before_action :delete_current_oauth_client, only: %i[create]
+ before_action :delete_current_oauth_client, only: %i[create update]
# menu_item is defined in the Redmine::MenuManager::MenuController
# module, included from ApplicationController.
@@ -62,37 +62,52 @@ class Storages::Admin::OAuthClientsController < ApplicationController
# Actually create a OAuthClient object.
# Use service pattern to create a new OAuthClient
# Called by: Global app/config/routes.rb to serve Web page
- def create # rubocop:disable Metrics/AbcSize
- service_result = ::OAuthClients::CreateService.new(user: User.current)
- .call(oauth_client_params.merge(integration: @storage))
- @oauth_client = service_result.result
- @storage = @storage.reload
+ def create # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
+ call_oauth_clients_create_service
service_result.on_failure do
render '/storages/admin/storages/new_oauth_client'
end
service_result.on_success do
- flash[:notice] = I18n.t(:notice_successful_create)
-
- if @storage.provider_type_nextcloud? && @storage.automatic_management_unspecified?
- if OpenProject::FeatureDecisions.storage_primer_design_active?
- @storage = ::Storages::Storages::SetNextcloudProviderFieldsAttributesService
- .new(user: current_user,
- model: @storage,
- contract_class: EmptyContract)
- .call
- .result
+ if OpenProject::FeatureDecisions.storage_primer_design_active?
+ if @storage.provider_type_nextcloud?
+ prepare_storage_for_automatic_management_form
respond_to do |format|
- format.html { redirect_to edit_admin_settings_storage_path(@storage) }
- format.turbo_stream
+ format.turbo_stream { render :create }
end
+ elsif @storage.provider_type_one_drive?
+ flash[:notice] = I18n.t(:'storages.notice_successful_storage_connection')
+ redirect_to admin_settings_storages_path
else
- redirect_to new_admin_settings_storage_automatically_managed_project_folders_path(@storage)
+ raise "Unsupported provider type: #{@storage.short_provider_type}"
end
else
- redirect_to edit_admin_settings_storage_path(@storage)
+ flash[:notice] = I18n.t(:notice_successful_create)
+
+ if @storage.provider_type_nextcloud? && @storage.automatic_management_unspecified?
+ prepare_storage_for_automatic_management_form
+ redirect_to new_admin_settings_storage_automatically_managed_project_folders_path(@storage)
+ else
+ redirect_to edit_admin_settings_storage_path(@storage)
+ end
+ end
+ end
+ end
+
+ def update
+ call_oauth_clients_create_service
+
+ service_result.on_failure do
+ respond_to do |format|
+ format.turbo_stream { render :new }
+ end
+ end
+
+ service_result.on_success do
+ respond_to do |format|
+ format.turbo_stream { render :update }
end
end
end
@@ -112,6 +127,25 @@ class Storages::Admin::OAuthClientsController < ApplicationController
private
+ attr_reader :service_result
+
+ def call_oauth_clients_create_service
+ @service_result = ::OAuthClients::CreateService
+ .new(user: User.current)
+ .call(oauth_client_params.merge(integration: @storage))
+ @oauth_client = service_result.result
+ @storage = @storage.reload
+ end
+
+ def prepare_storage_for_automatic_management_form
+ return unless @storage.automatic_management_unspecified?
+
+ @storage = ::Storages::Storages::SetNextcloudProviderFieldsAttributesService
+ .new(user: current_user, model: @storage, contract_class: EmptyContract)
+ .call
+ .result
+ end
+
# Called by create and update above in order to check if the
# update parameters are correctly set.
def oauth_client_params
@@ -125,6 +159,7 @@ class Storages::Admin::OAuthClientsController < ApplicationController
end
def delete_current_oauth_client
+ @previous_oauth_client_present = @storage.oauth_client.present?
::OAuthClients::DeleteService.new(user: User.current, model: @storage.oauth_client).call if @storage.oauth_client
end
end
diff --git a/modules/storages/app/models/storages/nextcloud_storage.rb b/modules/storages/app/models/storages/nextcloud_storage.rb
index 06451b2beb7..95f73c2d516 100644
--- a/modules/storages/app/models/storages/nextcloud_storage.rb
+++ b/modules/storages/app/models/storages/nextcloud_storage.rb
@@ -60,6 +60,15 @@ module Storages
Peripherals::OAuthConfigurations::NextcloudConfiguration.new(self)
end
+ def automatic_management_new_record?
+ if provider_fields_changed?
+ previous_configuration = provider_fields_change.first
+ previous_configuration.values_at('automatically_managed', 'password').compact.empty?
+ else
+ automatic_management_unspecified?
+ end
+ end
+
def automatic_management_unspecified?
automatically_managed.nil?
end
diff --git a/modules/storages/app/views/storages/admin/oauth_clients/create.turbo_stream.erb b/modules/storages/app/views/storages/admin/oauth_clients/create.turbo_stream.erb
index 7058cb5711b..3fd98e27146 100644
--- a/modules/storages/app/views/storages/admin/oauth_clients/create.turbo_stream.erb
+++ b/modules/storages/app/views/storages/admin/oauth_clients/create.turbo_stream.erb
@@ -31,6 +31,8 @@ See COPYRIGHT and LICENSE files for more details.
<%= render(Storages::Admin::OAuthClientInfoComponent.new(oauth_client: @oauth_client, storage: @storage)) %>
<% end %>
-<%= turbo_stream.update :automatically_managed_project_folders_section do %>
- <%= render(Storages::Admin::Forms::AutomaticallyManagedProjectFoldersFormComponent.new(@storage)) %>
+<% if @storage.provider_type_nextcloud? && @storage.automatic_management_new_record? %>
+ <%= turbo_stream.update :automatically_managed_project_folders_section do %>
+ <%= render(Storages::Admin::Forms::AutomaticallyManagedProjectFoldersFormComponent.new(@storage)) %>
+ <% end %>
<% end %>
diff --git a/modules/storages/app/views/storages/admin/oauth_clients/update.turbo_stream.erb b/modules/storages/app/views/storages/admin/oauth_clients/update.turbo_stream.erb
new file mode 100644
index 00000000000..9a27fd06284
--- /dev/null
+++ b/modules/storages/app/views/storages/admin/oauth_clients/update.turbo_stream.erb
@@ -0,0 +1,32 @@
+<%#-- 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.
+
+++#%>
+
+<%= turbo_stream.update :storage_oauth_client_section do %>
+ <%= render(Storages::Admin::OAuthClientInfoComponent.new(oauth_client: @oauth_client, storage: @storage)) %>
+<% end %>
diff --git a/modules/storages/config/routes.rb b/modules/storages/config/routes.rb
index 49d29a48594..f221bdef73f 100644
--- a/modules/storages/config/routes.rb
+++ b/modules/storages/config/routes.rb
@@ -32,7 +32,10 @@ OpenProject::Application.routes.draw do
namespace :admin do
namespace :settings do
resources :storages, controller: '/storages/admin/storages', except: [:show] do
- resource :oauth_client, controller: '/storages/admin/oauth_clients', only: %i[new create]
+ resource :oauth_client, controller: '/storages/admin/oauth_clients', only: %i[new create] do
+ patch :update, on: :member
+ end
+
resource :automatically_managed_project_folders, controller: '/storages/admin/automatically_managed_project_folders',
only: %i[new create edit update]
diff --git a/modules/storages/spec/features/admin_storages_spec.rb b/modules/storages/spec/features/admin_storages_spec.rb
index 971c06c247f..868dd8ea166 100644
--- a/modules/storages/spec/features/admin_storages_spec.rb
+++ b/modules/storages/spec/features/admin_storages_spec.rb
@@ -90,173 +90,247 @@ RSpec.describe 'Admin storages',
end
describe 'File storage edit view', with_flag: { storage_primer_design: true } do
- let(:storage) { create(:nextcloud_storage, :as_automatically_managed) }
- let(:oauth_application) { create(:oauth_application, integration: storage) }
- let(:oauth_client) { create(:oauth_client, integration: storage) }
+ it 'renders a delete button' do
+ storage = create(:nextcloud_storage, name: "Foo Nextcloud")
+ visit edit_admin_settings_storage_path(storage)
+
+ storage_delete_button = find_test_selector('storage-delete-button')
+ expect(storage_delete_button).to have_text('Delete')
- before do
- oauth_application
- oauth_client
+ accept_confirm do
+ storage_delete_button.click
+ end
+
+ expect(page).to have_current_path(admin_settings_storages_path)
+ expect(page).not_to have_text("Foo Nextcloud")
end
- it 'renders an edit view', :webmock do
- visit edit_admin_settings_storage_path(storage)
+ context 'with Nextcloud Storage' do
+ let(:storage) { create(:nextcloud_storage, :as_automatically_managed) }
+ let(:oauth_application) { create(:oauth_application, integration: storage) }
+ let(:oauth_client) { create(:oauth_client, integration: storage) }
- expect(page).to have_test_selector('storage-name-title', text: storage.name.capitalize)
-
- aggregate_failures 'Storage edit view' do
- # General information
- expect(page).to have_test_selector('storage-provider-label', text: 'Storage provider')
- expect(page).to have_test_selector('label-host_name_configured-status', text: 'Completed')
- expect(page).to have_test_selector('storage-description', text: [storage.short_provider_type.capitalize,
- storage.name,
- storage.host].join(' - '))
-
- # OAuth application
- expect(page).to have_test_selector('storage-openproject-oauth-label', text: 'OpenProject OAuth')
- expect(page).to have_test_selector('label-openproject_oauth_application_configured-status', text: 'Completed')
- expect(page).to have_test_selector('storage-openproject-oauth-application-description',
- text: "OAuth Client ID: #{oauth_application.uid}")
-
- # OAuth client
- expect(page).to have_test_selector('storage-oauth-client-label', text: 'Nextcloud OAuth')
- expect(page).to have_test_selector('label-storage_oauth_client_configured-status', text: 'Completed')
- expect(page).to have_test_selector('storage-oauth-client-id-description',
- text: "OAuth Client ID: #{oauth_client.client_id}")
-
- # Automatically managed project folders
- expect(page).to have_test_selector('storage-managed-project-folders-label',
- text: 'Automatically managed folders')
-
- expect(page).to have_test_selector('label-managed-project-folders-status', text: 'Active')
- expect(page).to have_test_selector('storage-automatically-managed-project-folders-description',
- text: 'Let OpenProject create folders per project automatically.')
+ before do
+ oauth_application
+ oauth_client
end
- aggregate_failures 'General information' do
- # Update a storage - happy path
- find_test_selector('storage-edit-host-button').click
- within_test_selector('storage-general-info-form') do
- expect(page).to have_css('#storages_nextcloud_storage_provider_type[disabled]')
+ it 'renders an edit view', :webmock do
+ visit edit_admin_settings_storage_path(storage)
- fill_in 'storages_nextcloud_storage_name', with: 'My Nextcloud'
- click_button 'Save and continue'
- end
+ expect(page).to have_test_selector('storage-name-title', text: storage.name.capitalize)
- expect(page).to have_test_selector('storage-name-title', text: 'My Nextcloud')
- expect(page).to have_test_selector('storage-description', text: [storage.short_provider_type.capitalize,
- 'My Nextcloud',
- storage.host].join(' - '))
+ aggregate_failures 'Storage edit view' do
+ # General information
+ expect(page).to have_test_selector('storage-provider-label', text: 'Storage provider')
+ expect(page).to have_test_selector('label-host_name_configured-status', text: 'Completed')
+ expect(page).to have_test_selector('storage-description', text: "Nextcloud - #{storage.name} - #{storage.host}")
- # Update a storage - unhappy path
- find_test_selector('storage-edit-host-button').click
- within_test_selector('storage-general-info-form') do
- fill_in 'storages_nextcloud_storage_name', with: nil
- fill_in 'storages_nextcloud_storage_host', with: nil
- click_button 'Save and continue'
+ # OAuth application
+ expect(page).to have_test_selector('storage-openproject-oauth-label', text: 'OpenProject OAuth')
+ expect(page).to have_test_selector('label-openproject_oauth_application_configured-status', text: 'Completed')
+ expect(page).to have_test_selector('storage-openproject-oauth-application-description',
+ text: "OAuth Client ID: #{oauth_application.uid}")
- expect(page).to have_text("Name can't be blank.")
- expect(page).to have_text("Host is not a valid URL.")
+ # OAuth client
+ expect(page).to have_test_selector('storage-oauth-client-label', text: 'Nextcloud OAuth')
+ expect(page).to have_test_selector('label-storage_oauth_client_configured-status', text: 'Completed')
+ expect(page).to have_test_selector('storage-oauth-client-id-description',
+ text: "OAuth Client ID: #{oauth_client.client_id}")
- click_link 'Cancel'
- end
- end
+ # Automatically managed project folders
+ expect(page).to have_test_selector('storage-managed-project-folders-label',
+ text: 'Automatically managed folders')
- aggregate_failures 'OAuth application' do
- accept_confirm do
- find_test_selector('storage-replace-openproject-oauth-application-button').click
+ expect(page).to have_test_selector('label-managed-project-folders-status', text: 'Active')
+ expect(page).to have_test_selector('storage-automatically-managed-project-folders-description',
+ text: 'Let OpenProject create folders per project automatically.')
end
- within_test_selector('storage-openproject-oauth-application-form') do
- warning_section = find_test_selector('storage-openproject_oauth_application_warning')
- expect(warning_section).to have_text('The client secret value will not be accessible again after you close ' \
- 'this window. Please copy these values into the Nextcloud ' \
- 'OpenProject Integration settings.')
- expect(warning_section).to have_link('Nextcloud OpenProject Integration settings',
- href: "#{storage.host}/settings/admin/openproject")
+ aggregate_failures 'General information' do
+ # Update a storage - happy path
+ find_test_selector('storage-edit-host-button').click
+ within_test_selector('storage-general-info-form') do
+ expect(page).to have_css('#storages_nextcloud_storage_provider_type[disabled]')
+
+ fill_in 'storages_nextcloud_storage_name', with: 'My Nextcloud'
+ click_button 'Save and continue'
+ end
- expect(page).to have_css('#openproject_oauth_application_uid',
- value: storage.reload.oauth_application.uid)
- expect(page).to have_css('#openproject_oauth_application_secret',
- value: storage.reload.oauth_application.secret)
+ expect(page).to have_test_selector('storage-name-title', text: 'My Nextcloud')
+ expect(page).to have_test_selector('storage-description', text: "Nextcloud - My Nextcloud - #{storage.host}")
- click_link 'Done, continue'
+ # Update a storage - unhappy path
+ find_test_selector('storage-edit-host-button').click
+ within_test_selector('storage-general-info-form') do
+ fill_in 'storages_nextcloud_storage_name', with: nil
+ fill_in 'storages_nextcloud_storage_host', with: nil
+ click_button 'Save and continue'
+
+ expect(page).to have_text("Name can't be blank.")
+ expect(page).to have_text("Host is not a valid URL.")
+
+ click_link 'Cancel'
+ end
end
- end
- aggregate_failures 'OAuth Client' do
- accept_confirm do
- find_test_selector('storage-edit-oauth-client-button').click
+ aggregate_failures 'OAuth application' do
+ accept_confirm do
+ find_test_selector('storage-replace-openproject-oauth-application-button').click
+ end
+
+ within_test_selector('storage-openproject-oauth-application-form') do
+ warning_section = find_test_selector('storage-openproject_oauth_application_warning')
+ expect(warning_section).to have_text('The client secret value will not be accessible again after you close ' \
+ 'this window. Please copy these values into the Nextcloud ' \
+ 'OpenProject Integration settings.')
+ expect(warning_section).to have_link('Nextcloud OpenProject Integration settings',
+ href: "#{storage.host}/settings/admin/openproject")
+
+ expect(page).to have_css('#openproject_oauth_application_uid',
+ value: storage.reload.oauth_application.uid)
+ expect(page).to have_css('#openproject_oauth_application_secret',
+ value: storage.reload.oauth_application.secret)
+
+ click_link 'Done, continue'
+ end
end
- within_test_selector('storage-oauth-client-form') do
- # With null values, submit button should be disabled
- expect(page).to have_css('#oauth_client_client_id', value: '')
- expect(page).to have_css('#oauth_client_client_secret', value: '')
- expect(find_test_selector('storage-oauth-client-submit-button')).to be_disabled
-
- # Happy path - Submit valid values
- fill_in 'oauth_client_client_id', with: '1234567890'
- fill_in 'oauth_client_client_secret', with: '0987654321'
- expect(find_test_selector('storage-oauth-client-submit-button')).not_to be_disabled
- click_button 'Save and continue'
+ aggregate_failures 'OAuth Client' do
+ accept_confirm do
+ find_test_selector('storage-edit-oauth-client-button').click
+ end
+
+ within_test_selector('storage-oauth-client-form') do
+ # With null values, submit button should be disabled
+ expect(page).to have_css('#oauth_client_client_id', value: '')
+ expect(page).to have_css('#oauth_client_client_secret', value: '')
+ expect(find_test_selector('storage-oauth-client-submit-button')).to be_disabled
+
+ # Happy path - Submit valid values
+ fill_in 'oauth_client_client_id', with: '1234567890'
+ fill_in 'oauth_client_client_secret', with: '0987654321'
+ expect(find_test_selector('storage-oauth-client-submit-button')).not_to be_disabled
+ click_button 'Save and continue'
+ end
+
+ expect(page).to have_test_selector('label-storage_oauth_client_configured-status', text: 'Completed')
+ expect(page).to have_test_selector('storage-oauth-client-id-description', text: "OAuth Client ID: 1234567890")
end
- expect(page).to have_test_selector('label-storage_oauth_client_configured-status', text: 'Completed')
- expect(page).to have_test_selector('storage-oauth-client-id-description', text: "OAuth Client ID: 1234567890")
+ aggregate_failures 'Automatically managed project folders' do
+ find_test_selector('storage-edit-automatically-managed-project-folders-button').click
+
+ within_test_selector('storage-automatically-managed-project-folders-form') do
+ automatically_managed_switch = page.find('[name="storages_nextcloud_storage[automatically_managed]"]')
+ application_password_input = page.find_by_id('storages_nextcloud_storage_password')
+ expect(automatically_managed_switch).to be_checked
+ expect(application_password_input.value).to be_empty
+
+ # Clicking submit with application password empty should show an error
+ click_button('Done, complete setup')
+ expect(page).to have_text("Password can't be blank.")
+
+ # Test the error path for an invalid storage password.
+ # Mock a valid response (=401) for example.com, so the password validation should fail
+ mock_nextcloud_application_credentials_validation(storage.host, password: "1234567890",
+ response_code: 401)
+ automatically_managed_switch = page.find('[name="storages_nextcloud_storage[automatically_managed]"]')
+ expect(automatically_managed_switch).to be_checked
+ fill_in 'storages_nextcloud_storage_password', with: "1234567890"
+ # Clicking submit with application password empty should show an error
+ click_button('Done, complete setup')
+ expect(page).to have_text("Password is not valid.")
+
+ # Test the happy path for a valid storage password.
+ # Mock a valid response (=200) for example.com, so the password validation should succeed
+ # Fill in application password and submit
+ mock_nextcloud_application_credentials_validation(storage.host, password: "1234567890")
+ automatically_managed_switch = page.find('[name="storages_nextcloud_storage[automatically_managed]"]')
+ expect(automatically_managed_switch).to be_checked
+ fill_in 'storages_nextcloud_storage_password', with: "1234567890"
+ click_button('Done, complete setup')
+ end
+
+ expect(page).to have_test_selector('label-managed-project-folders-status', text: 'Active')
+ end
end
+ end
- aggregate_failures 'Automatically managed project folders' do
- find_test_selector('storage-edit-automatically-managed-project-folders-button').click
-
- within_test_selector('storage-automatically-managed-project-folders-form') do
- automatically_managed_switch = page.find('[name="storages_nextcloud_storage[automatically_managed]"]')
- application_password_input = page.find_by_id('storages_nextcloud_storage_password')
- expect(automatically_managed_switch).to be_checked
- expect(application_password_input.value).to be_empty
-
- # Clicking submit with application password empty should show an error
- click_button('Done, complete setup')
- expect(page).to have_text("Password can't be blank.")
-
- # Test the error path for an invalid storage password.
- # Mock a valid response (=401) for example.com, so the password validation should fail
- mock_nextcloud_application_credentials_validation(storage.host, password: "1234567890",
- response_code: 401)
- automatically_managed_switch = page.find('[name="storages_nextcloud_storage[automatically_managed]"]')
- expect(automatically_managed_switch).to be_checked
- fill_in 'storages_nextcloud_storage_password', with: "1234567890"
- # Clicking submit with application password empty should show an error
- click_button('Done, complete setup')
- expect(page).to have_text("Password is not valid.")
-
- # Test the happy path for a valid storage password.
- # Mock a valid response (=200) for example.com, so the password validation should succeed
- # Fill in application password and submit
- mock_nextcloud_application_credentials_validation(storage.host, password: "1234567890")
- automatically_managed_switch = page.find('[name="storages_nextcloud_storage[automatically_managed]"]')
- expect(automatically_managed_switch).to be_checked
- fill_in 'storages_nextcloud_storage_password', with: "1234567890"
- click_button('Done, complete setup')
+ context 'with OneDrive Storage' do
+ let(:storage) { create(:one_drive_storage, name: 'Test Drive') }
+ let(:oauth_client) { create(:oauth_client, integration: storage) }
+
+ before { oauth_client }
+
+ it 'renders an edit view', :webmock do
+ visit edit_admin_settings_storage_path(storage)
+
+ expect(page).to have_test_selector('storage-name-title', text: 'Test Drive')
+
+ aggregate_failures 'Storage edit view' do
+ # General information
+ expect(page).to have_test_selector('storage-provider-label', text: 'Storage provider')
+ expect(page).to have_test_selector('label-host_name_configured-storage_tenant_drive_configured-status',
+ text: 'Completed')
+ expect(page).to have_test_selector('storage-description', text: 'OneDrive/SharePoint - Test Drive')
+
+ # OAuth client
+ expect(page).to have_test_selector('storage-oauth-client-label', text: 'Azure OAuth')
+ expect(page).to have_test_selector('label-storage_oauth_client_configured-status', text: 'Completed')
+ expect(page).to have_test_selector('storage-oauth-client-id-description',
+ text: "OAuth Client ID: #{oauth_client.client_id}")
end
- expect(page).to have_test_selector('label-managed-project-folders-status', text: 'Active')
- end
- end
+ aggregate_failures 'General information' do
+ # Update a storage - happy path
+ find_test_selector('storage-edit-host-button').click
+ within_test_selector('storage-general-info-form') do
+ expect(page).to have_css('#storages_one_drive_storage_provider_type[disabled]')
- it 'renders a delete button' do
- storage = create(:nextcloud_storage, name: "Foo Nextcloud")
- visit edit_admin_settings_storage_path(storage)
+ fill_in 'storages_one_drive_storage_name', with: 'My OneDrive'
+ click_button 'Save and continue'
+ end
- storage_delete_button = find_test_selector('storage-delete-button')
- expect(storage_delete_button).to have_text('Delete')
+ expect(page).to have_test_selector('storage-name-title', text: 'My OneDrive')
+ expect(page).to have_test_selector('storage-description', text: 'OneDrive/SharePoint - My OneDrive')
- accept_confirm do
- storage_delete_button.click
- end
+ # Update a storage - unhappy path
+ find_test_selector('storage-edit-host-button').click
+ within_test_selector('storage-general-info-form') do
+ fill_in 'storages_one_drive_storage_name', with: nil
+ fill_in 'storages_one_drive_storage_drive_id', with: nil
+ click_button 'Save and continue'
- expect(page).to have_current_path(admin_settings_storages_path)
- expect(page).not_to have_text("Foo Nextcloud")
+ expect(page).to have_text("Name can't be blank.")
+ expect(page).to have_text("Drive can't be blank.")
+
+ click_link 'Cancel'
+ end
+ end
+
+ aggregate_failures 'OAuth Client' do
+ accept_confirm do
+ find_test_selector('storage-edit-oauth-client-button').click
+ end
+
+ within_test_selector('storage-oauth-client-form') do
+ # With null values, submit button should be disabled
+ expect(page).to have_css('#oauth_client_client_id', value: '')
+ expect(page).to have_css('#oauth_client_client_secret', value: '')
+ expect(find_test_selector('storage-oauth-client-submit-button')).to be_disabled
+
+ # Happy path - Submit valid values
+ fill_in 'oauth_client_client_id', with: '1234567890'
+ fill_in 'oauth_client_client_secret', with: '0987654321'
+ expect(find_test_selector('storage-oauth-client-submit-button')).not_to be_disabled
+ click_button 'Save and continue'
+ end
+
+ expect(page).to have_test_selector('label-storage_oauth_client_configured-status', text: 'Completed')
+ expect(page).to have_test_selector('storage-oauth-client-id-description', text: "OAuth Client ID: 1234567890")
+ end
+ end
end
end
diff --git a/modules/storages/spec/models/nextcloud_storage_spec.rb b/modules/storages/spec/models/nextcloud_storage_spec.rb
index 25a03819a16..7996b84c89f 100644
--- a/modules/storages/spec/models/nextcloud_storage_spec.rb
+++ b/modules/storages/spec/models/nextcloud_storage_spec.rb
@@ -180,6 +180,31 @@ RSpec.describe Storages::NextcloudStorage do
it_behaves_like 'a stored boolean attribute', :automatically_managed
end
+ describe '#automatic_management_new_record?' do
+ context 'when automatic management has just been specified but not yet persisted' do
+ let(:storage) { build_stubbed(:nextcloud_storage, provider_fields: {}) }
+
+ before { storage.automatically_managed = false }
+
+ it { expect(storage).to be_provider_fields_changed }
+ it { expect(storage).to be_automatic_management_new_record }
+ end
+
+ context 'when automatic management was already specified' do
+ let(:storage) { build_stubbed(:nextcloud_storage, :as_not_automatically_managed) }
+
+ it { expect(storage).not_to be_provider_fields_changed }
+ it { expect(storage).not_to be_automatic_management_new_record }
+ end
+
+ context 'when automatic management is unspecified' do
+ let(:storage) { build_stubbed(:nextcloud_storage, provider_fields: {}) }
+
+ it { expect(storage).not_to be_provider_fields_changed }
+ it { expect(storage).to be_automatic_management_new_record }
+ end
+ end
+
describe '#automatic_management_unspecified?' do
context 'when automatically_managed is nil' do
let(:storage) { build(:nextcloud_storage, automatically_managed: nil) }