make more specs work

This commit is contained in:
2025-09-23 17:45:00 -05:00
parent 6596693238
commit 7dadb28004
7 changed files with 84 additions and 52 deletions
@@ -27,13 +27,13 @@ describe Suppliers::SectionsController, type: :controller do
describe "GET #show" do
it "assigns the requested section to @section" do
section = create :section, supplier: @supplier
get :show, id: section.id, format: :json
get :show, params: {id: section.id}, format: :json
assigns(:section).should eq(section)
end
it "should not display a section of another supplier" do
section = create :section
get :show, id: section, format: :json
get :show, params: {id: section.id}, format: :json
response.status.should == 404
end
end
@@ -42,14 +42,14 @@ describe Suppliers::SectionsController, type: :controller do
context "with valid attributes" do
it "creates a new section" do
expect{
post :create, section: valid_attributes
post :create, params: {section: valid_attributes}
}.to change(Section, :count).by(1)
expect( Section.last.supplier_id ).to eq @supplier.id
end
it "should not be possible to create a section for another supplier" do
supplier2 = create :supplier
post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id)
post :create, params: {section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id)}
expect( Section.find_by_title('Trying to hack').supplier_id ).to eq @supplier.id
end
end
@@ -57,12 +57,12 @@ describe Suppliers::SectionsController, type: :controller do
context "with invalid attributes" do
it "does not save the new section" do
expect{
post :create, format: :json, section: invalid_attributes
post :create, params: {section: invalid_attributes}, format: :json
}.to_not change(Section, :count)
end
it "returns an error response" do
post :create, format: :json, section: invalid_attributes
post :create, params: {section: invalid_attributes}, format: :json
expect( JSON.parse(response.body )['errors'] ).to be_present
end
end
@@ -75,27 +75,27 @@ describe Suppliers::SectionsController, type: :controller do
context "valid attributes" do
it "located the requested section" do
put :update, id: @section, format: :json, section: valid_attributes
put :update, params: {id: @section, section: valid_attributes}, format: :json
@section.reload
assigns(:section).should eq(@section)
end
it "changes @section's attributes" do
put :update, id: @section.id, format: :json, section: valid_attributes.merge(title: "ChangedByTest")
put :update, params: {id: @section.id, section: valid_attributes.merge(title: "ChangedByTest")}, format: :json
@section.reload
expect( @section.title ).to eq "ChangedByTest"
end
it "should not be possible to update a section to another supplier" do
supplier2 = create :supplier
put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id)
put :update, params: {id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id)}
@section.reload
@section.supplier_id.should == @supplier.id
end
it "should not be possible to update a section of another supplier" do
section = create :section, title: 'Other supplier section'
put :update, id: section.id, format: :json, section: {title: "Trying to hack"}
put :update, params: {id: section.id, section: {title: "Trying to hack"}}, format: :json
expect( response ).to be_not_found
section.reload
section.title.should == 'Other supplier section'
@@ -104,12 +104,12 @@ describe Suppliers::SectionsController, type: :controller do
context "invalid attributes" do
it "locates the requested section" do
put :update, id: @section, format: :json, section: invalid_attributes
put :update, params: {id: @section, section: invalid_attributes}, format: :json
assigns(:section).should eq(@section)
end
it "returns an error response" do
put :update, id: @section.id, format: :json, section: invalid_attributes
put :update, params: {id: @section.id, section: invalid_attributes}, format: :json
expect( JSON.parse(response.body )['errors'] ).to be_present
end
end
@@ -122,14 +122,14 @@ describe Suppliers::SectionsController, type: :controller do
it "deletes the section" do
expect{
delete :destroy, id: @section
delete :destroy, params: {id: @section.id}
}.to change(Section, :count).by(-1)
end
it "should not be possible to delete a section of another supplier" do
section = create :section
expect{
delete :destroy, id: section
delete :destroy, params: {id: section.id}
}.to_not change(Section, :count)
end
end