<commit_before>require 'spec_helper'

describe Mentor::CommentsController do
  render_views

  describe 'POST create' do
    context 'as an unauthenticated user' do
      it 'redirects to the landing page' do
        post :create
        expect(response).to redirect_to root_path
      end
    end

    context 'as an unauthorized user' do
      it 'redirects to the landing page' do
        sign_in create(:developer)
        post :create
        expect(response).to redirect_to root_path
      end
    end

    context 'as a mentor' do
      let(:mentor) { create(:mentor) }
      let(:params) {{ mentor_comment: { commentable_id: 1, text: 'something' } }}

      before { sign_in mentor }

      subject { post :create, params: params }

      it 'creates a new Mentor::Comment for the user' do
        expect { subject }.to change { Mentor::Comment.count }.by(1)
      end

      it 'sets the user and commentable_type' do
        subject
        expect(Mentor::Comment.last).to have_attributes(
          user:              mentor,
          text:              'something',
          commentable_id:   1,
          commentable_type: 'Mentor::Application')
      end

      it 'redirect_to the mentor application show view' do
        subject
        expect(response).to redirect_to mentor_application_path(id: 1, anchor: 'mentor_comment_1')
      end
    end
  end

  describe 'PUT update' do
    context 'as an unauthenticated user' do
      it 'redirects to the landing page' do
        put :update, params: { id: 1 }
        expect(response).to redirect_to root_path
      end
    end

    context 'as an unauthorized user' do
      it 'redirects to the landing page' do
        sign_in create(:developer)
        put :update, params: { id: 1 }
        expect(response).to redirect_to root_path
      end
    end

    context 'as a mentor' do
      let(:mentor) { create(:mentor) }

      before { sign_in mentor }

      context 'when comment exists' do
        let!(:comment) { Mentor::Comment.create(user: mentor, commentable_id: 1, text: 'something') }
        let(:params)   {{ id: comment.id, mentor_comment: { text: 'something else' } }}

        subject { put :update, params: params }

        it 'updates the comment' do
          expect { subject; comment.reload}.to change { comment.text }.
            from('something').to('something else')
        end

        it 'redirect_to the mentor application show view' do
          subject
          expect(response).to redirect_to mentor_application_path(id: 1, anchor: 'mentor_comment_1')
        end
      end

      context 'when comment does not exist' do
        it 'returns a 404' do
          params = { id: 1, mentor_comment: { text: 'something else' } }<commit_msg>Get rid of flaky test<commit_after>require 'spec_helper'

describe Mentor::CommentsController do
  render_views

  describe 'POST create' do
    context 'as an unauthenticated user' do
      it 'redirects to the landing page' do
        post :create
        expect(response).to redirect_to root_path
      end
    end

    context 'as an unauthorized user' do
      it 'redirects to the landing page' do
        sign_in create(:developer)
        post :create
        expect(response).to redirect_to root_path
      end
    end

    context 'as a mentor' do
      let(:mentor) { create(:mentor) }
      let(:params) {{ mentor_comment: { commentable_id: 1, text: 'something' } }}
      let(:comment) { proc { Mentor::Comment.last }}

      before { sign_in mentor }

      subject { post :create, params: params }

      it 'creates a new Mentor::Comment for the user' do
        expect { subject }.to change { Mentor::Comment.count }.by(1)
      end

      it 'sets the user and commentable_type' do
        subject
        expect(comment.call).to have_attributes(
          user:              mentor,
          text:              'something',
          commentable_id:   1,
          commentable_type: 'Mentor::Application')
      end

      it 'redirect_to the mentor application show view' do
        subject
        anchor = "mentor_comment_#{comment.call.id}"
        expect(response).to redirect_to mentor_application_path(id: 1, anchor: anchor)
      end
    end
  end

  describe 'PUT update' do
    context 'as an unauthenticated user' do
      it 'redirects to the landing page' do
        put :update, params: { id: 1 }
        expect(response).to redirect_to root_path
      end
    end

    context 'as an unauthorized user' do
      it 'redirects to the landing page' do
        sign_in create(:developer)
        put :update, params: { id: 1 }
        expect(response).to redirect_to root_path
      end
    end

    context 'as a mentor' do
      let(:mentor) { create(:mentor) }

      before { sign_in mentor }

      context 'when comment exists' do
        let!(:comment) { Mentor::Comment.create(user: mentor, commentable_id: 1, text: 'something') }
        let(:params)   {{ id: comment.id, mentor_comment: { text: 'something else' } }}

        subject { put :update, params: params }

        it 'updates the comment' do
          expect { subject; comment.reload}.to change { comment.text }.
            from('something').to('something else')
        end

        it 'redirect_to the mentor application show view' do
          subject
          anchor = "mentor_comment_#{comment.id}"
          expect(response).to redirect_to mentor_application_path(id: 1, anchor: anchor)
        end
      end

      context 'when comment does not exist' do
        it 'returns a 404' do
          params = { id: 1, mentor_comment: { text: 'something else' } }
{'fraction_non_alphanumeric': 0.0893048128342246, 'fraction_numerical': 0.004634581105169341, 'mean_word_length': 2.2986478542034097, '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': '7315282', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1559, 'n_words': 533}