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

embed UserPreferences in Configuration resource

ulferts3edab0d605ef
diff --git a/docs/api/apiv3/endpoints/configuration.apib b/docs/api/apiv3/endpoints/configuration.apib
index b30dcdb6fae..15da51dd08b 100644
--- a/docs/api/apiv3/endpoints/configuration.apib
+++ b/docs/api/apiv3/endpoints/configuration.apib
@@ -5,9 +5,13 @@ Note that there is no 1:1 relationship between this endpoint and the settings yo
For now this endpoint will only allow access to settings deemed useful for a client to know in general.
-| Link | Description | Type | Nullable | Supported operations |
-|:-------------------:| ------------------------------------------------ | ------------- | -------- | -------------------- |
-| self | The configuration | Configuration | | READ |
+As clients might rely on the combination of both, the system settings as well as the current user's preferences, the resource
+embeds the current user's preferences so client can fetch both with one request.
+
+| Link | Description | Type | Nullable | Supported operations |
+|:-------------------:| ------------------------------------------------ | ------------- | -------- | -------------------- |
+| self | The configuration | Configuration | | READ |
+| userPreferences | The preferences of the current user | UserPreferences | | READ |
## Local Properties
| Property | Description | Type | Condition | Supported operations |
@@ -23,7 +27,12 @@ For now this endpoint will only allow access to settings deemed useful for a cli
{
"_type": "Configuration",
"_links": {
- "href": "/api/v3/configuration"
+ "self": {
+ "href": "/api/v3/configuration"
+ },
+ "userPreferences": {
+ "href": "/api/v3/my_preferences"
+ }
},
"maximumAttachmentFileSize": 5242880,
"perPageOptions": [1, 10, 100]
diff --git a/lib/api/v3/configuration/configuration_api.rb b/lib/api/v3/configuration/configuration_api.rb
index 9e4558bf7b5..3b384a52cf5 100644
--- a/lib/api/v3/configuration/configuration_api.rb
+++ b/lib/api/v3/configuration/configuration_api.rb
@@ -34,7 +34,7 @@ module API
class ConfigurationAPI < ::API::OpenProjectAPI
resources :configuration do
get do
- ConfigurationRepresenter.new(Setting, current_user: current_user)
+ ConfigurationRepresenter.new(Setting, current_user: current_user, embed_links: true)
end
end
end
diff --git a/lib/api/v3/configuration/configuration_representer.rb b/lib/api/v3/configuration/configuration_representer.rb
index 25c51f65831..afdfbc65803 100644
--- a/lib/api/v3/configuration/configuration_representer.rb
+++ b/lib/api/v3/configuration/configuration_representer.rb
@@ -40,15 +40,33 @@ module API
}
end
+ link :userPreferences do
+ {
+ href: api_v3_paths.my_preferences
+ }
+ end
+
property :maximum_attachment_file_size,
getter: ->(*) { attachment_max_size.to_i.kilobyte }
property :per_page_options,
getter: ->(*) { per_page_options_array }
+ property :user_preferences,
+ embedded: true,
+ exec_context: :decorator,
+ if: ->(*) {
+ embed_links
+ }
+
def _type
'Configuration'
end
+
+ def user_preferences
+ UserPreferences::UserPreferencesRepresenter.new(current_user.pref,
+ current_user: current_user)
+ end
end
end
end
diff --git a/lib/api/v3/user_preferences/user_preferences_api.rb b/lib/api/v3/user_preferences/user_preferences_api.rb
index 2bf25dd2f42..54f2e0571e2 100644
--- a/lib/api/v3/user_preferences/user_preferences_api.rb
+++ b/lib/api/v3/user_preferences/user_preferences_api.rb
@@ -40,7 +40,6 @@ module API
end
after_validation do
- fail ::API::Errors::Unauthenticated unless current_user.logged?
@preferences = current_user.pref
end
@@ -49,6 +48,8 @@ module API
end
patch do
+ fail ::API::Errors::Unauthenticated unless current_user.logged?
+
representer = represent_preferences
representer.from_hash(request_body)
diff --git a/lib/api/v3/user_preferences/user_preferences_representer.rb b/lib/api/v3/user_preferences/user_preferences_representer.rb
index 5735d37a50c..b4f07f44771 100644
--- a/lib/api/v3/user_preferences/user_preferences_representer.rb
+++ b/lib/api/v3/user_preferences/user_preferences_representer.rb
@@ -56,7 +56,7 @@ module API
property :hide_mail
property :time_zone,
- getter: -> (*) { canonical_time_zone },
+ getter: ->(*) { canonical_time_zone },
render_nil: true
property :warn_on_leaving_unsaved
diff --git a/spec/lib/api/v3/configuration/configuration_representer_spec.rb b/spec/lib/api/v3/configuration/configuration_representer_spec.rb
index 02336fb2f34..41e05976122 100644
--- a/spec/lib/api/v3/configuration/configuration_representer_spec.rb
+++ b/spec/lib/api/v3/configuration/configuration_representer_spec.rb
@@ -32,8 +32,17 @@ describe ::API::V3::Configuration::ConfigurationRepresenter do
include ::API::V3::Utilities::PathHelper
let(:represented) { Setting }
- let(:current_user) { FactoryBot.build_stubbed(:user) }
- let(:representer) { described_class.new(represented, current_user: current_user) }
+ let(:current_user) do
+ FactoryBot.build_stubbed(:user).tap do |user|
+ allow(user)
+ .to receive(:preference)
+ .and_return(FactoryBot.build_stubbed(:user_preference))
+ end
+ end
+ let(:embed_links) { false }
+ let(:representer) do
+ described_class.new(represented, current_user: current_user, embed_links: embed_links)
+ end
context 'generation' do
subject { representer.to_json }
@@ -43,6 +52,24 @@ describe ::API::V3::Configuration::ConfigurationRepresenter do
let(:link) { 'self' }
let(:href) { api_v3_paths.configuration }
end
+
+ context 'userPreferences' do
+ context 'if logged in' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'userPreferences' }
+ let(:href) { api_v3_paths.my_preferences }
+ end
+ end
+
+ context 'if not logged in' do
+ let(:current_user) { FactoryBot.build_stubbed(:anonymous) }
+
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'userPreferences' }
+ let(:href) { api_v3_paths.my_preferences }
+ end
+ end
+ end
end
it 'indicates its type' do
@@ -58,5 +85,26 @@ describe ::API::V3::Configuration::ConfigurationRepresenter do
allow(Setting).to receive(:per_page_options).and_return('1, 50 , 100 ')
is_expected.to be_json_eql([1, 50, 100].to_json).at_path('perPageOptions')
end
+
+ describe '_embedded' do
+ context 'userPreferences' do
+ context 'if embedding' do
+ let(:embed_links) { true }
+
+ it 'embedds the user preferences' do
+ is_expected
+ .to be_json_eql('UserPreferences'.to_json)
+ .at_path('_embedded/userPreferences/_type')
+ end
+ end
+
+ context 'if not embedding' do
+ it 'embedds the user preferences' do
+ is_expected
+ .not_to have_json_path('_embedded/userPreferences/_type')
+ end
+ end
+ end
+ end
end
end
diff --git a/spec/requests/api/v3/configuration_resource_spec.rb b/spec/requests/api/v3/configuration_resource_spec.rb
new file mode 100644
index 00000000000..85e35fdbbc7
--- /dev/null
+++ b/spec/requests/api/v3/configuration_resource_spec.rb
@@ -0,0 +1,80 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+require 'rack/test'
+
+describe 'API v3 Configuration resource', type: :request do
+ include Rack::Test::Methods
+ include ::API::V3::Utilities::PathHelper
+
+ let(:user) { FactoryBot.create(:user) }
+ let(:configuration_path) { api_v3_paths.configuration }
+ subject(:response) { last_response }
+
+ before do
+ login_as(user)
+ end
+
+ describe '#GET' do
+ before do
+ get configuration_path
+ end
+
+ it 'returns 200 OK' do
+ expect(subject.status).to eq(200)
+ end
+
+ it 'returns the configuration', with_settings: { per_page_options: '3, 5, 8, 13' } do
+ expect(subject.body)
+ .to be_json_eql('Configuration'.to_json)
+ .at_path("_type")
+
+ expect(subject.body)
+ .to be_json_eql([3, 5, 8, 13].to_json)
+ .at_path('perPageOptions')
+ end
+
+ it 'embedds the current user preferences' do
+ expect(subject.body)
+ .to be_json_eql('UserPreferences'.to_json)
+ .at_path('_embedded/userPreferences/_type')
+ end
+
+ context 'for a non logged in user' do
+ it 'returns 200 OK' do
+ expect(subject.status).to eq(200)
+ end
+ end
+
+ it 'does not embed the preferences' do
+ expect(subject.body)
+ .not_to have_json_path('_embedded/user_preferences')
+ end
+ end
+end
diff --git a/spec/requests/api/v3/user_preferences/user_preferences_resource_spec.rb b/spec/requests/api/v3/user_preferences/user_preferences_resource_spec.rb
index 0f04639013c..7e5d48d1f6c 100644
--- a/spec/requests/api/v3/user_preferences/user_preferences_resource_spec.rb
+++ b/spec/requests/api/v3/user_preferences/user_preferences_resource_spec.rb
@@ -35,7 +35,6 @@ describe 'API v3 UserPreferences resource', type: :request do
let(:user) { FactoryBot.create(:user) }
let(:preference) { FactoryBot.create(:user_preference, user: user) }
- let(:representer) { described_class.new(preference, current_user: user) }
let(:preference_path) { api_v3_paths.my_preferences }
subject(:response) { last_response }
@@ -51,8 +50,13 @@ describe 'API v3 UserPreferences resource', type: :request do
context 'when not logged in' do
let(:user) { User.anonymous }
- it 'should respond with 401' do
- expect(subject.status).to eq(401)
+
+ it 'should respond with 200' do
+ expect(subject.status).to eq(200)
+ end
+
+ it 'should respond with a UserPreferences representer' do
+ expect(subject.body).to be_json_eql('UserPreferences'.to_json).at_path('_type')
end
end
@@ -60,6 +64,7 @@ describe 'API v3 UserPreferences resource', type: :request do
it 'should respond with 200' do
expect(subject.status).to eq(200)
end
+
it 'should respond with a UserPreferences representer' do
expect(subject.body).to be_json_eql('UserPreferences'.to_json).at_path('_type')
end