From 19d7ca70b4f6d247539cf7b12a8742d7a42bc94e Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 1 Sep 2021 12:10:07 +0200 Subject: [PATCH 1/3] Add devise-i18n gem to handle i18nization inside devise controllers --- Gemfile | 1 + Gemfile.lock | 3 +++ 2 files changed, 4 insertions(+) diff --git a/Gemfile b/Gemfile index 0e934d38fa..e34e5b6384 100644 --- a/Gemfile +++ b/Gemfile @@ -49,6 +49,7 @@ gem 'paypal-sdk-merchant', '1.117.2' gem 'stripe' gem 'devise' +gem 'devise-i18n' gem 'devise-encryptable' gem 'devise-token_authenticatable' gem 'jwt', '~> 2.2' diff --git a/Gemfile.lock b/Gemfile.lock index c7f19c4b54..74d396ca1e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -245,6 +245,8 @@ GEM warden (~> 1.2.3) devise-encryptable (0.2.0) devise (>= 2.1.0) + devise-i18n (1.10.0) + devise (>= 4.8.0) devise-token_authenticatable (1.1.0) devise (>= 4.0.0, < 5.0.0) diff-lcs (1.4.4) @@ -714,6 +716,7 @@ DEPENDENCIES debugger-linecache devise devise-encryptable + devise-i18n devise-token_authenticatable dfc_provider! dotenv-rails From 1b7c36664e3cac8ad8f9ec9ff1d126346f38f100 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 1 Sep 2021 16:08:30 +0200 Subject: [PATCH 2/3] Add missing i18n key for Devise and update tests as well: - from feature to system - Add the reset password scenario - Add some errors scenario to the reset password scenario: token expired, token invalid, and not the same password --- config/locales/en.yml | 6 ++ .../consumer/confirm_invitation_spec.rb | 34 -------- spec/system/consumer/user_password_spec.rb | 83 +++++++++++++++++++ 3 files changed, 89 insertions(+), 34 deletions(-) delete mode 100644 spec/features/consumer/confirm_invitation_spec.rb create mode 100644 spec/system/consumer/user_password_spec.rb diff --git a/config/locales/en.yml b/config/locales/en.yml index 98f1ded47f..8ba4952577 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -26,6 +26,7 @@ en: spree/user: password: "Password" password_confirmation: "Password confirmation" + reset_password_token: Reset password token enterprise_fee: fee_type: Fee Type spree/order: @@ -61,6 +62,8 @@ en: attributes: email: taken: "There's already an account for this email. Please login or reset your password." + reset_password_token: + invalid: is invalid spree/order: no_card: There are no authorised credit cards available to charge spree/credit_card: @@ -116,6 +119,9 @@ en: community_forum_url: "Community forum URL" customer_instructions: "Customer instructions" devise: + passwords: + spree_user: + cannot_be_blank: "User password cannot be blank. Please enter a password." confirmations: send_instructions: "You will receive an email with instructions about how to confirm your account in a few minutes." failed_to_send: "An error occurred whilst sending your confirmation email." diff --git a/spec/features/consumer/confirm_invitation_spec.rb b/spec/features/consumer/confirm_invitation_spec.rb deleted file mode 100644 index 967ecfb5fe..0000000000 --- a/spec/features/consumer/confirm_invitation_spec.rb +++ /dev/null @@ -1,34 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -feature "Confirm invitation as manager" do - include UIComponentHelper - include OpenFoodNetwork::EmailHelper - - describe "confirm email and set password" do - let(:email) { "test@example.org" } - let(:user) { Spree::User.create(email: email, unconfirmed_email: email, password: "secret") } - - before do - setup_email - user.reset_password_token = Devise.friendly_token - user.reset_password_sent_at = Time.now.utc - user.save! - end - - it "lets the user set a password" do - visit spree.spree_user_confirmation_path(confirmation_token: user.confirmation_token) - - expect(user.reload.confirmed?).to be true - expect(page).to have_text I18n.t(:change_my_password) - - fill_in "Password", with: "my secret" - fill_in "Password Confirmation", with: "my secret" - click_button - - expect(page).to have_no_text "Reset password token has expired" - expect(page).to be_logged_in_as user - end - end -end diff --git a/spec/system/consumer/user_password_spec.rb b/spec/system/consumer/user_password_spec.rb new file mode 100644 index 0000000000..93ff772369 --- /dev/null +++ b/spec/system/consumer/user_password_spec.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +require "system_helper" + +describe "User password confirm/reset page" do + include UIComponentHelper + include OpenFoodNetwork::EmailHelper + + let(:email) { "test@example.org" } + let(:user) { Spree::User.create(email: email, unconfirmed_email: email, password: "secret") } + + describe "can set a password" do + before do + user.reset_password_token = Devise.friendly_token + user.reset_password_sent_at = Time.now.utc + user.save! + end + + it "lets the user set a password" do + visit spree.spree_user_confirmation_path(confirmation_token: user.confirmation_token) + + expect(user.reload.confirmed?).to be true + expect(page).to have_text I18n.t(:change_my_password) + + fill_in "Password", with: "my secret" + fill_in "Password Confirmation", with: "my secret" + click_button + + expect(page).to have_no_text "Reset password token has expired" + expect(page).to be_logged_in_as user + end + end + + describe "can reset its own password" do + let(:reset_password_token) { user.regenerate_reset_password_token } + + it "has the right error when password aren't the same" do + visit spree.edit_spree_user_password_path(reset_password_token: reset_password_token) + + expect(page).to have_text "Change my password" + + fill_in "Password", with: "my secret" + fill_in "Password Confirmation", with: "my secret1" + click_button + + expect(page).to have_text "Password confirmation doesn't match Password" + end + + it "has the right error message whend reset token is invalid" do + visit spree.edit_spree_user_password_path(reset_password_token: "#{reset_password_token}-i") + + fill_in "Password", with: "my secret" + fill_in "Password Confirmation", with: "my secret" + click_button + + expect(page).to have_text "Reset password token is invalid" + end + + it "has the right error message whend reset token is invalid" do + reset_password_token = user.regenerate_reset_password_token + user.reset_password_sent_at = 2.days.ago + user.save! + + visit spree.edit_spree_user_password_path(reset_password_token: reset_password_token) + + fill_in "Password", with: "my secret" + fill_in "Password Confirmation", with: "my secret" + click_button + + expect(page).to have_text "Reset password token has expired, please request a new one" + end + + it "can actually reset its own password" do + visit spree.edit_spree_user_password_path(reset_password_token: reset_password_token) + + fill_in "Password", with: "my secret" + fill_in "Password Confirmation", with: "my secret" + click_button + + expect(page).to have_text "Your password has been changed successfully" + end + end +end From 97fd7e3d7807855fe4fd111c62ea30f10c7427a6 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Wed, 1 Sep 2021 12:21:22 +0200 Subject: [PATCH 3/3] Improve look and feel by adding some responsive margins --- app/views/user_passwords/edit.html.haml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/views/user_passwords/edit.html.haml b/app/views/user_passwords/edit.html.haml index eb1d9df0bc..f729191f28 100644 --- a/app/views/user_passwords/edit.html.haml +++ b/app/views/user_passwords/edit.html.haml @@ -1,5 +1,7 @@ = form_for @spree_user, :as => :spree_user, :url => spree.spree_user_password_path, :method => :put do |f| - = render :partial => 'spree/shared/error_messages', :locals => { :target => @spree_user } + .row + .small-12.medium-10.large-6.columns.medium-centered.large-centered + = render :partial => 'spree/shared/error_messages', :locals => { :target => @spree_user } %fieldset .row .small-12.medium-6.large-4.columns.medium-centered.large-centered