Fixes RSpec Rails HaveHttpStatus cop in api v0

This commit is contained in:
cyrillefr
2025-05-14 10:46:36 +02:00
parent b9e1eb2984
commit f810e500b5
17 changed files with 69 additions and 85 deletions

View File

@@ -221,28 +221,12 @@ Metrics/PerceivedComplexity:
- 'app/models/spree/ability.rb'
- 'app/models/spree/order/checkout.rb'
# Offense count: 137
# Offense count: 69
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: ResponseMethods.
# ResponseMethods: response, last_response
RSpecRails/HaveHttpStatus:
Exclude:
- 'spec/controllers/api/v0/base_controller_spec.rb'
- 'spec/controllers/api/v0/customers_controller_spec.rb'
- 'spec/controllers/api/v0/enterprises_controller_spec.rb'
- 'spec/controllers/api/v0/logos_controller_spec.rb'
- 'spec/controllers/api/v0/order_cycles_controller_spec.rb'
- 'spec/controllers/api/v0/orders_controller_spec.rb'
- 'spec/controllers/api/v0/product_images_controller_spec.rb'
- 'spec/controllers/api/v0/products_controller_spec.rb'
- 'spec/controllers/api/v0/promo_images_controller_spec.rb'
- 'spec/controllers/api/v0/reports/packing_report_spec.rb'
- 'spec/controllers/api/v0/reports_controller_spec.rb'
- 'spec/controllers/api/v0/shipments_controller_spec.rb'
- 'spec/controllers/api/v0/statuses_controller_spec.rb'
- 'spec/controllers/api/v0/taxons_controller_spec.rb'
- 'spec/controllers/api/v0/terms_and_conditions_controller_spec.rb'
- 'spec/controllers/api/v0/variants_controller_spec.rb'
- 'spec/controllers/cart_controller_spec.rb'
- 'spec/controllers/checkout_controller_spec.rb'
- 'spec/controllers/enterprises_controller_spec.rb'

View File

@@ -23,7 +23,7 @@ RSpec.describe Api::V0::BaseController do
it "can make a request" do
api_get :index
expect(json_response).to eq( "products" => [] )
expect(response.status).to eq(200)
expect(response).to have_http_status(:ok)
end
end
@@ -31,7 +31,7 @@ RSpec.describe Api::V0::BaseController do
it "without an API key" do
api_get :index
expect(json_response["products"]).to eq []
expect(response.status).to eq(200)
expect(response).to have_http_status(:ok)
end
end
@@ -40,7 +40,7 @@ RSpec.describe Api::V0::BaseController do
request.headers["X-Spree-Token"] = "fake_key"
get :index, params: {}
expect(json_response).to eq( "error" => "Invalid API key (fake_key) specified." )
expect(response.status).to eq(401)
expect(response).to have_http_status(:unauthorized)
end
it "using an invalid token param" do
@@ -62,6 +62,6 @@ RSpec.describe Api::V0::BaseController do
get :index
expect(json_response)
.to eq( "error" => "The resource you were looking for could not be found." )
expect(response.status).to eq(404)
expect(response).to have_http_status(:not_found)
end
end

View File

@@ -20,7 +20,7 @@ module Api
it "lists customers associated with the current user" do
get :index
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(json_response.length).to eq 1
expect(json_response.first[:id]).to eq customer1.id
end
@@ -49,7 +49,7 @@ module Api
context "when the update request is successful" do
it "returns the id of the updated customer" do
spree_post :update, params
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(json_response[:id]).to eq customer.id
end
end
@@ -59,7 +59,7 @@ module Api
it "returns a 422, with an error message" do
spree_post :update, params
expect(response.status).to be 422
expect(response).to have_http_status :unprocessable_entity
expect(json_response[:error]).to be
end
end

View File

@@ -24,7 +24,7 @@ RSpec.describe Api::V0::EnterprisesController, type: :controller do
it "changes the external_billing_id field" do
api_put :update, id: enterprise.id, enterprise: enterprise_params
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(enterprise.reload.external_billing_id).to eq('INV123456')
end
@@ -55,7 +55,7 @@ RSpec.describe Api::V0::EnterprisesController, type: :controller do
it "creates as sells=any when it is not a producer" do
api_post :create, { enterprise: new_enterprise_params }
expect(response.status).to eq 201
expect(response).to have_http_status :created
enterprise = Enterprise.last
expect(enterprise.sells).to eq('any')
@@ -63,7 +63,7 @@ RSpec.describe Api::V0::EnterprisesController, type: :controller do
it "creates a visible=hidden enterprise" do
api_post :create, { enterprise: new_enterprise_params }
expect(response.status).to eq 201
expect(response).to have_http_status :created
enterprise = Enterprise.last
expect(enterprise.visible).to eq("only_through_links")
@@ -76,7 +76,7 @@ RSpec.describe Api::V0::EnterprisesController, type: :controller do
enterprise: new_enterprise_params.
merge({ user_ids: [enterprise_owner.id, manager1.id, manager2.id] })
}
expect(response.status).to eq 201
expect(response).to have_http_status :created
enterprise = Enterprise.last
expect(enterprise.user_ids).to match_array([enterprise_owner.id, manager1.id, manager2.id])
@@ -115,14 +115,14 @@ RSpec.describe Api::V0::EnterprisesController, type: :controller do
it "I can update enterprise logo image" do
api_post :update_image, logo:, id: enterprise.id
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(response.content_type).to eq "text/html"
expect(response.body).to match /logo\.png$/
end
it "I can update enterprise promo image" do
api_post :update_image, promo: logo, id: enterprise.id
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(response.content_type).to eq "text/html"
expect(response.body).to match /logo\.png$/
end

View File

@@ -32,7 +32,7 @@ module Api
it "removes logo" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(json_response["id"]).to eq enterprise.id
enterprise.reload
expect(enterprise.logo).not_to be_attached
@@ -44,7 +44,7 @@ module Api
it "responds with error" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq(409)
expect(response).to have_http_status(:conflict)
expect(json_response['error']).to eq 'Logo does not exist'
end
end
@@ -55,7 +55,7 @@ module Api
it "allows removal of logo" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq 200
expect(response).to have_http_status :ok
end
end
@@ -64,7 +64,7 @@ module Api
it "allows removal of logo" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq 200
expect(response).to have_http_status :ok
end
end
@@ -73,7 +73,7 @@ module Api
it "does not allow removal of logo" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq(401)
expect(response).to have_http_status(:unauthorized)
enterprise.reload
expect(enterprise.logo).to be_attached
end
@@ -84,7 +84,7 @@ module Api
it "does not allow removal of logo" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq(401)
expect(response).to have_http_status(:unauthorized)
enterprise.reload
expect(enterprise.logo).to be_attached
end

View File

@@ -97,7 +97,7 @@ module Api
api_get :products, id: order_cycle.id, distributor: distributor.id,
q: { with_properties: [property1.id, property2.id] }
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(product_ids).to eq [product1.id, product2.id]
expect(product_ids).not_to include product3.id
end
@@ -117,7 +117,7 @@ module Api
api_get :products, id: order_cycle.id, distributor: distributor.id,
q: { with_variants_supplier_properties: [supplier_property.id] }
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(product_ids).to match_array [product1.id, product2.id]
expect(product_ids).not_to include product3.id
end

View File

@@ -378,7 +378,7 @@ module Api
private
def expect_order
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(json_response[:number]).to eq order.number
end

View File

@@ -23,7 +23,7 @@ RSpec.describe Api::V0::ProductImagesController, type: :controller do
product_id: product_without_image.id, file: image, use_route: :product_images
}
expect(response.status).to eq 201
expect(response).to have_http_status :created
expect(product_without_image.reload.image.id).to eq json_response['id']
end
@@ -32,7 +32,7 @@ RSpec.describe Api::V0::ProductImagesController, type: :controller do
product_id: product_with_image.id, file: image, use_route: :product_images
}
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(product_with_image.reload.image.id).to eq json_response['id']
end
@@ -41,7 +41,7 @@ RSpec.describe Api::V0::ProductImagesController, type: :controller do
product_id: product_without_image.id, file: pdf, use_route: :product_images
}
expect(response.status).to eq 422
expect(response).to have_http_status :unprocessable_entity
expect(product_without_image.image).to be_nil
expect(json_response["id"]).to eq nil
expect(json_response["errors"]).to include "Attachment has an invalid content type"

View File

@@ -50,7 +50,7 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
api_get :show, id: "non-existant"
expect(json_response["error"]).to eq("The resource you were looking for could not be found.")
expect(response.status).to eq(404)
expect(response).to have_http_status(:not_found)
end
it "cannot create a new product if not an admin" do
@@ -76,7 +76,7 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
expect(product.deleted_at).to be_nil
api_delete :destroy, id: product.to_param
expect(response.status).to eq(204)
expect(response).to have_http_status(:no_content)
expect { product.reload }.not_to raise_error
expect(product.deleted_at).not_to be_nil
end
@@ -108,14 +108,14 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
unit_description: "things" }
expect(all_attributes.all?{ |attr| json_response.keys.include? attr }).to eq(true)
expect(response.status).to eq(201)
expect(response).to have_http_status(:created)
expect(Spree::Product.last.variants.first.price).to eq 123.45
end
it "cannot create a new product with invalid attributes" do
api_post :create, product: {}
expect(response.status).to eq(422)
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
errors = json_response["errors"]
expect(errors.keys).to match_array([
@@ -127,13 +127,13 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
it "can update a product" do
api_put :update, id: product.to_param, product: { name: "New and Improved Product!" }
expect(response.status).to eq(200)
expect(response).to have_http_status(:ok)
end
it "cannot update a product with an invalid attribute" do
api_put :update, id: product.to_param, product: { name: "" }
expect(response.status).to eq(422)
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
expect(json_response["errors"]["name"]).to eq(["can't be blank"])
end
@@ -142,7 +142,7 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
expect(product.deleted_at).to be_nil
api_delete :destroy, id: product.to_param
expect(response.status).to eq(204)
expect(response).to have_http_status(:no_content)
expect(product.reload.deleted_at).not_to be_nil
end
end
@@ -168,7 +168,7 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
it 'responds with a successful response' do
spree_post :clone, product_id: product.id, format: :json
expect(response.status).to eq(201)
expect(response).to have_http_status(:created)
end
it 'clones the product' do
@@ -180,7 +180,7 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
it 'clones a product with image' do
spree_post :clone, product_id: product_with_image.id, format: :json
expect(response.status).to eq(201)
expect(response).to have_http_status(:created)
expect(json_response['name']).to eq("COPY OF #{product_with_image.name}")
end
@@ -208,7 +208,7 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
it 'responds with a successful response' do
spree_post :clone, product_id: product.id, format: :json
expect(response.status).to eq(201)
expect(response).to have_http_status(:created)
end
it 'clones the product' do
@@ -220,7 +220,7 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
it 'clones a product with image' do
spree_post :clone, product_id: product_with_image.id, format: :json
expect(response.status).to eq(201)
expect(response).to have_http_status(:created)
expect(json_response['name']).to eq("COPY OF #{product_with_image.name}")
end
end

View File

@@ -32,7 +32,7 @@ module Api
it "removes promo image" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(json_response["id"]).to eq enterprise.id
enterprise.reload
expect(enterprise.promo_image).not_to be_attached
@@ -44,7 +44,7 @@ module Api
it "responds with error" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq(409)
expect(response).to have_http_status(:conflict)
expect(json_response['error']).to eq 'Promo image does not exist'
end
end
@@ -55,7 +55,7 @@ module Api
it "allows removal of promo image" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq 200
expect(response).to have_http_status :ok
end
end
@@ -64,7 +64,7 @@ module Api
it "allows removal of promo image" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq 200
expect(response).to have_http_status :ok
end
end
@@ -73,7 +73,7 @@ module Api
it "does not allow removal of promo image" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq(401)
expect(response).to have_http_status(:unauthorized)
enterprise.reload
expect(enterprise.promo_image).to be_attached
end
@@ -84,7 +84,7 @@ module Api
it "does not allow removal of promo image" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq(401)
expect(response).to have_http_status(:unauthorized)
enterprise.reload
expect(enterprise.promo_image).to be_attached
end

View File

@@ -27,7 +27,7 @@ RSpec.describe Api::V0::ReportsController, type: :controller do
it "renders results" do
api_get :show, params
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(json_response[:data]).to match_array report_output(order, "distributor")
end
end
@@ -44,7 +44,7 @@ RSpec.describe Api::V0::ReportsController, type: :controller do
it "renders results" do
api_get :show, params
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(json_response[:data]).to match_array report_output(order, "supplier")
end
end

View File

@@ -42,7 +42,7 @@ RSpec.describe Api::V0::ReportsController, type: :controller do
it "returns an error" do
api_get :show, q: { example: 'test' }
expect(response.status).to eq 422
expect(response).to have_http_status :unprocessable_entity
expect(json_response["error"]).to eq 'Please specify a report type'
end
end
@@ -53,7 +53,7 @@ RSpec.describe Api::V0::ReportsController, type: :controller do
it "returns an error" do
api_get :show, report_type: "xxxxxx", q: { example: 'test' }
expect(response.status).to eq 422
expect(response).to have_http_status :unprocessable_entity
expect(json_response["error"]).to eq 'Report not found'
end
end
@@ -63,7 +63,7 @@ RSpec.describe Api::V0::ReportsController, type: :controller do
it "returns an error" do
api_get :show, report_type: "packing"
expect(response.status).to eq 422
expect(response).to have_http_status :unprocessable_entity
expect(json_response["error"]).to eq('Please supply Ransack search params in the request')
end
end

View File

@@ -126,7 +126,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do
api_put :ready, order_id: shipment.order.to_param, id: shipment.to_param
expect(json_response["error"]).to eq("Cannot ready shipment.")
expect(response.status).to eq(422)
expect(response).to have_http_status(:unprocessable_entity)
end
describe "#add and #remove" do
@@ -150,7 +150,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do
it 'adds a variant to a shipment' do
expect {
api_put :add, params.merge(variant_id: new_variant.to_param)
expect(response.status).to eq(200)
expect(response).to have_http_status(:ok)
}.to change { inventory_units_for(new_variant).size }.by(2)
end
@@ -163,7 +163,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do
it 'removes a variant from a shipment' do
expect {
api_put :remove, params.merge(variant_id: existing_variant.to_param)
expect(response.status).to eq(200)
expect(response).to have_http_status(:ok)
}.to change { inventory_units_for(existing_variant).size }.by(-2)
end
@@ -189,14 +189,14 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do
it "doesn't adjust stock when adding a variant" do
expect {
api_put :add, params.merge(variant_id: existing_variant.to_param)
expect(response.status).to eq(422)
expect(response).to have_http_status(:unprocessable_entity)
}.not_to change { existing_variant.reload.on_hand }
end
it "doesn't adjust stock when removing a variant" do
expect {
api_put :remove, params.merge(variant_id: existing_variant.to_param)
expect(response.status).to eq(422)
expect(response).to have_http_status(:unprocessable_entity)
}.not_to change { existing_variant.reload.on_hand }
end
end
@@ -283,7 +283,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do
expect(order.payment_state).to eq "paid" # order is fully paid for
api_put :update, params
expect(response.status).to eq 200
expect(response).to have_http_status :ok
order.reload
@@ -296,7 +296,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do
it "updates closed adjustments" do
expect {
api_put :update, params
expect(response.status).to eq 200
expect(response).to have_http_status :ok
}.to change { order.reload.shipment.fee_adjustment.amount }
end
end
@@ -419,7 +419,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do
end
def expect_valid_response
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(json_response.keys).to include(*attributes)
end
@@ -429,7 +429,7 @@ RSpec.describe Api::V0::ShipmentsController, type: :controller do
end
def expect_error_response
expect(response.status).to eq 422
expect(response).to have_http_status :unprocessable_entity
expect(json_response["exception"]).to eq error_message
end
end

View File

@@ -10,21 +10,21 @@ module Api
it "returns alive when up to date" do
Spree::Config.last_job_queue_heartbeat_at = Time.now.in_time_zone
get :job_queue
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(response.body).to eq({ alive: true }.to_json)
end
it "returns dead otherwise" do
Spree::Config.last_job_queue_heartbeat_at = 10.minutes.ago
get :job_queue
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(response.body).to eq({ alive: false }.to_json)
end
it "returns dead when no heartbeat recorded" do
Spree::Config.last_job_queue_heartbeat_at = nil
get :job_queue
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(response.body).to eq({ alive: false }.to_json)
end
end

View File

@@ -59,13 +59,13 @@ RSpec.describe Api::V0::TaxonsController do
api_post :create, taxon: { name: "Colors" }
expect(attributes.all? { |a| json_response.include? a }).to be true
expect(response.status).to eq(201)
expect(response).to have_http_status(:created)
end
it "cannot create a new taxon with invalid attributes" do
api_post :create, taxon: {}
expect(response.status).to eq(422)
expect(response).to have_http_status(:unprocessable_entity)
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
errors = json_response["errors"]
@@ -76,7 +76,7 @@ RSpec.describe Api::V0::TaxonsController do
it "can destroy" do
api_delete :destroy, id: taxon2.id
expect(response.status).to eq(204)
expect(response).to have_http_status(:no_content)
end
end
end

View File

@@ -26,7 +26,7 @@ module Api
it "removes terms and conditions file" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq 200
expect(response).to have_http_status :ok
expect(json_response["id"]).to eq enterprise.id
enterprise.reload
expect(enterprise.terms_and_conditions).not_to be_attached
@@ -42,7 +42,7 @@ module Api
it "responds with error" do
spree_delete :destroy, enterprise_id: enterprise
expect(response.status).to eq(409)
expect(response).to have_http_status(:conflict)
expect(json_response['error']).to eq 'Terms and Conditions file does not exist'
end
end

View File

@@ -107,7 +107,7 @@ RSpec.describe Api::V0::VariantsController, type: :controller do
it "deletes a variant" do
api_delete :destroy, id: variant_to_delete.id
expect(response.status).to eq(204)
expect(response).to have_http_status(:no_content)
expect { variant_to_delete.reload }.not_to raise_error
expect(variant_to_delete.deleted_at).to be_present
end
@@ -150,7 +150,7 @@ RSpec.describe Api::V0::VariantsController, type: :controller do
product_id: variant.product.id
expect(attributes.all?{ |attr| json_response.include? attr.to_s }).to eq(true)
expect(response.status).to eq(201)
expect(response).to have_http_status(:created)
expect(json_response["sku"]).to eq("12345")
expect(variant.product.variants.count).to eq(original_number_of_variants + 1)
end
@@ -158,13 +158,13 @@ RSpec.describe Api::V0::VariantsController, type: :controller do
it "can update a variant" do
api_put :update, id: variant.to_param, variant: { sku: "12345" }
expect(response.status).to eq(200)
expect(response).to have_http_status(:ok)
end
it "can delete a variant" do
api_delete :destroy, id: variant.to_param
expect(response.status).to eq(204)
expect(response).to have_http_status(:no_content)
expect { variant.reload }.not_to raise_error
expect(variant.deleted_at).not_to be_nil
end