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

accept HH:MM as valid times for the reminders

ulferts57cfa51d190f
diff --git a/.rubocop.yml b/.rubocop.yml
index 41016453f22..fc623bc2bf6 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -126,6 +126,11 @@ Rails/I18nLocaleAssignment:
Exclude:
- 'spec/**/*.rb'
+# The http verbs in Rack::Test do not accept named parameters (params: params)
+Rails/HttpPositionalArguments:
+ Exclude:
+ - 'spec/requests/api/v3/**/*.rb'
+
# For feature specs, we tend to have longer specs that cover a larger part of the functionality.
# This is done for multiple reasons:
# * performance, as setting up integration tests is costly
diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb
index 8b07444f306..a55b46e9582 100644
--- a/app/models/user_preference.rb
+++ b/app/models/user_preference.rb
@@ -33,8 +33,10 @@ class UserPreference < ApplicationRecord
delegate :notification_settings, to: :user
serialize :settings, ::Serializers::IndifferentHashSerializer
- validates_presence_of :user
- validate :time_zone_correctness, if: -> { time_zone.present? }
+ validates :user,
+ presence: true
+ validate :time_zone_correctness,
+ if: -> { time_zone.present? }
##
# Retrieve keys from settings, and allow accessing
diff --git a/lib/api/v3/user_preferences/user_preference_representer.rb b/lib/api/v3/user_preferences/user_preference_representer.rb
index 6c2b9f68569..d3363112929 100644
--- a/lib/api/v3/user_preferences/user_preference_representer.rb
+++ b/lib/api/v3/user_preferences/user_preference_representer.rb
@@ -65,7 +65,19 @@ module API
as: :commentSortDescending
property :auto_hide_popups
- property :daily_reminders
+ # Also accept times in the format of HH:MM which are also valid according to ISO8601
+ # and have those as the only times in the resource.
+ property :daily_reminders,
+ getter: ->(*) do
+ reminders = daily_reminders.dup
+ reminders['times'].map! { |time| time.gsub(/\A(\d{2}:\d{2}).*\z/, '\1') } if reminders
+ reminders
+ end,
+ setter: ->(fragment:, **) do
+ self.daily_reminders = fragment
+
+ daily_reminders['times'].map! { |time| time.gsub(/\A(\d{2}:\d{2})\z/, '\1:00+00:00') }
+ end
property :notification_settings,
as: :notifications,
diff --git a/spec/lib/api/v3/user_preferences/user_preference_representer_parsing_spec.rb b/spec/lib/api/v3/user_preferences/user_preference_representer_parsing_spec.rb
index 53f494f5367..259a65f630a 100644
--- a/spec/lib/api/v3/user_preferences/user_preference_representer_parsing_spec.rb
+++ b/spec/lib/api/v3/user_preferences/user_preference_representer_parsing_spec.rb
@@ -30,14 +30,14 @@ require 'spec_helper'
describe ::API::V3::UserPreferences::UserPreferenceRepresenter,
'parsing' do
+ subject(:parsed) { representer.from_hash request_body }
+
include ::API::V3::Utilities::PathHelper
let(:preference) { OpenStruct.new }
let(:user) { FactoryBot.build_stubbed(:user) }
let(:representer) { described_class.new(preference, current_user: user) }
- subject { representer.from_hash request_body }
-
describe 'notification_settings' do
let(:request_body) do
{
@@ -60,7 +60,7 @@ describe ::API::V3::UserPreferences::UserPreferenceRepresenter,
'href' => nil
}
}
- },
+ }
]
}
end
@@ -80,4 +80,23 @@ describe ::API::V3::UserPreferences::UserPreferenceRepresenter,
expect(global[:mentioned]).to eq true
end
end
+
+ describe 'daily_reminders' do
+ let(:request_body) do
+ {
+ "dailyReminders" => {
+ "enabled" => true,
+ "times" => %w[07:00 15:00 18:00:00+00:00]
+ }
+ }
+ end
+
+ it 'parses the times into full iso8601 time format' do
+ expect(parsed.daily_reminders)
+ .to eql({
+ "enabled" => true,
+ "times" => %w[07:00:00+00:00 15:00:00+00:00 18:00:00+00:00]
+ })
+ end
+ end
end
diff --git a/spec/lib/api/v3/user_preferences/user_preference_representer_rendering_spec.rb b/spec/lib/api/v3/user_preferences/user_preference_representer_rendering_spec.rb
index 50ae87395cf..daa1f6d07c9 100644
--- a/spec/lib/api/v3/user_preferences/user_preference_representer_rendering_spec.rb
+++ b/spec/lib/api/v3/user_preferences/user_preference_representer_rendering_spec.rb
@@ -32,7 +32,15 @@ describe ::API::V3::UserPreferences::UserPreferenceRepresenter,
'rendering' do
include ::API::V3::Utilities::PathHelper
- let(:preference) { FactoryBot.build(:user_preference) }
+ let(:preference) do
+ FactoryBot.build_stubbed(:user_preference,
+ settings: {
+ "daily_reminders" => {
+ "enabled" => true,
+ "times" => %w[07:00:00+00:00 15:00:00+00:00]
+ }
+ })
+ end
let(:notification_setting) { FactoryBot.build(:notification_setting) }
let(:user) { FactoryBot.build_stubbed(:user, preference: preference) }
let(:representer) { described_class.new(preference, current_user: user) }
@@ -44,7 +52,6 @@ describe ::API::V3::UserPreferences::UserPreferenceRepresenter,
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('commentSortDescending') }
@@ -89,6 +96,25 @@ describe ::API::V3::UserPreferences::UserPreferenceRepresenter,
end
end
+ describe 'properties' do
+ describe '_type' do
+ it_behaves_like 'property', :_type do
+ let(:value) { 'UserPreferences' }
+ end
+ end
+
+ describe 'dailyReminders' do
+ it_behaves_like 'property', :dailyReminders do
+ let(:value) do
+ {
+ "enabled" => true,
+ "times" => %w[07:00 15:00]
+ }
+ end
+ end
+ end
+ end
+
describe '_links' do
it_behaves_like 'has an untitled link' do
let(:link) { 'self' }
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 134bfcc4395..bec22395d5a 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
@@ -29,14 +29,22 @@
require 'spec_helper'
require 'rack/test'
-describe 'API v3 UserPreferences resource', type: :request do
+describe 'API v3 UserPreferences resource', type: :request, content_type: :json do
include Rack::Test::Methods
include ::API::V3::Utilities::PathHelper
subject(:response) { last_response }
let(:user) { FactoryBot.create(:user, preference: preference) }
- let(:preference) { FactoryBot.create(:user_preference) }
+ let(:preference) do
+ FactoryBot.create(:user_preference,
+ settings: {
+ daily_reminders: {
+ enabled: false,
+ times: %w[07:00:00+00:00 15:00:00+00:00]
+ }
+ })
+ end
let(:preference_path) { api_v3_paths.user_preferences(user.id) }
current_user { user }
@@ -49,21 +57,21 @@ describe 'API v3 UserPreferences resource', type: :request do
context 'when not logged in' do
let(:user) { User.anonymous }
- it 'should respond with 200' do
+ it 'responds with 200' do
expect(subject.status).to eq(200)
end
- it 'should respond with a UserPreferences representer' do
+ it 'responds with a UserPreferences representer' do
expect(subject.body).to be_json_eql('UserPreferences'.to_json).at_path('_type')
end
end
context 'when logged in' do
- it 'should respond with 200' do
+ it 'responds with 200' do
expect(subject.status).to eq(200)
end
- it 'should respond with a UserPreferences representer' do
+ it 'responds with a UserPreferences representer' do
expect(subject.body).to be_json_eql('UserPreferences'.to_json).at_path('_type')
end
end
@@ -71,7 +79,7 @@ describe 'API v3 UserPreferences resource', type: :request do
describe '#PATCH' do
before do
- patch preference_path, params.to_json, 'CONTENT_TYPE' => 'application/json'
+ patch preference_path, params.to_json
preference.reload
end
@@ -81,12 +89,61 @@ describe 'API v3 UserPreferences resource', type: :request do
{ whatever: true }
end
- it 'should respond with 401' do
+ it 'responds with 401' do
expect(subject.status).to eq(401)
end
end
- describe 'timezone' do
+ context 'when updating the email_reminder settings' do
+ let(:params) do
+ {
+ dailyReminders: {
+ enabled: true,
+ times: %w[08:00 18:00]
+ }
+ }
+ end
+
+ it 'responds with 200' do
+ expect(subject.status)
+ .to eq(200)
+ end
+
+ it 'updates the time zone' do
+ expect(preference.daily_reminders)
+ .to eql("enabled" => true, "times" => %w[08:00:00+00:00 18:00:00+00:00])
+ end
+ end
+
+ context 'when attempting to update the email_reminder settings with invalid values' do
+ let(:params) do
+ {
+ dailyReminders: {
+ enabled: true,
+ times: %w[invalid 18:00:00+00:00]
+ }
+ }
+ end
+
+ it 'responds with 422 and explains the error' do
+ expect(subject.status).to eq(422)
+
+ expect(subject.body)
+ .to be_json_eql("Daily reminders does not match the expected format 'time' at path 'times/0'.".to_json)
+ .at_path('message')
+
+ expect(subject.body)
+ .to be_json_eql("dailyReminders".to_json)
+ .at_path('_embedded/details/attribute')
+ end
+
+ it 'keeps the previous values' do
+ expect(preference.daily_reminders)
+ .to eql("enabled" => false, "times" => %w[07:00:00+00:00 15:00:00+00:00])
+ end
+ end
+
+ context 'when updating the timezone' do
context 'with invalid timezone' do
let(:params) do
{ timeZone: 'Europe/Awesomeland' }
@@ -101,7 +158,8 @@ describe 'API v3 UserPreferences resource', type: :request do
let(:params) do
{ timeZone: 'Europe/Paris' }
end
- it 'should respond with a UserPreferences representer' do
+
+ it 'responds 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
@@ -112,7 +170,7 @@ describe 'API v3 UserPreferences resource', type: :request do
{ timeZone: 'Hawaii' }
end
- it 'should respond with a UserPreferences representer' do
+ it 'responds 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')