Skip to content

Commit

Permalink
refactor: remove reliance on preseeded data (requests 2) (#4329)
Browse files Browse the repository at this point in the history
* refactor: remove reliance on preseeded data (requests)

* fix: kit naming collision

---------

Co-authored-by: Daniel Orner <[email protected]>
  • Loading branch information
elasticspoon and dorner authored May 12, 2024
1 parent 0803246 commit d103c7e
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 121 deletions.
7 changes: 4 additions & 3 deletions spec/models/kit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 22 additions & 17 deletions spec/requests/admin/users_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -124,55 +129,55 @@

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
end

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
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/dashboard_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 22 additions & 17 deletions spec/requests/partners/dashboard_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
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)
end

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
Expand All @@ -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")
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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|
Expand Down
16 changes: 8 additions & 8 deletions spec/requests/partners/requests_spec.rb
Original file line number Diff line number Diff line change
@@ -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") }

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
94 changes: 49 additions & 45 deletions spec/requests/product_drives_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -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 } }

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit d103c7e

Please sign in to comment.