<commit_before>require 'rails_helper'

describe Feed, type: :model do

  before :each do
    @feed = FactoryGirl.create :feed
  end

  context 'validations' do
    it 'accepts valid URLs' do
      @feed.url = 'http://www.xkcd.com'
      expect(@feed.valid?).to be true
    end

    it 'does not accept invalid URLs' do
      old_url = @feed.url
      @feed.update url: 'invalid_url'
      expect(@feed.url).to eq old_url
    end

    it 'accepts an empty URL' do
      @feed.url = ''
      expect(@feed.valid?).to be true
      @feed.url = nil
      expect(@feed.valid?).to be true
    end

    it 'accepts duplicate URLs' do
      feed_dupe = FactoryGirl.build :feed, url: @feed.url
      expect(feed_dupe.valid?).to be true
    end

    it 'accepts valid fetch URLs' do
      @feed.fetch_url = 'http://www.xkcd.com/rss.xml'
      expect(@feed.valid?).to be true
    end

    it 'does not accept invalid fetch URLs' do
      old_fetch_url = @feed.fetch_url
      @feed.update fetch_url: 'invalid_url'
      expect(@feed.fetch_url).to eq old_fetch_url
    end

    it 'does not accept an empty fetch URL' do
      old_fetch_url = @feed.fetch_url
      @feed.update fetch_url: ''
      expect(@feed.fetch_url).to eq old_fetch_url
      @feed.update fetch_url: nil
      expect(@feed.fetch_url).to eq old_fetch_url
    end

    it 'does not accept duplicate fetch URLs' do
      feed_dupe = FactoryGirl.build :feed, fetch_url: @feed.fetch_url
      expect(feed_dupe.valid?).to be false
    end

    it 'does not accept an empty title' do
      @feed.title = ''
      expect(@feed.valid?).to be false
      @feed.title = nil
      expect(@feed.valid?).to be false
    end

  end

  context 'default values' do

    it 'takes a default fetch interval value of 3600 seconds' do
      feed = FactoryGirl.build :feed, fetch_interval_secs: nil
      feed.save!
      expect(feed.fetch_interval_secs).to eq 3600
    end

    it 'does not change the fetch interval if a value is passed' do
      feed = FactoryGirl.build :feed, fetch_interval_secs: 1800
      feed.save!
      expect(feed.fetch_interval_secs).to eq 1800
    end

    it 'marks feed as available by default' do
      feed = FactoryGirl.build :feed, available: nil
      feed.save!
      expect(feed.available).to be true
    end

    it 'uses the fetch_url as default for the url attribute if no url is specified' do
      fetch_url = 'http://some.fetch.url.com/'
      feed = FactoryGirl.build :feed, fetch_url: fetch_url, url: nil
      feed.save!
      expect(feed.url).to eq fetch_url
    end
  end

  context 'sanitization' do

    it 'sanitizes title' do
      unsanitized_title = '<script>alert("pwned!");</script>title'
      sanitized_title = 'title'
      feed = FactoryGirl.create :feed, title: unsanitized_title
      expect(feed.title).to eq sanitized_title
    end

    it 'sanitizes url' do
      unsanitized_url = "http://xkcd.com<script>alert('pwned!');</script>"
      sanitized_url = 'http://xkcd.com/'
      feed = FactoryGirl.create :feed, url: unsanitized_url
      expect(feed.url).to eq sanitized_url
    end

    it 'sanitizes fetch url' do
      unsanitized_url = "http://xkcd.com<script>alert('pwned!');</script>"
      sanitized_url = 'http://xkcd.com/'
      feed = FactoryGirl.create :feed, fetch_url: unsanitized_url
      expect(feed.fetch_url).to eq sanitized_url
    end

  end

  context 'trimming' do

    it 'trims title' do
      untrimmed_title = "\n      title"<commit_msg>Fix tests to adapt to the new html sanitizer in rails 4.2<commit_after>require 'rails_helper'

describe Feed, type: :model do

  before :each do
    @feed = FactoryGirl.create :feed
  end

  context 'validations' do
    it 'accepts valid URLs' do
      @feed.url = 'http://www.xkcd.com'
      expect(@feed.valid?).to be true
    end

    it 'does not accept invalid URLs' do
      old_url = @feed.url
      @feed.update url: 'invalid_url'
      expect(@feed.url).to eq old_url
    end

    it 'accepts an empty URL' do
      @feed.url = ''
      expect(@feed.valid?).to be true
      @feed.url = nil
      expect(@feed.valid?).to be true
    end

    it 'accepts duplicate URLs' do
      feed_dupe = FactoryGirl.build :feed, url: @feed.url
      expect(feed_dupe.valid?).to be true
    end

    it 'accepts valid fetch URLs' do
      @feed.fetch_url = 'http://www.xkcd.com/rss.xml'
      expect(@feed.valid?).to be true
    end

    it 'does not accept invalid fetch URLs' do
      old_fetch_url = @feed.fetch_url
      @feed.update fetch_url: 'invalid_url'
      expect(@feed.fetch_url).to eq old_fetch_url
    end

    it 'does not accept an empty fetch URL' do
      old_fetch_url = @feed.fetch_url
      @feed.update fetch_url: ''
      expect(@feed.fetch_url).to eq old_fetch_url
      @feed.update fetch_url: nil
      expect(@feed.fetch_url).to eq old_fetch_url
    end

    it 'does not accept duplicate fetch URLs' do
      feed_dupe = FactoryGirl.build :feed, fetch_url: @feed.fetch_url
      expect(feed_dupe.valid?).to be false
    end

    it 'does not accept an empty title' do
      @feed.title = ''
      expect(@feed.valid?).to be false
      @feed.title = nil
      expect(@feed.valid?).to be false
    end

  end

  context 'default values' do

    it 'takes a default fetch interval value of 3600 seconds' do
      feed = FactoryGirl.build :feed, fetch_interval_secs: nil
      feed.save!
      expect(feed.fetch_interval_secs).to eq 3600
    end

    it 'does not change the fetch interval if a value is passed' do
      feed = FactoryGirl.build :feed, fetch_interval_secs: 1800
      feed.save!
      expect(feed.fetch_interval_secs).to eq 1800
    end

    it 'marks feed as available by default' do
      feed = FactoryGirl.build :feed, available: nil
      feed.save!
      expect(feed.available).to be true
    end

    it 'uses the fetch_url as default for the url attribute if no url is specified' do
      fetch_url = 'http://some.fetch.url.com/'
      feed = FactoryGirl.build :feed, fetch_url: fetch_url, url: nil
      feed.save!
      expect(feed.url).to eq fetch_url
    end
  end

  context 'sanitization' do

    it 'sanitizes title' do
      unsanitized_title = '<script>alert("pwned!");</script>title'
      sanitized_title = 'alert("pwned!");title'
      feed = FactoryGirl.create :feed, title: unsanitized_title
      expect(feed.title).to eq sanitized_title
    end

    it 'sanitizes url' do
      unsanitized_url = 'http://xkcd.com<script>alert("pwned!");</script>'
      sanitized_url = 'http://xkcd.comalert("pwned!");/'
      feed = FactoryGirl.create :feed, url: unsanitized_url
      expect(feed.url).to eq sanitized_url
    end

    it 'sanitizes fetch url' do
      unsanitized_url = 'http://xkcd.com<script>alert("pwned!");</script>'
      sanitized_url = 'http://xkcd.comalert("pwned!");/'
      feed = FactoryGirl.create :feed, fetch_url: unsanitized_url
      expect(feed.fetch_url).to eq sanitized_url
    end

  end

  context 'trimming' do

    it 'trims title' do
      untrimmed_title = "\n      title"
{'fraction_non_alphanumeric': 0.10398293029871977, 'fraction_numerical': 0.0048364153627311526, 'mean_word_length': 2.6524675324675324, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, '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': '911913', 'n_tokens_mistral': 2416, 'n_tokens_neox': 2278, 'n_words': 660}