From f8b008f89495b8bb1cd508d5fbf6ef1d0bc85553 Mon Sep 17 00:00:00 2001 From: elasticspoon Date: Sat, 13 Apr 2024 17:18:33 -0400 Subject: [PATCH] fix: remove more test order dependence As discussed here https://github.com/rubyforgood/human-essentials/pull/4264, some tests modify global variables and expect them to be reset before each test. This makes it more difficult to move forward with removal of pre-seeding. This commit refactors certain tests to be less brittle to changes in global state and to rely less on assumptions on global state. Ex: not assuming that there are 0 records of a certain type when a test starts, etc. --- .../application_controller_spec.rb | 2 +- spec/jobs/reminder_deadline_job_spec.rb | 2 +- spec/pdfs/distribution_pdf_spec.rb | 2 +- spec/requests/distributions_by_county_spec.rb | 10 +- spec/requests/distributions_requests_spec.rb | 86 +++++++++-------- spec/requests/organization_requests_spec.rb | 80 ++++++++-------- spec/requests/users_requests_spec.rb | 24 +++-- spec/services/calendar_service_spec.rb | 2 +- .../distribution_destroy_service_spec.rb | 2 +- ...ributions_by_county_report_service_spec.rb | 11 +-- spec/services/donation_create_service_spec.rb | 2 +- .../services/donation_destroy_service_spec.rb | 2 +- .../export_distributions_csv_service_spec.rb | 2 +- .../export_donations_csv_service_spec.rb | 2 +- .../export_purchases_csv_service_spec.rb | 2 +- .../exports/export_report_csv_service_spec.rb | 2 +- .../exports/export_request_service_spec.rb | 2 +- spec/services/kit_create_service_spec.rb | 2 +- .../organization_update_service_spec.rb | 2 +- .../services/partner_approval_service_spec.rb | 2 +- spec/services/partner_create_service_spec.rb | 2 +- ...er_fetch_requestable_items_service_spec.rb | 2 +- spec/services/partner_invite_service_spec.rb | 2 +- .../partner_profile_update_service_spec.rb | 2 +- ...er_request_recertification_service_spec.rb | 2 +- .../family_request_create_service_spec.rb | 2 +- ...tch_partners_to_remind_now_service_spec.rb | 2 +- .../partners/request_approval_service_spec.rb | 2 +- .../partners/request_create_service_spec.rb | 2 +- .../children_served_report_service_spec.rb | 22 ++--- .../service_object_errors_mixin_spec.rb | 2 +- spec/services/sync_ndbn_members_spec.rb | 2 +- .../date_range_picker_shared_example.rb | 6 +- .../distribution_by_county_shared_example.rb | 10 +- spec/system/admin/dashboard_system_spec.rb | 12 ++- spec/system/distribution_system_spec.rb | 61 +++++++----- .../distributions_by_county_system_spec.rb | 14 +-- spec/system/partner_system_spec.rb | 2 +- spec/system/purchase_system_spec.rb | 96 ++++++++++--------- 39 files changed, 259 insertions(+), 227 deletions(-) diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index fa56adc58a..07c825aa87 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -1,4 +1,4 @@ -describe ApplicationController do +RSpec.describe ApplicationController do describe "current_organization" do before(:each) do allow(controller).to receive(:current_user).and_return(user) diff --git a/spec/jobs/reminder_deadline_job_spec.rb b/spec/jobs/reminder_deadline_job_spec.rb index 6011ebdf36..2cb029a6af 100644 --- a/spec/jobs/reminder_deadline_job_spec.rb +++ b/spec/jobs/reminder_deadline_job_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe ReminderDeadlineJob, type: :job do +RSpec.describe ReminderDeadlineJob, type: :job do describe '#perform' do subject { -> { described_class.perform_now } } let(:partner) { create(:partner) } diff --git a/spec/pdfs/distribution_pdf_spec.rb b/spec/pdfs/distribution_pdf_spec.rb index cabf6455ef..1fd0ad4142 100644 --- a/spec/pdfs/distribution_pdf_spec.rb +++ b/spec/pdfs/distribution_pdf_spec.rb @@ -1,6 +1,6 @@ # avoid Rubocop failing with an infinite loop when it checks this cop # rubocop:disable Layout/ArrayAlignment -describe DistributionPdf do +RSpec.describe DistributionPdf do let(:distribution) { FactoryBot.create(:distribution) } let(:item1) { FactoryBot.create(:item, name: "Item 1", package_size: 50, value_in_cents: 100) } let(:item2) { FactoryBot.create(:item, name: "Item 2", value_in_cents: 200) } diff --git a/spec/requests/distributions_by_county_spec.rb b/spec/requests/distributions_by_county_spec.rb index 4c1d896533..7b75033ae6 100644 --- a/spec/requests/distributions_by_county_spec.rb +++ b/spec/requests/distributions_by_county_spec.rb @@ -2,7 +2,7 @@ RSpec.describe "DistributionsByCounties", type: :request do let(:default_params) do - {organization_name: @organization.to_param} + {organization_name: organization.to_param} end include_examples "distribution_by_county" @@ -15,11 +15,11 @@ context "While signed in as bank" do before do - sign_in(@user) + sign_in(user) end it "shows 'Unspecified 100%' if no served_areas" do - create(:distribution, :with_items, item: item_1, organization: @user.organization) + create(:distribution, :with_items, item: item_1, organization: organization) get distributions_by_county_report_path(default_params) expect(response.body).to include("Unspecified") expect(response.body).to include("100") @@ -28,8 +28,8 @@ context "basic behaviour with served areas" do it "handles multiple partners with overlapping service areas properly" do - create(:distribution, :with_items, item: item_1, organization: @user.organization, partner: partner_1, issued_at: issued_at_present) - create(:distribution, :with_items, item: item_1, organization: @user.organization, partner: partner_2, issued_at: issued_at_present) + create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_1, issued_at: issued_at_present) + create(:distribution, :with_items, item: item_1, organization: organization, partner: partner_2, issued_at: issued_at_present) get distributions_by_county_report_path(default_params) diff --git a/spec/requests/distributions_requests_spec.rb b/spec/requests/distributions_requests_spec.rb index 64d3635249..a82b134941 100644 --- a/spec/requests/distributions_requests_spec.rb +++ b/spec/requests/distributions_requests_spec.rb @@ -1,13 +1,17 @@ require 'rails_helper' RSpec.describe "Distributions", type: :request do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:organization_admin) { create(:organization_admin, organization: organization) } + let(:default_params) do - { organization_name: @organization.to_param } + { organization_name: organization.to_param } end let(:secret_key) { "HI MOM THIS IS ME AND I'M CODING" } let(:crypt) { ActiveSupport::MessageEncryptor.new(secret_key) } - let(:hashed_id) { CGI.escape(crypt.encrypt_and_sign(@organization.id)) } + let(:hashed_id) { CGI.escape(crypt.encrypt_and_sign(organization.id)) } before(:each) do allow(Rails.application).to receive(:secret_key_base).and_return(secret_key) allow(DistributionPdf).to receive(:new).and_return(double("DistributionPdf", compute_and_render: "PDF")) @@ -15,7 +19,7 @@ context "While signed in" do before do - sign_in(@user) + sign_in(user) end describe "GET #itemized_breakdown" do @@ -51,14 +55,14 @@ describe "GET #reclaim" do it "returns http success" do - get distributions_path(default_params.merge(organization_name: @organization, id: create(:distribution).id)) + get distributions_path(default_params.merge(organization_name: organization, id: create(:distribution).id)) expect(response).to be_successful end end describe "GET #index" do - let(:item) { create(:item) } - let!(:distribution) { create(:distribution, :with_items, :past, item: item, item_quantity: 10) } + let(:item) { create(:item, organization: organization) } + let!(:distribution) { create(:distribution, :with_items, :past, item: item, item_quantity: 10, organization: organization) } it "returns http success" do get distributions_path(default_params) @@ -66,7 +70,7 @@ end it "sums distribution totals accurately" do - create(:distribution, :with_items, item_quantity: 5) + create(:distribution, :with_items, item_quantity: 5, organization: organization) create(:line_item, :distribution, itemizable_id: distribution.id, quantity: 7) get distributions_path(default_params) expect(assigns(:total_items_all_distributions)).to eq(22) @@ -101,8 +105,8 @@ end describe "POST #create" do - let!(:storage_location) { create(:storage_location) } - let!(:partner) { create(:partner) } + let!(:storage_location) { create(:storage_location, organization: organization) } + let!(:partner) { create(:partner, organization: organization) } let(:distribution) do { distribution: { storage_location_id: storage_location.id, partner_id: partner.id, delivery_method: :delivery } } end @@ -128,10 +132,10 @@ end describe "GET #new" do - let!(:partner) { create(:partner) } - let(:request) { create(:request, partner: partner) } - let(:storage_location) { create(:storage_location, :with_items) } - let(:default_params) { { organization_name: @organization.to_param, request_id: request.id } } + let!(:partner) { create(:partner, organization: organization) } + let(:request) { create(:request, partner: partner, organization: organization) } + let(:storage_location) { create(:storage_location, :with_items, organization: organization) } + let(:default_params) { { organization_name: organization.to_param, request_id: request.id } } it "returns http success" do get new_distribution_path(default_params) @@ -143,7 +147,7 @@ context "with org default but no partner default" do it "selects org default" do - @organization.update!(default_storage_location: storage_location.id) + organization.update!(default_storage_location: storage_location.id) get new_distribution_path(default_params) expect(response).to be_successful page = Nokogiri::HTML(response.body) @@ -154,7 +158,7 @@ context "with partner default" do it "selects partner default" do location2 = create(:storage_location, :with_items) - @organization.update!(default_storage_location: location2.id) + organization.update!(default_storage_location: location2.id) partner.update!(default_storage_location_id: storage_location.id) get new_distribution_path(default_params) expect(response).to be_successful @@ -165,11 +169,11 @@ end describe "GET #show" do - let(:item) { create(:item) } - let!(:distribution) { create(:distribution, :with_items, item: item, item_quantity: 1) } + let(:item) { create(:item, organization: organization) } + let!(:distribution) { create(:distribution, :with_items, item: item, item_quantity: 1, organization: organization) } it "sums distribution totals accurately" do - distribution = create(:distribution, :with_items, item_quantity: 1) + distribution = create(:distribution, :with_items, item_quantity: 1, organization: organization) item_quantity = 6 package_size = 2 @@ -219,7 +223,7 @@ page = Nokogiri::HTML(response.body) url = page.at_css('#copy-calendar-button').attributes['data-url'].value hash = url.match(/\?hash=(.*)&/)[1] - expect(crypt.decrypt_and_verify(CGI.unescape(hash))).to eq(@organization.id) + expect(crypt.decrypt_and_verify(CGI.unescape(hash))).to eq(organization.id) end end @@ -227,7 +231,7 @@ subject { patch picked_up_distribution_path(default_params.merge(id: distribution.id)) } context 'when the distribution is successfully updated' do - let(:distribution) { create(:distribution, state: :scheduled) } + let(:distribution) { create(:distribution, state: :scheduled, organization: organization) } it "updates the state to 'complete'" do subject @@ -247,10 +251,10 @@ end it "correctly sums the item counts from distributions" do - first_item = create(:item) - second_item = create(:item) - first_distribution = create(:distribution) - second_distribution = create(:distribution) + first_item = create(:item, organization: organization) + second_item = create(:item, organization: organization) + first_distribution = create(:distribution, organization: organization) + second_distribution = create(:distribution, organization: organization) create(:line_item, :distribution, item_id: first_item.id, itemizable_id: first_distribution.id, quantity: 7) create(:line_item, :distribution, item_id: first_item.id, itemizable_id: second_distribution.id, quantity: 4) create(:line_item, :distribution, item_id: second_item.id, itemizable_id: second_distribution.id, quantity: 5) @@ -261,10 +265,10 @@ end it "correctly sums the item package counts from distributions" do - first_item = create(:item, package_size: 2) - second_item = create(:item, package_size: 3) - first_distribution = create(:distribution) - second_distribution = create(:distribution) + first_item = create(:item, package_size: 2, organization: organization) + second_item = create(:item, package_size: 3, organization: organization) + first_distribution = create(:distribution, organization: organization) + second_distribution = create(:distribution, organization: organization) create(:line_item, :distribution, item_id: first_item.id, itemizable_id: first_distribution.id, quantity: 7) create(:line_item, :distribution, item_id: first_item.id, itemizable_id: second_distribution.id, quantity: 4) @@ -282,10 +286,10 @@ end describe "POST #update" do - let(:location) { create(:storage_location) } - let(:partner) { create(:partner) } + let(:location) { create(:storage_location, organization: organization) } + let(:partner) { create(:partner, organization: organization) } - let(:distribution) { create(:distribution, partner: partner) } + let(:distribution) { create(:distribution, partner: partner, organization: organization) } let(:issued_at) { distribution.issued_at } let(:distribution_params) do default_params.merge( @@ -306,11 +310,11 @@ end describe "when changing storage location" do - let(:item) { create(:item) } + let(:item) { create(:item, organization: organization) } it "updates storage quantity correctly" do - new_storage_location = create(:storage_location) - create(:donation, :with_items, item: item, item_quantity: 30, storage_location: new_storage_location) - distribution = create(:distribution, :with_items, item: item, item_quantity: 10) + new_storage_location = create(:storage_location, organization: organization) + create(:donation, :with_items, item: item, item_quantity: 30, storage_location: new_storage_location, organization: organization) + distribution = create(:distribution, :with_items, item: item, item_quantity: 10, organization: organization) original_storage_location = distribution.storage_location line_item = distribution.line_items.first line_item_params = { @@ -330,9 +334,9 @@ # TODO this test is invalid in event-world since it's handled by the aggregate it "rollsback updates if quantity would go below 0" do - next if Event.read_events?(@organization) + next if Event.read_events?(organization) - distribution = create(:distribution, :with_items, item_quantity: 10) + distribution = create(:distribution, :with_items, item_quantity: 10, organization: organization) original_storage_location = distribution.storage_location # adjust inventory so that updating will set quantity below 0 @@ -387,8 +391,8 @@ end describe "GET #edit" do - let(:location) { create(:storage_location) } - let(:partner) { create(:partner) } + let(:location) { create(:storage_location, organization: organization) } + let(:partner) { create(:partner, organization: organization) } let(:distribution) { create(:distribution, partner: partner) } @@ -400,7 +404,7 @@ it "should show a warning if there is an inteverning audit" do distribution.update!(created_at: 1.week.ago) - create(:audit, storage_location: distribution.storage_location) + create(:audit, storage_location: distribution.storage_location, organization: organization) get edit_distribution_path(default_params.merge(id: distribution.id)) expect(response.body).to include("You’ve had an audit since this distribution was started.") end @@ -428,7 +432,7 @@ context 'with a correct hash id' do it 'should render the calendar' do get distributions_calendar_path(hash: hashed_id) - expect(CalendarService).to have_received(:calendar).with(@organization.id) + expect(CalendarService).to have_received(:calendar).with(organization.id) expect(response.media_type).to include('text/calendar') expect(response.body).to eq('SOME ICS STRING') end diff --git a/spec/requests/organization_requests_spec.rb b/spec/requests/organization_requests_spec.rb index 9352388f68..77978477d1 100644 --- a/spec/requests/organization_requests_spec.rb +++ b/spec/requests/organization_requests_spec.rb @@ -1,13 +1,17 @@ require "rails_helper" RSpec.describe "Organizations", type: :request do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:organization_admin) { create(:organization_admin, organization: organization) } + let(:default_params) do - { organization_name: @organization.to_param } + { organization_name: organization.to_param } end context "While signed in as a normal user" do before do - sign_in(@user) + sign_in(user) end describe "GET #show" do @@ -17,9 +21,9 @@ it 'load the current organization' do expect(assigns(:organization)).to have_attributes( - name: @organization.name, - email: @organization.email, - url: @organization.url + name: organization.name, + email: organization.email, + url: organization.url ) end end @@ -45,7 +49,7 @@ context "While signed in as an organization admin" do before do - sign_in(@organization_admin) + sign_in(organization_admin) end describe "GET #edit" do @@ -56,9 +60,9 @@ it 'initializing the given organization' do expect(assigns(:organization)).to be_a(Organization) & have_attributes( - name: @organization.name, - email: @organization.email, - url: @organization.url + name: organization.name, + email: organization.email, + url: organization.url ) end end @@ -75,7 +79,7 @@ expect(response).to redirect_to(organization_path) end it "can update name" do - expect { subject }.to change { @organization.reload.name }.to "Thunder Pants" + expect { subject }.to change { organization.reload.name }.to "Thunder Pants" end context "when organization can not be updated" do @@ -95,18 +99,18 @@ end describe "POST #promote_to_org_admin" do - subject { post promote_to_org_admin_organization_path(default_params.merge(user_id: @user.id)) } + subject { post promote_to_org_admin_organization_path(default_params.merge(user_id: user.id)) } it "runs successfully" do subject - expect(@user.has_role?(Role::ORG_ADMIN, @organization)).to eq(true) + expect(user.has_role?(Role::ORG_ADMIN, organization)).to eq(true) expect(response).to redirect_to(organization_path) end end describe "POST #demote_to_user" do let(:admin_user) do - create(:organization_admin, organization: @organization, name: "ADMIN USER") + create(:organization_admin, organization: organization, name: "ADMIN USER") end subject { post demote_to_user_organization_path(default_params.merge(user_id: admin_user.id)) } @@ -118,27 +122,27 @@ end describe "PUT #deactivate_user" do - subject { put deactivate_user_organization_path(default_params.merge(user_id: @user.id)) } + subject { put deactivate_user_organization_path(default_params.merge(user_id: user.id)) } it "redirect after update" do subject expect(response).to redirect_to(organization_path) end it "deactivates the user" do - expect { subject }.to change { @user.reload.discarded_at }.to be_present + expect { subject }.to change { user.reload.discarded_at }.to be_present end end describe "PUT #reactivate_user" do - subject { put reactivate_user_organization_path(default_params.merge(user_id: @user.id)) } - before { @user.discard! } + subject { put reactivate_user_organization_path(default_params.merge(user_id: user.id)) } + before { user.discard! } it "redirect after update" do subject expect(response).to redirect_to(organization_path) end it "reactivates the user" do - expect { subject }.to change { @user.reload.discarded_at }.to be_nil + expect { subject }.to change { user.reload.discarded_at }.to be_nil end end @@ -152,7 +156,7 @@ before { get organization_path(other_organization_params) } it "shows your own anyway" do - expect(response.body).to include(@organization.name) + expect(response.body).to include(organization.name) end end @@ -160,7 +164,7 @@ before { get edit_organization_path(other_organization_params) } it "shows your own anyway" do - expect(response.body).to include(@organization.name) + expect(response.body).to include(organization.name) end end @@ -172,7 +176,7 @@ it "redirects after update" do subject expect(response).to have_http_status(:not_found) - expect(other_user.reload.has_role?(Role::ORG_ADMIN, @organization)).to eq(false) + expect(other_user.reload.has_role?(Role::ORG_ADMIN, organization)).to eq(false) expect(other_user.reload.has_role?(Role::ORG_ADMIN, other_organization)).to eq(false) end end @@ -185,63 +189,63 @@ end describe "GET #show" do - before { get admin_organizations_path({ id: @organization.id }) } + before { get admin_organizations_path({ id: organization.id }) } it { expect(response).to be_successful } it 'organization details' do - expect(response.body).to include(@organization.name) - expect(response.body).to include(@organization.email) - expect(response.body).to include(@organization.created_at.strftime("%Y-%m-%d")) - expect(response.body).to include(@organization.display_last_distribution_date) + expect(response.body).to include(organization.name) + expect(response.body).to include(organization.email) + expect(response.body).to include(organization.created_at.strftime("%Y-%m-%d")) + expect(response.body).to include(organization.display_last_distribution_date) end end describe "POST #promote_to_org_admin" do - subject { post promote_to_org_admin_organization_path(default_params.merge(user_id: @user.id)) } + subject { post promote_to_org_admin_organization_path(default_params.merge(user_id: user.id)) } it "runs successfully" do subject - expect(@user.has_role?(:org_admin, @organization)).to eq(true) - expect(response).to redirect_to(admin_organization_path(@organization.id)) + expect(user.has_role?(:org_admin, organization)).to eq(true) + expect(response).to redirect_to(admin_organization_path(organization.id)) end end describe "POST #demote_to_user" do let(:admin_user) do - create(:organization_admin, organization: @organization, name: "ADMIN USER") + create(:organization_admin, organization: organization, name: "ADMIN USER") end subject { post demote_to_user_organization_path(default_params.merge(user_id: admin_user.id)) } it "runs successfully" do subject - expect(response).to redirect_to(admin_organization_path(@organization.id)) + expect(response).to redirect_to(admin_organization_path(organization.id)) expect(admin_user.reload.has_role?(Role::ORG_ADMIN, admin_user.organization)).to be_falsey end end describe "PUT #deactivate_user" do - subject { put deactivate_user_organization_path(default_params.merge(user_id: @user.id)) } + subject { put deactivate_user_organization_path(default_params.merge(user_id: user.id)) } it "redirect after update" do subject - expect(response).to redirect_to(admin_organization_path(@organization.id)) + expect(response).to redirect_to(admin_organization_path(organization.id)) end it "deactivates the user" do - expect { subject }.to change { @user.reload.discarded_at }.to be_present + expect { subject }.to change { user.reload.discarded_at }.to be_present end end describe "PUT #reactivate_user" do - subject { put reactivate_user_organization_path(default_params.merge(user_id: @user.id)) } - before { @user.discard! } + subject { put reactivate_user_organization_path(default_params.merge(user_id: user.id)) } + before { user.discard! } it "redirect after update" do subject - expect(response).to redirect_to(admin_organization_path(@organization.id)) + expect(response).to redirect_to(admin_organization_path(organization.id)) end it "reactivates the user" do - expect { subject }.to change { @user.reload.discarded_at }.to be_nil + expect { subject }.to change { user.reload.discarded_at }.to be_nil end end end diff --git a/spec/requests/users_requests_spec.rb b/spec/requests/users_requests_spec.rb index 3f678a0e63..ecc47ab4b8 100644 --- a/spec/requests/users_requests_spec.rb +++ b/spec/requests/users_requests_spec.rb @@ -1,13 +1,17 @@ require "rails_helper" RSpec.describe "Users", type: :request do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:organization_admin) { create(:organization_admin, organization: organization) } + let(:partner) { create(:partner) } let(:default_params) do - { organization_name: @organization.to_param } + { organization_name: organization.to_param } end before do - sign_in(@user) + sign_in(user) end describe "GET #index" do @@ -25,13 +29,13 @@ end describe "POST #send_partner_user_reset_password" do - let(:partner) { create(:partner) } + let(:partner) { create(:partner, organization: organization) } let!(:user) { create(:partner_user, partner: partner, email: "me@partner.com") } let(:params) { default_params.merge(partner_id: partner.id, email: "me@partner.com") } it "should send a password" do post partner_user_reset_password_users_path(params) - expect(response).to redirect_to(root_path(organization_name: @organization.to_param)) + expect(response).to redirect_to(root_path(organization_name: organization.to_param)) expect(ActionMailer::Base.deliveries.size).to eq(1) end @@ -62,21 +66,21 @@ end context "with a partner role" do it "should redirect to the partner path" do - @user.add_role(Role::PARTNER, partner) - get switch_to_role_users_path(@organization, - role_id: @user.roles.find { |r| r.name == Role::PARTNER.to_s }) + user.add_role(Role::PARTNER, partner) + get switch_to_role_users_path(organization, + role_id: user.roles.find { |r| r.name == Role::PARTNER.to_s }) # all bank controllers add organization_id to all routes - there's no way to # avoid it - expect(response).to redirect_to(partners_dashboard_path(organization_name: @organization.to_param)) + expect(response).to redirect_to(partners_dashboard_path(organization_name: organization.to_param)) end end context "without a partner role" do it "should redirect to the root path with an error" do - get switch_to_role_users_path(@organization, role_id: admin_user.roles.first.id) + get switch_to_role_users_path(organization, role_id: admin_user.roles.first.id) message = "Attempted to switch to a role that doesn't belong to you!" expect(flash[:alert]).to eq(message) - expect(response).to redirect_to(root_path(@organization)) + expect(response).to redirect_to(root_path(organization)) end end end diff --git a/spec/services/calendar_service_spec.rb b/spec/services/calendar_service_spec.rb index 5c39fd4257..54252aa432 100644 --- a/spec/services/calendar_service_spec.rb +++ b/spec/services/calendar_service_spec.rb @@ -1,4 +1,4 @@ -describe CalendarService do +RSpec.describe CalendarService do describe ".calendar" do it "should print the calendar correctly" do storage_location = create(:storage_location, diff --git a/spec/services/distribution_destroy_service_spec.rb b/spec/services/distribution_destroy_service_spec.rb index 79c34a51cb..677f901519 100644 --- a/spec/services/distribution_destroy_service_spec.rb +++ b/spec/services/distribution_destroy_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe DistributionDestroyService do +RSpec.describe DistributionDestroyService do describe '#call' do subject { described_class.new(distribution_id).call } let(:distribution_id) { Faker::Number.number } diff --git a/spec/services/distributions_by_county_report_service_spec.rb b/spec/services/distributions_by_county_report_service_spec.rb index bb203d6746..1b330722ee 100644 --- a/spec/services/distributions_by_county_report_service_spec.rb +++ b/spec/services/distributions_by_county_report_service_spec.rb @@ -1,5 +1,4 @@ RSpec.describe DistributionByCountyReportService, type: :service do - let(:organization) { create(:organization) } let(:year) { Time.current.year } let(:issued_at_last_year) { Time.current.utc.change(year: year - 1).to_datetime } let(:distributions) { [] } @@ -7,12 +6,12 @@ include_examples "distribution_by_county" before do - @storage_location = create(:storage_location, organization: @organization) + create(:storage_location, organization: organization) end describe "get_breakdown" do it "will have 100% unspecified shows if no served_areas" do - distribution_1 = create(:distribution, :with_items, item: item_1, organization: @user.organization) + distribution_1 = create(:distribution, :with_items, item: item_1, organization: user.organization) breakdown = DistributionByCountyReportService.new.get_breakdown([distribution_1]) expect(breakdown.size).to eq(1) expect(breakdown[0].num_items).to eq(100) @@ -20,7 +19,7 @@ end it "divides the item numbers and values according to the partner profile" do - distribution_1 = create(:distribution, :with_items, item: item_1, organization: @user.organization, partner: partner_1) + distribution_1 = create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1) breakdown = DistributionByCountyReportService.new.get_breakdown([distribution_1]) expect(breakdown.size).to eq(5) expect(breakdown[4].num_items).to eq(0) @@ -32,8 +31,8 @@ end it "handles multiple partners with overlapping service areas properly" do - distribution_p1 = create(:distribution, :with_items, item: item_1, organization: @user.organization, partner: partner_1, issued_at: issued_at_present) - distribution_p2 = create(:distribution, :with_items, item: item_1, organization: @user.organization, partner: partner_2, issued_at: issued_at_present) + distribution_p1 = create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: issued_at_present) + distribution_p2 = create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_2, issued_at: issued_at_present) breakdown = DistributionByCountyReportService.new.get_breakdown([distribution_p1, distribution_p2]) num_with_45 = 0 num_with_20 = 0 diff --git a/spec/services/donation_create_service_spec.rb b/spec/services/donation_create_service_spec.rb index 8ced472b2d..eace51cb59 100644 --- a/spec/services/donation_create_service_spec.rb +++ b/spec/services/donation_create_service_spec.rb @@ -1,4 +1,4 @@ -describe DonationCreateService do +RSpec.describe DonationCreateService do describe "#call" do let(:donation) { FactoryBot.build(:donation) } diff --git a/spec/services/donation_destroy_service_spec.rb b/spec/services/donation_destroy_service_spec.rb index 5ebeabd2ce..33381eabfe 100644 --- a/spec/services/donation_destroy_service_spec.rb +++ b/spec/services/donation_destroy_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe DonationDestroyService do +RSpec.describe DonationDestroyService do describe '#call' do subject { described_class.new(organization_id: organization_id, donation_id: donation_id) } let(:organization_id) { Faker::Number.number } diff --git a/spec/services/exports/export_distributions_csv_service_spec.rb b/spec/services/exports/export_distributions_csv_service_spec.rb index 8f35ac5314..8de9f96ff9 100644 --- a/spec/services/exports/export_distributions_csv_service_spec.rb +++ b/spec/services/exports/export_distributions_csv_service_spec.rb @@ -1,4 +1,4 @@ -describe Exports::ExportDistributionsCSVService do +RSpec.describe Exports::ExportDistributionsCSVService do describe '#generate_csv_data' do subject { described_class.new(distributions: distributions, organization: Organization.first, filters: filters).generate_csv_data } let(:distributions) { distributions } diff --git a/spec/services/exports/export_donations_csv_service_spec.rb b/spec/services/exports/export_donations_csv_service_spec.rb index ad5245e8f5..237c1a4513 100644 --- a/spec/services/exports/export_donations_csv_service_spec.rb +++ b/spec/services/exports/export_donations_csv_service_spec.rb @@ -1,4 +1,4 @@ -describe Exports::ExportDonationsCSVService do +RSpec.describe Exports::ExportDonationsCSVService do describe '#generate_csv_data' do subject { described_class.new(donation_ids: donation_ids).generate_csv_data } let(:donation_ids) { donations.map(&:id) } diff --git a/spec/services/exports/export_purchases_csv_service_spec.rb b/spec/services/exports/export_purchases_csv_service_spec.rb index d5ae03abb2..2293379363 100644 --- a/spec/services/exports/export_purchases_csv_service_spec.rb +++ b/spec/services/exports/export_purchases_csv_service_spec.rb @@ -1,4 +1,4 @@ -describe Exports::ExportPurchasesCSVService do +RSpec.describe Exports::ExportPurchasesCSVService do describe "#generate_csv_data" do subject { described_class.new(purchase_ids: purchase_ids).generate_csv_data } let(:purchase_ids) { purchases.map(&:id) } diff --git a/spec/services/exports/export_report_csv_service_spec.rb b/spec/services/exports/export_report_csv_service_spec.rb index 54e3307a4d..66f76cabfa 100644 --- a/spec/services/exports/export_report_csv_service_spec.rb +++ b/spec/services/exports/export_report_csv_service_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Exports::ExportReportCSVService do +RSpec.describe Exports::ExportReportCSVService do describe ".generate_csv_data" do it "creates CSV data including headers" do report = { diff --git a/spec/services/exports/export_request_service_spec.rb b/spec/services/exports/export_request_service_spec.rb index 17e547dfad..b36732996c 100644 --- a/spec/services/exports/export_request_service_spec.rb +++ b/spec/services/exports/export_request_service_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Exports::ExportRequestService do +RSpec.describe Exports::ExportRequestService do let(:org) { create(:organization) } let(:item_3t) { create :item, name: "3T Diapers" } diff --git a/spec/services/kit_create_service_spec.rb b/spec/services/kit_create_service_spec.rb index bac8eba480..a0aa526ce2 100644 --- a/spec/services/kit_create_service_spec.rb +++ b/spec/services/kit_create_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe KitCreateService do +RSpec.describe KitCreateService do describe '#call' do subject { described_class.new(**args).call } let(:args) do diff --git a/spec/services/organization_update_service_spec.rb b/spec/services/organization_update_service_spec.rb index 8b82d45156..f936a2a5ec 100644 --- a/spec/services/organization_update_service_spec.rb +++ b/spec/services/organization_update_service_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe OrganizationUpdateService, skip_seed: true do +RSpec.describe OrganizationUpdateService, skip_seed: true do let(:organization) { create(:organization) } describe "#update" do diff --git a/spec/services/partner_approval_service_spec.rb b/spec/services/partner_approval_service_spec.rb index 585a963b12..c961fd3c57 100644 --- a/spec/services/partner_approval_service_spec.rb +++ b/spec/services/partner_approval_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe PartnerApprovalService do +RSpec.describe PartnerApprovalService do describe '#call' do subject { described_class.new(partner: partner).call } let(:partner) { create(:partner) } diff --git a/spec/services/partner_create_service_spec.rb b/spec/services/partner_create_service_spec.rb index a747e68694..1de1daf4c4 100644 --- a/spec/services/partner_create_service_spec.rb +++ b/spec/services/partner_create_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe PartnerCreateService do +RSpec.describe PartnerCreateService do describe '#call' do subject { described_class.new(organization: organization, partner_attrs: partner_attrs).call } let(:organization) { diff --git a/spec/services/partner_fetch_requestable_items_service_spec.rb b/spec/services/partner_fetch_requestable_items_service_spec.rb index 41816de76f..d41c1a1d91 100644 --- a/spec/services/partner_fetch_requestable_items_service_spec.rb +++ b/spec/services/partner_fetch_requestable_items_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe PartnerFetchRequestableItemsService do +RSpec.describe PartnerFetchRequestableItemsService do describe '#call' do subject { described_class.new(partner_id: partner.id).call } let!(:organization) { create(:organization, skip_items: true, items: org_items) } diff --git a/spec/services/partner_invite_service_spec.rb b/spec/services/partner_invite_service_spec.rb index 95a34d5d45..9651fb9ba5 100644 --- a/spec/services/partner_invite_service_spec.rb +++ b/spec/services/partner_invite_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe PartnerInviteService do +RSpec.describe PartnerInviteService do subject { described_class.new(partner: partner).call } let(:partner) { create(:partner) } let(:user) { instance_double(User, reload: -> {}, deliver_invitation: -> {}) } diff --git a/spec/services/partner_profile_update_service_spec.rb b/spec/services/partner_profile_update_service_spec.rb index 59b1e7dba5..e6184b778c 100644 --- a/spec/services/partner_profile_update_service_spec.rb +++ b/spec/services/partner_profile_update_service_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe PartnerProfileUpdateService do +RSpec.describe PartnerProfileUpdateService do let(:county_1) { create(:county, name: "county1", region: "region1") } let(:county_2) { create(:county, name: "county2", region: "region2") } diff --git a/spec/services/partner_request_recertification_service_spec.rb b/spec/services/partner_request_recertification_service_spec.rb index 1ea80658f1..524ddf885f 100644 --- a/spec/services/partner_request_recertification_service_spec.rb +++ b/spec/services/partner_request_recertification_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe PartnerRequestRecertificationService do +RSpec.describe PartnerRequestRecertificationService do describe '#call' do subject { described_class.new(partner: partner).call } let(:partner) { create(:partner) } diff --git a/spec/services/partners/family_request_create_service_spec.rb b/spec/services/partners/family_request_create_service_spec.rb index 964534af1e..ce2517f50b 100644 --- a/spec/services/partners/family_request_create_service_spec.rb +++ b/spec/services/partners/family_request_create_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe Partners::FamilyRequestCreateService do +RSpec.describe Partners::FamilyRequestCreateService do describe '#call' do subject { described_class.new(**args).call } let(:args) do diff --git a/spec/services/partners/fetch_partners_to_remind_now_service_spec.rb b/spec/services/partners/fetch_partners_to_remind_now_service_spec.rb index 31275dda3e..9c2a44d3ab 100644 --- a/spec/services/partners/fetch_partners_to_remind_now_service_spec.rb +++ b/spec/services/partners/fetch_partners_to_remind_now_service_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -describe Partners::FetchPartnersToRemindNowService do +RSpec.describe Partners::FetchPartnersToRemindNowService do describe ".fetch" do subject { described_class.new.fetch } let(:current_day) { 14 } diff --git a/spec/services/partners/request_approval_service_spec.rb b/spec/services/partners/request_approval_service_spec.rb index d99e33c5a2..c1061a7007 100644 --- a/spec/services/partners/request_approval_service_spec.rb +++ b/spec/services/partners/request_approval_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe Partners::RequestApprovalService do +RSpec.describe Partners::RequestApprovalService do describe '#call' do subject { described_class.new(partner: partner).call } let(:partner) { create(:partner) } diff --git a/spec/services/partners/request_create_service_spec.rb b/spec/services/partners/request_create_service_spec.rb index f3f45bd254..abb1a3b20f 100644 --- a/spec/services/partners/request_create_service_spec.rb +++ b/spec/services/partners/request_create_service_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe Partners::RequestCreateService do +RSpec.describe Partners::RequestCreateService do describe '#call' do subject { described_class.new(**args).call } let(:args) do diff --git a/spec/services/reports/children_served_report_service_spec.rb b/spec/services/reports/children_served_report_service_spec.rb index 9ec0163b0c..dadcd7368e 100644 --- a/spec/services/reports/children_served_report_service_spec.rb +++ b/spec/services/reports/children_served_report_service_spec.rb @@ -1,14 +1,11 @@ RSpec.describe Reports::ChildrenServedReportService, type: :service do let(:year) { 2020 } - let(:organization) { create(:organization) } - - subject(:report) do - described_class.new(organization: organization, year: year) - end describe '#report' do it 'should report zero values' do - expect(report.report).to eq({ + organization = create(:organization, skip_items: true) + report = described_class.new(organization: organization, year: year).report + expect(report).to eq({ name: 'Children Served', entries: { 'Average children served monthly' => "0", @@ -20,8 +17,7 @@ end it 'should report normal values' do - Organization.seed_items(organization) - organization.update!(distribute_monthly: true, repackage_essentials: true) + organization = create(:organization, :with_items, distribute_monthly: true, repackage_essentials: true) within_time = Time.zone.parse("2020-05-31 14:00:00") outside_time = Time.zone.parse("2019-05-31 14:00:00") @@ -53,7 +49,8 @@ create(:line_item, :distribution, quantity: 10, item: toddler_disposable_kit_item, itemizable: infant_distribution) create(:line_item, :distribution, quantity: 10, item: infant_disposable_kit_item, itemizable: toddler_distribution) - expect(report.report).to eq({ + report = described_class.new(organization: organization, year: within_time.year).report + expect(report).to eq({ name: 'Children Served', entries: { 'Average children served monthly' => "8", @@ -65,8 +62,7 @@ end it 'should work with no distribution_quantity' do - Organization.seed_items(organization) - organization.update!(distribute_monthly: true, repackage_essentials: true) + organization = create(:organization, :with_items, distribute_monthly: true, repackage_essentials: true) within_time = Time.zone.parse("2020-05-31 14:00:00") outside_time = Time.zone.parse("2019-05-31 14:00:00") @@ -97,7 +93,8 @@ create(:line_item, :distribution, quantity: 10, item: toddler_disposable_kit_item, itemizable: infant_distribution) create(:line_item, :distribution, quantity: 10, item: infant_disposable_kit_item, itemizable: toddler_distribution) - expect(report.report).to eq({ + report = described_class.new(organization: organization, year: within_time.year).report + expect(report).to eq({ name: 'Children Served', entries: { 'Average children served monthly' => "3", @@ -110,6 +107,7 @@ end describe "#disposable_diapers_from_kits_total" do it "calculates the number of disposable diapers that have been distributed within kits" do + organization = create(:organization, skip_items: true) toddler_disposable_kit = create(:kit, :with_item, organization: organization) infant_disposable_kit = create(:kit, :with_item, organization: organization) infant_cloth_kit = create(:kit, :with_item, organization: organization) diff --git a/spec/services/service_object_errors_mixin_spec.rb b/spec/services/service_object_errors_mixin_spec.rb index d632c9ec11..68406c5e97 100644 --- a/spec/services/service_object_errors_mixin_spec.rb +++ b/spec/services/service_object_errors_mixin_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -describe ServiceObjectErrorsMixin do +RSpec.describe ServiceObjectErrorsMixin do describe 'self.included' do before do stub_const 'TestClass', Class.new diff --git a/spec/services/sync_ndbn_members_spec.rb b/spec/services/sync_ndbn_members_spec.rb index cddaabe37e..c8fbbfa0af 100644 --- a/spec/services/sync_ndbn_members_spec.rb +++ b/spec/services/sync_ndbn_members_spec.rb @@ -1,4 +1,4 @@ -describe SyncNDBNMembers do +RSpec.describe SyncNDBNMembers do describe ".sync" do subject { -> { described_class.sync } } let(:ndbn_member_id) { 200040 } diff --git a/spec/support/date_range_picker_shared_example.rb b/spec/support/date_range_picker_shared_example.rb index 1d1a086240..15d136ff03 100644 --- a/spec/support/date_range_picker_shared_example.rb +++ b/spec/support/date_range_picker_shared_example.rb @@ -17,9 +17,9 @@ def date_range_picker_select_range(range_name) described_class.destroy_all end - let!(:very_old) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2000, 7, 31)) } - let!(:recent) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2019, 7, 24)) } - let!(:today) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2019, 7, 31)) } + let!(:very_old) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2000, 7, 31), :organization => @organization) } + let!(:recent) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2019, 7, 24), :organization => @organization) } + let!(:today) { create(described_class.to_s.underscore.to_sym, date_field.to_sym => Time.zone.local(2019, 7, 31), :organization => @organization) } context "when choosing 'All Time'" do before do diff --git a/spec/support/distribution_by_county_shared_example.rb b/spec/support/distribution_by_county_shared_example.rb index f0af5dc393..1a97f751b4 100644 --- a/spec/support/distribution_by_county_shared_example.rb +++ b/spec/support/distribution_by_county_shared_example.rb @@ -1,15 +1,19 @@ # frozen_string_literal: true shared_examples_for "distribution_by_county" do - let(:item_1) { create(:item, value_in_cents: 1050) } + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:organization_admin) { create(:organization_admin, organization: organization) } + + let(:item_1) { create(:item, value_in_cents: 1050, organization: organization) } let(:issued_at_present) { Time.current.utc.to_datetime } let(:partner_1) { - p1 = create(:partner, organization: @organization) + p1 = create(:partner, organization: organization) p1.profile.served_areas << create_list(:partners_served_area, 4, partner_profile: p1.profile, client_share: 25) p1 } let(:partner_2) { - p2 = create(:partner, organization: @organization) + p2 = create(:partner, organization: organization) p2.profile.served_areas << create_list(:partners_served_area, 5, partner_profile: partner_1.profile, client_share: 20) p2.profile.served_areas[0].county = partner_1.profile.served_areas[0].county p2.profile.served_areas[0].save diff --git a/spec/system/admin/dashboard_system_spec.rb b/spec/system/admin/dashboard_system_spec.rb index 33af393645..10e4517bc0 100644 --- a/spec/system/admin/dashboard_system_spec.rb +++ b/spec/system/admin/dashboard_system_spec.rb @@ -1,10 +1,12 @@ RSpec.describe "Dashboard", type: :system, js: true do subject { admin_dashboard_path } + let(:organization) { create(:organization, skip_items: true) } + let(:super_admin) { create(:super_admin, organization: organization) } context "When the super admin user also has an organization assigned" do before do - @super_admin.add_role(Role::ORG_USER, @organization) - sign_in(@super_admin) + super_admin.add_role(Role::ORG_USER, organization) + sign_in(super_admin) visit subject end @@ -15,9 +17,9 @@ context "When the super admin user does not have an organization assigned" do before do - @super_admin.remove_role(Role::ORG_USER, @organization) - @super_admin.save - sign_in(@super_admin) + super_admin.remove_role(Role::ORG_USER, organization) + super_admin.save + sign_in(super_admin) visit subject end diff --git a/spec/system/distribution_system_spec.rb b/spec/system/distribution_system_spec.rb index e8e534de17..5dbe43d636 100644 --- a/spec/system/distribution_system_spec.rb +++ b/spec/system/distribution_system_spec.rb @@ -530,10 +530,10 @@ context "when filtering on the index page" do subject { @url_prefix + "/distributions" } let(:item_category) { create(:item_category) } - let(:item1) { create(:item, name: "Good item", item_category: item_category) } - let(:item2) { create(:item, name: "Crap item") } - let(:partner1) { create(:partner, name: "This Guy", email: "thisguy@example.com") } - let(:partner2) { create(:partner, name: "Not This Guy", email: "ntg@example.com") } + let(:item1) { create(:item, name: "Good item", item_category: item_category, organization: @organization) } + let(:item2) { create(:item, name: "Crap item", organization: @organization) } + let(:partner1) { create(:partner, name: "This Guy", email: "thisguy@example.com", organization: @organization) } + let(:partner2) { create(:partner, name: "Not This Guy", email: "ntg@example.com", organization: @organization) } it "filters by item id" do create(:distribution, :with_items, item: item1) @@ -555,25 +555,40 @@ expect(page).to have_css("table tbody tr td", text: stored_item1_total) end - it "filters by item category id" do - @organization.item_categories << item_category - create(:distribution, :with_items, item: item1) - create(:distribution, :with_items, item: item2) - - visit subject - # check for all distributions - expect(page).to have_css("table tbody tr", count: 2) - # filter - select(item_category.name, from: "filters[by_item_category_id]") - click_button("Filter") - # check for filtered distributions - expect(page).to have_css("table tbody tr", count: 1) - - # check for heading text - expect(page).to have_css("table thead tr th", text: "Total in #{item_category.name}") - # check for count update - stored_item1_total = @storage_location.item_total(item1.id) - expect(page).to have_css("table tbody tr td", text: stored_item1_total) + context "this test needs fresh items" do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:storage_location) { create(:storage_location, organization: organization) } + let(:item_category) { create(:item_category, organization: organization) } + let(:item1) { create(:item, name: "Good item", item_category: item_category, organization: organization) } + let(:item2) { create(:item, name: "Crap item", organization: organization) } + let(:partner1) { create(:partner, name: "This Guy", email: "thisguy@example.com", organization: organization) } + let(:partner2) { create(:partner, name: "Not This Guy", email: "ntg@example.com", organization: organization) } + + it "filters by item category id" do + setup_storage_location(storage_location) + + sign_out(user) + sign_in(user) + + create(:distribution, :with_items, item: item1, organization: organization) + create(:distribution, :with_items, item: item2, organization: organization) + + visit "/#{organization.to_param}/distributions" + # check for all distributions + expect(page).to have_css("table tbody tr", count: 2) + # filter + select(item_category.name, from: "filters[by_item_category_id]") + click_button("Filter") + # check for filtered distributions + expect(page).to have_css("table tbody tr", count: 1) + + # check for heading text + expect(page).to have_css("table thead tr th", text: "Total in #{item_category.name}") + # check for count update + stored_item1_total = storage_location.item_total(item1.id) + expect(page).to have_css("table tbody tr td", text: stored_item1_total) + end end it "filters by partner" do diff --git a/spec/system/distributions_by_county_system_spec.rb b/spec/system/distributions_by_county_system_spec.rb index 926f0450fb..ad71fdea78 100644 --- a/spec/system/distributions_by_county_system_spec.rb +++ b/spec/system/distributions_by_county_system_spec.rb @@ -1,6 +1,6 @@ RSpec.feature "Distributions by County", type: :system do let(:default_params) do - {organization_name: @organization.to_param} + {organization_name: organization.to_param} end include_examples "distribution_by_county" @@ -9,15 +9,15 @@ let(:issued_at_last_year) { Time.current.utc.change(year: year - 1).to_datetime } before do - sign_in(@user) - @storage_location = create(:storage_location, organization: @organization) + sign_in(user) + @storage_location = create(:storage_location, organization: organization) setup_storage_location(@storage_location) end context "handles time ranges properly" do it("works for all time") do - @distribution_last_year = create(:distribution, :with_items, item: item_1, organization: @user.organization, partner: partner_1, issued_at: issued_at_last_year) - @distribution_current = create(:distribution, :with_items, item: item_1, organization: @user.organization, partner: partner_1, issued_at: issued_at_present) + @distribution_last_year = create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: issued_at_last_year) + @distribution_current = create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: issued_at_present) visit_distribution_by_county_with_specified_date_range("All Time") partner_1.profile.served_areas.each do |served_area| expect(page).to have_text(served_area.county.name) @@ -27,8 +27,8 @@ end it("works for this year") do - @distribution_current = create(:distribution, :with_items, item: item_1, organization: @user.organization, partner: partner_1, issued_at: issued_at_present) - @distribution_last_year = create(:distribution, :with_items, item: item_1, organization: @user.organization, partner: partner_1, issued_at: issued_at_last_year) + @distribution_current = create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: issued_at_present) + @distribution_last_year = create(:distribution, :with_items, item: item_1, organization: user.organization, partner: partner_1, issued_at: issued_at_last_year) visit_distribution_by_county_with_specified_date_range("This Year") diff --git a/spec/system/partner_system_spec.rb b/spec/system/partner_system_spec.rb index f6e0d1cf29..7436a65eab 100644 --- a/spec/system/partner_system_spec.rb +++ b/spec/system/partner_system_spec.rb @@ -437,7 +437,7 @@ context 'when a partner is assigned to partner group' do before do assert page.has_content? 'All Items Requestable' - expect(@partner.partner_group).to be_nil + @partner.update!(partner_group: nil) end context 'that has requestable item categories' do diff --git a/spec/system/purchase_system_spec.rb b/spec/system/purchase_system_spec.rb index 790e6dcf0b..e9dbe2ec25 100644 --- a/spec/system/purchase_system_spec.rb +++ b/spec/system/purchase_system_spec.rb @@ -29,17 +29,17 @@ end it "User sees purchased date column" do - storage1 = create(:storage_location, name: "storage1") + storage1 = create(:storage_location, name: "storage1", organization: @organization) purchase_date = 1.week.ago - create(:purchase, storage_location: storage1, issued_at: purchase_date) + create(:purchase, storage_location: storage1, issued_at: purchase_date, organization: @organization) page.refresh expect(page).to have_text("Purchased Date") expect(page).to have_text(1.week.ago.strftime("%Y-%m-%d")) end it "User sees total purchases value" do - purchase1 = create(:purchase, amount_spent_in_cents: 1234) - purchase2 = create(:purchase, amount_spent_in_cents: 2345) + purchase1 = create(:purchase, amount_spent_in_cents: 1234, organization: @organization) + purchase2 = create(:purchase, amount_spent_in_cents: 2345, organization: @organization) purchases = [purchase1, purchase2] page.refresh expect(page).to have_text("Total") @@ -50,15 +50,15 @@ end context "When filtering on the index page" do - let!(:item) { create(:item) } - let(:storage) { create(:storage_location) } + let!(:item) { create(:item, organization: @organization) } + let(:storage) { create(:storage_location, organization: @organization) } subject { url_prefix + "/purchases" } it "User can filter the #index by storage location" do - storage1 = create(:storage_location, name: "storage1") - storage2 = create(:storage_location, name: "storage2") - create(:purchase, storage_location: storage1) - create(:purchase, storage_location: storage2) + storage1 = create(:storage_location, name: "storage1", organization: @organization) + storage2 = create(:storage_location, name: "storage2", organization: @organization) + create(:purchase, storage_location: storage1, organization: @organization) + create(:purchase, storage_location: storage2, organization: @organization) visit subject expect(page).to have_css("table tbody tr", count: 2) select storage1.name, from: "filters[at_storage_location]" @@ -67,11 +67,15 @@ end it "User can filter the #index by vendor" do - vendor1 = create(:vendor, business_name: "vendor 1") - vendor2 = create(:vendor, business_name: "vendor 2") - create(:purchase, vendor: vendor1) - create(:purchase, vendor: vendor2) - visit subject + organization = create(:organization, skip_items: true) + vendor1 = create(:vendor, business_name: "vendor 1", organization: organization) + vendor2 = create(:vendor, business_name: "vendor 2", organization: organization) + create(:purchase, vendor: vendor1, organization: organization) + create(:purchase, vendor: vendor2, organization: organization) + + sign_in create(:user, organization: organization) + visit "/#{organization.short_name}/purchases" + expect(page).to have_css("table tbody tr", count: 2) select vendor1.business_name, from: "filters[from_vendor]" click_button "Filter" @@ -84,9 +88,9 @@ context "When creating a new purchase" do before(:each) do - create(:item, organization: @organization) - create(:storage_location, organization: @organization) - create(:vendor, organization: @organization) + @item = create(:item, organization: @organization) + @storage_location = create(:storage_location, organization: @organization) + @vendor = create(:vendor, organization: @organization) @organization.reload end subject { url_prefix + "/purchases/new" } @@ -105,16 +109,15 @@ fill_in "vendor_business_name", with: "businesstest" fill_in "vendor_contact_name", with: "test" fill_in "vendor_email", with: "123@mail.ru" - sleep(0.3) - click_on "vendor-submit" + find("#vendor-submit").click select "businesstest", from: "purchase_vendor_id" expect(page).to have_no_content("New Vendor") end it "User can create a purchase using dollars decimal amount" do - select StorageLocation.first.name, from: "purchase_storage_location_id" - select Item.alphabetized.first.name, from: "purchase_line_items_attributes_0_item_id" - select Vendor.first.business_name, from: "purchase_vendor_id" + select @storage_location.name, from: "purchase_storage_location_id" + select @item.name, from: "purchase_line_items_attributes_0_item_id" + select @vendor.business_name, from: "purchase_vendor_id" fill_in "purchase_line_items_attributes_0_quantity", with: "5" fill_in "purchase_amount_spent", with: "1,234.56" @@ -127,9 +130,9 @@ end it "User can create a purchase IN THE PAST" do - select StorageLocation.first.name, from: "purchase_storage_location_id" - select Item.alphabetized.first.name, from: "purchase_line_items_attributes_0_item_id" - select Vendor.first.business_name, from: "purchase_vendor_id" + select @storage_location.name, from: "purchase_storage_location_id" + select @item.name, from: "purchase_line_items_attributes_0_item_id" + select @vendor.business_name, from: "purchase_vendor_id" fill_in "purchase_line_items_attributes_0_quantity", with: "5" fill_in "purchase_issued_at", with: "2001-01-01" fill_in "purchase_amount_spent", with: "10" @@ -141,7 +144,7 @@ visit url_prefix + "/purchases/#{Purchase.last.id}" expected_date = "January 1 2001 (entered: #{Purchase.last.created_at.to_fs(:distribution_date)})" - expected_breadcrumb_date = "#{Vendor.first.business_name} on January 1 2001" + expected_breadcrumb_date = "#{@vendor.business_name} on January 1 2001" aggregate_failures do expect(page).to have_text(expected_date) expect(page).to have_text(expected_breadcrumb_date) @@ -151,26 +154,24 @@ it "Does not include inactive items in the line item fields" do visit url_prefix + "/purchases/new" - item = Item.alphabetized.first + select @storage_location.name, from: "purchase_storage_location_id" + expect(page).to have_content(@item.name) + select @item.name, from: "purchase_line_items_attributes_0_item_id" - select StorageLocation.first.name, from: "purchase_storage_location_id" - expect(page).to have_content(item.name) - select item.name, from: "purchase_line_items_attributes_0_item_id" - - item.update(active: false) + @item.update(active: false) page.refresh - select StorageLocation.first.name, from: "purchase_storage_location_id" - expect(page).to have_no_content(item.name) + select @storage_location.name, from: "purchase_storage_location_id" + expect(page).to have_no_content(@item.name) end it "multiple line items for the same item type are accepted and combined on the backend" do - select StorageLocation.first.name, from: "purchase_storage_location_id" - select Item.alphabetized.last.name, from: "purchase_line_items_attributes_0_item_id" - select Vendor.first.business_name, from: "purchase_vendor_id" + select @storage_location.name, from: "purchase_storage_location_id" + select @item.name, from: "purchase_line_items_attributes_0_item_id" + select @vendor.business_name, from: "purchase_vendor_id" fill_in "purchase_line_items_attributes_0_quantity", with: "5" page.find(:css, "#__add_line_item").click - all(".li-name select").last.find('option', text: Item.alphabetized.last.name).select_option + all(".li-name select").last.find('option', text: @item.name).select_option all(".li-quantity input").last.set(11) fill_in "purchase_amount_spent", with: "10" @@ -203,10 +204,10 @@ end # Bug fix -- Issue #378 - # A user can view another organizations purchase + # A user can view another @organizations purchase context "Editing purchase" do it "A user can see purchased_from value" do - purchase = create(:purchase, purchased_from: "Old Vendor") + purchase = create(:purchase, purchased_from: "Old Vendor", organization: @organization) visit edit_purchase_path(@organization.to_param, purchase) expect(page).to have_content("Vendor (Old Vendor)") end @@ -220,7 +221,9 @@ context "via barcode entry" do before(:each) do - initialize_barcodes + @existing_barcode = create(:barcode_item, organization: @organization) + @item_with_barcode = @existing_barcode.item + @item_no_barcode = create(:item, organization: @organization) visit url_prefix + "/purchases/new" end @@ -265,8 +268,7 @@ within ".modal-content" do fill_in "barcode_item_quantity", with: 3 select Item.alphabetized.first.name, from: "barcode_item_barcodeable_id" - sleep(0.3) - click_button "Save" + find("button", text: "Save").click end expect(page).to have_field "purchase_line_items_attributes_0_quantity", with: 3 @@ -290,15 +292,15 @@ subject { url_prefix + "/purchases" } it "does not allow deletion of a purchase" do - purchase = create(:purchase) + purchase = create(:purchase, organization: @organization) visit "#{subject}/#{purchase.id}" expect(page).to_not have_link("Delete") end end end - context "while signed in as an organization admin" do - let!(:purchase) { create(:purchase, :with_items, item_quantity: 10) } + context "while signed in as an @organization admin" do + let!(:purchase) { create(:purchase, :with_items, item_quantity: 10, organization: @organization) } subject { url_prefix + "/purchases" } before do