require 'rails_helper'

module UnsFeeder
  RSpec.describe FeedsController, type: :controller do
    routes { UnsFeeder::Engine.routes }
    let(:valid_attributes) do
      {
        :feed => {
          :title => 'test title',
          :url => 'http://www.testurl.com',
        },  
        :river_id => parent_river.to_param,
      }
    end
    let(:invalid_attributes) do
      {
        :uns_feeder_feed => {
          :title => 'test title',
          :url => 'not a url',
        },  
        :river_id => parent_river.to_param,
      }
    end

    let(:test_user) { User.create!(:email => 'a@b.com', :password => 'asdasdasd', :password_confirmation => 'asdasdasd') }
    let(:parent_river) { test_user.rivers.create(:name => 'a river') }
    let(:valid_session) { Hash.new }
    let(:test_feed) { parent_river.feeds.create! valid_attributes[:feed] }

    describe "POST #create" do
      context "when successful" do
        before(:each) do
          sign_in(test_user)
          allow(FeedValidityPolicy).to receive(:feed_valid?).and_return(true)
        end

        it "creates a new Feed" do
          expect {
            post :create, valid_attributes, valid_session
          }.to change(UnsFeeder::Feed, :count).by(1)
        end

        it "assigns a newly created feed as @feed" do
          post :create, valid_attributes, valid_session
          expect(assigns(:feed)).to be_a(Feed)
          expect(assigns(:feed)).to be_persisted
        end

        it "redirects to the parent river" do
          post :create, valid_attributes, valid_session
          expect(response).to redirect_to(river_path(parent_river))
        end
      end

      context "redirects to root after unsuccessful situations" do
        it 'requires user with river' do
          sign_in(test_user)
          post :create, valid_attributes, valid_session
          expect(response).to redirect_to '/'
        end

        it 'requires a river id' do
          sign_in(test_user)
          post :create, {}, valid_session
          expect(response).to redirect_to '/'
        end

        it 'requires valid feed params' do
          sign_in(test_user)
          allow(FeedValidityPolicy).to receive(:feed_valid?).and_return(true)
          post :create, invalid_attributes, valid_session
          expect(response).to redirect_to '/'
        end

        it 'requires the feed to be valid' do
          sign_in(test_user)
          allow(FeedValidityPolicy).to receive(:feed_valid?).and_return(false)
          post :create, valid_attributes, valid_session
          expect(response).to redirect_to '/'
        end
      end
    end

    describe "DELETE #destroy" do
      before(:each) do
        sign_in(test_user)
        allow(FeedValidityPolicy).to receive(:feed_valid?).and_return(true)
      end

      it "destroys the requested feed" do
        feed = test_feed
        expect {
          delete :destroy, {:id => feed.to_param, :river_id => parent_river.to_param}, valid_session
        }.to change(Feed, :count).by(-1)
      end

      it "redirects to the feeds list" do
        delete :destroy, {:id => test_feed.to_param, :river_id => parent_river.to_param}, valid_session
        expect(response).to redirect_to('/')
      end
    end

    describe 'GET #subscribe' do
      it 'requires user with river' do
        sign_in(test_user)
        expect {
          get :subscribe, {:id => 1}, valid_session
        }.to raise_error ActiveRecord::RecordNotFound
      end

      it 'requires a river id' do
        sign_in(test_user)
        expect_any_instance_of(Feed).to receive :subscribe
        get :subscribe, {:id => test_feed.to_param, :river_id => parent_river.to_param}, valid_session
      end
    end
  end
end
