diff --git a/spec/support/date_range_picker_shared_example.rb b/spec/support/date_range_picker_shared_example.rb index 15d136ff03..9ca36e45eb 100644 --- a/spec/support/date_range_picker_shared_example.rb +++ b/spec/support/date_range_picker_shared_example.rb @@ -16,16 +16,18 @@ def date_range_picker_select_range(range_name) # I'm looking at you, spec/system/request_system_spec.rb:4 described_class.destroy_all end + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } - 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) } + 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 - sign_out @user + sign_out user travel_to Time.zone.local(2019, 7, 31) - sign_in @user + sign_in user end after do @@ -43,9 +45,9 @@ def date_range_picker_select_range(range_name) context "when choosing 'Last Month'" do before do - sign_out @user + sign_out user travel_to Time.zone.local(2019, 8, 1) - sign_in @user + sign_in user end after do diff --git a/spec/support/inventory_assistant.rb b/spec/support/inventory_assistant.rb index e37df5b497..37d7646827 100644 --- a/spec/support/inventory_assistant.rb +++ b/spec/support/inventory_assistant.rb @@ -1,11 +1,12 @@ def setup_storage_location(storage_location, *items) + organization = storage_location.organization if items.empty? - items << create(:item, organization: @organization) - items << create(:item, organization: @organization) - items << create(:item, organization: @organization) + items << create(:item, organization: organization) + items << create(:item, organization: organization) + items << create(:item, organization: organization) end - TestInventory.create_inventory(storage_location.organization, { + TestInventory.create_inventory(organization, { storage_location.id => items.map { |i| [i.id, 50] } }) end diff --git a/spec/system/adjustment_system_spec.rb b/spec/system/adjustment_system_spec.rb index 0801878b2a..324d76d030 100644 --- a/spec/system/adjustment_system_spec.rb +++ b/spec/system/adjustment_system_spec.rb @@ -1,18 +1,22 @@ -RSpec.describe "Adjustment management", type: :system, js: true do - let!(:url_prefix) { "/#{@organization.to_param}" } - let!(:storage_location) { create(:storage_location, :with_items, organization: @organization) } +RSpec.describe "Adjustment management", type: :system, js: true, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:organization_admin) { create(:organization_admin, organization: organization) } + + let!(:url_prefix) { "/#{organization.to_param}" } + let!(:storage_location) { create(:storage_location, :with_items, organization: organization) } let(:add_quantity) { 10 } let(:sub_quantity) { -10 } subject { url_prefix + "/adjustments" } before do - sign_in(@user) + sign_in(user) end context "With a new adjustment" do context "with a storage location that is bare", js: true do - let!(:bare_storage_location) { create(:storage_location, name: "We Got Nothin", organization: @organization) } + let!(:bare_storage_location) { create(:storage_location, name: "We Got Nothin", organization: organization) } before do visit subject @@ -61,7 +65,7 @@ it "politely informs the user that they're adjusting way too hard", js: true do sub_quantity = -9001 - storage_location = create(:storage_location, :with_items, name: "PICK THIS ONE", item_quantity: 10, organization: @organization) + storage_location = create(:storage_location, :with_items, name: "PICK THIS ONE", item_quantity: 10, organization: organization) visit url_prefix + "/adjustments" click_on "New Adjustment" select storage_location.name, from: "From storage location" @@ -78,7 +82,7 @@ it "politely informs the user if they try to adjust down below zero, even if they use multiple adjustments to do so" do sub_quantity = -9 - storage_location = create(:storage_location, :with_items, name: "PICK THIS ONE", item_quantity: 10, organization: @organization) + storage_location = create(:storage_location, :with_items, name: "PICK THIS ONE", item_quantity: 10, organization: organization) visit url_prefix + "/adjustments" click_on "New Adjustment" select storage_location.name, from: "From storage location" @@ -111,9 +115,9 @@ end it "can filter the #index by storage location" do - storage_location2 = create(:storage_location, name: "there", organization: @organization) - create(:adjustment, organization: @organization, storage_location: storage_location) - create(:adjustment, organization: @organization, storage_location: storage_location2) + storage_location2 = create(:storage_location, name: "there", organization: organization) + create(:adjustment, organization: organization, storage_location: storage_location) + create(:adjustment, organization: organization, storage_location: storage_location2) visit subject select storage_location.name, from: "filters[at_location]" @@ -123,12 +127,12 @@ end it "can filter the #index by user" do - storage_location2 = create(:storage_location, name: "there", organization: @organization) - create(:adjustment, organization: @organization, storage_location: storage_location, user_id: @user.id) - create(:adjustment, organization: @organization, storage_location: storage_location2, user_id: @organization_admin.id) + storage_location2 = create(:storage_location, name: "there", organization: organization) + create(:adjustment, organization: organization, storage_location: storage_location, user_id: user.id) + create(:adjustment, organization: organization, storage_location: storage_location2, user_id: organization_admin.id) visit subject - select @user.name, from: "filters[by_user]" + select user.name, from: "filters[by_user]" click_on "Filter" expect(page).to have_css("table tr", count: 2) diff --git a/spec/system/distribution_system_spec.rb b/spec/system/distribution_system_spec.rb index 209998d3fe..a0dcce80ba 100644 --- a/spec/system/distribution_system_spec.rb +++ b/spec/system/distribution_system_spec.rb @@ -1,23 +1,25 @@ -RSpec.feature "Distributions", type: :system do - before do - sign_in(@user) - @url_prefix = "/#{@organization.to_param}" - - @partner = create(:partner, organization: @organization) +RSpec.feature "Distributions", type: :system, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:storage_location) { create(:storage_location, organization: organization) } + let(:organization_admin) { create(:organization_admin, organization: organization) } + let!(:partner) { create(:partner, organization: organization) } + let(:url_prefix) { "/#{organization.to_param}" } - @storage_location = create(:storage_location, organization: @organization) - setup_storage_location(@storage_location) + before do + sign_in(user) + setup_storage_location(storage_location) end context "When going to the Pick Ups & Deliveries page" do let(:issued_at) { Time.current.utc.change(hour: 19, minute: 0).to_datetime } before do - item1 = create(:item, value_in_cents: 1050) - @distribution = create(:distribution, :with_items, item: item1, agency_rep: "A Person", organization: @user.organization, issued_at: issued_at) + item1 = create(:item, value_in_cents: 1050, organization: organization) + @distribution = create(:distribution, :with_items, item: item1, agency_rep: "A Person", organization: user.organization, issued_at: issued_at) end it "appears distribution in calendar with correct time & timezone" do - visit @url_prefix + "/distributions/schedule" + visit url_prefix + "/distributions/schedule" expect(page.find(".fc-event-time")).to have_content "7p" expect(page.find(".fc-event-title")).to have_content @distribution.partner.name end @@ -26,10 +28,10 @@ context "When creating a new distribution manually" do context "when the delivery_method is not shipped" do it "Allows a distribution to be created and shipping cost field not visible" do - visit @url_prefix + "/distributions/new" + visit url_prefix + "/distributions/new" - select @partner.name, from: "Partner" - select @storage_location.name, from: "From storage location" + select partner.name, from: "Partner" + select storage_location.name, from: "From storage location" choose "Pick up" fill_in "Comment", with: "Take my wipes... please" @@ -47,12 +49,12 @@ end it "Displays a complete form after validation errors" do - visit @url_prefix + "/distributions/new" + visit url_prefix + "/distributions/new" # verify line items appear on initial load expect(page).to have_selector "#distribution_line_items" - select @partner.name, from: "Partner" + select partner.name, from: "Partner" expect do click_button "Save" end.not_to change { ActionMailer::Base.deliveries.count } @@ -64,10 +66,10 @@ context "when the delivery_method is shipped and shipping cost is none-negative" do it "Allows a distribution to be created" do - visit @url_prefix + "/distributions/new" + visit url_prefix + "/distributions/new" - select @partner.name, from: "Partner" - select @storage_location.name, from: "From storage location" + select partner.name, from: "Partner" + select storage_location.name, from: "From storage location" choose "Shipped" # to check if shipping_cost field exist @@ -86,18 +88,18 @@ context "when the quantity is lower than the on hand minimum quantity" do it "should display an error" do - item = View::Inventory.new(@organization.id).items_for_location(@storage_location.id).first.db_item + item = View::Inventory.new(organization.id).items_for_location(storage_location.id).first.db_item item.update!(on_hand_minimum_quantity: 5) - TestInventory.create_inventory(@organization, + TestInventory.create_inventory(organization, { - @storage_location.id => { item.id => 20 } + storage_location.id => { item.id => 20 } }) - visit @url_prefix + "/distributions/new" - select @partner.name, from: "Partner" - select @storage_location.name, from: "From storage location" + visit url_prefix + "/distributions/new" + select partner.name, from: "Partner" + select storage_location.name, from: "From storage location" select2(page, 'distribution_line_items_item_id', item.name, position: 1) - select @storage_location.name, from: "distribution_storage_location_id" + select storage_location.name, from: "distribution_storage_location_id" fill_in "distribution_line_items_attributes_0_quantity", with: 18 click_button "Save" @@ -109,18 +111,18 @@ context "when the quantity is lower than the on hand recommended quantity" do it "should display an alert" do - item = View::Inventory.new(@organization.id).items_for_location(@storage_location.id).first.db_item + item = View::Inventory.new(organization.id).items_for_location(storage_location.id).first.db_item item.update!(on_hand_minimum_quantity: 1, on_hand_recommended_quantity: 5) - TestInventory.create_inventory(@organization, + TestInventory.create_inventory(organization, { - @storage_location.id => { item.id => 20 } + storage_location.id => { item.id => 20 } }) - visit @url_prefix + "/distributions/new" - select @partner.name, from: "Partner" + visit url_prefix + "/distributions/new" + select partner.name, from: "Partner" await_select2("#distribution_line_items_attributes_0_item_id") do - select @storage_location.name, from: "From storage location" + select storage_location.name, from: "From storage location" end select item.name, from: "distribution_line_items_attributes_0_item_id" @@ -134,15 +136,15 @@ context "when there is insufficient inventory to fulfill the Distribution" do it "gracefully handles the error" do - visit @url_prefix + "/distributions/new" + visit url_prefix + "/distributions/new" - select @partner.name, from: "Partner" - select @storage_location.name, from: "From storage location" + select partner.name, from: "Partner" + select storage_location.name, from: "From storage location" choose "Delivery" fill_in "Comment", with: "Take my wipes... please" - item = View::Inventory.new(@organization.id).items_for_location(@storage_location.id).first + item = View::Inventory.new(organization.id).items_for_location(storage_location.id).first quantity = item.quantity select item.name, from: "distribution_line_items_attributes_0_item_id" fill_in "distribution_line_items_attributes_0_quantity", with: quantity * 2 @@ -153,29 +155,29 @@ end.not_to change { Distribution.count } expect(page).to have_content("New Distribution") - message = Event.read_events?(@organization) ? 'Could not reduce quantity' : 'items exceed the available inventory' + message = Event.read_events?(organization) ? 'Could not reduce quantity' : 'items exceed the available inventory' expect(page.find(".alert")).to have_content message end end context "when there is a default storage location" do it "automatically selects the default storage location" do - @organization.default_storage_location = @storage_location.id - visit @url_prefix + "/distributions/new" - expect(find("#distribution_storage_location_id").text).to eq(@storage_location.name) + organization.default_storage_location = storage_location.id + visit url_prefix + "/distributions/new" + expect(find("#distribution_storage_location_id").text).to eq(storage_location.name) end end it "should not display inactive storage locations in dropdown" do inactive_location = create(:storage_location, name: "Inactive R Us", discarded_at: Time.zone.now) setup_storage_location(inactive_location) - visit @url_prefix + "/distributions/new" + visit url_prefix + "/distributions/new" expect(page).to have_no_content "Inactive R Us" end end it "errors if user does not fill storage_location" do - visit @url_prefix + "/distributions/new" + visit url_prefix + "/distributions/new" - select @partner.name, from: "Partner" + select partner.name, from: "Partner" select "", from: "From storage location" click_button "Save", match: :first @@ -184,12 +186,12 @@ end context "With an existing distribution" do - let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", delivery_method: delivery_method, organization: @user.organization) } + let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", delivery_method: delivery_method, organization: user.organization) } let(:delivery_method) { "pick_up" } before do - sign_in(@organization_admin) - visit @url_prefix + "/distributions" + sign_in(organization_admin) + visit url_prefix + "/distributions" end it "the user can make changes" do @@ -206,7 +208,7 @@ allow(DistributionMailer).to receive(:reminder_email).and_return(job) allow(job).to receive(:deliver_later) - visit @url_prefix + "/distributions" + visit url_prefix + "/distributions" click_on "Edit", match: :first fill_in "Agency representative", with: "SOMETHING DIFFERENT" click_on "Save", match: :first @@ -231,7 +233,7 @@ click_on "Save", match: :first end.not_to change { distribution.line_items.first.quantity } within ".alert" do - message = Event.read_events?(@organization) ? 'Could not reduce quantity' : 'items exceed the available inventory' + message = Event.read_events?(organization) ? 'Could not reduce quantity' : 'items exceed the available inventory' expect(page).to have_content message end end @@ -287,18 +289,18 @@ context "When attempting to edit a distribution" do context "after the distribution issued_at has passed or it has been marked complete" do - let!(:past_distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: @user.organization, issued_at: Time.zone.yesterday, state: :scheduled) } - let!(:complete_distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: @user.organization, issued_at: Time.zone.today, state: :complete) } + let!(:past_distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: user.organization, issued_at: Time.zone.yesterday, state: :scheduled) } + let!(:complete_distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: user.organization, issued_at: Time.zone.today, state: :complete) } it "does not contain a Edit button" do - visit @url_prefix + "/distributions" + visit url_prefix + "/distributions" expect(page).not_to have_button("Edit") end it "cannot be accessed directly" do - visit @url_prefix + "/distributions/#{past_distribution.id}/edit" + visit url_prefix + "/distributions/#{past_distribution.id}/edit" expect(page.find(".alert-danger")).to have_content "you must be an organization admin" - visit @url_prefix + "/distributions/#{complete_distribution.id}/edit" + visit url_prefix + "/distributions/#{complete_distribution.id}/edit" expect(page.find(".alert-danger")).to have_content "you must be an organization admin" end end @@ -309,19 +311,19 @@ # since we're creating a distribution yesterday (i.e. last year) # and it won't show any distributions for this year travel_to Time.zone.local(2023, 5, 5) - sign_in(@organization_admin) + sign_in(organization_admin) end - let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: @user.organization, issued_at: Time.zone.today.prev_day, state: :complete) } + let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: user.organization, issued_at: Time.zone.today.prev_day, state: :complete) } it "can click on Edit button and a warning appears " do - visit @url_prefix + "/distributions" + visit url_prefix + "/distributions" click_on "Edit", match: :first expect(page.find(".alert-warning")).to have_content "The current date is past the date this distribution was scheduled for." end it "can be accessed directly" do - visit @url_prefix + "/distributions/#{distribution.id}/edit" + visit url_prefix + "/distributions/#{distribution.id}/edit" expect(page).to have_no_css(".alert-danger") expect(page.find(".alert-warning")).to have_content "The current date is past the date this distribution was scheduled for." end @@ -333,10 +335,10 @@ item1 = create(:item, value_in_cents: 1050) item2 = create(:item) item3 = create(:item, value_in_cents: 100) - @distribution1 = create(:distribution, :with_items, item: item1, agency_rep: "A Person", organization: @user.organization) - create(:distribution, :with_items, item: item2, agency_rep: "A Person", organization: @user.organization) - @distribution3 = create(:distribution, :with_items, item: item3, agency_rep: "A Person", organization: @user.organization) - visit @url_prefix + "/distributions" + @distribution1 = create(:distribution, :with_items, item: item1, agency_rep: "A Person", organization: user.organization) + create(:distribution, :with_items, item: item2, agency_rep: "A Person", organization: user.organization) + @distribution3 = create(:distribution, :with_items, item: item3, agency_rep: "A Person", organization: user.organization) + visit url_prefix + "/distributions" end it 'the user sees value in row on index page' do @@ -351,21 +353,21 @@ it 'the user sees value per item on show page' do # item value 10.50 - visit @url_prefix + "/distributions/#{@distribution1.id}" + visit url_prefix + "/distributions/#{@distribution1.id}" expect(page).to have_content "$10.50" end it 'the user sees total value on show page' do # 100 items * 10.5 - visit @url_prefix + "/distributions/#{@distribution1.id}" + visit url_prefix + "/distributions/#{@distribution1.id}" expect(page).to have_content "$1,050" end end context "When showing a individual distribution" do - let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: @user.organization, issued_at: Time.zone.today, state: :complete, delivery_method: "pick_up") } + let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: user.organization, issued_at: Time.zone.today, state: :complete, delivery_method: "pick_up") } - before { visit @url_prefix + "/distributions/#{distribution.id}" } + before { visit url_prefix + "/distributions/#{distribution.id}" } it "Show partner name in title" do expect(page).to have_content("Distribution from #{distribution.storage_location.name} to #{distribution.partner.name}") @@ -375,11 +377,11 @@ context "When creating a distribution from a donation" do let(:donation) { create :donation, :with_items } before do - visit @url_prefix + "/donations/#{donation.id}" - sign_in(@organization_admin) + visit url_prefix + "/donations/#{donation.id}" + sign_in(organization_admin) click_on "Start a new Distribution" within "#new_distribution" do - select @partner.name, from: "Partner" + select partner.name, from: "Partner" choose "Pick up" click_button "Save" end @@ -415,7 +417,7 @@ expect(page).to have_no_content "Distribution updated!" message = 'items exceed the available inventory' number = 999_999 - if Event.read_events?(@organization) + if Event.read_events?(organization) message = 'Could not reduce quantity' number = 999_899 end @@ -453,14 +455,14 @@ # TODO: This should probably be in the Request resource specs, not Distribution context "When creating a distribution from a request" do it "sets the distribution id and fulfilled status on the request" do - items = @storage_location.items.pluck(:id).sample(2) + items = storage_location.items.pluck(:id).sample(2) request_items = [{ "item_id" => items[0], "quantity" => 10 }, { "item_id" => items[1], "quantity" => 10 }] - @request = create :request, organization: @organization, request_items: request_items + @request = create :request, organization: organization, request_items: request_items - visit @url_prefix + "/requests/#{@request.id}" + visit url_prefix + "/requests/#{@request.id}" click_on "Fulfill request" within "#new_distribution" do - select @storage_location.name, from: "From storage location" + select storage_location.name, from: "From storage location" choose "Delivery" click_on "Save" end @@ -473,14 +475,14 @@ end it "maintains the connection with the request even when there are initial errors" do - items = @storage_location.items.pluck(:id).sample(2) + items = storage_location.items.pluck(:id).sample(2) request_items = [{ "item_id" => items[0], "quantity" => 1000000 }, { "item_id" => items[1], "quantity" => 10 }] - @request = create :request, organization: @organization, request_items: request_items + @request = create :request, organization: organization, request_items: request_items - visit @url_prefix + "/requests/#{@request.id}" + visit url_prefix + "/requests/#{@request.id}" click_on "Fulfill request" within "#new_distribution" do - select @storage_location.name, from: "From storage location" + select storage_location.name, from: "From storage location" choose "Delivery" click_on "Save" end @@ -499,44 +501,52 @@ end context "via barcode entry" do - before(:each) do - initialize_barcodes - visit @url_prefix + "/distributions/new" - end + let(:existing_barcode) { create(:barcode_item) } + let(:item_with_barcode) { existing_barcode.item } + let(:item_no_barcode) { create(:item) } it "allows users to add items via scanning them in by barcode", js: true do - Barcode.boop(@existing_barcode.value) + visit url_prefix + "/distributions/new" + + Barcode.boop(existing_barcode.value) # the form should update page.find_field(id: "distribution_line_items_attributes_0_quantity", with: "50") qty = page.find(:xpath, '//input[@id="distribution_line_items_attributes_0_quantity"]').value - expect(qty).to eq(@existing_barcode.quantity.to_s) + expect(qty).to eq(existing_barcode.quantity.to_s) end - it "a user can add items that do not yet have a barcode" do - barcode_value = "123123123321" - Barcode.boop(barcode_value) - - within ".modal-content" do - page.fill_in "Quantity", with: "51" - select "Adult Briefs (Large/X-Large)", from: "Item" - click_on "Save" + context 'when a specific item exists' do + before do + create(:item, organization: organization, name: "VerySpecificItem") + visit url_prefix + "/distributions/new" end - visit @url_prefix + "/distributions/new" - Barcode.boop(barcode_value) + it "a user can add items that do not yet have a barcode" do + barcode_value = "123123123321" + Barcode.boop(barcode_value) + + within ".modal-content" do + page.fill_in "Quantity", with: "51" + select "VerySpecificItem", from: "Item" + click_on "Save" + end + + visit url_prefix + "/distributions/new" + Barcode.boop(barcode_value) - expect(page).to have_text("Adult Briefs (Large/X-Large)") - expect(page).to have_field("Quantity", with: "51") + expect(page).to have_text("VerySpecificItem") + expect(page).to have_field("Quantity", with: "51") + end end end context "when filtering on the index page" do - subject { @url_prefix + "/distributions" } + subject { url_prefix + "/distributions" } let(:item_category) { create(:item_category) } - 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) } + 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) @@ -554,7 +564,7 @@ # check for heading text expect(page).to have_css("table thead tr th", text: "Total #{item1.name}") # check for count update - stored_item1_total = @storage_location.item_total(item1.id) + stored_item1_total = storage_location.item_total(item1.id) expect(page).to have_css("table tbody tr td", text: stored_item1_total) end @@ -632,15 +642,15 @@ end it "allows completion of corrected distribution with depleted inventory item" do - visit @url_prefix + "/distributions/new" - item = View::Inventory.new(@organization.id).items_for_location(@storage_location.id).first.db_item - TestInventory.create_inventory(@organization, + visit url_prefix + "/distributions/new" + item = View::Inventory.new(organization.id).items_for_location(storage_location.id).first.db_item + TestInventory.create_inventory(organization, { - @storage_location.id => { item.id => 20 } + storage_location.id => { item.id => 20 } }) - select @partner.name, from: "Partner" - select @storage_location.name, from: "From storage location" + select partner.name, from: "Partner" + select storage_location.name, from: "From storage location" choose "Delivery" select item.name, from: "distribution_line_items_attributes_0_item_id" fill_in "distribution_line_items_attributes_0_quantity", with: 15 @@ -656,9 +666,9 @@ expect(page).to have_content("Distribution Complete") expect(page).to have_link("Distribution Complete") - expect(@storage_location.inventory_items.first.quantity).to eq(0) - expect(View::Inventory.new(@organization.id) - .quantity_for(item_id: item.id, storage_location: @storage_location.id)).to eq(0) + expect(storage_location.inventory_items.first.quantity).to eq(0) + expect(View::Inventory.new(organization.id) + .quantity_for(item_id: item.id, storage_location: storage_location.id)).to eq(0) click_link "Distribution Complete" expect(page).to have_content('Distribution') diff --git a/spec/system/donation_system_spec.rb b/spec/system/donation_system_spec.rb index bc1a1724c0..16a59af8d2 100644 --- a/spec/system/donation_system_spec.rb +++ b/spec/system/donation_system_spec.rb @@ -1,11 +1,15 @@ -RSpec.describe "Donations", type: :system, js: true do +RSpec.describe "Donations", type: :system, js: true, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:organization_admin) { create(:organization_admin, organization: organization) } + before do - @url_prefix = "/#{@organization.short_name}" + @url_prefix = "/#{organization.short_name}" end context "When signed in as a normal user" do before do - sign_in @user + sign_in user end context "When visiting the index page" do @@ -20,7 +24,7 @@ it "Allows User to click to the new donation form" do find(".fa-plus").click - expect(current_path).to eq(new_donation_path(@organization)) + expect(current_path).to eq(new_donation_path(organization)) expect(page).to have_content "Start a new donation" end @@ -150,13 +154,13 @@ context "When creating a new donation" do before do - create(:item, organization: @organization) - create(:storage_location, organization: @organization) - create(:donation_site, organization: @organization) - create(:product_drive, organization: @organization) - create(:product_drive_participant, organization: @organization) - create(:manufacturer, organization: @organization) - @organization.reload + create(:item, organization: organization) + create(:storage_location, organization: organization) + create(:donation_site, organization: organization) + create(:product_drive, organization: organization) + create(:product_drive_participant, organization: organization) + create(:manufacturer, organization: organization) + organization.reload end context "Via manual entry" do @@ -176,7 +180,7 @@ click_button "Save" end.to change { Donation.count }.by(1) - expect(DonationEvent.last.user).to eq(@user) + expect(DonationEvent.last.user).to eq(user) expect(Donation.last.issued_at).to eq(Time.zone.parse("2001-01-01")) end @@ -365,7 +369,7 @@ # When a user creates a donation without it passing validation, the items # dropdown is not populated on the return trip. it "Repopulates items dropdown even if initial submission doesn't validate" do - item_count = @organization.items.count + 1 # Adds 1 for the "choose an item" option + item_count = organization.items.count + 1 # Adds 1 for the "choose an item" option expect(page).to have_xpath("//select[@id='donation_line_items_attributes_0_item_id']/option", count: item_count + 1) click_button "Save" @@ -508,7 +512,7 @@ # make sure there are no other items associated with that base_item in this org Item.where(partner_key: base_item.partner_key).delete_all # Now create an item that's associated with that base item, - @item = create(:item, base_item: base_item, organization: @organization, created_at: 1.week.ago) + @item = create(:item, base_item: base_item, organization: organization, created_at: 1.week.ago) end it "Adds the oldest item it can find for the global barcode" do @@ -561,14 +565,14 @@ context "When editing an existing donation" do before do - item = create(:item, organization: @organization, name: "Rare Candy") - create(:storage_location, organization: @organization) - create(:donation_site, organization: @organization) - create(:product_drive, organization: @organization) - create(:product_drive_participant, organization: @organization) - create(:manufacturer, organization: @organization) - create(:donation, :with_items, item: item, organization: @organization) - @organization.reload + item = create(:item, organization: organization, name: "Rare Candy") + create(:storage_location, organization: organization) + create(:donation_site, organization: organization) + create(:product_drive, organization: organization) + create(:product_drive_participant, organization: organization) + create(:manufacturer, organization: organization) + create(:donation, :with_items, item: item, organization: organization) + organization.reload visit @url_prefix + "/donations/" end @@ -664,7 +668,7 @@ context "while signed in as an organization admin" do before do - sign_in(@organization_admin) + sign_in(organization_admin) end context "When viewing an existing donation" do diff --git a/spec/system/kit_system_spec.rb b/spec/system/kit_system_spec.rb index 293507a768..8e6cdd594b 100644 --- a/spec/system/kit_system_spec.rb +++ b/spec/system/kit_system_spec.rb @@ -1,26 +1,31 @@ -RSpec.describe "Kit management", type: :system do +RSpec.describe "Kit management", type: :system, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + before do - sign_in(@user) + sign_in(user) end - let!(:storage_location) { create(:storage_location, organization: @organization) } + + let!(:storage_location) { create(:storage_location, organization: organization) } let!(:existing_kit) do kit_params = { - organization_id: @organization.id, + organization_id: organization.id, name: Faker::Appliance.equipment, line_items_attributes: { "0": { item_id: existing_kit_item_1.id, quantity: existing_kit_item_1_quantity }, "1": { item_id: existing_kit_item_2.id, quantity: existing_kit_item_2_quantity } } } - kit_creation_service = KitCreateService.new(organization_id: @organization.id, kit_params: kit_params).tap(&:call) + kit_creation_service = KitCreateService.new(organization_id: organization.id, kit_params: kit_params).tap(&:call) kit_creation_service.kit end + let!(:existing_kit_item_1) { create(:item) } let!(:existing_kit_item_1_quantity) { 5 } let!(:existing_kit_item_2) { create(:item) } let!(:existing_kit_item_2_quantity) { 3 } before(:each) do - TestInventory.create_inventory(@organization, { + TestInventory.create_inventory(organization, { storage_location.id => { existing_kit_item_1.id => 50, existing_kit_item_2.id => 50 @@ -28,7 +33,7 @@ }) end - let!(:url_prefix) { "/#{@organization.to_param}" } + let!(:url_prefix) { "/#{organization.to_param}" } it "can create a new kit as a user with the proper quantity" do visit url_prefix + "/kits/new" @@ -54,7 +59,7 @@ click_on 'Modify Allocation' - inventory = View::Inventory.new(@organization.id) + inventory = View::Inventory.new(organization.id) original_kit_count = inventory.quantity_for(item_id: existing_kit.item.id, storage_location: storage_location.id) original_item_1_count = inventory.quantity_for(item_id: existing_kit_item_1.id, storage_location: storage_location.id) original_item_2_count = inventory.quantity_for(item_id: existing_kit_item_2.id, storage_location: storage_location.id) @@ -108,13 +113,13 @@ end it 'should not display inactive storage locations under allocations' do - inactive_location = create(:storage_location, organization_id: @organization.id, name: "Inactive R Us", discarded_at: Time.zone.now) + inactive_location = create(:storage_location, organization_id: organization.id, name: "Inactive R Us", discarded_at: Time.zone.now) setup_storage_location(inactive_location) kit_params = { - organization_id: @organization.id, + organization_id: organization.id, name: "Fake Kit" } - KitCreateService.new(organization_id: @organization.id, kit_params: kit_params).tap(&:call).kit + KitCreateService.new(organization_id: organization.id, kit_params: kit_params).tap(&:call).kit visit url_prefix + "/kits/" expect(page).to have_no_text("Inactive R Us") end @@ -130,7 +135,7 @@ click_on 'Modify Allocation' - inventory = View::Inventory.new(@organization.id) + inventory = View::Inventory.new(organization.id) original_kit_count = inventory.quantity_for(item_id: existing_kit.item.id, storage_location: storage_location.id) original_item_1_count = inventory.quantity_for(item_id: existing_kit_item_1.id, storage_location: storage_location.id) original_item_2_count = inventory.quantity_for(item_id: existing_kit_item_2.id, storage_location: storage_location.id) @@ -167,7 +172,7 @@ context 'when there is no kit quantity' do before do # Force there to be no kit quantity available - TestInventory.create_inventory(@organization, { + TestInventory.create_inventory(organization, { storage_location.id => { existing_kit.item.id => 0, existing_kit_item_1.id => 50, @@ -181,7 +186,7 @@ click_on 'Modify Allocation' - inventory = View::Inventory.new(@organization.id) + inventory = View::Inventory.new(organization.id) original_kit_count = inventory.quantity_for(item_id: existing_kit.item.id, storage_location: storage_location.id) original_item_1_count = inventory.quantity_for(item_id: existing_kit_item_1.id, storage_location: storage_location.id) original_item_2_count = inventory.quantity_for(item_id: existing_kit_item_2.id, storage_location: storage_location.id) diff --git a/spec/system/purchase_system_spec.rb b/spec/system/purchase_system_spec.rb index 2ee921fe8f..3238bd8cb9 100644 --- a/spec/system/purchase_system_spec.rb +++ b/spec/system/purchase_system_spec.rb @@ -1,11 +1,15 @@ -RSpec.describe "Purchases", type: :system, js: true do +RSpec.describe "Purchases", type: :system, js: true, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:organization_admin) { create(:organization_admin, organization: organization) } + include ItemsHelper - let(:url_prefix) { "/#{@organization.short_name}" } + let(:url_prefix) { "/#{organization.short_name}" } context "while signed in as a normal user" do before :each do - sign_in @user + sign_in user end context "When visiting the index page" do @@ -24,22 +28,22 @@ it "User can click to the new purchase form" do find(".fa-plus").click - expect(current_path).to eq(new_purchase_path(@organization)) + expect(current_path).to eq(new_purchase_path(organization)) expect(page).to have_content "Start a new purchase" end it "User sees purchased date column" do - storage1 = create(:storage_location, name: "storage1", organization: @organization) + storage1 = create(:storage_location, name: "storage1", organization: organization) purchase_date = 1.week.ago - create(:purchase, storage_location: storage1, issued_at: purchase_date, organization: @organization) + 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, organization: @organization) - purchase2 = create(:purchase, amount_spent_in_cents: 2345, organization: @organization) + 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 +54,15 @@ end context "When filtering on the index page" do - let!(:item) { create(:item, organization: @organization) } - let(:storage) { create(:storage_location, organization: @organization) } + 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", organization: @organization) - storage2 = create(:storage_location, name: "storage2", organization: @organization) - create(:purchase, storage_location: storage1, organization: @organization) - create(:purchase, storage_location: storage2, organization: @organization) + 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]" @@ -88,10 +92,10 @@ context "When creating a new purchase" do before(:each) do - @item = create(:item, organization: @organization) - @storage_location = create(:storage_location, organization: @organization) - @vendor = create(:vendor, organization: @organization) - @organization.reload + @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" } @@ -188,7 +192,7 @@ # When a user creates a purchase without it passing validation, the items # dropdown is not populated on the return trip. it "items dropdown is still repopulated even if initial submission doesn't validate" do - item_count = @organization.items.count + 1 # Adds 1 for the "choose an item" option + item_count = organization.items.count + 1 # Adds 1 for the "choose an item" option expect(page).to have_css("#purchase_line_items_attributes_0_item_id option", count: item_count + 1) click_button "Save" @@ -204,26 +208,26 @@ 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", organization: @organization) - visit edit_purchase_path(@organization.to_param, purchase) + 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 it "A user can view another organizations purchase" do purchase = create(:purchase, organization: create(:organization)) - visit edit_purchase_path(@user.organization.short_name, purchase) + visit edit_purchase_path(user.organization.short_name, purchase) expect(page).to have_content("Still haven't found what you're looking for") end end context "via barcode entry" do before(:each) do - @existing_barcode = create(:barcode_item, organization: @organization) + @existing_barcode = create(:barcode_item, organization: organization) @item_with_barcode = @existing_barcode.item - @item_no_barcode = create(:item, organization: @organization) + @item_no_barcode = create(:item, organization: organization) visit url_prefix + "/purchases/new" end @@ -288,19 +292,19 @@ subject { url_prefix + "/purchases" } it "does not allow deletion of a purchase" do - purchase = create(:purchase, organization: @organization) + 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, organization: @organization) } + 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 - sign_in @organization_admin + sign_in organization_admin end it "allows deletion of a purchase" do diff --git a/spec/system/request_system_spec.rb b/spec/system/request_system_spec.rb index 7fb0026267..6e4c7ab4c9 100644 --- a/spec/system/request_system_spec.rb +++ b/spec/system/request_system_spec.rb @@ -1,16 +1,18 @@ -RSpec.describe "Requests", type: :system, js: true do - let!(:url_prefix) { "/#{@organization.to_param}" } +RSpec.describe "Requests", type: :system, js: true, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let!(:url_prefix) { "/#{organization.to_param}" } let(:item1) { create(:item, name: "Good item") } let(:item2) { create(:item, name: "Crap item") } let(:partner1) { create(:partner, name: "This Guy", email: "thisguy@example.com") } let(:partner2) { create(:partner, name: "That Guy", email: "ntg@example.com") } - let!(:storage_location) { create(:storage_location, organization: @organization) } + let!(:storage_location) { create(:storage_location, organization: organization) } before do - sign_in(@user) + sign_in(user) travel_to Time.zone.local(2020, 1, 1) - TestInventory.create_inventory(@organization, { + TestInventory.create_inventory(organization, { storage_location.id => { item1.id => 500, item2.id => 500 @@ -127,7 +129,7 @@ { item_id: item2.id, quantity: 100} ] } - let!(:request) { create(:request, request_items: request_items, organization: @organization) } + let!(:request) { create(:request, request_items: request_items, organization: organization) } it "should show the request with a request sender if a partner user is set" do visit subject @@ -154,10 +156,10 @@ # Create a secondary storage location to test the sum view of estimated on-hand items # Add inventory items to both storage locations #### - second_storage_location = create(:storage_location, organization: @organization) + second_storage_location = create(:storage_location, organization: organization) TestInventory.clear_inventory(storage_location) travel 1.second - TestInventory.create_inventory(@organization, + TestInventory.create_inventory(organization, { storage_location.id => { item1.id => 234, @@ -203,7 +205,7 @@ end describe 'canceling a request as a bank user' do - let!(:request) { create(:request, organization: @organization) } + let!(:request) { create(:request, organization: organization) } context 'when a bank user cancels a request' do let(:reason) { Faker::Lorem.sentence } diff --git a/spec/system/transfer_system_spec.rb b/spec/system/transfer_system_spec.rb index f125220f0b..faa993ee0b 100644 --- a/spec/system/transfer_system_spec.rb +++ b/spec/system/transfer_system_spec.rb @@ -1,8 +1,12 @@ -RSpec.describe "Transfer management", type: :system do +RSpec.describe "Transfer management", type: :system, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:organization_admin) { create(:organization_admin, organization: organization) } + before do - sign_in(@user) + sign_in(user) end - let!(:url_prefix) { "/#{@organization.to_param}" } + let!(:url_prefix) { "/#{organization.to_param}" } let(:item) { create(:item) } def create_transfer(amount, from_name, to_name) @@ -19,17 +23,17 @@ def create_transfer(amount, from_name, to_name) end it "can transfer an inventory from a storage location to another as a user" do - from_storage_location = create(:storage_location, :with_items, item: item, name: "From me", organization: @organization) - to_storage_location = create(:storage_location, :with_items, name: "To me", organization: @organization) + from_storage_location = create(:storage_location, :with_items, item: item, name: "From me", organization: organization) + to_storage_location = create(:storage_location, :with_items, name: "To me", organization: organization) create_transfer("10", from_storage_location.name, to_storage_location.name) expect(page).to have_content("10 items have been transferred") end it "can delete a transfer to undo the inventory count changes" do - from_storage_location = create(:storage_location, :with_items, item: item, name: "From me", organization: @organization) - to_storage_location = create(:storage_location, :with_items, name: "To me", organization: @organization) + from_storage_location = create(:storage_location, :with_items, item: item, name: "From me", organization: organization) + to_storage_location = create(:storage_location, :with_items, name: "To me", organization: organization) - inventory = View::Inventory.new(@organization.id) + inventory = View::Inventory.new(organization.id) original_from_storage_item_count = inventory.quantity_for(storage_location: from_storage_location.id, item_id: item.id) original_from_ii_storage_item_count = from_storage_location.inventory_items.find_by(item_id: item.id).quantity expect(original_from_storage_item_count).to eq(original_from_ii_storage_item_count) @@ -62,10 +66,10 @@ def create_transfer(amount, from_name, to_name) end it 'shows a error when deleting a transfer that causes an insufficient inventory counts' do - from_storage_location = create(:storage_location, :with_items, item: item, name: "From me", organization: @organization) - to_storage_location = create(:storage_location, :with_items, name: "To me", organization: @organization) + from_storage_location = create(:storage_location, :with_items, item: item, name: "From me", organization: organization) + to_storage_location = create(:storage_location, :with_items, name: "To me", organization: organization) - inventory = View::Inventory.new(@organization.id) + inventory = View::Inventory.new(organization.id) original_from_storage_item_count = inventory.quantity_for(storage_location: from_storage_location.id, item_id: item.id) original_from_ii_storage_item_count = from_storage_location.inventory_items.find_by(item_id: item.id).quantity expect(original_from_storage_item_count).to eq(original_from_ii_storage_item_count) @@ -105,8 +109,8 @@ def create_transfer(amount, from_name, to_name) end context "when there's insufficient inventory at the origin to cover the move" do - let!(:from_storage_location) { create(:storage_location, :with_items, item: item, item_quantity: 10, name: "From me", organization: @organization) } - let!(:to_storage_location) { create(:storage_location, :with_items, name: "To me", organization: @organization) } + let!(:from_storage_location) { create(:storage_location, :with_items, item: item, item_quantity: 10, name: "From me", organization: organization) } + let!(:to_storage_location) { create(:storage_location, :with_items, name: "To me", organization: organization) } scenario "User can transfer an inventory from a storage location to another" do create_transfer("100", from_storage_location.name, to_storage_location.name) @@ -117,10 +121,10 @@ def create_transfer(amount, from_name, to_name) context "when viewing the index page" do subject { url_prefix + "/transfers" } it "can filter the #index by storage location both from and to as a user" do - from_storage_location = create(:storage_location, name: "here", organization: @organization) - to_storage_location = create(:storage_location, name: "there", organization: @organization) - create(:transfer, organization: @organization, from: from_storage_location, to: to_storage_location) - create(:transfer, organization: @organization, from: to_storage_location, to: from_storage_location) + from_storage_location = create(:storage_location, name: "here", organization: organization) + to_storage_location = create(:storage_location, name: "there", organization: organization) + create(:transfer, organization: organization, from: from_storage_location, to: to_storage_location) + create(:transfer, organization: organization, from: to_storage_location, to: from_storage_location) visit subject select to_storage_location.name, from: "filters[to_location]"