Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 112 lines (92 sloc) 3.585 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
require_relative '../test_helper'

require 'mocha/setup'

describe FeedService do
  describe "#find_or_create!" do
    let(:service) { FeedService.new(target_feed) }
    let(:existing_feed) { mock }
    let(:new_feed) { mock }

    describe "when the feed can be found by ID" do
      # the BSON ID of user 'gavinlaking@rstat.us' (follow me!)
      let(:target_feed) { "505cc1beb4f2cd000200022c" }

      before do
        Feed.stubs(:first)
          .with(has_entry(:id, target_feed))
          .returns(existing_feed)
      end

      it "returns the feed" do
        service.find_or_create!.must_equal existing_feed
      end
    end

    describe "when the feed has a local URL" do
      let(:target_feed) { "someone@example.com" }
      let(:service) { FeedService.new(target_feed, "http://example.com/") }
      let(:user) { mock }
      let(:author) { mock }
      let(:feed) { mock }
      let(:uri) { mock }

      describe "when the feed exists" do
        before do
          User.stubs(:find_by_case_insensitive_username).returns(user)
          user.stubs(:author).returns(author)
          Kernel.stubs(:URI).returns(uri)
          uri.stubs(:host).returns("example.com")
          author.stubs(:feed).returns(feed)

          service.stubs(:find_feed_by_id).returns(nil)
          service.stubs(:find_feed_by_remote_url).returns nil
        end

        it "returns the feed" do
          service.find_or_create!.must_equal feed
        end
      end

      describe "when the feed does not exist" do
        before do
          User.stubs(:find_by_case_insensitive_username).returns(nil)
          Kernel.stubs(:URI).returns(uri)
          uri.stubs(:host).returns("example.com")

          service.stubs(:find_feed_by_id).returns(nil)
          service.stubs(:find_feed_by_remote_url).returns(existing_feed)
        end

        it "moves on to trying to find by remote url" do
          service.find_or_create!.must_equal existing_feed
        end
      end
    end

    describe "when the feed can by found by remote URL" do
      # the remote URL of user 'gavinlaking@rstat.us' (follow me!)
      let(:target_feed) { "https://rstat.us/feeds/505cc1beb4f2cd000200022c.atom" }
      let(:feed_data_cov) { mock }
      let(:feed_data) { mock }

      before do
        Feed.stubs(:first)
          .with(has_entry(:remote_url, "feed_url"))
          .returns(existing_feed)
        SubscriberToFeedDataConverter.stubs(:new).returns(feed_data_cov)
        feed_data_cov.stubs(:get_feed_data!).returns(feed_data)
        feed_data.stubs(:url).returns("feed_url")

        service.stubs(:find_feed_by_id).returns nil
        service.stubs(:find_feed_by_username).returns nil
      end

      it "returns the feed" do
        service.find_or_create!.must_equal existing_feed
      end
    end

    describe "when the feed doesn't exist" do
      let(:target_feed) { "gavinlaking@rstat.us" } # (follow me!)
      let(:feed_data_cov) { mock }
      let(:feed_data) { mock }

      before do
        service.stubs(:find_feed_by_id).returns nil
        service.stubs(:find_feed_by_username).returns nil
        service.stubs(:find_feed_by_remote_url).returns nil
        SubscriberToFeedDataConverter.stubs(:new).returns(feed_data_cov)
        feed_data_cov.stubs(:get_feed_data!).returns(feed_data)
      end

      it "creates the feed" do
        Feed.expects(:create_and_populate!)
          .with(feed_data)
          .returns(new_feed)
        service.find_or_create!.must_equal new_feed
      end
    end
  end
end
Something went wrong with that request. Please try again.