require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator.  If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails.  There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec.  Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

describe OrganizationsController, :type => :controller do
  render_views

  # This should return the minimal set of attributes required to create a valid
  # Organization. As you add validations to Organization, be sure to
  # update the return value of this method accordingly.
  def valid_attributes
    { "name" => "MyString", "url" => "http://yelp.com/biz/1", "category_id" => "1", "address" => "500 college ave., Swarthmore PA 19081" }
  end

  # This should return the minimal set of values that should be in the session
  # in order to pass any filters (e.g. authentication) defined in
  # OrganizationsController. Be sure to keep this updated too.
  # def valid_session
  #   {}
  # end

  describe "GET index" do
    it "assigns all organizations as @organizations" do
      organization = FactoryGirl.create(:organization)
      get :index, {}
      expect(assigns(:organizations)).to eq([organization])
    end
    
    it "should not show the organization because of the filter" do
      organization = FactoryGirl.create(:organization)
      get :index, { :category => organization.category_id + 1 }
      expect(assigns(:organizations)).to eq([])
    end
  end

  describe "GET show" do
    before(:each) do
      @organization = FactoryGirl.create(:organization)
    end
    
    it "assigns the requested organization as @organization" do
      get :show, {:id => @organization.to_param}
      expect(assigns(:organization)).to eq(@organization)
    end
    
    it "should assign a new review to review" do
      get :show, {:id => @organization.to_param}
      expect(assigns(:review).organization).to eq(@organization)
      expect(assigns(:review)).to be_new_record
    end
    
    it "should assign the reviews" do
      prev_review = FactoryGirl.create(:review, :organization => @organization)
      get :show, {:id => @organization.to_param}
      expect(assigns(:reviews)).to eq([prev_review])
    end
  end

  describe "CRUD actions" do
    before(:each) do
      @user = FactoryGirl.create(:user, :admin => true)
      sign_in @user
    end
    
    describe "GET new" do
      it "assigns a new organization as @organization" do
        get :new, {}
        expect(assigns(:organization)).to be_a_new(Organization)
      end
      
      it "should set name, url, homepage_url, and address from url parameters" do
        name = "Woot"
        url = "http://www.yelp.com/biz/123"
        homepage_url = "http://www.google.com"
        address = "500 College Ave, Swarthmore Pa, 19081"

        get :new, { :name => name, :url => url, :homepage_url => homepage_url, :address => address }

        expect(assigns(:organization).name).to eq(name)
        expect(assigns(:organization).url).to eq(url)
        expect(assigns(:organization).homepage_url).to eq(homepage_url)
        expect(assigns(:organization).address).to eq(address)
      end
    end

    describe "GET edit" do
      it "assigns the requested organization as @organization" do
        organization = FactoryGirl.create(:organization)
        get :edit, {:id => organization.to_param}
        expect(assigns(:organization)).to eq(organization)
      end
    end

    describe "POST create" do
      describe "with valid params" do
        it "creates a new Organization" do
          expect {
            post :create, {:organization => valid_attributes}
          }.to change(Organization, :count).by(1)
        end

        it "assigns a newly created organization as @organization" do
          post :create, {:organization => valid_attributes}
          expect(assigns(:organization)).to be_a(Organization)
          expect(assigns(:organization)).to be_persisted
        end

        it "redirects to the created organization" do
          post :create, {:organization => valid_attributes}
          expect(response).to redirect_to(Organization.last)
        end
      end

      describe "with invalid params" do
        it "assigns a newly created but unsaved organization as @organization" do
          # Trigger the behavior that occurs when invalid params are submitted
          allow_any_instance_of(Organization).to receive(:save).and_return(false)
          post :create, {:organization => { "name" => "invalid value" }}
          expect(assigns(:organization)).to be_a_new(Organization)
        end

        it "re-renders the 'new' template" do
          # Trigger the behavior that occurs when invalid params are submitted
          allow_any_instance_of(Organization).to receive(:save).and_return(false)
          post :create, {:organization => { "name" => "invalid value" }}
          expect(response).to render_template("new")
        end
      end
      
      describe "organization with that url already exists" do
        it "should redirect to that organization" do
          organization = FactoryGirl.create(:organization)
          post :create, { :organization => valid_attributes.merge(:url => organization.url) }
          expect(response).to redirect_to(organization)
        end
        
        it "should not create a new organization" do
          organization = FactoryGirl.create(:organization)
          expect {
            post :create, { :organization => valid_attributes.merge(:url => organization.url) } 
          }.to_not change(Organization, :count)
        end
      end
    end

    describe "PUT update" do
      describe "with valid params" do
        it "updates the requested organization" do
          organization = FactoryGirl.create(:organization)
          # Assuming there are no other organizations in the database, this
          # specifies that the Organization created on the previous line
          # receives the :update_attributes message with whatever params are
          # submitted in the request.
          expect_any_instance_of(Organization).to receive(:update_attributes).with({ "name" => "MyString" })
          put :update, {:id => organization.to_param, :organization => { "name" => "MyString" }}
        end

        it "assigns the requested organization as @organization" do
          organization = FactoryGirl.create(:organization)
          put :update, {:id => organization.to_param, :organization => valid_attributes}
          expect(assigns(:organization)).to eq(organization)
        end

        it "redirects to the organization" do
          organization = FactoryGirl.create(:organization)
          put :update, {:id => organization.to_param, :organization => valid_attributes}
          expect(response).to redirect_to(organization)
        end
      end

      describe "with invalid params" do
        it "assigns the organization as @organization" do
          organization = FactoryGirl.create(:organization)
          # Trigger the behavior that occurs when invalid params are submitted
          allow_any_instance_of(Organization).to receive(:save).and_return(false)
          put :update, {:id => organization.to_param, :organization => { "name" => "invalid value" }}
          expect(assigns(:organization)).to eq(organization)
        end

        it "re-renders the 'edit' template" do
          organization = FactoryGirl.create(:organization)
          # Trigger the behavior that occurs when invalid params are submitted
          allow_any_instance_of(Organization).to receive(:save).and_return(false)
          put :update, {:id => organization.to_param, :organization => { "name" => "invalid value" }}
          expect(response).to render_template("edit")
        end
      end
    end

    describe "DELETE destroy" do
      it "destroys the requested organization" do
        organization = FactoryGirl.create(:organization)
        expect {
          delete :destroy, {:id => organization.to_param}
        }.to change(Organization, :count).by(-1)
      end

      it "redirects to the organizations list" do
        organization = FactoryGirl.create(:organization)
        delete :destroy, {:id => organization.to_param}
        expect(response).to redirect_to(organizations_url)
      end
    end
  end
  
end
