<commit_before>require 'rails_helper'

describe Api::ApiKeysController do # rubocop:disable Metrics/BlockLength
  it { should be_a(Api::BaseController) }

  it { should use_before_action(:authenticate!) }

  describe '#index.json' do
    context 'authorized' do
      render_views

      let!(:session) { create(:session) }

      before { request.env['HTTP_AUTHORIZATION'] = "Bearer #{ session.token }" }

      before { get :index, format: :json }

      it { should render_template(:index) }

      it { should respond_with(:ok) }
    end

    context 'not authorized' do
      before { get :index, format: :json }

      it { should respond_with(:unauthorized) }
    end
  end

  describe '#show.json' do
    context 'authorized' do
      let!(:user) { create(:user) }

      let!(:session) { create(:session, user: user) }

      let!(:api_key) { create(:api_key, user: user) }

      before { request.env['HTTP_AUTHORIZATION'] = "Bearer #{ session.token }" }

      before { get :show, params: { id: api_key.id }, format: :json }

      it { should render_template(:show) }

      it { should respond_with(:ok) }
    end

    context 'not authorized' do
      before { get :show, params: { id: '42' }, format: :json }

      it { should respond_with(:unauthorized) }
    end
  end

  describe '#create.json' do # rubocop:disable Metrics/BlockLength
    context 'authorized' do
      before { expect(subject).to receive(:verify_authorized).and_return(true) }

      let(:api_key) { double }

      let(:params) { { api_key: { key_id: '1234567', v_code: 'abc' } } }

      before do
        #
        # subject.current_user.api_keys.build(params) => api_key
        #
        expect(subject).to receive(:current_user) do
          double.tap do |a|
            expect(a).to receive(:api_keys) do
              double.tap do |b|
                expect(b).to receive(:build).with(permit!(params[:api_key])).and_return(api_key)
              end
            end
          end
        end
      end

      before { expect(subject).to receive(:authorize).with(api_key) }

      before { expect(api_key).to receive(:save!) }

      before { sign_in }

      before { post :create, params: params, format: :json }

      it { should render_template(:create) }

      it { should respond_with(:ok) }
    end

    context 'not authorized' do
      let(:api_key) { build(:api_key) }

      before { post :create, params: { api_key: api_key }, format: :json }<commit_msg>Use api_key factory<commit_after>require 'rails_helper'

describe Api::ApiKeysController do # rubocop:disable Metrics/BlockLength
  it { should be_a(Api::BaseController) }

  it { should use_before_action(:authenticate!) }

  describe '#index.json' do
    context 'authorized' do
      render_views

      let!(:session) { create(:session) }

      before { request.env['HTTP_AUTHORIZATION'] = "Bearer #{ session.token }" }

      before { get :index, format: :json }

      it { should render_template(:index) }

      it { should respond_with(:ok) }
    end

    context 'not authorized' do
      before { get :index, format: :json }

      it { should respond_with(:unauthorized) }
    end
  end

  describe '#show.json' do
    context 'authorized' do
      let!(:user) { create(:user) }

      let!(:session) { create(:session, user: user) }

      let!(:api_key) { create(:api_key, user: user) }

      before { request.env['HTTP_AUTHORIZATION'] = "Bearer #{ session.token }" }

      before { get :show, params: { id: api_key.id }, format: :json }

      it { should render_template(:show) }

      it { should respond_with(:ok) }
    end

    context 'not authorized' do
      before { get :show, params: { id: '42' }, format: :json }

      it { should respond_with(:unauthorized) }
    end
  end

  describe '#create.json' do # rubocop:disable Metrics/BlockLength
    context 'authorized' do
      let!(:user) { create(:user) }

      let!(:session) { create(:session, user: user) }

      let(:api_key) { attributes_for(:api_key) }

      before { request.env['HTTP_AUTHORIZATION'] = "Bearer #{ session.token }" }

      before { post :create, params: { api_key: api_key }, format: :json }

      it { should render_template(:create) }

      it { should respond_with(:ok) }
    end

    context 'not authorized' do
      let(:api_key) { build(:api_key) }

      before { post :create, params: { api_key: api_key }, format: :json }
{'fraction_non_alphanumeric': 0.13348467650397275, 'fraction_numerical': 0.002497162315550511, 'mean_word_length': 2.7819742489270385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '3131299', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1351, 'n_words': 337}