mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
On enterprise confirmation: create a new user based on enterprise contact email if one does not already exist, and add it as a manager
This commit is contained in:
@@ -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
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user