Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Superadmin Partners View (4682) & Category Dropdown Error (4674) #4703

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion app/controllers/admin/partners_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Admin::PartnersController < AdminController
def index
@partners = Partner.all.includes(:organization)
@partners = Partner.all.includes(:organization).order("LOWER(name)")
end

def show
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def create
@base_items = BaseItem.without_kit.alphabetized
# Define a @item to be used in the `new` action to be rendered with
# the provided parameters. This is required to render the page again
# with the error + the invalid parameters
# with the error + the invalid parameters.
@item_categories = current_organization.item_categories.order('name ASC') # Load categories here
@item = current_organization.items.new(item_params)
flash[:error] = result.error.record.errors.full_messages.to_sentence
render action: :new
Expand Down
49 changes: 49 additions & 0 deletions spec/controllers/admin/partners_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
RSpec.describe Admin::PartnersController, type: :controller do
let(:organization) { create(:organization) }

# Use the super_admin instead of organization_admin
let(:super_admin) { create(:super_admin) }

# Ensure partners are created before the test
let!(:partner2) { create(:partner, name: "Bravo", organization: organization) }
let!(:partner1) { create(:partner, name: "alpha", organization: organization) }
let!(:partner3) { create(:partner, name: "Zeus", organization: organization) }

let(:default_params) do
{ organization_id: organization.id }
end

context "When logged in as a super admin" do
before do
sign_in(super_admin) # Sign in as the super_admin
end

describe "GET #index" do
it "returns http success" do
get :index
expect(response).to be_successful
end

it "assigns partners ordered by name (case-insensitive)" do
get :index
expect(assigns(:partners)).to eq([partner1, partner2, partner3].sort_by { |p| p.name.downcase })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

expect(assigns(:partners).map(&:name)).to eq(%w(alpha Bravo Zeus))

But if you really want to test this, you should swap the names of partners 1 and 2. This way the default order would not be alphabetical and you'd actually be testing that the sort works. 😄

end
end
end

context "When logged in as a non-admin user" do
let(:user) { create(:user) }

before do
sign_in(user)
end

describe "GET #index" do
it "redirects to login or unauthorized page" do
get :index
expect(response).to be_redirect
end
end
end
end

7 changes: 2 additions & 5 deletions spec/controllers/admins_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
=begin

RSpec.describe AdminsController, type: :controller do
RSpec.describe AdminController, type: :controller do
let(:organization) { create(:organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }

Expand Down Expand Up @@ -103,5 +101,4 @@
end
end
end
end
=end
end
34 changes: 34 additions & 0 deletions spec/requests/items_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@

describe "CREATE #create" do
let!(:existing_item) { create(:item, organization: organization, name: "Really Good Item") }
let!(:item_category) { create(:item_category, organization: organization, name: "Test Category") }

describe "with an already existing item name" do
let(:item_params) do
Expand All @@ -134,6 +135,39 @@
end
end

describe "with invalid parameters" do
let(:invalid_item_params) do
{
item: {
name: "", # Invalid name
partner_key: create(:base_item).partner_key,
value_in_cents: -100, # Invalid value
package_size: nil,
distribution_quantity: nil
}
}
end

it "loads and displays the item categories when rendering new" do
# Create some item categories for the organization
item_categories = create_list(:item_category, 3, organization: organization)

# Attempt to create an item with invalid parameters
post items_path, params: invalid_item_params

# Expect to render the new template
expect(response).to render_template(:new)

# Ensure the item categories are assigned in the controller
expect(assigns(:item_categories)).to eq(organization.item_categories.order('name ASC'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I much prefer giving the categories explicit values and testing against those values rather than pulling them from the DB. You end up basically just copying the code you're testing into the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm having trouble understanding exactly what you mean here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean doing something like

let(:categories) do 
  [
    FactoryBot.create(:item_category, name: 'Apples'),  # include whatever other IDs need to be passed in
    FactoryBot.create(:item_category, name: 'Bananas')
  ] 
end
# ...
expect(assigns(:item_categories)).to eq(categories))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bdeanhardt -- do you need further guidance on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @cielf! Past 2 weeks have been chaos, but I'm back! I'm taking a look today :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So sorry about the delay! How does it look now?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to specify an order in the spec. 

To be a bit more directive, here's what I had in mind:

let!(:category1) { FactoryBot.create(:item_category, name: 'Bananas', organization: organization) }
let!(:category2) { FactoryBot.create(:item_category, name: 'Apples' organization: organization) }
let!(:category3) { FactoryBot.create(:item_category, name: 'Umbrella', organization: organization) }

# ...

expect(assigns(:item_categories)).to eq([category2, category1, category3])


# Verify the categories are included in the response body
item_categories.each do |category|
expect(response.body).to include("<option value=\"#{category.id}\">#{category.name}</option>")
end
end
end

describe 'GET #index' do
let(:storage_location) { create(:storage_location, organization: organization) }
let!(:item) { create(:item, organization: organization, name: "ACTIVEITEM") }
Expand Down
Loading