<NME> user_registrations_controller_spec.rb
<BEF> # frozen_string_literal: true

require 'spec_helper'

describe UserRegistrationsController, type: :controller do
  include OpenFoodNetwork::EmailHelper

  before(:all) do
    setup_email
  end

  before do
    @request.env["devise.mapping"] = Devise.mappings[:spree_user]
  end

  describe "via ajax" do
    render_views

    let(:user_params) do
      {
        email: "test@test.com",
        password: "testy123",
        password_confirmation: "testy123"
      }
    end

    it "returns validation errors" do
      post :create, xhr: true, params: { spree_user: {}, use_route: :spree }
      expect(response.status).to eq(401)
      json = JSON.parse(response.body)
      expect(json).to eq("email" => ["can't be blank"], "password" => ["can't be blank"])
    end

    it "returns error when emailing fails" do
      allow(Spree::UserMailer).to receive(:confirmation_instructions).and_raise("Some error")
      expect(OpenFoodNetwork::ErrorLogger).to receive(:notify)

      post :create, xhr: true, params: { spree_user: user_params, use_route: :spree }

      expect(response.status).to eq(401)
      json = JSON.parse(response.body)
      expect(json).to eq("message" => I18n.t('devise.user_registrations.spree_user.unknown_error'))
    end

    it "returns 200 when registration succeeds" do
      post :create, xhr: true, params: { spree_user: user_params, use_route: :spree }
      expect(response.status).to eq(200)
      json = JSON.parse(response.body)
      expect(json).to eq("email" => "test@test.com")
      expect(controller.spree_current_user).to be_nil
    end

    it "sets user.locale from cookie on create" do
      original_i18n_locale = I18n.locale
      original_locale_cookie = cookies[:locale]

      cookies[:locale] = "pt"
      post :create, xhr: true, params: { spree_user: user_params, use_route: :spree }
      cookies[:locale] = original_locale_cookie
    end
  end

  context "when registration succeeds" do
    context "when referer is not '/checkout'" do
      it "redirects to root" do
        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree
        expect(response).to redirect_to root_path
        expect(assigns[:user].email).to eq("test@test.com")
      end
    end

    context "when referer is '/checkout'" do
      before { @request.env['HTTP_REFERER'] = 'http://test.com/checkout' }

      it "redirects to checkout" do
        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree
        expect(response).to redirect_to checkout_path
        expect(assigns[:user].email).to eq("test@test.com")
      end
    end
  end
end
    end
  end
end

<MSG> Remove more dead code from usersregistration (signup calls are always ajax)

<DFF> @@ -59,24 +59,4 @@ describe UserRegistrationsController, type: :controller do
       cookies[:locale] = original_locale_cookie
     end
   end
-
-  context "when registration succeeds" do
-    context "when referer is not '/checkout'" do
-      it "redirects to root" do
-        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree
-        expect(response).to redirect_to root_path
-        expect(assigns[:user].email).to eq("test@test.com")
-      end
-    end
-
-    context "when referer is '/checkout'" do
-      before { @request.env['HTTP_REFERER'] = 'http://test.com/checkout' }
-
-      it "redirects to checkout" do
-        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree
-        expect(response).to redirect_to checkout_path
-        expect(assigns[:user].email).to eq("test@test.com")
-      end
-    end
-  end
 end

{'commit': 'diff --git a/app/controllers/user_registrations_controller.rb b/app/controllers/user_registrations_controller.rb\nindex 2a3b3aaaa..787c8a901 100644\n--- a/app/controllers/user_registrations_controller.rb\n+++ b/app/controllers/user_registrations_controller.rb\n@@ -21,10 +21,6 @@ class UserRegistrationsController < Spree::UserRegistrationsController\n     associate_user\n \n     respond_to do |format|\n-      format.html do\n-        set_flash_message(:success, :signed_up_but_unconfirmed)\n-        redirect_to after_sign_in_path_for(@user)\n-      end\n       format.js do\n         render json: { email: @user.email }\n       end\ndiff --git a/spec/controllers/user_registrations_controller_spec.rb b/spec/controllers/user_registrations_controller_spec.rb\nindex bb725945d..ae2f671a2 100644\n--- a/spec/controllers/user_registrations_controller_spec.rb\n+++ b/spec/controllers/user_registrations_controller_spec.rb\n@@ -59,24 +59,4 @@ describe UserRegistrationsController, type: :controller do\n       cookies[:locale] = original_locale_cookie\n     end\n   end\n-\n-  context "when registration succeeds" do\n-    context "when referer is not \'/checkout\'" do\n-      it "redirects to root" do\n-        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree\n-        expect(response).to redirect_to root_path\n-        expect(assigns[:user].email).to eq("test@test.com")\n-      end\n-    end\n-\n-    context "when referer is \'/checkout\'" do\n-      before { @request.env[\'HTTP_REFERER\'] = \'http://test.com/checkout\' }\n-\n-      it "redirects to checkout" do\n-        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree\n-        expect(response).to redirect_to checkout_path\n-        expect(assigns[:user].email).to eq("test@test.com")\n-      end\n-    end\n-  end\n end\n', 'commit_subject': 'Remove more dead code from usersregistration (signup calls are always ajax)', 'repo_name': 'openfoodfoundation/openfoodnetwork', 'committer': {'name': 'luisramos0', 'email': '76e2c1b37d8af1e69d2a845c129e65d6effab7d3@gmail.com', 'time_sec': '1558435169', 'tz_offset': '120', 'date': {'seconds': '1558435169'}}, 'commit_tree': '0c5f414dae7adf5d5c71ad144ed0ba4fd4edc31a', 'commit_message': 'Remove more dead code from usersregistration (signup calls are always ajax)\n', 'language_name': 'Shell', 'license': 'agpl-3.0', 'hunks': '@@ -59,24 +59,4 @@ describe UserRegistrationsController, type: :controller do\n       cookies[:locale] = original_locale_cookie\n     end\n   end\n-\n-  context "when registration succeeds" do\n-    context "when referer is not \'/checkout\'" do\n-      it "redirects to root" do\n-        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree\n-        expect(response).to redirect_to root_path\n-        expect(assigns[:user].email).to eq("test@test.com")\n-      end\n-    end\n-\n-    context "when referer is \'/checkout\'" do\n-      before { @request.env[\'HTTP_REFERER\'] = \'http://test.com/checkout\' }\n-\n-      it "redirects to checkout" do\n-        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree\n-        expect(response).to redirect_to checkout_path\n-        expect(assigns[:user].email).to eq("test@test.com")\n-      end\n-    end\n-  end\n end\n', 'addition_count': 0, 'deletion_count': 20, 'src_file': 'a/spec/controllers/user_registrations_controller_spec.rb', 'tgt_file': 'b/spec/controllers/user_registrations_controller_spec.rb', 'file_extension': '.rb', 'before_file': '# frozen_string_literal: true\n\nrequire \'spec_helper\'\n\ndescribe UserRegistrationsController, type: :controller do\n  include OpenFoodNetwork::EmailHelper\n\n  before(:all) do\n    setup_email\n  end\n\n  before do\n    @request.env["devise.mapping"] = Devise.mappings[:spree_user]\n  end\n\n  describe "via ajax" do\n    render_views\n\n    let(:user_params) do\n      {\n        email: "test@test.com",\n        password: "testy123",\n        password_confirmation: "testy123"\n      }\n    end\n\n    it "returns validation errors" do\n      post :create, xhr: true, params: { spree_user: {}, use_route: :spree }\n      expect(response.status).to eq(401)\n      json = JSON.parse(response.body)\n      expect(json).to eq("email" => ["can\'t be blank"], "password" => ["can\'t be blank"])\n    end\n\n    it "returns error when emailing fails" do\n      allow(Spree::UserMailer).to receive(:confirmation_instructions).and_raise("Some error")\n      expect(OpenFoodNetwork::ErrorLogger).to receive(:notify)\n\n      post :create, xhr: true, params: { spree_user: user_params, use_route: :spree }\n\n      expect(response.status).to eq(401)\n      json = JSON.parse(response.body)\n      expect(json).to eq("message" => I18n.t(\'devise.user_registrations.spree_user.unknown_error\'))\n    end\n\n    it "returns 200 when registration succeeds" do\n      post :create, xhr: true, params: { spree_user: user_params, use_route: :spree }\n      expect(response.status).to eq(200)\n      json = JSON.parse(response.body)\n      expect(json).to eq("email" => "test@test.com")\n      expect(controller.spree_current_user).to be_nil\n    end\n\n    it "sets user.locale from cookie on create" do\n      original_i18n_locale = I18n.locale\n      original_locale_cookie = cookies[:locale]\n\n      cookies[:locale] = "pt"\n      post :create, xhr: true, params: { spree_user: user_params, use_route: :spree }\n      cookies[:locale] = original_locale_cookie\n    end\n  end\n\n  context "when registration succeeds" do\n    context "when referer is not \'/checkout\'" do\n      it "redirects to root" do\n        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree\n        expect(response).to redirect_to root_path\n        expect(assigns[:user].email).to eq("test@test.com")\n      end\n    end\n\n    context "when referer is \'/checkout\'" do\n      before { @request.env[\'HTTP_REFERER\'] = \'http://test.com/checkout\' }\n\n      it "redirects to checkout" do\n        spree_post :create, spree_user: {email: "test@test.com", password: "testy123", password_confirmation: "testy123"}, :use_route => :spree\n        expect(response).to redirect_to checkout_path\n        expect(assigns[:user].email).to eq("test@test.com")\n      end\n    end\n  end\nend\n    end\n  end\nend\n', 'message': 'Remove more dead code from usersregistration (signup calls are always ajax)\n', 'author_name': 'luisramos0', 'lang': 'rb'}