From c68459f212a7aefb69863f896de7174da25f70fc Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 2 Jun 2020 16:16:07 +0100 Subject: [PATCH 1/4] Make enterprises_controller_spec use api_post instead of spree_post --- spec/controllers/api/enterprises_controller_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/controllers/api/enterprises_controller_spec.rb b/spec/controllers/api/enterprises_controller_spec.rb index 54c5efc3b3..d0e8209f31 100644 --- a/spec/controllers/api/enterprises_controller_spec.rb +++ b/spec/controllers/api/enterprises_controller_spec.rb @@ -23,14 +23,14 @@ module Api address1: '123 Abc Street', city: 'Northcote', zipcode: '3070', - state_id: australia.states.first, + state_id: australia.states.first.id, country_id: australia.id } } end it "creates as sells=any when it is not a producer" do - spree_post :create, { enterprise: new_enterprise_params } + api_post :create, { enterprise: new_enterprise_params } expect(response).to be_success enterprise = Enterprise.last From ff0c93a76bc34edb7d91fb03d9fce65726ae0594 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 2 Jun 2020 18:22:30 +0100 Subject: [PATCH 2/4] Join module and describe declaration so that rspec picks up correct controller under Api namespace and not the controller with the same name in the base namespace --- .../api/enterprises_controller_spec.rb | 142 +++++++++--------- 1 file changed, 70 insertions(+), 72 deletions(-) diff --git a/spec/controllers/api/enterprises_controller_spec.rb b/spec/controllers/api/enterprises_controller_spec.rb index d0e8209f31..c407a2b918 100644 --- a/spec/controllers/api/enterprises_controller_spec.rb +++ b/spec/controllers/api/enterprises_controller_spec.rb @@ -1,95 +1,93 @@ require 'spec_helper' -module Api - describe EnterprisesController, type: :controller do - include AuthenticationWorkflow - render_views +describe Api::EnterprisesController, type: :controller do + include AuthenticationWorkflow + render_views - let(:enterprise) { create(:distributor_enterprise) } + let(:enterprise) { create(:distributor_enterprise) } - context "as an enterprise owner" do - let(:enterprise_owner) { create_enterprise_user enterprise_limit: 10 } - let!(:enterprise) { create(:distributor_enterprise, owner: enterprise_owner) } + context "as an enterprise owner" do + let(:enterprise_owner) { create_enterprise_user enterprise_limit: 10 } + let!(:enterprise) { create(:distributor_enterprise, owner: enterprise_owner) } - before do - allow(controller).to receive(:spree_current_user) { enterprise_owner } - end - - describe "creating an enterprise" do - let(:australia) { Spree::Country.find_by(name: 'Australia') } - let(:new_enterprise_params) do - { - name: 'name', contact_name: 'Sheila', address_attributes: { - address1: '123 Abc Street', - city: 'Northcote', - zipcode: '3070', - state_id: australia.states.first.id, - country_id: australia.id - } - } - end - - it "creates as sells=any when it is not a producer" do - api_post :create, { enterprise: new_enterprise_params } - expect(response).to be_success - - enterprise = Enterprise.last - expect(enterprise.sells).to eq('any') - end - - it "saves all user ids submitted" do - manager1 = create(:user) - manager2 = create(:user) - spree_post :create, { - enterprise: new_enterprise_params. - merge({ user_ids: [enterprise_owner.id, manager1.id, manager2.id] }) - } - expect(response).to be_success - - enterprise = Enterprise.last - expect(enterprise.user_ids).to match_array([enterprise_owner.id, manager1.id, manager2.id]) - end - end + before do + allow(controller).to receive(:spree_current_user) { enterprise_owner } end - context "as an enterprise manager" do - let(:enterprise_manager) { create_enterprise_user } - - before do - enterprise_manager.enterprise_roles.build(enterprise: enterprise).save - allow(controller).to receive(:spree_current_user) { enterprise_manager } + describe "creating an enterprise" do + let(:australia) { Spree::Country.find_by(name: 'Australia') } + let(:new_enterprise_params) do + { + name: 'name', contact_name: 'Sheila', address_attributes: { + address1: '123 Abc Street', + city: 'Northcote', + zipcode: '3070', + state_id: australia.states.first.id, + country_id: australia.id + } + } end - describe "submitting a valid image" do - before do - allow(Enterprise) - .to receive(:find_by).with({ permalink: enterprise.id.to_s }) { enterprise } - allow(enterprise).to receive(:update_attributes).and_return(true) - end + it "creates as sells=any when it is not a producer" do + api_post :create, { enterprise: new_enterprise_params } + expect(response).to be_success - it "I can update enterprise image" do - spree_post :update_image, logo: 'a logo', id: enterprise.id - expect(response).to be_success - end + enterprise = Enterprise.last + expect(enterprise.sells).to eq('any') + end + + it "saves all user ids submitted" do + manager1 = create(:user) + manager2 = create(:user) + spree_post :create, { + enterprise: new_enterprise_params. + merge({ user_ids: [enterprise_owner.id, manager1.id, manager2.id] }) + } + expect(response).to be_success + + enterprise = Enterprise.last + expect(enterprise.user_ids).to match_array([enterprise_owner.id, manager1.id, manager2.id]) end end + end - context "as an non-managing user" do - let(:non_managing_user) { create_enterprise_user } + context "as an enterprise manager" do + let(:enterprise_manager) { create_enterprise_user } + before do + enterprise_manager.enterprise_roles.build(enterprise: enterprise).save + allow(controller).to receive(:spree_current_user) { enterprise_manager } + end + + describe "submitting a valid image" do before do allow(Enterprise) .to receive(:find_by).with({ permalink: enterprise.id.to_s }) { enterprise } - allow(controller).to receive(:spree_current_user) { non_managing_user } + allow(enterprise).to receive(:update_attributes).and_return(true) end - describe "submitting a valid image" do - before { allow(enterprise).to receive(:update_attributes).and_return(true) } + it "I can update enterprise image" do + spree_post :update_image, logo: 'a logo', id: enterprise.id + expect(response).to be_success + end + end + end - it "I can't update enterprise image" do - spree_post :update_image, logo: 'a logo', id: enterprise.id - assert_unauthorized! - end + context "as an non-managing user" do + let(:non_managing_user) { create_enterprise_user } + + before do + allow(Enterprise) + .to receive(:find_by).with({ permalink: enterprise.id.to_s }) { enterprise } + allow(controller).to receive(:spree_current_user) { non_managing_user } + end + + describe "submitting a valid image" do + before { allow(enterprise).to receive(:update_attributes).and_return(true) } + + it "I can't update enterprise image" do + spree_post :update_image, logo: 'a logo', id: enterprise.id + assert_unauthorized! end end end From 251c04f2d91ed06abb1e5b2770bbafa9e25de6e6 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 2 Jun 2020 19:08:13 +0100 Subject: [PATCH 3/4] Make all tests in enterprises_controller_spec use api_post instead of spree_post --- spec/controllers/api/enterprises_controller_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/controllers/api/enterprises_controller_spec.rb b/spec/controllers/api/enterprises_controller_spec.rb index c407a2b918..1b3382274e 100644 --- a/spec/controllers/api/enterprises_controller_spec.rb +++ b/spec/controllers/api/enterprises_controller_spec.rb @@ -39,7 +39,7 @@ describe Api::EnterprisesController, type: :controller do it "saves all user ids submitted" do manager1 = create(:user) manager2 = create(:user) - spree_post :create, { + api_post :create, { enterprise: new_enterprise_params. merge({ user_ids: [enterprise_owner.id, manager1.id, manager2.id] }) } @@ -67,7 +67,7 @@ describe Api::EnterprisesController, type: :controller do end it "I can update enterprise image" do - spree_post :update_image, logo: 'a logo', id: enterprise.id + api_post :update_image, logo: 'a logo', id: enterprise.id expect(response).to be_success end end @@ -86,7 +86,7 @@ describe Api::EnterprisesController, type: :controller do before { allow(enterprise).to receive(:update_attributes).and_return(true) } it "I can't update enterprise image" do - spree_post :update_image, logo: 'a logo', id: enterprise.id + api_post :update_image, logo: 'a logo', id: enterprise.id assert_unauthorized! end end From f9d86eb7edce9e32b3547fe0e243796bbd11d927 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 2 Jun 2020 20:17:46 +0100 Subject: [PATCH 4/4] Join module and describe declaration so that rspec picks up correct controller under Api namespace and not the controller with the same name in the base namespace --- spec/controllers/api/shops_controller_spec.rb | 64 +++++++++---------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/spec/controllers/api/shops_controller_spec.rb b/spec/controllers/api/shops_controller_spec.rb index 33343e8d84..127dd83848 100644 --- a/spec/controllers/api/shops_controller_spec.rb +++ b/spec/controllers/api/shops_controller_spec.rb @@ -2,45 +2,43 @@ require 'spec_helper' -module Api - describe ShopsController, type: :controller do - include AuthenticationWorkflow - render_views +describe Api::ShopsController, type: :controller do + include AuthenticationWorkflow + render_views - context "as a non-authenticated user" do - let!(:hub) { - create(:distributor_enterprise, with_payment_and_shipping: true, name: 'Shopfront Test Hub') - } - let!(:producer) { create(:supplier_enterprise, name: 'Shopfront Test Producer') } - let!(:category) { create(:taxon, name: 'Fruit') } - let!(:product) { create(:product, supplier: producer, primary_taxon: category ) } - let!(:relationship) { create(:enterprise_relationship, parent: hub, child: producer) } - let!(:closed_hub1) { create(:distributor_enterprise) } - let!(:closed_hub2) { create(:distributor_enterprise) } + context "as a non-authenticated user" do + let!(:hub) { + create(:distributor_enterprise, with_payment_and_shipping: true, name: 'Shopfront Test Hub') + } + let!(:producer) { create(:supplier_enterprise, name: 'Shopfront Test Producer') } + let!(:category) { create(:taxon, name: 'Fruit') } + let!(:product) { create(:product, supplier: producer, primary_taxon: category ) } + let!(:relationship) { create(:enterprise_relationship, parent: hub, child: producer) } + let!(:closed_hub1) { create(:distributor_enterprise) } + let!(:closed_hub2) { create(:distributor_enterprise) } - before do - allow(controller).to receive(:spree_current_user) { nil } + before do + allow(controller).to receive(:spree_current_user) { nil } + end + + describe "#show" do + it "returns shopfront data for an enterprise" do + spree_get :show, id: producer.id + + expect(json_response['name']).to eq 'Shopfront Test Producer' + expect(json_response['hubs'][0]['name']).to eq 'Shopfront Test Hub' + expect(json_response['supplied_taxons'][0]['name']).to eq 'Fruit' end + end - describe "#show" do - it "returns shopfront data for an enterprise" do - spree_get :show, id: producer.id + describe "#closed_shops" do + it "returns data for all closed shops" do + spree_get :closed_shops, nil - expect(json_response['name']).to eq 'Shopfront Test Producer' - expect(json_response['hubs'][0]['name']).to eq 'Shopfront Test Hub' - expect(json_response['supplied_taxons'][0]['name']).to eq 'Fruit' - end - end + expect(json_response).not_to match hub.name - describe "#closed_shops" do - it "returns data for all closed shops" do - spree_get :closed_shops, nil - - expect(json_response).not_to match hub.name - - response_ids = json_response.map { |shop| shop['id'] } - expect(response_ids).to contain_exactly(closed_hub1.id, closed_hub2.id) - end + response_ids = json_response.map { |shop| shop['id'] } + expect(response_ids).to contain_exactly(closed_hub1.id, closed_hub2.id) end end end