From 456a6f94f5dba03d5129b85fa674cd203480f79c Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 17 Apr 2015 10:24:37 +1000 Subject: [PATCH] On enterprise confirmation: create a new user based on enterprise contact email if one does not already exist, and add it as a manager --- .../enterprise_confirmations_controller.rb | 22 +++++++- ...nterprise_confirmations_controller_spec.rb | 53 +++++++++++++++---- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/app/controllers/enterprise_confirmations_controller.rb b/app/controllers/enterprise_confirmations_controller.rb index b9868c6f98..d275260599 100644 --- a/app/controllers/enterprise_confirmations_controller.rb +++ b/app/controllers/enterprise_confirmations_controller.rb @@ -32,6 +32,24 @@ class EnterpriseConfirmationsController < DeviseController set_flash_message(:error, :not_confirmed) if is_navigational_format? end - respond_with_navigational(resource){ redirect_to spree.admin_path } + respond_with_navigational(resource){ redirect_to redirect_path(resource) } end -end \ No newline at end of file + + private + + def new_user_reset_path(resource) + password = Devise.friendly_token.first(8) + user = Spree::User.create(email: resource.email, password: password, password_confirmation: password) + user.send_reset_password_instructions + resource.users << user + spree.edit_spree_user_password_path(user, :reset_password_token => user.reset_password_token, return_to: spree.admin_path) + end + + def redirect_path(resource) + if resource.persisted? && !Spree::User.exists?(email: resource.email) + new_user_reset_path(resource) + else + spree.admin_path + end + end +end diff --git a/spec/controllers/enterprise_confirmations_controller_spec.rb b/spec/controllers/enterprise_confirmations_controller_spec.rb index fa7746df36..6fdab61be2 100644 --- a/spec/controllers/enterprise_confirmations_controller_spec.rb +++ b/spec/controllers/enterprise_confirmations_controller_spec.rb @@ -4,25 +4,58 @@ describe EnterpriseConfirmationsController do include AuthenticationWorkflow let!(:user) { create_enterprise_user( enterprise_limit: 10 ) } let!(:unconfirmed_enterprise) { create(:distributor_enterprise, confirmed_at: nil, owner: user) } - let!(:confirmed_enterprise) { create(:distributor_enterprise, owner: user) } + let!(:confirmed_enterprise) { create(:distributor_enterprise, confirmed_at: nil, owner: user) } + let!(:confirmed_token) { confirmed_enterprise.confirmation_token } let!(:unowned_enterprise) { create(:distributor_enterprise) } before do controller.stub spree_current_user: user @request.env["devise.mapping"] = Devise.mappings[:enterprise] + confirmed_enterprise.confirm! end context "confirming an enterprise" do - it "that has already been confirmed" do - spree_get :show, confirmation_token: confirmed_enterprise.confirmation_token - expect(response).to redirect_to spree.admin_path - expect(flash[:error]).to eq I18n.t('devise.enterprise_confirmations.enterprise.not_confirmed') + context "that has already been confirmed" do + + before do + spree_get :show, confirmation_token: confirmed_token + end + + it "redirects the user to admin" do + expect(response).to redirect_to spree.admin_path + expect(flash[:error]).to eq I18n.t('devise.enterprise_confirmations.enterprise.not_confirmed') + end end - it "that has not already been confirmed" do - spree_get :show, confirmation_token: unconfirmed_enterprise.confirmation_token - expect(response).to redirect_to spree.admin_path - expect(flash[:success]).to eq I18n.t('devise.enterprise_confirmations.enterprise.confirmed') + context "that has not been confirmed" do + context "where the enterprise contact email maps to an existing user account" do + before do + unconfirmed_enterprise.update_attribute(:email, user.email) + end + + it "redirects the user to admin" do + spree_get :show, confirmation_token: unconfirmed_enterprise.confirmation_token + expect(response).to redirect_to spree.admin_path + expect(flash[:success]).to eq I18n.t('devise.enterprise_confirmations.enterprise.confirmed') + end + end + + context "where the enterprise contact email doesn't map to an existing user account" do + let(:new_user) { create_enterprise_user } + + before do + unconfirmed_enterprise.update_attribute(:email, 'random@email.com') + allow(Spree::User).to receive(:create) { new_user } + allow(new_user).to receive(:reset_password_token) { "token" } + end + + it "redirects to the user to reset their password" do + spree_get :show, confirmation_token: unconfirmed_enterprise.confirmation_token + expect(response).to redirect_to spree.edit_spree_user_password_path(new_user, :reset_password_token => "token", return_to: spree.admin_path) + expect(flash[:success]).to eq I18n.t('devise.enterprise_confirmations.enterprise.confirmed') + expect(unconfirmed_enterprise.users(:reload)).to include new_user + end + end end end @@ -39,4 +72,4 @@ describe EnterpriseConfirmationsController do expect(flash[:error]).to eq "Authorization Failure" end end -end \ No newline at end of file +end