<commit_before>require 'spec_helper'

describe ScrapersController do
  let(:user) { create(:user, nickname: "mlandauer") }
  let(:organization) do
    o = create(:organization, nickname: "org")
    o.users << user
    o
  end

  describe "#destroy" do
    context "not signed in" do
      it "should not allow you to delete a scraper" do
        create(:scraper, owner: user, name: "a_scraper", full_name: "mlandauer/a_scraper")
        delete :destroy, id: "mlandauer/a_scraper"
        Scraper.count.should == 1
      end
    end

    context "signed in" do
      before :each do
        sign_in user
      end

      context "you own the scraper" do
        before :each do
          Scraper.create(owner: user, name: "a_scraper", full_name: "mlandauer/a_scraper")
        end

        it "should allow you to delete the scraper" do
          delete :destroy, id: "mlandauer/a_scraper"
          Scraper.count.should == 0
        end

        it "should redirect to the owning user" do
          delete :destroy, id: "mlandauer/a_scraper"
          response.should redirect_to user_url(user)     
        end
      end

      context "an organisation you're part of owns the scraper" do
        before :each do
          Scraper.create(owner: organization, name: "a_scraper", full_name: "org/a_scraper")
        end

        it "should allow you to delete a scraper if it's owner by an organisation you're part of" do
          delete :destroy, id: "org/a_scraper"
          Scraper.count.should == 0
        end

        it "should redirect to the owning organisation" do
          delete :destroy, id: "org/a_scraper"
          response.should redirect_to organization_url(organization)     
        end
      end

      it "should not allow you to delete a scraper if you don't own the scraper" do
        other_user = User.create(nickname: "otheruser")
        scraper = Scraper.create(owner: other_user, name: "a_scraper", full_name: "otheruser/a_scraper")
        delete :destroy, id: "otheruser/a_scraper"
        Scraper.count.should == 1
      end

      it "should not allow you to delete a scraper if it's owner is an organisation your're not part of" do
        other_organisation = Organization.create(nickname: "otherorg")
        scraper = Scraper.create(owner: other_organisation, name: "a_scraper", full_name: "otherorg/a_scraper")
        delete :destroy, id: "otherorg/a_scraper"
        Scraper.count.should == 1
      end
    end
  end
end<commit_msg>Test that duplicate scraper on Morph errors<commit_after>require 'spec_helper'

describe ScrapersController do
  let(:user) { create(:user, nickname: "mlandauer") }
  let(:organization) do
    o = create(:organization, nickname: "org")
    o.users << user
    o
  end

  describe "#destroy" do
    context "not signed in" do
      it "should not allow you to delete a scraper" do
        create(:scraper, owner: user, name: "a_scraper", full_name: "mlandauer/a_scraper")
        delete :destroy, id: "mlandauer/a_scraper"
        Scraper.count.should == 1
      end
    end

    context "signed in" do
      before :each do
        sign_in user
      end

      context "you own the scraper" do
        before :each do
          Scraper.create(owner: user, name: "a_scraper", full_name: "mlandauer/a_scraper")
        end

        it "should allow you to delete the scraper" do
          delete :destroy, id: "mlandauer/a_scraper"
          Scraper.count.should == 0
        end

        it "should redirect to the owning user" do
          delete :destroy, id: "mlandauer/a_scraper"
          response.should redirect_to user_url(user)     
        end
      end

      context "an organisation you're part of owns the scraper" do
        before :each do
          Scraper.create(owner: organization, name: "a_scraper", full_name: "org/a_scraper")
        end

        it "should allow you to delete a scraper if it's owner by an organisation you're part of" do
          delete :destroy, id: "org/a_scraper"
          Scraper.count.should == 0
        end

        it "should redirect to the owning organisation" do
          delete :destroy, id: "org/a_scraper"
          response.should redirect_to organization_url(organization)     
        end
      end

      it "should not allow you to delete a scraper if you don't own the scraper" do
        other_user = User.create(nickname: "otheruser")
        scraper = Scraper.create(owner: other_user, name: "a_scraper", full_name: "otheruser/a_scraper")
        delete :destroy, id: "otheruser/a_scraper"
        Scraper.count.should == 1
      end

      it "should not allow you to delete a scraper if it's owner is an organisation your're not part of" do
        other_organisation = Organization.create(nickname: "otherorg")
        scraper = Scraper.create(owner: other_organisation, name: "a_scraper", full_name: "otherorg/a_scraper")
        delete :destroy, id: "otherorg/a_scraper"
        Scraper.count.should == 1
      end
    end
  end

  describe '#create_scraperwiki' do
    it 'should error when the scraper already exists on Morph' do
      sign_in user
      create :scraper, full_name: "#{user.nickname}/my_scraper"
      post :create_scraperwiki, scraper: { name: 'my_scraper', owner_id: user.id }
      assigns(:scraper).errors[:name].should == ['is already taken on Morph']
    end
  end
end
{'fraction_non_alphanumeric': 0.08799850215315484, 'fraction_numerical': 0.001872308556450103, 'mean_word_length': 2.5354070152217076, 'pattern_counts': {'":': 0, '<': 7, '<?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': '815154', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1646, 'n_words': 532}