<commit_before># frozen_string_literal: true

require 'spec_helper'

describe DfcProvider::Api::ProductsController, type: :controller do
  render_views

  let(:user) { create(:user) }
  let(:enterprise) { create(:distributor_enterprise, owner: user) }
  let(:product) { create(:simple_product, supplier: enterprise ) }
  let!(:visible_inventory_item) do
    create(:inventory_item,
           enterprise: enterprise,
           variant: product.variants.first,
           visible: true)
  end

  describe('.index') do
    context 'with authorization token' do
      before do
        request.env['Authorization'] = 'Bearer 123456.abcdef.123456'
      end

      context 'with an authenticated user' do
        before do
          allow_any_instance_of(DfcProvider::AuthorizationControl)
            .to receive(:process)
            .and_return(user)
        end

        context 'with an enterprise' do
          context 'given with an id' do
            context 'related to the user' do
              before { get :index, enterprise_id: 'default' }

              it 'is successful' do
                expect(response.status).to eq 200
              end

              it 'renders the related product' do
                expect(response.body)
                  .to include(product.variants.first.name)
              end
            end

            context 'not related to the user' do
              let(:enterprise) { create(:enterprise) }

              it 'returns not_found head' do
                get :index, enterprise_id: enterprise.id
                expect(response.status).to eq 404
              end
            end
          end

          context 'as default' do
            before { get :index, enterprise_id: 'default' }

            it 'is successful' do
              expect(response.status).to eq 200
            end

            it 'renders the related product' do
              expect(response.body)
                .to include(product.variants.first.name)
            end
          end
        end

        context 'without a recorded enterprise' do
          let(:enterprise) { create(:enterprise) }

          it 'returns not_found head' do
            get :index, enterprise_id: 'default'
            expect(response.status).to eq 404
          end
        end
      end

      context 'without an authenticated user' do
        it 'returns unauthorized head' do
          allow_any_instance_of(DfcProvider::AuthorizationControl)
            .to receive(:process)
            .and_return(nil)

          get :index, enterprise_id: 'default'
          expect(response.status).to eq 401
        end
      end
    end

    context 'without an authorization token' do
      it 'returns unprocessable_entity head' do
        get :index, enterprise_id: enterprise.id
        expect(response.status).to eq 422
      end
    end
  end
end<commit_msg>Update get calls in DFC controller spec<commit_after># frozen_string_literal: true

require 'spec_helper'

describe DfcProvider::Api::ProductsController, type: :controller do
  render_views

  let(:user) { create(:user) }
  let(:enterprise) { create(:distributor_enterprise, owner: user) }
  let(:product) { create(:simple_product, supplier: enterprise ) }
  let!(:visible_inventory_item) do
    create(:inventory_item,
           enterprise: enterprise,
           variant: product.variants.first,
           visible: true)
  end

  describe('.index') do
    context 'with authorization token' do
      before do
        request.env['Authorization'] = 'Bearer 123456.abcdef.123456'
      end

      context 'with an authenticated user' do
        before do
          allow_any_instance_of(DfcProvider::AuthorizationControl)
            .to receive(:process)
            .and_return(user)
        end

        context 'with an enterprise' do
          context 'given with an id' do
            context 'related to the user' do
              before { api_get :index, enterprise_id: 'default' }

              it 'is successful' do
                expect(response.status).to eq 200
              end

              it 'renders the related product' do
                expect(response.body)
                  .to include(product.variants.first.name)
              end
            end

            context 'not related to the user' do
              let(:enterprise) { create(:enterprise) }

              it 'returns not_found head' do
                api_get :index, enterprise_id: enterprise.id
                expect(response.status).to eq 404
              end
            end
          end

          context 'as default' do
            before { api_get :index, enterprise_id: 'default' }

            it 'is successful' do
              expect(response.status).to eq 200
            end

            it 'renders the related product' do
              expect(response.body)
                .to include(product.variants.first.name)
            end
          end
        end

        context 'without a recorded enterprise' do
          let(:enterprise) { create(:enterprise) }

          it 'returns not_found head' do
            api_get :index, enterprise_id: 'default'
            expect(response.status).to eq 404
          end
        end
      end

      context 'without an authenticated user' do
        it 'returns unauthorized head' do
          allow_any_instance_of(DfcProvider::AuthorizationControl)
            .to receive(:process)
            .and_return(nil)

          api_get :index, enterprise_id: 'default'
          expect(response.status).to eq 401
        end
      end
    end

    context 'without an authorization token' do
      it 'returns unprocessable_entity head' do
        api_get :index, enterprise_id: enterprise.id
        expect(response.status).to eq 422
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07601935038009675, 'fraction_numerical': 0.010366275051831375, 'mean_word_length': 1.825280624694973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '833627', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1467, 'n_words': 447}