<commit_before>require 'rails_helper'

describe ImportOpmlWorker do

  before :each do
    # Ensure files are not deleted, we will need them for running tests again!
    allow(File).to receive(:delete).and_return 1

    @user = FactoryGirl.create :user
    @opml_import_job_state = FactoryGirl.build :opml_import_job_state, user_id: @user.id, state: OpmlImportJobState::RUNNING,
                                     total_feeds: 0, processed_feeds: 0
    @user.opml_import_job_state = @opml_import_job_state

    @filename = '1371324422.opml'
    @filepath = File.join __dir__, '..', '..', 'attachments', @filename
    @file_contents = File.read @filepath

    allow(Feedbunch::Application.config.uploads_manager).to receive :read do |user_id, folder, filename|
      expect(user_id).to eq @user.id
      if filename == @filename
        @file_contents
      else
        nil
      end
    end
    allow(Feedbunch::Application.config.uploads_manager).to receive :save
    allow(Feedbunch::Application.config.uploads_manager).to receive :delete

    allow(ImportSubscriptionsWorker).to receive :perform_async
  end

  context 'validations' do

    it 'sets data import state to ERROR if the file does not exist' do
      expect {ImportOpmlWorker.new.perform 'not.a.real.file', @user.id}.to raise_error OpmlImportError
      @user.reload
      expect(@user.opml_import_job_state.state).to eq OpmlImportJobState::ERROR
    end

    it 'sets data import state to ERROR if the file is not well formed XML' do
      not_valid_xml_filename = File.join __dir__, '..', '..', 'attachments', 'not-well-formed-xml.opml'
      file_contents = File.read not_valid_xml_filename
      allow(Feedbunch::Application.config.uploads_manager).to receive(:read).and_return file_contents
      expect {ImportOpmlWorker.new.perform not_valid_xml_filename, @user.id}.to raise_error Nokogiri::XML::SyntaxError
      @user.reload
      expect(@user.opml_import_job_state.state).to eq OpmlImportJobState::ERROR
    end

    it 'sets data import state to ERROR if the file is not valid OPML' do
      not_valid_opml_filename = File.join __dir__, '..', '..', 'attachments', 'not-valid-opml.opml'
      file_contents = File.read not_valid_opml_filename
      allow(Feedbunch::Application.config.uploads_manager).to receive(:read).and_return file_contents
      expect {ImportOpmlWorker.new.perform not_valid_opml_filename, @user.id}.to raise_error OpmlImportError<commit_msg>Fix test. A malformed XML uploaded by the user no longer means that ImportOpmlWorker raises an error to be handled by Sidekiq.<commit_after>require 'rails_helper'

describe ImportOpmlWorker do

  before :each do
    # Ensure files are not deleted, we will need them for running tests again!
    allow(File).to receive(:delete).and_return 1

    @user = FactoryGirl.create :user
    @opml_import_job_state = FactoryGirl.build :opml_import_job_state, user_id: @user.id, state: OpmlImportJobState::RUNNING,
                                     total_feeds: 0, processed_feeds: 0
    @user.opml_import_job_state = @opml_import_job_state

    @filename = '1371324422.opml'
    @filepath = File.join __dir__, '..', '..', 'attachments', @filename
    @file_contents = File.read @filepath

    allow(Feedbunch::Application.config.uploads_manager).to receive :read do |user_id, folder, filename|
      expect(user_id).to eq @user.id
      if filename == @filename
        @file_contents
      else
        nil
      end
    end
    allow(Feedbunch::Application.config.uploads_manager).to receive :save
    allow(Feedbunch::Application.config.uploads_manager).to receive :delete

    allow(ImportSubscriptionsWorker).to receive :perform_async
  end

  context 'validations' do

    it 'sets data import state to ERROR if the file does not exist' do
      expect {ImportOpmlWorker.new.perform 'not.a.real.file', @user.id}.to raise_error OpmlImportError
      @user.reload
      expect(@user.opml_import_job_state.state).to eq OpmlImportJobState::ERROR
    end

    it 'sets data import state to ERROR if the file is not well formed XML' do
      not_valid_xml_filename = File.join __dir__, '..', '..', 'attachments', 'not-well-formed-xml.opml'
      file_contents = File.read not_valid_xml_filename
      allow(Feedbunch::Application.config.uploads_manager).to receive(:read).and_return file_contents
      # a malformed XML uploaded by the user is an expected and controlled error, no error will be raised to Sidekiq
      expect {ImportOpmlWorker.new.perform not_valid_xml_filename, @user.id}.not_to raise_error
      @user.reload
      expect(@user.opml_import_job_state.state).to eq OpmlImportJobState::ERROR
    end

    it 'sets data import state to ERROR if the file is not valid OPML' do
      not_valid_opml_filename = File.join __dir__, '..', '..', 'attachments', 'not-valid-opml.opml'
      file_contents = File.read not_valid_opml_filename
      allow(Feedbunch::Application.config.uploads_manager).to receive(:read).and_return file_contents
      expect {ImportOpmlWorker.new.perform not_valid_opml_filename, @user.id}.to raise_error OpmlImportError
{'fraction_non_alphanumeric': 0.09342492639842984, 'fraction_numerical': 0.005103042198233563, 'mean_word_length': 3.753731343283582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4886331', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1532, 'n_words': 391}