diff --git a/spec/models/kit_spec.rb b/spec/models/kit_spec.rb index 0f23999719..c388765b7b 100644 --- a/spec/models/kit_spec.rb +++ b/spec/models/kit_spec.rb @@ -59,10 +59,11 @@ end it "->alphabetized retrieves items in alphabetical order" do - kit_c = create(:kit, name: "C") - kit_b = create(:kit, name: "B") - kit_a = create(:kit, name: "A") + kit_c = create(:kit, name: "KitC") + kit_b = create(:kit, name: "KitB") + kit_a = create(:kit, name: "KitA") alphabetized_list = [kit_a.name, kit_b.name, kit_c.name] + expect(Kit.alphabetized.count).to eq(3) expect(Kit.alphabetized.map(&:name)).to eq(alphabetized_list) end diff --git a/spec/requests/admin/users_requests_spec.rb b/spec/requests/admin/users_requests_spec.rb index a9e9249bb7..75e26bfdcf 100644 --- a/spec/requests/admin/users_requests_spec.rb +++ b/spec/requests/admin/users_requests_spec.rb @@ -1,17 +1,22 @@ require 'rails_helper' -RSpec.describe "Admin::UsersController", type: :request do +RSpec.describe "Admin::UsersController", type: :request, 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(:super_admin) { create(:super_admin, organization: organization) } + let(:default_params) do - { organization_name: @organization.id } + { organization_name: organization.id } end - let(:org) { FactoryBot.create(:organization, name: 'Org ABC') } - let(:partner) { FactoryBot.create(:partner, name: 'Partner XYZ') } - let(:user) { FactoryBot.create(:user, organization: org, name: 'User 123') } + + let(:org) { create(:organization, name: 'Org ABC', skip_items: true) } + let(:partner) { create(:partner, name: 'Partner XYZ', organization: org) } + let(:user) { create(:user, organization: org, name: 'User 123') } context "When logged in as a super admin" do before do - sign_in(@super_admin) - create(:organization) + sign_in(super_admin) AddRoleService.call(user_id: user.id, resource_type: Role::PARTNER, resource_id: partner.id) end @@ -124,12 +129,12 @@ describe "POST #create" do it "returns http success" do - post admin_users_path, params: { user: { email: @organization.email, organization_id: @organization.id } } + post admin_users_path, params: { user: { email: organization.email, organization_id: organization.id } } expect(response).to redirect_to(admin_users_path) end it "preloads organizations" do - post admin_users_path, params: { user: { organization_id: @organization.id } } + post admin_users_path, params: { user: { organization_id: organization.id } } expect(assigns(:organizations)).to eq(Organization.all.alphabetized) end end @@ -137,42 +142,42 @@ context "When logged in as an organization_admin" do before do - sign_in @organization_admin + sign_in organization_admin create(:organization) end describe "GET #new" do it "redirects" do get new_admin_user_path - expect(response).to redirect_to(dashboard_path(organization_name: @organization_admin.organization)) + expect(response).to redirect_to(dashboard_path(organization_name: organization_admin.organization)) end end describe "POST #create" do it "redirects" do - post admin_users_path, params: { user: { organization_id: @organization.id } } - expect(response).to redirect_to(dashboard_path(organization_name: @organization_admin.organization)) + post admin_users_path, params: { user: { organization_id: organization.id } } + expect(response).to redirect_to(dashboard_path(organization_name: organization_admin.organization)) end end end context "When logged in as a non-admin user" do before do - sign_in @user + sign_in user create(:organization) end describe "GET #new" do it "redirects" do get new_admin_user_path - expect(response).to redirect_to(dashboard_path(organization_name: @user.organization)) + expect(response).to redirect_to(dashboard_path(organization_name: user.organization)) end end describe "POST #create" do it "redirects" do - post admin_users_path, params: { user: { organization_id: @organization.id } } - expect(response).to redirect_to(dashboard_path(organization_name: @user.organization)) + post admin_users_path, params: { user: { organization_id: organization.id } } + expect(response).to redirect_to(dashboard_path(organization_name: user.organization)) end end end diff --git a/spec/requests/dashboard_requests_spec.rb b/spec/requests/dashboard_requests_spec.rb index 522549e620..c12715ec8d 100644 --- a/spec/requests/dashboard_requests_spec.rb +++ b/spec/requests/dashboard_requests_spec.rb @@ -31,7 +31,7 @@ context "for another org" do it "still displays the user's org" do - # nother org + # another org get dashboard_path(organization_name: create(:organization).to_param) expect(response.body).to include(organization.name) end diff --git a/spec/requests/partners/dashboard_requests_spec.rb b/spec/requests/partners/dashboard_requests_spec.rb index 4aa1ae0760..bbbb877ae0 100644 --- a/spec/requests/partners/dashboard_requests_spec.rb +++ b/spec/requests/partners/dashboard_requests_spec.rb @@ -1,12 +1,15 @@ require "rails_helper" -RSpec.describe "/partners/dashboard", type: :request do - let(:partner) { create(:partner) } +RSpec.describe "/partners/dashboard", type: :request, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } + let(:user) { create(:user, organization: organization) } + let(:partner) { create(:partner, organization: organization) } let(:partner_user) { partner.primary_user } + let(:date) { 1.week.from_now } let(:past_date) { 1.week.ago } - let(:item1) { create(:item, name: "Good item") } - let(:item2) { create(:item, name: "Crap item") } + let(:item1) { create(:item, name: "Good item", organization: organization) } + let(:item2) { create(:item, name: "Crap item", organization: organization) } before do sign_in(partner_user) @@ -14,29 +17,31 @@ describe "GET #index" do it "displays requests that are pending" do - FactoryBot.create(:request, :pending, partner: partner, - request_items: [{item_id: item1.id, quantity: "16"}]) - FactoryBot.create(:request, :pending, partner: partner, - request_items: [{item_id: item2.id, quantity: "16"}]) + create(:request, :pending, partner: partner, request_items: [{item_id: item1.id, quantity: "16"}]) + create(:request, :pending, partner: partner, request_items: [{item_id: item2.id, quantity: "16"}]) + get partners_dashboard_path + expect(response.body).to include(item1.name) expect(response.body).to include(item2.name) end it "does not display requests in other states" do - FactoryBot.create(:request, :fulfilled, partner: partner, - request_items: [{item_id: item1.id, quantity: "16"}]) - FactoryBot.create(:request, :started, partner: partner, - request_items: [{item_id: item2.id, quantity: "16"}]) + create(:request, :fulfilled, partner: partner, request_items: [{item_id: item1.id, quantity: "16"}]) + create(:request, :started, partner: partner, request_items: [{item_id: item2.id, quantity: "16"}]) + get partners_dashboard_path + expect(response.body).to_not include(item1.name) expect(response.body).to_not include(item2.name) end end it "displays upcoming distributions" do - FactoryBot.create(:distribution, :with_items, partner: partner, organization: partner.organization, issued_at: date) + create(:distribution, :with_items, partner: partner, organization: partner.organization, issued_at: date) + get partners_dashboard_path + expect(response.body).to include("100") expect(response.body).to include(date.strftime("%m/%d/%Y")) end @@ -50,7 +55,7 @@ context "with both roles" do it "should include the switch link" do - partner_user.add_role(Role::ORG_USER, @organization) + partner_user.add_role(Role::ORG_USER, organization) allow(UsersRole).to receive(:current_role_for).and_return(partner_user.roles.find_by(name: "partner")) get partners_dashboard_path expect(response.body).to include("switch_to_role") @@ -59,19 +64,19 @@ context "BroadcastAnnouncement card" do it "displays announcements if there are valid ones" do - BroadcastAnnouncement.create(message: "test announcement", user_id: @user.id, organization_id: @organization.id) + BroadcastAnnouncement.create(message: "test announcement", user_id: user.id, organization_id: organization.id) get partners_dashboard_path expect(response.body).to include("test announcement") end it "doesn't display announcements if there are not valid ones" do - BroadcastAnnouncement.create(expiry: 5.days.ago, message: "test announcement", user_id: @user.id, organization_id: @organization.id) + BroadcastAnnouncement.create(expiry: 5.days.ago, message: "test announcement", user_id: user.id, organization_id: organization.id) get partners_dashboard_path expect(response.body).not_to include("test announcement") end it "doesn't display announcements from super admins" do - BroadcastAnnouncement.create(message: "test announcement", user_id: @user.id, organization_id: nil) + BroadcastAnnouncement.create(message: "test announcement", user_id: user.id, organization_id: nil) get partners_dashboard_path expect(response.body).not_to include("test announcement") end diff --git a/spec/requests/partners/individuals_requests_controller_spec.rb b/spec/requests/partners/individuals_requests_controller_spec.rb index 9e9c18243f..daa6220ee2 100644 --- a/spec/requests/partners/individuals_requests_controller_spec.rb +++ b/spec/requests/partners/individuals_requests_controller_spec.rb @@ -1,8 +1,10 @@ require "rails_helper" -RSpec.describe Partners::IndividualsRequestsController, type: :request do - let(:partner) { create(:partner, status: :approved) } +RSpec.describe Partners::IndividualsRequestsController, type: :request, skip_seed: true do + let(:organization) { create(:organization, :with_items) } + let(:partner) { create(:partner, status: :approved, organization: organization) } let(:partner_user) { partner.primary_user } + let(:items_to_select) { partner_user.partner.organization.valid_items.sample(3) } let(:items_attributes) do items_to_select.each_with_index.each_with_object({}) do |(item, index), hash| diff --git a/spec/requests/partners/requests_spec.rb b/spec/requests/partners/requests_spec.rb index 6132911d6b..0f722ec1f1 100644 --- a/spec/requests/partners/requests_spec.rb +++ b/spec/requests/partners/requests_spec.rb @@ -1,10 +1,12 @@ require 'rails_helper' -RSpec.describe "/partners/requests", type: :request do +RSpec.describe "/partners/requests", type: :request, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } + let(:partner) { create(:partner, organization: organization) } + let(:partner_user) { partner.primary_user } + describe "GET #index" do subject { -> { get partners_requests_path } } - let(:partner_user) { partner.primary_user } - let(:partner) { create(:partner) } let(:item1) { create(:item, name: "First item") } let(:item2) { create(:item, name: "Second item") } @@ -34,8 +36,6 @@ describe "GET #new" do subject { get new_partners_request_path } - let(:partner_user) { partner.primary_user } - let(:partner) { create(:partner) } before do sign_in(partner_user) @@ -93,21 +93,21 @@ describe "POST #create" do subject { post partners_requests_path, params: request_attributes } + let(:item1) { create(:item, name: "First item", organization: organization) } + let(:request_attributes) do { request: { comments: Faker::Lorem.paragraph, item_requests_attributes: { "0" => { - item_id: Item.all.sample.id, + item_id: item1.id, quantity: Faker::Number.within(range: 4..13) } } } } end - let(:partner_user) { partner.primary_user } - let(:partner) { create(:partner) } before do sign_in(partner_user) diff --git a/spec/requests/product_drives_requests_spec.rb b/spec/requests/product_drives_requests_spec.rb index 0120f2a76b..773c8bd9d7 100644 --- a/spec/requests/product_drives_requests_spec.rb +++ b/spec/requests/product_drives_requests_spec.rb @@ -1,7 +1,7 @@ require 'rails_helper' -RSpec.describe "ProductDrives", type: :request do - let(:organization) { create(:organization) } +RSpec.describe "ProductDrives", type: :request, skip_seed: true do + let(:organization) { create(:organization, skip_items: true) } let(:user) { create(:user, organization: organization) } let(:default_params) { { organization_name: organization.to_param } } @@ -117,49 +117,53 @@ expect(response.body).not_to include('late_product_drive') end - it "returns the quantity of all organization's items" do - product_drive = create(:product_drive, name: 'product_drive', organization: organization) - - active_item, inactive_item = organization.items.first(2) - inactive_item.update!(active: false) - - donation = create(:product_drive_donation, product_drive: product_drive) - create(:line_item, :donation, itemizable_id: donation.id, item_id: active_item.id, quantity: 4) - create(:line_item, :donation, itemizable_id: donation.id, item_id: inactive_item.id, quantity: 5) - - subject - - row = response.body.split("\n")[1] - cells = row.split(',') - expect(response.body).to include(active_item.name) - expect(response.body).to include(inactive_item.name) - expect(cells.count('4')).to eq(1) - expect(cells.count('5')).to eq(1) - expect(cells.count('0')).to eq(organization.items.count - 2) - end - - it "only counts items within the selected date range" do - default_params[:filters] = { date_range: date_range_picker_params(Date.parse('20/01/2023'), Date.parse('25/01/2023')) } - item = organization.items.first - product_drive = create( - :product_drive, - name: 'product_drive_within_date_range', - start_date: '20/01/2023', - end_date: '30/01/2023', - organization: organization - ) - - donation = create(:product_drive_donation, product_drive: product_drive, issued_at: '21/01/2023') - create(:line_item, :donation, itemizable_id: donation.id, item_id: item.id, quantity: 4) - donation = create(:product_drive_donation, product_drive: product_drive, issued_at: '26/01/2023') - create(:line_item, :donation, itemizable_id: donation.id, item_id: item.id, quantity: 10) - - subject - - row = response.body.split("\n")[1] - cells = row.split(',') - expect(cells.count('4')).to eq(2) - expect(cells.count('0')).to eq(organization.items.count - 1) + context "when organization has items" do + let(:organization) { create(:organization, :with_items) } + + it "returns the quantity of all organization's items" do + product_drive = create(:product_drive, name: 'product_drive', organization: organization) + + active_item, inactive_item = organization.items.first(2) + inactive_item.update!(active: false) + + donation = create(:product_drive_donation, product_drive: product_drive) + create(:line_item, :donation, itemizable_id: donation.id, item_id: active_item.id, quantity: 4) + create(:line_item, :donation, itemizable_id: donation.id, item_id: inactive_item.id, quantity: 5) + + subject + + row = response.body.split("\n")[1] + cells = row.split(',') + expect(response.body).to include(active_item.name) + expect(response.body).to include(inactive_item.name) + expect(cells.count('4')).to eq(1) + expect(cells.count('5')).to eq(1) + expect(cells.count('0')).to eq(organization.items.count - 2) + end + + it "only counts items within the selected date range" do + default_params[:filters] = { date_range: date_range_picker_params(Date.parse('20/01/2023'), Date.parse('25/01/2023')) } + item = organization.items.first + product_drive = create( + :product_drive, + name: 'product_drive_within_date_range', + start_date: '20/01/2023', + end_date: '30/01/2023', + organization: organization + ) + + donation = create(:product_drive_donation, product_drive: product_drive, issued_at: '21/01/2023') + create(:line_item, :donation, itemizable_id: donation.id, item_id: item.id, quantity: 4) + donation = create(:product_drive_donation, product_drive: product_drive, issued_at: '26/01/2023') + create(:line_item, :donation, itemizable_id: donation.id, item_id: item.id, quantity: 10) + + subject + + row = response.body.split("\n")[1] + cells = row.split(',') + expect(cells.count('4')).to eq(2) + expect(cells.count('0')).to eq(organization.items.count - 1) + end end end end diff --git a/spec/requests/static_requests_spec.rb b/spec/requests/static_requests_spec.rb index b2958d98c5..1ca1d34590 100644 --- a/spec/requests/static_requests_spec.rb +++ b/spec/requests/static_requests_spec.rb @@ -1,6 +1,10 @@ require "rails_helper" -RSpec.describe "Static", type: :request do +RSpec.describe "Static", type: :request, 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) } + describe "Not signed in" do describe "GET #index" do it "returns http success" do @@ -23,13 +27,13 @@ describe "Signed in" do before do - sign_in(@user) + sign_in(user) end describe "GET #index" do it "redirects to organization dashboard" do get root_path - expect(response).to redirect_to(dashboard_url(@organization)) + expect(response).to redirect_to(dashboard_url(organization)) end end end @@ -51,13 +55,14 @@ describe "Super user without org signed in" do before do - sign_in(@super_admin_no_org) + sign_in(create(:super_admin, organization: nil)) end describe "GET #index" do it "redirects to admin dashboard" do get root_path - expect(response).to redirect_to(admin_dashboard_url(@admin)) + + expect(response).to redirect_to(admin_dashboard_url) end end end diff --git a/spec/requests/storage_locations_requests_spec.rb b/spec/requests/storage_locations_requests_spec.rb index 762ab86d9c..0d7a2e57ba 100644 --- a/spec/requests/storage_locations_requests_spec.rb +++ b/spec/requests/storage_locations_requests_spec.rb @@ -1,11 +1,15 @@ -RSpec.describe "StorageLocations", type: :request do +RSpec.describe "StorageLocations", type: :request, 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(:default_params) do - { organization_name: @organization.to_param } + { organization_name: organization.to_param } end context "While signed in" do before do - sign_in(@user) + sign_in(user) end describe "GET #index" do @@ -121,7 +125,7 @@ describe "GET #edit" do it "returns http success" do - get edit_storage_location_path(default_params.merge(id: create(:storage_location, organization: @organization))) + get edit_storage_location_path(default_params.merge(id: create(:storage_location, organization: organization))) expect(response).to be_successful end end @@ -184,8 +188,8 @@ item1 = create(:item) item2 = create(:item) item3 = create(:item) - storage_location_with_items = create(:storage_location, organization: @organization) - TestInventory.create_inventory(@organization, + storage_location_with_items = create(:storage_location, organization: organization) + TestInventory.create_inventory(organization, { storage_location_with_items.id => { item1.id => 30, @@ -196,7 +200,7 @@ file = fixture_file_upload("inventory.csv", "text/csv") params = { file: file, storage_location: storage_location_with_items.id } - post import_inventory_storage_locations_path(organization_name: @organization.to_param), params: params + post import_inventory_storage_locations_path(organization_name: organization.to_param), params: params expect(response).to be_redirect expect(response).to have_error "Could not complete action: inventory already has items stored" @@ -208,7 +212,7 @@ let(:item2) { create(:item, name: "Test Item2") } let(:item3) { create(:item, name: "Test Item3", active: false) } - let(:storage_location) { create(:storage_location, organization: @organization) } + let(:storage_location) { create(:storage_location, organization: organization) } before(:each) do TestInventory.create_inventory(storage_location.organization, { storage_location.id => { @@ -261,7 +265,7 @@ expect(response.body).to include("Smithsonian") expect(response.body).to include("Test Item") # event world doesn't care about versions - expect(response.body).to include("N/A") unless Event.read_events?(@organization) + expect(response.body).to include("N/A") unless Event.read_events?(organization) end end end @@ -279,14 +283,14 @@ describe "GET #destroy" do it "redirects to #index" do - delete storage_location_path(default_params.merge(id: create(:storage_location, organization: @organization))) + delete storage_location_path(default_params.merge(id: create(:storage_location, organization: organization))) expect(response).to redirect_to(storage_locations_path) end end describe "PUT #deactivate" do context "with inventory" do - let(:storage_location) { create(:storage_location, :with_items, organization: @organization) } + let(:storage_location) { create(:storage_location, :with_items, organization: organization) } it "does not discard" do put storage_location_deactivate_path(default_params.merge(storage_location_id: storage_location.id, format: :json)) @@ -294,7 +298,7 @@ end end - let(:storage_location) { create(:storage_location, organization: @organization) } + let(:storage_location) { create(:storage_location, organization: organization) } it "discards" do put storage_location_deactivate_path(default_params.merge(storage_location_id: storage_location.id, format: :json)) @@ -303,7 +307,7 @@ end describe "PUT #reactivate" do - let(:storage_location) { create(:storage_location, organization: @organization, discarded_at: Time.zone.now) } + let(:storage_location) { create(:storage_location, organization: organization, discarded_at: Time.zone.now) } it "undiscards" do put storage_location_reactivate_path(default_params.merge(storage_location_id: storage_location.id, format: :json)) @@ -320,14 +324,14 @@ def item_to_h(view_item) } end - let(:storage_location) { create(:storage_location, :with_items, organization: @organization) } + let(:storage_location) { create(:storage_location, :with_items, organization: organization) } let(:inventory_items_at_storage_location) { storage_location.inventory_items.map(&:to_h) } - let(:inactive_inventory_items) { @organization.inventory_items.inactive.map(&:to_h) } + let(:inactive_inventory_items) { organization.inventory_items.inactive.map(&:to_h) } let(:items_at_storage_location) do - View::Inventory.new(@organization.id).items_for_location(storage_location.id).map(&method(:item_to_h)) + View::Inventory.new(organization.id).items_for_location(storage_location.id).map(&method(:item_to_h)) end let(:inactive_items) do - View::Inventory.new(@organization.id).items_for_location(storage_location.id) + View::Inventory.new(organization.id).items_for_location(storage_location.id) .select { |i| !i.active } .map(&method(:item_to_h)) end @@ -341,10 +345,12 @@ def item_to_h(view_item) end context "when also including inactive items" do + let(:organization) { create(:organization, :with_items) } + it "returns a collection that also includes items that have been deactivated" do - @organization.items.first.update(active: false) + organization.items.first.update(active: false) get inventory_storage_location_path(storage_location, default_params.merge(format: :json, include_deactivated_items: true)) - @organization.items.first.update(active: true) + organization.items.first.update(active: true) expect(response.parsed_body).to eq(items_at_storage_location + inactive_items) expect(response.parsed_body).to eq(inventory_items_at_storage_location + inactive_inventory_items) end @@ -353,7 +359,7 @@ def item_to_h(view_item) context "when also including omitted items" do it "returns a collection that also includes all items, but with zeroed quantities" do get inventory_storage_location_path(storage_location, default_params.merge(format: :json, include_omitted_items: true)) - expect(response.parsed_body.count).to eq(@organization.items.count) + expect(response.parsed_body.count).to eq(organization.items.count) end it "contains a collection of ducktyped entries that respond the same" do diff --git a/spec/requests/users/omniauth_callbacks_requests_spec.rb b/spec/requests/users/omniauth_callbacks_requests_spec.rb index 3a6d3617ca..12d1e2d047 100644 --- a/spec/requests/users/omniauth_callbacks_requests_spec.rb +++ b/spec/requests/users/omniauth_callbacks_requests_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe "Users - Omniauth Callbacks", type: :request do +RSpec.describe "Users - Omniauth Callbacks", type: :request, skip_seed: true do before do Rails.application.env_config["devise.mapping"] = Devise.mappings[:user] OmniAuth.config.test_mode = true @@ -25,17 +25,21 @@ describe "GET #google_oauth2" do context "with a valid user" do it "redirects correctly" do - FactoryBot.create(:user, email: "me@me.com") + organization = create(:organization, skip_items: true) + user = create(:user, email: "me@me.com", organization: organization) + post "/users/auth/google_oauth2/callback" + expect(session["google.token"]).to eq("token") expect(session["google.refresh_token"]).to eq("refresh token") - expect(response).to redirect_to("/?organization_name=#{@user.organization.short_name}") + expect(response).to redirect_to("/?organization_name=#{user.organization.short_name}") end end context "without a valid user" do it "should redirect to new registration URL" do post "/users/auth/google_oauth2/callback" + expect(response).to redirect_to(new_user_registration_url) expect(flash[:alert]).to eq("Authentication failed: User not found!") end