<commit_before>require 'spec_helper'

describe TransferOwnershipController do
  describe 'PUT #transfer' do
    let(:cookbook) { create(:cookbook) }
    let(:new_owner) { create(:user) }

    before do
      cookbook_collection = double('cookbook_collection', :first! => cookbook)
      allow(Cookbook).to receive(:with_name) { cookbook_collection }
    end

    shared_examples 'admin_or_owner' do
      before { sign_in(user) }

      it 'attempts to change the cookbooks owner' do
        expect(cookbook).to receive(:transfer_ownership).with(
          user,
          new_owner
        ) { 'cookbook.ownership_transfer.done' }
        put :transfer, id: cookbook, cookbook: { user_id: new_owner.id }
      end

      it 'redirects back to the cookbook' do
        put :transfer, id: cookbook, cookbook: { user_id: new_owner.id }
        expect(response).to redirect_to(assigns[:cookbook])
      end
    end

    context 'the current user is an admin' do
      let(:user) { create(:admin) }
      it_behaves_like 'admin_or_owner'
    end

    context 'the current user is the cookbook owner' do
      let(:user) { cookbook.owner }
      it_behaves_like 'admin_or_owner'
    end

    context 'the current user is not an admin nor an owner of the cookbook' do
      before { sign_in(create(:user)) }<commit_msg>adds failing spec to test different params for the checkbox for it if the user wants to add the current user as a contributor in the controller<commit_after>require 'spec_helper'

describe TransferOwnershipController do
  describe 'PUT #transfer' do
    let(:cookbook) { create(:cookbook) }
    let(:new_owner) { create(:user) }

    before do
      cookbook_collection = double('cookbook_collection', :first! => cookbook)
      allow(Cookbook).to receive(:with_name) { cookbook_collection }
    end

    shared_examples 'admin_or_owner' do
      before { sign_in(user) }

      it 'attempts to change the cookbooks owner' do
        expect(cookbook).to receive(:transfer_ownership).with(
          user,
          new_owner,
          false
        ) { 'cookbook.ownership_transfer.done' }
        put :transfer, id: cookbook, cookbook: { user_id: new_owner.id, add_owner_as_collaborator: "0" }
      end

      it 'attempts to change the cookbooks owner and save the current owner as a contributor' do
        expect(cookbook).to receive(:transfer_ownership).with(
          user,
          new_owner,
          true
        ) { 'cookbook.ownership_transfer.done' }
        put :transfer, id: cookbook, cookbook: { user_id: new_owner.id, add_owner_as_collaborator: "1" }
      end

      it 'redirects back to the cookbook' do
        put :transfer, id: cookbook, cookbook: { user_id: new_owner.id }
        expect(response).to redirect_to(assigns[:cookbook])
      end
    end

    context 'the current user is an admin' do
      let(:user) { create(:admin) }
      it_behaves_like 'admin_or_owner'
    end

    context 'the current user is the cookbook owner' do
      let(:user) { cookbook.owner }
      it_behaves_like 'admin_or_owner'
    end

    context 'the current user is not an admin nor an owner of the cookbook' do
      before { sign_in(create(:user)) }
{'fraction_non_alphanumeric': 0.0901948460087995, 'fraction_numerical': 0.0006285355122564425, 'mean_word_length': 2.9540372670807455, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6648782', 'n_tokens_mistral': 975, 'n_tokens_neox': 945, 'n_words': 283}