require 'spec_helper' describe AccountsController, "routes" do it { should route(:get, "/plans/abc/accounts/new"). to(:action => :new, :plan_id => :abc) } it { should route(:get, "/accounts").to(:action => :index) } it { should route(:post, "/plans/abc/accounts"). to(:action => :create, :plan_id => :abc) } it { should route(:put, "/accounts/1").to(:action => :update, :id => 1) } it { should route(:get, "/accounts/1/edit").to(:action => :edit, :id => 1) } end describe AccountsController, "new" do let(:signup) { stub('signup') } let(:plan) { Factory(:plan) } before do Signup.stubs(:new => signup) get :new, :plan_id => plan.to_param end it "renders the new account form" do should respond_with(:success) should render_template(:new) end it "assigns a new signup" do Signup.should have_received(:new) should assign_to(:signup).with(signup) end end describe AccountsController, "successful create for a confirmed user" do let(:user) { Factory.stub(:email_confirmed_user) } let(:signup) { stub('signup', :user => user, :email_confirmed? => true, :user= => nil) } let(:signup_attributes) { "attributes" } let(:plan) { Factory(:plan) } before do Signup.stubs(:new => signup) signup.stubs(:save => true) sign_in_as user post :create, :signup => signup_attributes, :plan_id => plan.to_param end it "creates an signup" do Signup.should have_received(:new).with(signup_attributes) signup.should have_received(:save) end it "redirects to the root" do should redirect_to(root_url) end it "sets the current user" do signup.should have_received(:user=).with(user) end it { should set_the_flash.to(/created/i) } it { should_not set_the_flash.to(/confirm/i) } it { should be_signed_in.as(user) } end describe AccountsController, "successful create for an unconfirmed user" do let(:user) { Factory.stub(:user) } let(:signup) { stub('signup', :user => user, :email_confirmed? => false, :user= => nil) } let(:signup_attributes) { "attributes" } let(:plan) { Factory(:plan) } before do Signup.stubs(:new => signup) signup.stubs(:save => true) post :create, :signup => signup_attributes, :plan_id => plan.to_param end it "creates an signup" do Signup.should have_received(:new).with(signup_attributes) signup.should have_received(:save) end it "redirects to the sign in url" do should redirect_to(sign_in_url) end it { should set_the_flash.to(/created/i) } it { should set_the_flash.to(/confirm/i) } end describe AccountsController, "failed create" do let(:signup) { stub('signup', :email_confirmed? => false, :user= => nil) } let(:signup_attributes) { "attributes" } let(:plan) { Factory(:plan) } before do Signup.stubs(:new => signup) signup.stubs(:save => false) post :create, :signup => signup_attributes, :plan_id => plan.to_param end it "creates an signup" do Signup.should have_received(:new).with(signup_attributes) signup.should have_received(:save) end it "renders the new signup form" do should respond_with(:success) should render_template(:new) end it "assigns a new signup" do Signup.should have_received(:new) should assign_to(:signup).with(signup) end end describe AccountsController, "index with multiple projects" do let(:user) { Factory.stub(:user) } let(:accounts) { %w(one two) } let(:projects) { %w(one two) } before do user.stubs(:accounts => accounts) user.stubs(:projects => projects) sign_in_as user get :index end it "renders the dashboard page" do should respond_with(:success) should render_template(:index) end it "assigns the user's accounts" do user.should have_received(:accounts) should assign_to(:accounts).with(accounts) end end describe AccountsController, "index with one project" do let(:user) { Factory.stub(:user) } let(:accounts) { %w(one two) } let(:projects) { %w(one) } before do user.stubs(:accounts => accounts) user.stubs(:projects => projects) sign_in_as user get :index end it "redirects to the project" do should redirect_to(project_url(projects.first)) end end describe AccountsController, "valid update", :as => :account_admin do before do put :update, :account => Factory.attributes_for(:account), :id => account.to_param end it "redirects to settings" do should redirect_to(edit_profile_url) end it { should set_the_flash.to(/updated/) } end describe AccountsController, "invalid update", :as => :account_admin do before do put :update, :account => {:name => ""}, :id => account.to_param end it { should respond_with(:success) } it { should render_template(:edit) } end describe AccountsController, "edit", :as => :account_admin do before do get :edit, :id => account.to_param end it "renders the edit template" do should respond_with(:success) should render_template(:edit) end it "assigns the account" do should assign_to(:account).with(account) end end describe AccountsController, "permissions", :as => :account_member do it { should deny_access. on(:get, :edit, :id => account.to_param). flash(/admin/) } it { should deny_access. on(:put, :update, :id => account.to_param). flash(/admin/) } end