<commit_before>require 'spec_helper'

describe Admin::CommentsController, type: :controller do

  # It is necessary to use bang version of let to build roles before user
  let(:conference) { create(:conference) }
  let!(:first_user) { create(:user) }
  let!(:organizer_role) { create(:role, name: 'organizer', resource: conference) }
  let(:organizer) { create(:user, role_ids: organizer_role.id, last_sign_in_at: Time.now - 1.day) }
  let(:participant) { create(:user) }
  let(:event) { create(:event, conference: conference) }
  let(:comment) { create(:comment, commentable_type: 'Event', commentable_id: event.id) }

  context 'not logged in user' do
    describe 'GET #index' do
      it 'renders the :index template' do
        comment
        get :index
        expect(response).to redirect_to(user_session_path)
      end
    end
  end

  context 'logged in as admin, organizer or cfp' do
    before :each do
      sign_in(organizer)
    end
    describe 'GET #index' do
      it 'populates a hash with comments' do
        comment
        get :index
        expect(assigns(:comments)).to be_a(Hash)
        # assigns(:comments).first returns an array of first pair key-value from hash.
        # Calling again 'first' returns the key, meaning the Conference object.
        expect(assigns(:comments).first.first.title).to eq(comment.commentable.conference.title)
      end
      it 'has status 200: OK' do
        comment
        get :index
        expect(response).to have_http_status(:ok)
      end
      it 'renders the :index template' do
        comment
        get :index
        expect(response).to render_template(:index)
      end
    end
  end

  context 'logged in with any other role or normal user' do
    describe 'GET#index' do
      it 'requires organizer privileges' do
        sign_in(participant)
        comment
        get :index
        expect(response).to redirect_to(root_path)
        expect(flash[:alert]).to match('You are not authorized to access this area!')
      end
    end
  end
end
<commit_msg>moved comment instance into before block<commit_after>require 'spec_helper'

describe Admin::CommentsController, type: :controller do

  # It is necessary to use bang version of let to build roles before user
  let(:conference) { create(:conference) }
  let!(:first_user) { create(:user) }
  let!(:organizer_role) { create(:role, name: 'organizer', resource: conference) }
  let(:organizer) { create(:user, role_ids: organizer_role.id, last_sign_in_at: Time.now - 1.day) }
  let(:participant) { create(:user) }
  let(:event) { create(:event, conference: conference) }
  let(:comment) { create(:comment, commentable_type: 'Event', commentable_id: event.id) }

  context 'not logged in user' do
    describe 'GET #index' do
      it 'renders the :index template' do
        comment
        get :index
        expect(response).to redirect_to(user_session_path)
      end
    end
  end

  context 'logged in as admin, organizer or cfp' do
    before :each do
      sign_in(organizer)
      comment
    end
    describe 'GET #index' do
      it 'populates a hash with comments' do
        get :index
        expect(assigns(:comments)).to be_a(Hash)
        # assigns(:comments).first returns an array of first pair key-value from hash.
        # Calling again 'first' returns the key, meaning the Conference object.
        expect(assigns(:comments).first.first.title).to eq(comment.commentable.conference.title)
      end
      it 'has status 200: OK' do
        get :index
        expect(response).to have_http_status(:ok)
      end
      it 'renders the :index template' do
        get :index
        expect(response).to render_template(:index)
      end
    end
  end

  context 'logged in with any other role or normal user' do
    describe 'GET#index' do
      it 'requires organizer privileges' do
        sign_in(participant)
        comment
        get :index
        expect(response).to redirect_to(root_path)
        expect(flash[:alert]).to match('You are not authorized to access this area!')
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.0974169741697417, 'fraction_numerical': 0.0019680196801968018, 'mean_word_length': 2.9475728155339804, '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': '3515390', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1174, 'n_words': 385}