Merge pull request #3550 from oliverguenther/feature/user_preference
Feature/user preferencepull/3639/head
commit
19c317e866
@ -0,0 +1,65 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 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-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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require_dependency 'api/v3/user_preferences/user_preferences_representer' |
||||
|
||||
module API |
||||
module V3 |
||||
module UserPreferences |
||||
class UserPreferencesAPI < ::API::OpenProjectAPI |
||||
resource :my_preferences do |
||||
helpers do |
||||
def represent_preferences |
||||
UserPreferencesRepresenter.new(@preferences, current_user: current_user) |
||||
end |
||||
end |
||||
|
||||
before do |
||||
fail ::API::Errors::Unauthenticated unless current_user.logged? |
||||
@preferences = current_user.pref |
||||
end |
||||
|
||||
get do |
||||
represent_preferences |
||||
end |
||||
|
||||
patch do |
||||
representer = represent_preferences |
||||
representer.from_hash(request_body) |
||||
|
||||
if @preferences.save |
||||
representer |
||||
else |
||||
raise ::API::Errors::ErrorBase.create_and_merge_errors(@preferences.errors) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,76 @@ |
||||
#-- encoding: UTF-8 |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 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-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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'roar/decorator' |
||||
require 'roar/json/hal' |
||||
|
||||
module API |
||||
module V3 |
||||
module UserPreferences |
||||
class UserPreferencesRepresenter < ::API::Decorators::Single |
||||
link :self do |
||||
{ |
||||
href: api_v3_paths.my_preferences |
||||
} |
||||
end |
||||
|
||||
link :user do |
||||
{ |
||||
href: api_v3_paths.user(represented.user.id), |
||||
title: represented.user.name |
||||
} |
||||
end |
||||
|
||||
link :updateImmediately do |
||||
{ |
||||
href: api_v3_paths.my_preferences, |
||||
method: :patch |
||||
} |
||||
end |
||||
|
||||
property :hide_mail |
||||
property :time_zone, |
||||
getter: -> (*) { canonical_time_zone }, |
||||
render_nil: true |
||||
|
||||
property :theme |
||||
property :warn_on_leaving_unsaved |
||||
property :comments_in_reverse_order, |
||||
as: :commentSortDescending |
||||
|
||||
property :impaired?, |
||||
as: :accessibilityMode |
||||
|
||||
def _type |
||||
'UserPreferences' |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,103 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 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-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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
|
||||
describe ::API::V3::UserPreferences::UserPreferencesRepresenter do |
||||
include ::API::V3::Utilities::PathHelper |
||||
|
||||
let(:preference) { FactoryGirl.build(:user_preference) } |
||||
let(:user) { FactoryGirl.build_stubbed(:user, preference: preference) } |
||||
let(:representer) { described_class.new(preference, current_user: user) } |
||||
|
||||
before do |
||||
allow(preference).to receive(:user).and_return(user) |
||||
end |
||||
|
||||
context 'generation' do |
||||
subject(:generated) { representer.to_json } |
||||
|
||||
it { is_expected.to include_json('UserPreferences'.to_json).at_path('_type') } |
||||
it { is_expected.to have_json_path('hideMail') } |
||||
it { is_expected.to have_json_path('timeZone') } |
||||
it { is_expected.to have_json_path('theme') } |
||||
it { is_expected.to have_json_path('commentSortDescending') } |
||||
it { is_expected.to have_json_path('warnOnLeavingUnsaved') } |
||||
it { is_expected.to have_json_path('accessibilityMode') } |
||||
|
||||
describe 'timeZone' do |
||||
context 'no time zone set' do |
||||
let(:preference) { FactoryGirl.build(:user_preference, time_zone: '') } |
||||
|
||||
it 'shows the timeZone as nil' do |
||||
is_expected.to be_json_eql(nil.to_json).at_path('timeZone') |
||||
end |
||||
end |
||||
|
||||
context 'short timezone set' do |
||||
let(:preference) { FactoryGirl.build(:user_preference, time_zone: 'Berlin') } |
||||
|
||||
it 'shows the canonical time zone' do |
||||
is_expected.to be_json_eql('Europe/Berlin'.to_json).at_path('timeZone') |
||||
end |
||||
end |
||||
|
||||
context 'canonical timezone set' do |
||||
let(:preference) { FactoryGirl.build(:user_preference, time_zone: 'Europe/Paris') } |
||||
|
||||
it 'shows the canonical time zone' do |
||||
is_expected.to be_json_eql('Europe/Paris'.to_json).at_path('timeZone') |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe '_links' do |
||||
it_behaves_like 'has an untitled link' do |
||||
let(:link) { 'self' } |
||||
let(:href) { api_v3_paths.my_preferences } |
||||
end |
||||
|
||||
it_behaves_like 'has a titled link' do |
||||
let(:link) { 'user' } |
||||
let(:title) { user.name } |
||||
let(:href) { api_v3_paths.user(user.id) } |
||||
end |
||||
|
||||
describe 'immediate update' do |
||||
it_behaves_like 'has an untitled link' do |
||||
let(:link) { 'updateImmediately' } |
||||
let(:href) { api_v3_paths.my_preferences } |
||||
end |
||||
|
||||
it 'is a patch link' do |
||||
is_expected.to be_json_eql('patch'.to_json).at_path('_links/updateImmediately/method') |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,141 @@ |
||||
#-- copyright |
||||
# OpenProject is a project management system. |
||||
# Copyright (C) 2012-2015 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-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 doc/COPYRIGHT.rdoc for more details. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
require 'rack/test' |
||||
|
||||
describe 'API v3 UserPreferences resource', type: :request do |
||||
include Rack::Test::Methods |
||||
include ::API::V3::Utilities::PathHelper |
||||
|
||||
let(:user) { FactoryGirl.create(:user) } |
||||
let(:preference) { FactoryGirl.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 } |
||||
|
||||
before do |
||||
allow(User).to receive(:current).and_return user |
||||
allow(User).to receive(:preference).and_return preference |
||||
end |
||||
|
||||
describe '#GET' do |
||||
before do |
||||
get preference_path |
||||
end |
||||
|
||||
context 'when not logged in' do |
||||
let(:user) { User.anonymous } |
||||
it 'should respond with 401' do |
||||
expect(subject.status).to eq(401) |
||||
end |
||||
end |
||||
|
||||
context 'when logged in' 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 |
||||
end |
||||
end |
||||
|
||||
describe '#PATCH' do |
||||
before do |
||||
patch preference_path, params.to_json, 'CONTENT_TYPE' => 'application/json' |
||||
preference.reload |
||||
end |
||||
|
||||
context 'when not logged in' do |
||||
let(:user) { User.anonymous } |
||||
let(:params) do |
||||
{ whatever: true } |
||||
end |
||||
it 'should respond with 401' do |
||||
expect(subject.status).to eq(401) |
||||
end |
||||
end |
||||
|
||||
describe 'theme' do |
||||
context 'with invalid identifier' do |
||||
let(:params) do |
||||
{ theme: 'mycoolthemethatisnotavailableyet' } |
||||
end |
||||
|
||||
it_behaves_like 'constraint violation' do |
||||
let(:message) { 'Theme is not set to one of the allowed values.' } |
||||
end |
||||
end |
||||
|
||||
context 'with correct identifier' do |
||||
let(:params) do |
||||
{ theme: 'default' } |
||||
end |
||||
it 'should respond with a UserPreferences representer' do |
||||
expect(subject.body).to be_json_eql(:default.to_json).at_path('theme') |
||||
expect(preference.theme).to eq(:default) |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe 'timezone' do |
||||
context 'with invalid timezone' do |
||||
let(:params) do |
||||
{ timeZone: 'Europe/Awesomeland' } |
||||
end |
||||
|
||||
it_behaves_like 'constraint violation' do |
||||
let(:message) { 'Time zone is not set to one of the allowed values.' } |
||||
end |
||||
end |
||||
|
||||
context 'with full time zone' do |
||||
let(:params) do |
||||
{ timeZone: 'Europe/Paris' } |
||||
end |
||||
it 'should respond with a UserPreferences representer' do |
||||
expect(subject.body).to be_json_eql('Europe/Paris'.to_json).at_path('timeZone') |
||||
expect(preference.time_zone).to eq('Europe/Paris') |
||||
end |
||||
end |
||||
|
||||
context 'with short time zone' do |
||||
let(:params) do |
||||
{ timeZone: 'Hawaii' } |
||||
end |
||||
|
||||
it 'should respond with a UserPreferences representer' do |
||||
expect(subject.body).to be_json_eql('Pacific/Honolulu'.to_json).at_path('timeZone') |
||||
expect(preference.time_zone).to eq('Hawaii') |
||||
expect(preference.canonical_time_zone).to eq('Pacific/Honolulu') |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue