From 207a15e55c85f019e04718564eb01535fe6a5ef4 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 10 May 2023 15:19:36 +1000 Subject: [PATCH] Convert SuppliedProducts controller spec to request spec --- .../supplied_products_controller_spec.rb | 80 ------------------- .../spec/requests/supplied_products_spec.rb | 58 ++++++++++++++ 2 files changed, 58 insertions(+), 80 deletions(-) delete mode 100644 engines/dfc_provider/spec/controllers/dfc_provider/supplied_products_controller_spec.rb create mode 100644 engines/dfc_provider/spec/requests/supplied_products_spec.rb diff --git a/engines/dfc_provider/spec/controllers/dfc_provider/supplied_products_controller_spec.rb b/engines/dfc_provider/spec/controllers/dfc_provider/supplied_products_controller_spec.rb deleted file mode 100644 index ca04ec9b91..0000000000 --- a/engines/dfc_provider/spec/controllers/dfc_provider/supplied_products_controller_spec.rb +++ /dev/null @@ -1,80 +0,0 @@ -# frozen_string_literal: true - -require DfcProvider::Engine.root.join("spec/spec_helper") - -describe DfcProvider::SuppliedProductsController, type: :controller do - include AuthorizationHelper - - render_views - - let!(:user) { create(:oidc_user) } - let!(:enterprise) { create(:distributor_enterprise, owner: user) } - let!(:product) { create(:simple_product, supplier: enterprise ) } - let!(:variant) { product.variants.first } - - describe '.show' do - context 'with authorization token' do - before do - request.headers['Authorization'] = 'Bearer 123456.abcdef.123456' - end - - context 'with an authenticated user' do - before do - allow_any_instance_of(AuthorizationControl) - .to receive(:user) - .and_return(user) - end - - context 'with an enterprise' do - context 'given with an id' do - before do - api_get :show, enterprise_id: 'default', id: variant.id - end - - it 'is successful' do - expect(response).to be_successful - end - - it 'renders the required content' do - expect(response.body).to include(variant.name) - end - end - - context 'given with a wrong id' do - before { api_get :show, enterprise_id: 'default', id: 999 } - - it 'is not found' do - expect(response).to be_not_found - end - end - end - end - end - end - - describe "#update" do - routes { DfcProvider::Engine.routes } - - it "requires authorisation" do - api_put :update, enterprise_id: "default", id: "0" - expect(response).to have_http_status :unauthorized - end - - describe "with authorisation" do - before { authorise user.email } - - it "updates the variant's name" do - params = { enterprise_id: enterprise.id, id: variant.id } - request_body = File.read(File.join(__dir__, "../../support/patch_product.json")) - - expect { - put(:update, params: params, body: request_body) - expect(response).to have_http_status :success - variant.reload - }.to change { - variant.name - } - end - end - end -end diff --git a/engines/dfc_provider/spec/requests/supplied_products_spec.rb b/engines/dfc_provider/spec/requests/supplied_products_spec.rb new file mode 100644 index 0000000000..766e38e641 --- /dev/null +++ b/engines/dfc_provider/spec/requests/supplied_products_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require DfcProvider::Engine.root.join("spec/spec_helper") + +describe "SuppliedProducts", type: :request do + let!(:user) { create(:oidc_user) } + let!(:enterprise) { create(:distributor_enterprise, owner: user) } + let!(:product) { create(:simple_product, supplier: enterprise ) } + let!(:variant) { product.variants.first } + + describe :show do + it "returns variants" do + get enterprise_supplied_product_path( + variant.id, enterprise_id: enterprise.id + ), headers: auth_header(user.uid) + + expect(response).to have_http_status :ok + expect(response.body).to include variant.name + end + + it "doesn't find unrelated variants" do + other_variant = create(:variant) + + get enterprise_supplied_product_path( + other_variant.id, enterprise_id: enterprise.id + ), headers: auth_header(user.uid) + + expect(response).to have_http_status :not_found + end + end + + describe :update do + it "requires authorisation" do + put enterprise_supplied_product_path( + variant.id, enterprise_id: enterprise.id + ), headers: {} + + expect(response).to have_http_status :unauthorized + end + + it "updates a variant's name" do + params = { enterprise_id: enterprise.id, id: variant.id } + request_body = DfcProvider::Engine.root.join("spec/support/patch_product.json").read + + expect { + put( + enterprise_supplied_product_path(params), + params: request_body, + headers: auth_header(user.uid) + ) + expect(response).to have_http_status :success + variant.reload + }.to change { + variant.name + } + end + end +end