<commit_before>require 'rails_helper'

module BeachApiCore
  describe 'V1::Interaction', type: :request do
    include_context 'signed up developer'
    include_context 'authenticated user'
    include_context 'bearer token authentication'

    INTERACTION_KEYS = %i(id created_at kind template user interaction_attributes interaction_keepers).freeze

    describe 'when create' do
      context 'when invalid' do
        it_behaves_like 'an authenticated resource' do
          before { post beach_api_core.v1_interactions_path }
        end
        it_behaves_like 'an authenticated resource' do
          before { post beach_api_core.v1_interactions_path, headers: invalid_bearer_auth }
        end

        it 'should return bad request status if interaction is invalid' do
          interaction_params = { kind: Faker::Lorem.word }
          expect do
            post beach_api_core.v1_interactions_path, params: { interaction: interaction_params }, headers: bearer_auth
          end.not_to change(Interaction, :count)
          expect(response.status).to eq 400
          expect(json_body[:error]).to be_present
        end
      end

      context 'when valid' do
        let(:entity) { create :entity }
        let(:interaction_params) do
          { kind: Faker::Lorem.word,
            interaction_attributes_attributes: [key: "#{Faker::Lorem.word}-#{Faker::Number.number(2)}",
                                                values: { 'text' => Faker::Lorem.sentence }],
            interaction_keepers_attributes: [keeper_type: entity.class.to_s,
                                             keeper_id: entity.id] }
        end

        it 'should create new interaction with correct fields' do
          expect do
            post beach_api_core.v1_interactions_path, params: { interaction: interaction_params }, headers: bearer_auth
          end.to change(Interaction, :count).by(1)
          interaction = Interaction.last
          expect(interaction.kind).to eq interaction_params[:kind]
          expect(interaction.user_id).to eq oauth_user.id<commit_msg>Add templete to interaction create spec<commit_after>require 'rails_helper'

module BeachApiCore
  describe 'V1::Interaction', type: :request do
    include_context 'signed up developer'
    include_context 'authenticated user'
    include_context 'bearer token authentication'

    INTERACTION_KEYS = %i(id created_at kind template user interaction_attributes interaction_keepers).freeze

    describe 'when create' do
      context 'when invalid' do
        it_behaves_like 'an authenticated resource' do
          before { post beach_api_core.v1_interactions_path }
        end
        it_behaves_like 'an authenticated resource' do
          before { post beach_api_core.v1_interactions_path, headers: invalid_bearer_auth }
        end

        it 'should return bad request status if interaction is invalid' do
          interaction_params = { kind: Faker::Lorem.word }
          expect do
            post beach_api_core.v1_interactions_path, params: { interaction: interaction_params }, headers: bearer_auth
          end.not_to change(Interaction, :count)
          expect(response.status).to eq 400
          expect(json_body[:error]).to be_present
        end
      end

      context 'when valid' do
        let(:entity) { create :entity }
        let(:interaction_params) do
          { kind: Faker::Lorem.word,
            interaction_attributes_attributes: [key: "#{Faker::Lorem.word}-#{Faker::Number.number(2)}",
                                                values: { 'text' => Faker::Lorem.sentence }],
            interaction_keepers_attributes: [keeper_type: entity.class.to_s,
                                             keeper_id: entity.id] }
        end
        before { create(:template, name: interaction_params[:kind]) }

        it 'should create new interaction with correct fields' do
          expect do
            post beach_api_core.v1_interactions_path, params: { interaction: interaction_params }, headers: bearer_auth
          end.to change(Interaction, :count).by(1)
          interaction = Interaction.last
          expect(interaction.kind).to eq interaction_params[:kind]
          expect(interaction.user_id).to eq oauth_user.id
{'fraction_non_alphanumeric': 0.07905882352941176, 'fraction_numerical': 0.004705882352941176, 'mean_word_length': 2.584317032040472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7485437', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1113, 'n_words': 290}