Skip to content

Commit

Permalink
Merge pull request #5825 from solidusio/remove-unused-load-methods-ad…
Browse files Browse the repository at this point in the history
…d-request-specs

Remove unused load methods & Add more request spec coverage
  • Loading branch information
MadelineCollier authored Aug 14, 2024
2 parents 8308687 + 8dc4c1f commit 2ae2bfd
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ def destroy

private

def load_adjustment_reason
@adjustment_reason = Spree::AdjustmentReason.find_by!(id: params[:id])
authorize! action_name, @adjustment_reason
end

def find_adjustment_reason
@adjustment_reason = Spree::AdjustmentReason.find(params[:id])
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ def index

private

def load_reimbursement_type
@reimbursement_type = Spree::ReimbursementType.find_by!(id: params[:id])
authorize! action_name, @reimbursement_type
end

def reimbursement_type_params
params.require(:reimbursement_type).permit(:reimbursement_type_id, permitted_reimbursement_type_attributes)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ def destroy

private

def load_return_reason
@return_reason = Spree::ReturnReason.find_by!(id: params[:id])
authorize! action_name, @return_reason
end

def return_reason_params
params.require(:return_reason).permit(:return_reason_id, permitted_return_reason_attributes)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ def destroy

private

def load_shipping_category
@shipping_category = Spree::ShippingCategory.find_by!(id: params[:id])
authorize! action_name, @shipping_category
end

def find_shipping_category
@shipping_category = Spree::ShippingCategory.find(params[:id])
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ def destroy

private

def load_shipping_method
@shipping_method = Spree::ShippingMethod.find_by!(number: params[:id])
authorize! action_name, @shipping_method
end

def shipping_method_params
params.require(:shipping_method).permit(:shipping_method_id, permitted_shipping_method_attributes)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ def destroy

private

def load_stock_location
@stock_location = Spree::StockLocation.find_by!(number: params[:id])
authorize! action_name, @stock_location
end

def stock_location_params
params.require(:stock_location).permit(:stock_location_id, permitted_stock_location_attributes)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ def destroy

private

def load_store_credit_reason
@store_credit_reason = Spree::StoreCreditReason.find_by!(id: params[:id])
authorize! action_name, @store_credit_reason
end

def find_store_credit_reason
@store_credit_reason = Spree::StoreCreditReason.find(params[:id])
end
Expand Down
5 changes: 0 additions & 5 deletions admin/app/controllers/solidus_admin/stores_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ def destroy

private

def load_store
@store = Spree::Store.find_by!(number: params[:id])
authorize! action_name, @store
end

def store_params
params.require(:store).permit(:store_id, permitted_store_attributes)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ def destroy

private

def load_tax_category
@tax_category = Spree::TaxCategory.find_by!(number: params[:id])
authorize! action_name, @tax_category
end

def find_tax_category
@tax_category = Spree::TaxCategory.find(params[:id])
end
Expand Down
5 changes: 0 additions & 5 deletions admin/app/controllers/solidus_admin/tax_rates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ def destroy

private

def load_tax_rate
@tax_rate = Spree::TaxRate.find_by!(number: params[:id])
authorize! action_name, @tax_rate
end

def tax_rate_params
params.require(:tax_rate).permit(:tax_rate_id, permitted_tax_rate_attributes)
end
Expand Down
5 changes: 0 additions & 5 deletions admin/app/controllers/solidus_admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ def destroy

private

def load_user
@user = Spree.user_class.find_by!(number: params[:id])
authorize! action_name, @user
end

def user_params
params.require(:user).permit(:user_id, permitted_user_attributes)
end
Expand Down
5 changes: 0 additions & 5 deletions admin/app/controllers/solidus_admin/zones_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ def destroy

private

def load_zone
@zone = Spree::Zone.find_by!(number: params[:id])
authorize! action_name, @zone
end

def zone_params
params.require(:zone).permit(:zone_id, permitted_zone_attributes)
end
Expand Down
134 changes: 134 additions & 0 deletions admin/spec/requests/solidus_admin/adjustment_reasons_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe "SolidusAdmin::AdjustmentReasonsController", type: :request do
let(:admin_user) { create(:admin_user) }
let(:adjustment_reason) { create(:adjustment_reason) }

before do
allow_any_instance_of(SolidusAdmin::BaseController).to receive(:spree_current_user).and_return(admin_user)
end

describe "GET /index" do
it "renders the index template with a 200 OK status" do
get solidus_admin.adjustment_reasons_path
expect(response).to have_http_status(:ok)
end
end

describe "GET /new" do
it "renders the new template with a 200 OK status" do
get solidus_admin.new_adjustment_reason_path
expect(response).to have_http_status(:ok)
end
end

describe "POST /create" do
context "with valid parameters" do
let(:valid_attributes) { { name: "Price Adjustment", code: "PRICE_ADJUST", active: true } }

it "creates a new AdjustmentReason" do
expect {
post solidus_admin.adjustment_reasons_path, params: { adjustment_reason: valid_attributes }
}.to change(Spree::AdjustmentReason, :count).by(1)
end

it "redirects to the index page with a 303 See Other status" do
post solidus_admin.adjustment_reasons_path, params: { adjustment_reason: valid_attributes }
expect(response).to redirect_to(solidus_admin.adjustment_reasons_path)
expect(response).to have_http_status(:see_other)
end

it "displays a success flash message" do
post solidus_admin.adjustment_reasons_path, params: { adjustment_reason: valid_attributes }
follow_redirect!
expect(response.body).to include("Adjustment reason was successfully created.")
end
end

context "with invalid parameters" do
let(:invalid_attributes) { { name: "", code: "", active: true } }

it "does not create a new AdjustmentReason" do
expect {
post solidus_admin.adjustment_reasons_path, params: { adjustment_reason: invalid_attributes }
}.not_to change(Spree::AdjustmentReason, :count)
end

it "renders the new template with unprocessable_entity status" do
post solidus_admin.adjustment_reasons_path, params: { adjustment_reason: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

describe "GET /edit" do
it "renders the edit template with a 200 OK status" do
get solidus_admin.edit_adjustment_reason_path(adjustment_reason)
expect(response).to have_http_status(:ok)
end
end

describe "PATCH /update" do
context "with valid parameters" do
let(:valid_attributes) { { name: "Updated Adjustment Reason", code: "UPD_ADJ", active: false } }

it "updates the adjustment reason" do
patch solidus_admin.adjustment_reason_path(adjustment_reason), params: { adjustment_reason: valid_attributes }
adjustment_reason.reload
expect(adjustment_reason.name).to eq("Updated Adjustment Reason")
expect(adjustment_reason.code).to eq("UPD_ADJ")
expect(adjustment_reason.active).to be(false)
end

it "redirects to the index page with a 303 See Other status" do
patch solidus_admin.adjustment_reason_path(adjustment_reason), params: { adjustment_reason: valid_attributes }
expect(response).to redirect_to(solidus_admin.adjustment_reasons_path)
expect(response).to have_http_status(:see_other)
end

it "displays a success flash message" do
patch solidus_admin.adjustment_reason_path(adjustment_reason), params: { adjustment_reason: valid_attributes }
follow_redirect!
expect(response.body).to include("Adjustment reason was successfully updated.")
end
end

context "with invalid parameters" do
let(:invalid_attributes) { { name: "", code: "UPD_ADJ", active: false } }

it "does not update the adjustment reason" do
original_name = adjustment_reason.name
patch solidus_admin.adjustment_reason_path(adjustment_reason), params: { adjustment_reason: invalid_attributes }
adjustment_reason.reload
expect(adjustment_reason.name).to eq(original_name)
end

it "renders the edit template with unprocessable_entity status" do
patch solidus_admin.adjustment_reason_path(adjustment_reason), params: { adjustment_reason: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end

describe "DELETE /destroy" do
it "deletes the adjustment reason and redirects to the index page with a 303 See Other status" do
# This ensures the adjustment_reason exists prior to deletion.
adjustment_reason

expect {
delete solidus_admin.adjustment_reason_path(adjustment_reason)
}.to change(Spree::AdjustmentReason, :count).by(-1)

expect(response).to redirect_to(solidus_admin.adjustment_reasons_path)
expect(response).to have_http_status(:see_other)
end

it "displays a success flash message after deletion" do
delete solidus_admin.adjustment_reason_path(adjustment_reason)
follow_redirect!
expect(response.body).to include("Adjustment reasons were successfully removed.")
end
end
end
Loading

0 comments on commit 2ae2bfd

Please sign in to comment.