Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix option to only allow users to edit their profile within first 24 hours #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Finally, run the test suite to verify that everything is working correctly:
```
$ rails test
```

If the test suite passes, you’ll be ready to seed the database with sample users and run the app in a local server:

```
Expand Down
10 changes: 7 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ def user_params
# Before filters

# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url, status: :see_other) unless current_user?(@user)
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
Loading