Skip to content

Commit

Permalink
Update correct_user to fix broken test and add new test for time based
Browse files Browse the repository at this point in the history
profile editing
  • Loading branch information
petecheslock committed Dec 22, 2023
1 parent 0b9f965 commit 9a32d27
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 7 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ def user_params
# Before filters

# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
if Time.now - @user.created_at > 24.hours
flash[:danger] = "You can only edit your profile within 24 hours of account creation."
end
def correct_user
@user = User.find(params[:id])
if Time.current - @user.created_at > 24.hours
flash[:danger] = "You can only edit your profile within 24 hours of account creation."
redirect_to(root_url, status: :see_other) and return
end
redirect_to(root_url, status: :see_other) unless current_user?(@user)
end

# Confirms an admin user.
def admin_user
Expand Down
17 changes: 17 additions & 0 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ def setup
assert_redirected_to root_url
end

test "should not allow the user to edit profile after 24 hours of account creation" do
@user.update(created_at: 2.days.ago)
log_in_as(@user)
get edit_user_path(@user)
assert_not flash.empty?
assert_equal "You can only edit your profile within 24 hours of account creation.", flash[:danger]
assert_redirected_to root_url
end

test "should allow the user to edit profile within 24 hours of account creation" do
@user.update(created_at: 1.hour.ago)
log_in_as(@user)
get edit_user_path(@user)
assert flash.empty?
assert_response :success
end

test "should redirect update when logged in as wrong user" do
log_in_as(@other_user)
patch user_path(@user), params: { user: { name: @user.name,
Expand Down

0 comments on commit 9a32d27

Please sign in to comment.