<commit_before>require 'pavlov_helper'
require 'active_support/core_ext/object/blank'
require_relative '../../../../app/interactors/interactors/facts/create.rb'

describe Interactors::Facts::Create do
  include PavlovSupport

  before do
    stub_classes 'Fact', 'Blacklist'
  end

  describe 'validation' do
    it 'requires displaystring to be a nonempty string' do
      hash = { displaystring: '', url: 'u', title: 't'}
      expect_validating(hash)
        .to fail_validation('displaystring should be a nonempty string.')
    end

    it 'requires title to be a string' do
      hash = { displaystring: 'ds', url: 'u', title: 4}
      expect_validating(hash)
        .to fail_validation('title should be a string.')
    end

    it 'requires url to be a string' do
      hash = { displaystring: 'ds', url: 6, title: 't'}
      expect_validating(hash)
        .to fail_validation('url should be a string.')
    end
  end

  it '.authorized raises when not able to create facts' do
    ability = double
    ability.stub(:can?)
             .with(:create, Fact)
             .and_return(false)

    interactor = described_class.new displaystring: 'displaystring', url: 'url',
                                     title: 'title', pavlov_options: { ability:ability }

    expect { interactor.call }
      .to raise_error Pavlov::AccessDenied, "Unauthorized"
  end

  describe '#call' do
    it 'correctly when site doesn''t exist' do
      url = 'www.fmf.nl'
      displaystring = 'this is the annotated text'
      title = 'this is the title'
      site = double
      fact_data = double(persisted?: true)
      fact = double(id: '1', errors: [], data: fact_data)
      user = double(id: '123abc')
      Blacklist.stub default: double
      Blacklist.default.stub(:matches?).with(url).and_return false

      pavlov_options = { current_user: user, ability: double(can?: true) }
      interactor = described_class.new displaystring: displaystring, url: url,
                                       title: title,
                                       pavlov_options: pavlov_options

      Pavlov.stub(:query)
            .with(:'sites/for_url',
                      url: url, pavlov_options: pavlov_options)
            .and_return(nil)

      Pavlov.should_receive(:command)
            .with(:'sites/create',
                      url: url, pavlov_options: pavlov_options)
            .and_return(site)

      Pavlov.should_receive(:command)
            .with(:'facts/create',
                      displaystring: displaystring, title: title, creator: user,
                      site: site, pavlov_options: pavlov_options)
            .and_return(fact)

      Pavlov.should_receive(:command)
            .with(:'facts/add_to_recently_viewed',
                      fact_id: fact.id.to_i, user_id: user.id.to_s,
                      pavlov_options: pavlov_options)

      expect(interactor.call).to eq fact
    end

    it 'correctly when site exists' do
      url = 'www.fmf.nl'
      displaystring = 'this is the annotated text'
      title = 'this is the title'
      site = double
      fact_data = double(persisted?: true)
      fact = double(id: '1', errors: [], data: fact_data)
      user = double(id: '123abc')
      Blacklist.stub default: double
      Blacklist.default.stub(:matches?).with(url).and_return false

      pavlov_options = { current_user: user, ability: double(can?: true) }
      interactor = described_class.new displaystring: displaystring, url: url,
                                       title: title,
                                       pavlov_options: pavlov_options

      Pavlov.stub(:query)<commit_msg>Removed another test for creating fact without site<commit_after>require 'pavlov_helper'
require 'active_support/core_ext/object/blank'
require_relative '../../../../app/interactors/interactors/facts/create.rb'

describe Interactors::Facts::Create do
  include PavlovSupport

  before do
    stub_classes 'Fact', 'Blacklist'
  end

  describe 'validation' do
    it 'requires displaystring to be a nonempty string' do
      hash = { displaystring: '', url: 'u', title: 't'}
      expect_validating(hash)
        .to fail_validation('displaystring should be a nonempty string.')
    end

    it 'requires title to be a string' do
      hash = { displaystring: 'ds', url: 'u', title: 4}
      expect_validating(hash)
        .to fail_validation('title should be a string.')
    end

    it 'requires url to be a string' do
      hash = { displaystring: 'ds', url: 6, title: 't'}
      expect_validating(hash)
        .to fail_validation('url should be a string.')
    end
  end

  it '.authorized raises when not able to create facts' do
    ability = double
    ability.stub(:can?)
             .with(:create, Fact)
             .and_return(false)

    interactor = described_class.new displaystring: 'displaystring', url: 'url',
                                     title: 'title', pavlov_options: { ability:ability }

    expect { interactor.call }
      .to raise_error Pavlov::AccessDenied, "Unauthorized"
  end

  describe '#call' do
    it 'works when site exists' do
      url = 'www.fmf.nl'
      displaystring = 'this is the annotated text'
      title = 'this is the title'
      site = double
      fact_data = double(persisted?: true)
      fact = double(id: '1', errors: [], data: fact_data)
      user = double(id: '1<PASSWORD>')
      Blacklist.stub default: double
      Blacklist.default.stub(:matches?).with(url).and_return false

      pavlov_options = { current_user: user, ability: double(can?: true) }
      interactor = described_class.new displaystring: displaystring, url: url,
                                       title: title,
                                       pavlov_options: pavlov_options

      Pavlov.stub(:query)
{'fraction_non_alphanumeric': 0.10543572044866265, 'fraction_numerical': 0.0024158757549611735, 'mean_word_length': 2.308219178082192, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5449285', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1651, 'n_words': 464}