diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index a6d940b04d..7dabfb32ae 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -211,22 +211,13 @@ def seed_base_data_for_tests Capybara.server = :puma, { Silent: true } end - config.before(:each, clean_and_seed_db: true) do - seed_base_data_for_tests - define_global_variables - end - - config.after(:each, clean_and_seed_db: true) do - DatabaseCleaner.clean_with(:truncation, except: %w[ar_internal_metadata]) - end - - config.before(:all, seed_db: true) do + config.before(:all, :seed_db) do seed_base_data_for_tests define_global_variables end config.after(:all, :seed_db) do - DatabaseCleaner.clean_with(:truncation, except: %w[ar_internal_metadata]) + DatabaseCleaner.clean_with(:truncation) end config.before(:each) do diff --git a/spec/requests/distributions_by_county_spec.rb b/spec/requests/distributions_by_county_spec.rb index 434fc0ddce..b161b4cf8c 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, seed_db: true 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 a6d7241a04..b16b4aa9de 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, clean_and_seed_db: true do +RSpec.describe "Distributions", type: :request, seed_db: true 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/users_requests_spec.rb b/spec/requests/users_requests_spec.rb index 794a9ae5df..6fe47bcd4f 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, clean_and_seed_db: true do +RSpec.describe "Users", type: :request, seed_db: true 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/distributions_by_county_report_service_spec.rb b/spec/services/distributions_by_county_report_service_spec.rb index 1f37147ddc..ce34dc49d5 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, seed_db: true 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/reports/children_served_report_service_spec.rb b/spec/services/reports/children_served_report_service_spec.rb index 99075a261e..083abcd733 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, clean_and_seed_db: true do +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", @@ -111,6 +108,7 @@ 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/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/distribution_system_spec.rb b/spec/system/distribution_system_spec.rb index 688cf4c2b8..4a48ddf9fa 100644 --- a/spec/system/distribution_system_spec.rb +++ b/spec/system/distribution_system_spec.rb @@ -1,4 +1,4 @@ -RSpec.feature "Distributions", type: :system, clean_and_seed_db: true do +RSpec.feature "Distributions", type: :system, seed_db: true do before do sign_in(@user) @url_prefix = "/#{@organization.to_param}" @@ -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 17c6a4fabf..58b77e93a8 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, clean_and_seed_db: true do +RSpec.feature "Distributions by County", type: :system, seed_db: true 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 0f866a92fa..58d3828031 100644 --- a/spec/system/partner_system_spec.rb +++ b/spec/system/partner_system_spec.rb @@ -1,5 +1,5 @@ Capybara.using_wait_time 10 do # allow up to 10 seconds for content to load in the test - RSpec.describe "Partner management", type: :system, js: true, clean_and_seed_db: true do + RSpec.describe "Partner management", type: :system, js: true, seed_db: true do before do sign_in(@user) end @@ -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 e46aeaaafc..18341c23d6 100644 --- a/spec/system/purchase_system_spec.rb +++ b/spec/system/purchase_system_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe "Purchases", type: :system, js: true, clean_and_seed_db: true do +RSpec.describe "Purchases", type: :system, js: true, seed_db: true do include ItemsHelper let(:url_prefix) { "/#{@organization.short_name}" } @@ -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