diff --git a/spec/flows/user_spec.cr b/spec/flows/user_spec.cr index 6e11d60..97fa99a 100644 --- a/spec/flows/user_spec.cr +++ b/spec/flows/user_spec.cr @@ -12,4 +12,19 @@ describe "Visit a user to follow them" do flow.should_have_flash("Your invite has been sent for approval!") end + + it "can update password on account page" do + old_pass = Authentic.generate_encrypted_password("password") + user = UserBox.create(&.encrypted_password(old_pass)) + flow = UserFlow.new(user) + + flow.sign_in + flow.visit_my_account + sleep(0.01) + flow.update_password("my great password") + flow.sign_out + flow.sign_in(email: user.email, password: "my great password") + + flow.should_be_signed_in + end end diff --git a/spec/support/flows/authenticated_base_flow.cr b/spec/support/flows/authenticated_base_flow.cr index 198ab1f..c3991ac 100644 --- a/spec/support/flows/authenticated_base_flow.cr +++ b/spec/support/flows/authenticated_base_flow.cr @@ -18,7 +18,6 @@ abstract class AuthenticatedBaseFlow < BaseFlow end def sign_out - visit_my_page sign_out_button.click end @@ -34,6 +33,10 @@ abstract class AuthenticatedBaseFlow < BaseFlow click "@main-follow-nav" end + def visit_my_account + click "@nav-account" + end + def should_be_signed_in sign_out_button.should be_on_page end diff --git a/spec/support/flows/user_flow.cr b/spec/support/flows/user_flow.cr index 97c1a1b..cc92391 100644 --- a/spec/support/flows/user_flow.cr +++ b/spec/support/flows/user_flow.cr @@ -6,4 +6,11 @@ class UserFlow < AuthenticatedBaseFlow def request_follow click "@request-user-follow" end + + def update_password(password : String) + fill_form PasswordResetForm, + password: password, + password_confirmation: password + click "@update-password-button" + end end diff --git a/src/actions/me/show.cr b/src/actions/me/show.cr index f949be7..b19af9c 100644 --- a/src/actions/me/show.cr +++ b/src/actions/me/show.cr @@ -1,5 +1,5 @@ class Me::Show < BrowserAction get "/me" do - render ShowPage, bits: BitQuery.from(current_user) + render ShowPage, form: PasswordResetForm.new end end diff --git a/src/actions/users/update_password.cr b/src/actions/users/update_password.cr new file mode 100644 index 0000000..d89d00b --- /dev/null +++ b/src/actions/users/update_password.cr @@ -0,0 +1,16 @@ +class Users::UpdatePassword < BrowserAction + include Auth::PasswordResets::FindUser + + post "/users/:user_id/password_update" do + PasswordResetForm.update(user, params) do |form, user| + if form.saved? + flash.success = "Your password has been updated" + redirect to: Me::Show + else + puts params.to_h + puts form.errors + render Me::ShowPage, form: form + end + end + end +end diff --git a/src/pages/main_layout.cr b/src/pages/main_layout.cr index 58065ea..b98c204 100644 --- a/src/pages/main_layout.cr +++ b/src/pages/main_layout.cr @@ -24,6 +24,7 @@ abstract class MainLayout li { link "My Bits", to: Users::Show.with(@current_user) } li { link "New Bit", to: Bits::New, flow_id: "new-bit-link" } li { link "Follow", to: Follows::Index, flow_id: "main-follow-nav" } + li { link "Account", to: Me::Show, flow_id: "nav-account" } li { link "Sign Out", to: SignIns::Delete, flow_id: "sign-out-button" } end end diff --git a/src/pages/me/show_page.cr b/src/pages/me/show_page.cr index 29a8e9e..2430e02 100644 --- a/src/pages/me/show_page.cr +++ b/src/pages/me/show_page.cr @@ -1,6 +1,18 @@ class Me::ShowPage < MainLayout + needs form : PasswordResetForm def content h3 "#{@current_user.username}" + + render_password_reset_form(@form) + end + + private def render_password_reset_form(form) + form_for Users::UpdatePassword.with(@current_user.id) do + field(form.password) { |i| password_input i, autofocus: "true" } + field(form.password_confirmation) { |i| password_input i } + + submit "Update Password", flow_id: "update-password-button" + end end end