Files
openfoodnetwork/spec/controllers/spree/admin/overview_controller_spec.rb
Pau Perez 889199a525 Refactor Overview Controller to make it more clear
Assigns meaningful names to the boolean conditions to make it easier
to understand, breaks down the big and nested if/else and converts the
specs to RSpec 3.

Note the check `!spree_current_user.admin?` has been removed because
in admin/base_controller_decorator.rb `#authorize_admin` is already
called.
2018-05-22 10:21:35 +02:00

91 lines
2.8 KiB
Ruby

require 'spec_helper'
describe Spree::Admin::OverviewController, type: :controller do
include AuthenticationWorkflow
describe "#index" do
before do
allow(controller).to receive(:spree_current_user).and_return(user)
end
context "when user owns only one enterprise" do
let(:user) { create_enterprise_user }
let!(:enterprise) { create(:distributor_enterprise, owner: user) }
context "when the referer is not an admin page" do
before do
@request.env['HTTP_REFERER'] = 'http://test.com/not_admin_path'
end
context "and the enterprise has sells='unspecified'" do
before do
enterprise.update_attribute(:sells, "unspecified")
end
it "redirects to the welcome page for the enterprise" do
spree_get :index
expect(response)
.to redirect_to welcome_admin_enterprise_path(enterprise)
end
end
context "and the enterprise does not have sells='unspecified'" do
it "renders the single enterprise dashboard" do
spree_get :index
expect(response).to render_template :single_enterprise_dashboard
end
end
end
context "when the refer is an admin page" do
before { @request.env['HTTP_REFERER'] = 'http://test.com/admin' }
it "renders the single enterprise dashboard" do
spree_get :index
expect(response).to render_template :single_enterprise_dashboard
end
end
end
context "when user owns multiple enterprises" do
let(:user) { create_enterprise_user(enterprise_limit: 2) }
let!(:enterprise1) { create(:distributor_enterprise, owner: user) }
before { create(:distributor_enterprise, owner: user) }
context "when the referer is not an admin page" do
before do
@request.env['HTTP_REFERER'] = 'http://test.com/not_admin_path'
end
context "and at least one owned enterprise has sells='unspecified'" do
before do
enterprise1.update_attribute(:sells, "unspecified")
end
it "redirects to the enterprises index" do
spree_get :index
expect(response).to redirect_to admin_enterprises_path
end
end
context "and no owned enterprises have sells='unspecified'" do
it "renders the multiple enterprise dashboard" do
spree_get :index
expect(response).to render_template :multi_enterprise_dashboard
end
end
end
context "when the refer is an admin page" do
before { @request.env['HTTP_REFERER'] = 'http://test.com/admin' }
it "renders the multiple enterprise dashboard" do
spree_get :index
expect(response).to render_template :multi_enterprise_dashboard
end
end
end
end
end