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

Open
wants to merge 11 commits into
base: main
Choose a base branch
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
48 changes: 48 additions & 0 deletions spec/controllers/admin/partners_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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.

You shouldn't need to sort since you know the partners' names. :) Just reorder the array to partner2, partner1, partner3.

Actually... can you switch it so the partner1line is moved above the partner2 line? That way they'd actually be created out of order. The order of the let! statements is what matters.

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
25 changes: 11 additions & 14 deletions spec/controllers/admins_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
=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) }

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

context "When logged in as an organization admin" do
Expand All @@ -22,7 +20,7 @@

describe "POST #create" do
it "redirects" do
post :create, params: { organization: attributes_for(:organization) }
post :create, params: {organization: attributes_for(:organization)}
expect(response).to be_redirect
end
end
Expand All @@ -36,28 +34,28 @@

describe "GET #show" do
it "returns http success" do
get :show, params: { id: organization.id }
get :show, params: {id: organization.id}
expect(response).to be_successful
end
end

describe "GET #edit" do
it "returns http success" do
get :edit, params: { id: organization.id }
get :edit, params: {id: organization.id}
expect(response).to be_successful
end
end

describe "PUT #update" do
it "redirect" do
put :update, params: { id: organization.id, organization: { name: "Foo" } }
put :update, params: {id: organization.id, organization: {name: "Foo"}}
expect(response).to be_redirect
end
end

describe "DELETE #destroy" do
it "redirects" do
delete :destroy, params: { id: organization.id }
delete :destroy, params: {id: organization.id}
expect(response).to be_redirect
end
end
Expand All @@ -77,7 +75,7 @@

describe "POST #create" do
it "redirects" do
post :create, params: { organization: attributes_for(:organization) }
post :create, params: {organization: attributes_for(:organization)}
expect(response).to be_redirect
end
end
Expand All @@ -91,17 +89,16 @@

describe "GET #edit" do
it "redirects" do
get :edit, params: { id: organization.id }
get :edit, params: {id: organization.id}
expect(response).to be_redirect
end
end

describe "PUT #update" do
it "redirects" do
put :update, params: { id: organization.id, organization: { name: "Foo" } }
put :update, params: {id: organization.id, organization: {name: "Foo"}}
expect(response).to be_redirect
end
end
end
end
=end
end
44 changes: 44 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,49 @@
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

let(:categories) do
[
FactoryBot.create(:item_category, name: 'Apples', organization: organization),
FactoryBot.create(:item_category, name: 'Umbrella', organization: organization),
FactoryBot.create(:item_category, name: 'Bananas', organization: organization)
]
end

before do
categories # Ensure categories are created before the request
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can remove this before block and replace the let with let! to ensure they are created.


it "loads and displays the item categories when rendering new" do

# 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
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
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
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
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