mirror of
https://github.com/rickbarrette/stopwatch.git
synced 2026-11-17 19:50:36 -05:00
Timer safety: length limit, server-side saving, stop on close
- Maximum timer length setting (default 10h): a timer that runs past it is stopped at the limit and its entry's comment is flagged, so a forgotten timer can't add a night or weekend of hours - StopwatchTimerTickJob saves a running timer's time every minute on the server (no browser needed) and stops it at the limit on time; open tabs keep saving too - Timer updates lock the user's preference row and re-read the timer, so simultaneous saves (several tabs, the job) never add time twice - Closing an issue stops timers running on it before anything else (e.g. billing) uses its time; deleting a time entry only stops the timer running on that entry - Issue timers require a visible issue; starting again moves the timer instead of raising "timer is already running" - Tests updated and extended (limit, locking, tick job, close, delete, visibility, issue context menu); CI runs on Redmine 7.0 - Upstream's German notes translated Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
15 files changed
+366
-36
No files matched your search
@@ -73,6 +73,19 @@ class TicketTimerTest < Redmine::IntegrationTest
|
||||
assert_not_running
|
||||
end
|
||||
|
||||
test "should not start a timer on an issue the user cannot see" do
|
||||
log_user 'jsmith', 'jsmith'
|
||||
post "/issues/999999/timer/start", xhr: true
|
||||
assert_response 404
|
||||
end
|
||||
|
||||
test "should offer start tracking in the issue context menu" do
|
||||
log_user 'jsmith', 'jsmith'
|
||||
get "/issues/context_menu", params: { ids: [@issue.id] }, xhr: true
|
||||
assert_response :success
|
||||
assert_select "a.stopwatch_issue_timer", text: /start tracking/i
|
||||
end
|
||||
|
||||
test "should ask by default" do
|
||||
log_user 'jsmith', 'jsmith'
|
||||
TimeEntry.delete_all
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
class IssueCloseTest < ActiveSupport::TestCase
|
||||
fixtures :projects, :users, :user_preferences, :roles, :members, :member_roles,
|
||||
:trackers, :projects_trackers, :enabled_modules, :issue_statuses, :issues,
|
||||
:enumerations, :time_entries
|
||||
|
||||
setup do
|
||||
@user = User.find 2
|
||||
@issue = Issue.find 1
|
||||
@time_entry = TimeEntry.create!(project: @issue.project, issue: @issue, user: @user, author: @user,
|
||||
spent_on: Date.today, hours: 1, activity: TimeEntryActivity.first)
|
||||
@timer = Stopwatch::Timer.new(@user)
|
||||
@timer.start @time_entry
|
||||
data = @timer.send(:data)
|
||||
data[:started_at] = 1.hour.ago.to_i
|
||||
@timer.save
|
||||
end
|
||||
|
||||
test "closing an issue stops a timer running on it and saves the time" do
|
||||
@issue.status = IssueStatus.where(is_closed: true).first
|
||||
@issue.save!
|
||||
|
||||
refute Stopwatch::Timer.new(User.find(@user.id)).running?
|
||||
assert_in_delta 2, @time_entry.reload.hours, 0.01
|
||||
end
|
||||
|
||||
test "other changes to the issue leave the timer running" do
|
||||
@issue.subject = 'Renamed'
|
||||
@issue.save!
|
||||
|
||||
assert Stopwatch::Timer.new(User.find(@user.id)).running?
|
||||
end
|
||||
|
||||
test "deleting another time entry leaves the timer running" do
|
||||
other = TimeEntry.create!(project: @issue.project, issue: @issue, user: @user, author: @user,
|
||||
spent_on: Date.today, hours: 1, activity: TimeEntryActivity.first)
|
||||
other.destroy
|
||||
|
||||
assert Stopwatch::Timer.new(User.find(@user.id)).running?
|
||||
end
|
||||
end
|
||||
+120
-11
@@ -1,53 +1,68 @@
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
class TimerTest < ActiveSupport::TestCase
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
fixtures :users, :user_preferences, :time_entries
|
||||
|
||||
setup do
|
||||
@user = User.find 1
|
||||
@timer = Stopwatch::Timer.new @user
|
||||
@data = @timer.send :data
|
||||
@time_entry = TimeEntry.last
|
||||
@time_entry.update_column :comments, 'work'
|
||||
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('max_timer_hours' => '10')
|
||||
end
|
||||
|
||||
# The timer re-reads its saved state under a lock, so always look at the current data
|
||||
def data
|
||||
@timer.send :data
|
||||
end
|
||||
|
||||
# Pretend the timer was last saved `ago` seconds ago
|
||||
def started(ago)
|
||||
data[:started_at] = ago.ago.to_i
|
||||
@timer.save
|
||||
end
|
||||
|
||||
test "should know wether its running" do
|
||||
refute @timer.running?
|
||||
@data[:started_at] = 5.minutes.ago
|
||||
data[:started_at] = 5.minutes.ago
|
||||
assert @timer.running?
|
||||
end
|
||||
|
||||
test "should start timer" do
|
||||
assert_nil @data[:started_at]
|
||||
assert_nil data[:started_at]
|
||||
@timer.start @time_entry
|
||||
assert @data[:started_at].present?
|
||||
assert_equal @time_entry.id, @data[:time_entry_id]
|
||||
assert data[:started_at].present?
|
||||
assert_equal @time_entry.id, data[:time_entry_id]
|
||||
end
|
||||
|
||||
test "should stop timer and update time entry" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
@data[:started_at] = 1.hour.ago.to_i
|
||||
started 1.hour
|
||||
@timer.stop
|
||||
|
||||
@time_entry.reload
|
||||
assert_equal hours + 1, @time_entry.hours
|
||||
refute @timer.running?
|
||||
end
|
||||
|
||||
test "should update time entry" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
@data[:started_at] = 1.hour.ago.to_i
|
||||
started 1.hour
|
||||
@timer.update
|
||||
|
||||
@time_entry.reload
|
||||
assert_equal hours + 1, @time_entry.hours
|
||||
end
|
||||
assert @timer.running?
|
||||
end
|
||||
|
||||
test "should save and restore" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
@data[:started_at] = 1.hour.ago.to_i
|
||||
@timer.save
|
||||
started 1.hour
|
||||
|
||||
timer = Stopwatch::Timer.new User.find(@user.id)
|
||||
assert timer.running?
|
||||
@@ -57,6 +72,100 @@ class TimerTest < ActiveSupport::TestCase
|
||||
assert_equal hours + 1, @time_entry.hours
|
||||
end
|
||||
|
||||
test "should not add the same time twice when updated twice" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 1.hour
|
||||
|
||||
# e.g. two open tabs checking the timer at the same moment
|
||||
other_tab = Stopwatch::Timer.new User.find(@user.id)
|
||||
@timer.update
|
||||
other_tab.update
|
||||
|
||||
@time_entry.reload
|
||||
assert_in_delta hours + 1, @time_entry.hours, 0.01
|
||||
end
|
||||
|
||||
test "should stop a forgotten timer at the limit and flag the entry" do
|
||||
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('max_timer_hours' => '2')
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 60.hours # left running over a weekend
|
||||
@timer.stop
|
||||
|
||||
@time_entry.reload
|
||||
assert_equal hours + 2, @time_entry.hours
|
||||
assert_includes @time_entry.comments, 'work'
|
||||
assert_includes @time_entry.comments, '2 hour limit'
|
||||
refute @timer.running?
|
||||
end
|
||||
|
||||
test "should count earlier saves toward the limit" do
|
||||
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('max_timer_hours' => '2')
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 90.minutes
|
||||
@timer.update
|
||||
assert @timer.running?
|
||||
|
||||
started 1.hour
|
||||
@timer.update
|
||||
|
||||
@time_entry.reload
|
||||
assert_in_delta hours + 2, @time_entry.hours, 0.01
|
||||
refute @timer.running?
|
||||
end
|
||||
|
||||
test "should not limit timers when the limit is blank" do
|
||||
Setting.plugin_stopwatch = Setting.plugin_stopwatch.merge('max_timer_hours' => '')
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 30.hours
|
||||
@timer.stop
|
||||
|
||||
@time_entry.reload
|
||||
assert_in_delta hours + 30, @time_entry.hours, 0.01
|
||||
end
|
||||
|
||||
test "starting again moves the timer instead of failing" do
|
||||
other_entry = TimeEntry.where.not(id: @time_entry.id).first
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 1.hour
|
||||
|
||||
@timer.start other_entry # e.g. a double click, or starting another entry
|
||||
|
||||
assert_equal other_entry.id, data[:time_entry_id]
|
||||
assert_in_delta hours + 1, @time_entry.reload.hours, 0.01
|
||||
end
|
||||
|
||||
test "should schedule server-side saves while running" do
|
||||
assert_enqueued_with(job: StopwatchTimerTickJob) do
|
||||
@timer.start @time_entry
|
||||
end
|
||||
end
|
||||
|
||||
test "tick job saves the time and schedules the next tick" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 1.hour
|
||||
token = data[:tick_token]
|
||||
|
||||
clear_enqueued_jobs
|
||||
assert_enqueued_with(job: StopwatchTimerTickJob, args: [@user.id, token]) do
|
||||
StopwatchTimerTickJob.perform_now(@user.id, token)
|
||||
end
|
||||
assert_in_delta hours + 1, @time_entry.reload.hours, 0.01
|
||||
end
|
||||
|
||||
test "tick job from an earlier start does nothing" do
|
||||
hours = @time_entry.hours
|
||||
@timer.start @time_entry
|
||||
started 1.hour
|
||||
|
||||
clear_enqueued_jobs
|
||||
StopwatchTimerTickJob.perform_now(@user.id, 'old-token')
|
||||
assert_no_enqueued_jobs
|
||||
assert_equal hours, @time_entry.reload.hours
|
||||
end
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user