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

Always override logged_by with whoever logged

Oliver Güntherd00d7bfa77a2
diff --git a/modules/costs/app/services/time_entries/set_attributes_service.rb b/modules/costs/app/services/time_entries/set_attributes_service.rb
index 0d787b8d6f3..c273c2fc7ea 100644
--- a/modules/costs/app/services/time_entries/set_attributes_service.rb
+++ b/modules/costs/app/services/time_entries/set_attributes_service.rb
@@ -38,14 +38,15 @@ module TimeEntries
if no_project_or_context_changed?
model.project = model.work_package&.project
end
+
+ # Always set the logging user as logged_by
+ set_logged_by
end
- def set_default_attributes(attributes)
+ def set_default_attributes(*)
set_default_user
set_default_activity
set_default_hours
-
- set_logged_by unless attributes[:logged_by]
end
def set_logged_by
diff --git a/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb b/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb
index 12b678d2bc7..790e5c170e7 100644
--- a/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb
+++ b/modules/costs/spec/services/time_entries/set_attributes_service_spec.rb
@@ -34,11 +34,11 @@ describe TimeEntries::SetAttributesService, type: :model do
let!(:default_activity) { build_stubbed(:time_entry_activity, project:, is_default: true) }
let(:work_package) { build_stubbed(:work_package) }
let(:project) { build_stubbed(:project) }
- let(:spent_on) { Date.today.to_s }
+ let(:spent_on) { Time.zone.today.to_s }
let(:hours) { 5.0 }
let(:comments) { 'some comment' }
let(:contract_instance) do
- contract = double('contract_instance')
+ contract = double('contract_instance') # rubocop:disable RSpec/VerifiedDoubles
allow(contract)
.to receive(:validate)
.and_return(contract_valid)
@@ -48,7 +48,7 @@ describe TimeEntries::SetAttributesService, type: :model do
contract
end
- let(:contract_errors) { double('contract_errors') }
+ let(:contract_errors) { double('contract_errors') } # rubocop:disable RSpec/VerifiedDoubles
let(:contract_valid) { true }
let(:time_entry_valid) { true }
@@ -178,11 +178,29 @@ describe TimeEntries::SetAttributesService, type: :model do
end
end
+ context 'with another user setting logged by' do
+ let(:other_user) { create :user }
+ let(:time_entry_instance) { create :time_entry, user: other_user, logged_by: other_user, hours: 1 }
+
+ let(:params) do
+ {
+ hours: 1234
+ }
+ end
+
+ it 'updates the entry, and updates the logged by' do
+ expect { subject }
+ .to change(time_entry_instance, :hours).from(1).to(1234)
+ .and change(time_entry_instance, :logged_by).from(other_user).to(user)
+
+ expect(time_entry_instance.user).to eq other_user
+ end
+ end
+
context 'with an invalid contract' do
let(:contract_valid) { false }
let(:expect_time_instance_save) do
- expect(time_entry_instance)
- .not_to receive(:save)
+ expect(time_entry_instance).not_to receive(:save) # rubocop:disable RSpec/MessageSpies
end
it 'returns failure' do