<commit_before>require 'rails_helper'

RSpec.describe CampaignPagesController, type: :controller do

  let(:english) { create :language }
  let( :admin ) { create :admin }
  let(:petition_widget_params) { attributes_for :petition_widget }
  let(:text_widget_params_1) { attributes_for :text_widget }
  let(:text_widget_params_2) { attributes_for :text_widget }
  # let(:bad_widget_params) { attributes_for :text_widget, content: {} }
  let(:widget_params) { [petition_widget_params, text_widget_params_1, text_widget_params_2] }
  let(:page_params) { attributes_for :widgetless_page, language_id: english.id }
  let(:page_widget_params) { page_params.merge({widgets_attributes: widget_params}) }
  # let(:simple_page) { CampaignPage.new(page_params) }
  # let(:existing_page) { p = CampaignPage.new(page_widget_params); p.save!; p }

  describe "logged in as admin" do

    before :each do
      expect(admin).to be_persisted
      allow(controller).to receive(:current_user) { admin }
    end

    describe 'create' do

      it 'should be able to create a page without widgets' do
        expect{ post :create, campaign_page: page_params }.to change{ CampaignPage.count }.by 1
        expect(response).to redirect_to(CampaignPage.last)
      end

      it 'should be able to create a page with widgets' do
        expect{ post :create, campaign_page: page_widget_params }.to change{ CampaignPage.count }.by 1
        expect(response).to redirect_to(CampaignPage.last)
      end

      it 'should be able to create widgets with a page' do
        expect{ post :create, campaign_page: page_widget_params }.to change{ Widget.count }.by 3
        expect(response).to redirect_to(CampaignPage.last)
      end
    end
  end
end

<commit_msg>added failure test cases to CampaignPagesController#create tests<commit_after>require 'rails_helper'

RSpec.describe CampaignPagesController, type: :controller do

  let(:english) { create :language }
  let( :admin ) { create :admin }
  let(:petition_widget_params) { attributes_for :petition_widget }
  let(:text_widget_params_1) { attributes_for :text_widget }
  let(:text_widget_params_2) { attributes_for :text_widget }
  let(:widget_params) { [petition_widget_params, text_widget_params_1, text_widget_params_2] }
  let(:page_params) { attributes_for :widgetless_page, language_id: english.id }
  let(:page_widget_params) { page_params.merge({widgets_attributes: widget_params}) }

  # for error cases
  let(:bad_widget_params) { attributes_for :text_widget, content: { body_html: nil } }
  let(:bad_page_params) { attributes_for :widgetless_page, language_id: nil }
  let(:bad_widget_page_params) { page_params.merge({widgets_attributes: bad_widget_params}) }
  let(:widget_bad_page_params) { bad_page_params.merge({widgets_attributes: widget_params}) }

  # let(:existing_page) { p = CampaignPage.new(page_widget_params); p.save!; p }

  describe "logged in as admin" do

    before :each do
      expect(admin).to be_persisted
      allow(controller).to receive(:current_user) { admin }
    end

    describe 'create' do

      describe 'success' do

        after :each do
          expect(response).to redirect_to CampaignPage.last
        end

        it 'should be able to create a page without widgets' do
          expect{ post :create, campaign_page: page_params }.to change{ CampaignPage.count }.by 1
        end

        it 'should be able to create a page with widgets' do
          expect{ post :create, campaign_page: page_widget_params }.to change{ CampaignPage.count }.by 1
        end

        it 'should be able to create widgets with a page' do
          expect{ post :create, campaign_page: page_widget_params }.to change{ Widget.count }.by 3
        end
      end

      describe 'failure' do

        after :each do
          expect(response).to render_template :new
        end

        it 'should not create a page if page errors' do
          expect{ post :create, campaign_page: widget_bad_page_params }.to change{ CampaignPage.count }.by 0
        end

        it 'should not create a page if widget errors' do
          expect{ post :create, campaign_page: widget_bad_page_params }.to change{ CampaignPage.count }.by 0
        end

        it 'should not create widgets if page errors' do
          pending('moving forward')
          expect{ post :create, campaign_page: bad_widget_page_params }.to change{ Widget.count }.by 0
        end

        it 'should not create widgets if widget errors' do
          pending('moving forward')
          expect{ post :create, campaign_page: bad_widget_page_params }.to change{ Widget.count }.by 0
        end
      end
    end
  end
end


{'fraction_non_alphanumeric': 0.09008815308535799, 'fraction_numerical': 0.0038701354547409157, 'mean_word_length': 3.5385365853658537, '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': '6640963', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1375, 'n_words': 400}