require File.dirname(__FILE__) + '/../spec_helper' include PersonSpecHelper include EmailConfirmationSpecHelpers describe PeopleController, ": handling POST /people" do before(:each) do setup_person_mock end def do_post post :create, {:person => valid_person_attributes, :debug => true} end it "should create a new person" do Person.should_receive(:find).with(anything(), duck_type(:keys,:values)).and_return(nil) lambda { do_post }.should change { Person.count }.by(1) end end describe PeopleController, ": Requesting /people.xml using POST with valid user" do integrate_views before(:each) do setup_person_mock end def do_post @request.env["HTTP_ACCEPT"] = "application/xml" post :create, :person => valid_person_attributes end it "should not have a body" do do_post response.should_not have_tag(:xml) response.should_not have_tag(:html) end end describe PeopleController, ": Requesting /people.xml using POST with invalid user" do integrate_views before(:each) do setup_person_mock end def do_post accept("application/xml") post :create, :person => (valid_person_attributes.except :first_name) end it "should have an errors tag with one nested error tag" do do_post response.should have_tag("errors") do with_tag("error") end end end ############### Move to a model test ################ # describe PeopleController, ": Requesting /people/1.xml" do # integrate_views # # before(:each) do # setup_person_mock(:to_xml => 'xml') # end # # def do_get # @request.env["HTTP_ACCEPT"] = "application/xml" # get :show, :id => "1" # end # # it "should return person_id = 1" do # do_get # response.body.should have_tag("person") do # with_tag("id", "1") # end # end # # it "should be populated with the correct name fields" do # do_get # response.should have_tag("person") do # with_tag("first-name", "John") # with_tag("last-name", "Goodsen") # end # end # end #################################################### # Tests brought from the users controller spec # necessary for User refactor into Person/Account #################################################### describe "non-existant person", :shared => true do it "should have status of 404 with non-existant object" do do_method @response.response_code.should == 404 end end describe PeopleController,": Requesting /people using GET" do # /people with no id maps to People#show def do_get get :index end it_should_redirect_to(:login) it_should_set_flash end describe PeopleController,": Requesting /people using GET while logged in" do before(:each) do setup_person_mock end def do_get get :index, {:id => @request.session[:person]} end it_should_redirect_to(:home) it_should_set_flash end describe PeopleController,": Requesting /people.xml using GET while logged in" do before(:each) do setup_person_mock end def do_get accept("application/xml") get :index end it "should populate people to be rendered" do do_get assigns[:people].should_not be_nil end end describe PeopleController,": Requesting /people/new using GET" do before(:each) do setup_person_mock end def do_get get :new end it_should_be_success it_should_render(:new) end describe PeopleController,": Requesting /people/1;edit using GET when logged in" do before(:each) do setup_person_mock(:id => 1) end def do_get get :edit, { :id => "1" } end it "should fail if current person doesn't match" do controller.should_receive(:current_person).at_least(1).and_return(Person.new) do_get response.should be_redirect end it "should render edit template" do do_get response.should render_template(:edit) end it "should assign the found Person for the view" do do_get assigns[:current_person].should equal(@person) end end describe PeopleController,": PeopleController create (POST -> /people)" do before(:each) do @mockperson = null_mock_model( Person, :save => true) @mockconfirmation = null_mock_model( PasswordResetConfirmation) Person.stub!(:new).and_return(@mockperson) PersonNotifier.stub!(:deliver_signup_notification).and_return(true) controller.stub!(:captcha_valid?).and_return(true) end def do_post post :create, { :person => valid_person_attributes } end it "should create a new person" do Person.should_receive(:new).with(valid_person_attributes).and_return(@mockperson) do_post end it "should save new person" do @mockperson.should_receive(:save).and_return(true) do_post end it "should not send a Password Reset Confirmation" do PersonNotifier.should_not_receive(:deliver_password_reset_confirmation) do_post end it "should send a signup notification" do PersonNotifier.should_receive(:deliver_signup_notification) do_post end it "should re-render registration form if captcha is wrong" do controller.stub!(:captcha_valid?).and_return(false) do_post response.should render_template(:new) end it "should be redirect to person's home page" do do_post response.should redirect_to(person_path(@mockperson)) end it_should_set_flash it "should re-render new.rhtml if the save fails" do @mockperson.stub!(:save).and_return(false) do_post response.should_not be_redirect response.should render_template( :new ) end it "should set session[:person]" do do_post session[:person].should_not be_nil end end describe PeopleController,": Requesting stuff from people we shouldn't get" do before(:each) do setup_user_mock end it "should not show people index" do get :index, {} flash[:notice].should eql("Requested page was not found") response.should be_redirect end def do_method controller.should_receive(:current_person).at_least(:once).and_return(@person) Person.should_receive(:find).with("5").and_raise(ActiveRecord::RecordNotFound.new) get :show, {:id => 5} end #it_should_behave_like "non-existant person" # it "should redirect to homepage for non-existant people" do # get :show, { :id => "value" } # response.should render_template('people/show') # end end # # describe "PeopleController: update (PUT -> /people/1)" do # # # before(:each) do # setup_user_mock(:id => 1) # end # # def do_update # put :update, {:id => "1", :person => {:first_name => "Bill"}} # end # # it "should find the current person" do # Person.should_receive(:find).and_return(@person) # do_update # end # # it "should redirect because person5 is attempting to update person1" do # @person.stub!(:id).and_return(5) # do_update # response.should be_redirect # end # # it "should update attributes of the found person" do # @person.should_receive(:update_attributes).and_return(true) # do_update # end # # it "should retrieve update name" do # @person.should_receive(:name).and_return("Bill Doe") # do_update # end # # it "should set flash :notice to 'updated' message" do # do_update # flash[:notice].should match(/updated/) # end # end # # describe "PeopleController: change_email update (PUT -> /people/1?commit=Change+Email)" do # # # before(:each) do # @new_email = 'test2@testy.com' # setup_mock_user(:id => 1) # @mock_email_confirmation = null_mock_model(ChangeEmailConfirmation, :new_email => "test@testy.com") # ChangeEmailConfirmation.stub!(:new).and_return(@mock_email_confirmation) # end # # def do_update # put :update, {:id => "1", :commit => 'Change Email', # :current_user => { :email => @new_email, # :email_confirmation => @new_email } } # end # # it "should find the current person" do # Person.should_receive(:find).and_return(@person) # do_update # end # # it "should redirect" do # @session = { :person => 3 } # @person.stub!(:id).and_return(3) # do_update # response.should be_redirect # end # # it "should create a ChangeEmailConfirmation object" do # ChangeEmailConfirmation.should_receive(:new).and_return(@mock_email_confirmation) # do_update # end # # it "should save the ChangeEmailConfirmation record" do # @mock_email_confirmation.should_receive(:save).and_return(true) # do_update # end # # it "should render a confirmation page to the person" do # do_update # response.should render_template('confirm') # end # # it "should set error in flash notice on invalid email" do # controller.stub!(:valid_email?).and_return(false) # do_update # flash[:error].should match(/Could not change email./) # end # # it "should render back to edit page on invalid email" do # controller.stub!(:valid_email?).and_return(false) # do_update # response.should render_template(:edit) # end # # end # # describe "PeopleController: change_password update (PUT -> /people/1?commit=Change+Password)" do # # # before(:each) do # @new_password = 'newpassword' # setup_mock_user(:id => 1) # end # # def do_update # put :update, {:id => "1", :commit => 'Change Password', # :old_password => valid_person_attributes['password'], # :current_user => { :password => @new_password, # :password_confirmation => @new_password } } # end # # it "should succeed" do # Person.should_receive(:find).and_return(@person) # @person.should_receive(:authenticated?).and_return(true) # @person.should_receive(:update_attributes).and_return(true) # do_update # end # # it "should find the current person" do # Person.should_receive(:find).and_return(@person) # do_update # end # # it "should redirect" do # @session = { :person => 3 } # @person.stub!(:id).and_return(3) # do_update # response.should be_redirect # end # # it "should check to make sure that the old password entered matches the person's password" do # @person.should_receive(:authenticated?).and_return(true) # do_update # end # # it "with commit = 'Change Password' should assign password and password_confirmation in Person model" do # @person.should_receive(:update_attributes).and_return(true) # do_update # end # # it "should return to the edit page" do # do_update # response.should be_redirect # end # # it "should set flash to success message" do # do_update # flash[:notice].should match(/password has been changed/i) # end # # it "should set error in flash notice with blank password" do # put :update, {:id => "1", :commit => 'Change Password', # :old_password => '', # :current_user => { :password => @new_password, # :password_confirmation => @new_password } } # flash[:error].should match(/Please enter your current password/) # end # # it "should set error in flash notice with bad old_password" do # @person.stub!(:authenticated?).and_return(false) # do_update # flash[:error].should match(/Old Password doesn't match/) # end # # it "should set error in flash notice with blank new password" do # put :update, {:id => "1", :commit => 'Change Password', # :old_password => valid_person_attributes['password'], # :current_user => { :password => '', # :password_confirmation => @new_password } } # flash[:error].should match(/New password must be entered/) # end # end include PersonCreditcardValidateSpecHelper describe PeopleController, ": Signing up for a new subscription" do before(:each) do setup_person_mock Person.stub!(:purchase_subscription) end it "should have a link from the main page that links to a subscription page" do get :purchase_subscription, {:id =>1} response.should render_template(:purchase_subscription) end it "should redirect to success page upon positive validation" do @person.stub!(:create_paid_subscription) attributes = valid_person_creditcard_validate_attributes attributes["exp(3i)"] = nil attributes["exp(2i)"] = attributes.delete(:exp_month) attributes["exp(1i)"] = attributes.delete(:exp_year) post :purchase_subscription, {:id =>1, :person_creditcard_validate => attributes} response.should redirect_to( person_path(@person) ) end it "should re-render page with error callouts if validation fails" do post :purchase_subscription, {:id =>1, :person_creditcard_validate => {}} response.should render_template(:purchase_subscription) flash.now[:error].should match(/denied/i) end end describe PeopleController ,"#confirm_email: Clicking link from email before expiration (GET /confirm/email/)" do before(:each) do @confirmation_code = '123456' @email = 'test@person.com' @mock_person = mock_model(Person, :save => true, :email => @email) @mock_email_confirmation = mock_model(ChangeEmailConfirmation, :person => @mock_person, :new_email => @email) ChangeEmailConfirmation.stub!(:find_by_confirmation_code).and_return(@mock_email_confirmation) @mock_person.stub!(:change_email_confirmation).and_return(@mock_email_confirmation) @mock_person.stub!(:email=) { |val| return val } end def do_get get :confirm_email, { :confirmation_code => @confirmation_code } end it "should receive a confirmation_code in params (from incoming link)" do do_get params[:confirmation_code].should eql(@confirmation_code) end it "should find a matching ChangeEmailConfirmation record matching confirmation_code" do ChangeEmailConfirmation.should_receive(:find_by_confirmation_code).with(@confirmation_code).and_return(@mock_email_confirmation) do_get end it "should find a valid Person for the ChangeEmailConfirmation found" do @mock_email_confirmation.should_receive(:person).and_return(@mock_person) do_get end it "should set the Person's email to the saved new email" do @mock_email_confirmation.should_receive(:new_email).and_return(@email) @mock_person.should_receive(:email=).with(@email).and_return(@email) do_get end it "should successfully save the updated Person" do @mock_person.should_receive(:save).and_return(true) do_get end it "should set flash to an 'email changed' message" do do_get flash[:notice].should match(/email address has been changed/i) end it "should redirect back to the Person home page" do do_get response.should redirect_to(person_path(@mock_person.id)) end it "should redirect to /login if a matching EmailConfirmation cannot be found" do ChangeEmailConfirmation.stub!(:find_by_confirmation_code).and_return(nil) do_get response.response_code.should == 404 end end