require 'spec_helper'

describe ProjectsController, "routes" do
  it { should route(:get, "/accounts/abc/projects/new").
                to(:action => :new, :account_id => 'abc') }
  it { should route(:post, "/accounts/abc/projects").
                to(:action => :create, :account_id => 'abc') }
  it { should route(:get, "/accounts/abc/projects/def/edit").
                to(:action => :edit, :account_id => 'abc', :id => 'def') }
  it { should route(:put, "/accounts/abc/projects/def").
                to(:action => :update, :account_id => 'abc', :id => 'def') }
  it { should route(:delete, "/accounts/abc/projects/def").
                to(:action => :destroy, :account_id => 'abc', :id => 'def') }
  it { should route(:get, "/accounts/abc/projects").
                to(:action => :index, :account_id => 'abc') }
end

describe ProjectsController, "new", :as => :account_admin do
  before do
    get :new, :account_id => account.to_param
  end

  it { should respond_with(:success) }
  it { should render_template(:new) }
  it { should_not set_the_flash }
  it { should assign_to( :project) }

  it "has a new project that belongs to account" do
    assigns(:project).account.should == account
  end
end

describe ProjectsController, "create", :as => :account_admin do
  before do
    @project_count = Project.count
    post :create,
         :project    => Factory.attributes_for(:project),
         :account_id => account.to_param
  end

  it "should change Project count by 1" do
    Project.count.should == @project_count + 1
  end

  it "should place the new project into the account" do
    Project.last.account.should == account
  end

  it "should redirect to the edit page" do
    should redirect_to(controller.edit_project_url(assigns(:project)))
  end
end

describe ProjectsController, "edit", :as => :project_admin do
  before do
    get :edit, :id => project.to_param, :account_id => account.to_param
  end

  it { should respond_with(:success) }
  it { should render_template(:edit) }
  it { should_not set_the_flash }
  it { should assign_to(:project) }
end

describe ProjectsController, "update", :as => :project_admin do
  before do
    put :update, :project    => Factory.attributes_for(:project),
                 :id         => project.to_param,
                 :account_id => account.to_param
  end

  it "should redirect to account_projects_url" do
    should redirect_to(account_projects_url(account))
  end
end

describe ProjectsController, "destroy", :as => :project_admin do
  before do
    delete :destroy, :id => project.to_param, :account_id => account.to_param
  end

  it { should set_the_flash.to(/deleted/) }
  it { should assign_to( :project) }

  it "should redirect to account_projects_url" do
    should redirect_to(account_projects_url(account))
  end
end

describe ProjectsController, "index", :as => :account_admin do
  let(:projects) { ['one', 'two'] }

  before do
    Account.stubs(:find_by_keyword! => account)
    account.stubs(:projects => projects)
    get :index, :account_id => account.to_param
  end

  it "renders the index template" do
    should respond_with(:success)
    should render_template(:index)
  end

  it "assigns projects" do
    account.should have_received(:projects)
    should assign_to(:projects).with(projects)
  end
end

describe ProjectsController, "as a non-admin", :as => :project_member do
  it { should deny_access.on(:get, :edit, :id         => project.to_param,
                                          :account_id => account.to_param).
                          flash(/admin/) }

  it { should deny_access.on(:put, :update, :id         => project.to_param,
                                            :account_id => account.to_param).
                          flash(/admin/) }

  it { should deny_access.on(:delete, :destroy, :id         => project.to_param,
                                                :account_id => account.to_param).
                          flash(/admin/) }

  it { should deny_access.on(:get, :new, :account_id => account.to_param).flash(/admin/) }

  it { should deny_access.on(:post, :create, :account_id => account.to_param).
                          flash(/admin/) }
end