Guest User

Untitled

a guest
Apr 12th, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.40 KB | None | 0 0
  1. require File.dirname(__FILE__) + '/../spec_helper'
  2. include PersonSpecHelper
  3. include EmailConfirmationSpecHelpers
  4.  
  5. describe PeopleController, ": handling POST /people" do
  6. before(:each) do
  7. setup_person_mock
  8. end
  9.  
  10. def do_post
  11. post :create, {:person => valid_person_attributes, :debug => true}
  12. end
  13.  
  14. it "should create a new person" do
  15. Person.should_receive(:find).with(anything(), duck_type(:keys,:values)).and_return(nil)
  16. lambda { do_post }.should change { Person.count }.by(1)
  17. end
  18.  
  19. end
  20.  
  21. describe PeopleController, ": Requesting /people.xml using POST with valid user" do
  22. integrate_views
  23.  
  24. before(:each) do
  25. setup_person_mock
  26. end
  27.  
  28. def do_post
  29. @request.env["HTTP_ACCEPT"] = "application/xml"
  30. post :create, :person => valid_person_attributes
  31. end
  32.  
  33. it "should not have a body" do
  34. do_post
  35. response.should_not have_tag(:xml)
  36. response.should_not have_tag(:html)
  37. end
  38. end
  39.  
  40. describe PeopleController, ": Requesting /people.xml using POST with invalid user" do
  41. integrate_views
  42.  
  43. before(:each) do
  44. setup_person_mock
  45. end
  46.  
  47. def do_post
  48. accept("application/xml")
  49. post :create, :person => (valid_person_attributes.except :first_name)
  50. end
  51.  
  52. it "should have an errors tag with one nested error tag" do
  53. do_post
  54. response.should have_tag("errors") do
  55. with_tag("error")
  56. end
  57. end
  58. end
  59. ############### Move to a model test ################
  60. # describe PeopleController, ": Requesting /people/1.xml" do
  61. # integrate_views
  62. #
  63. # before(:each) do
  64. # setup_person_mock(:to_xml => 'xml')
  65. # end
  66. #
  67. # def do_get
  68. # @request.env["HTTP_ACCEPT"] = "application/xml"
  69. # get :show, :id => "1"
  70. # end
  71. #
  72. # it "should return person_id = 1" do
  73. # do_get
  74. # response.body.should have_tag("person") do
  75. # with_tag("id", "1")
  76. # end
  77. # end
  78. #
  79. # it "should be populated with the correct name fields" do
  80. # do_get
  81. # response.should have_tag("person") do
  82. # with_tag("first-name", "John")
  83. # with_tag("last-name", "Goodsen")
  84. # end
  85. # end
  86. # end
  87.  
  88. ####################################################
  89. # Tests brought from the users controller spec
  90. # necessary for User refactor into Person/Account
  91. ####################################################
  92.  
  93.  
  94.  
  95. describe "non-existant person", :shared => true do
  96. it "should have status of 404 with non-existant object" do
  97. do_method
  98. @response.response_code.should == 404
  99. end
  100. end
  101.  
  102. describe PeopleController,": Requesting /people using GET" do
  103.  
  104. # /people with no id maps to People#show
  105. def do_get
  106. get :index
  107. end
  108.  
  109. it_should_redirect_to(:login)
  110. it_should_set_flash
  111. end
  112.  
  113. describe PeopleController,": Requesting /people using GET while logged in" do
  114.  
  115. before(:each) do
  116. setup_person_mock
  117. end
  118.  
  119. def do_get
  120. get :index, {:id => @request.session[:person]}
  121. end
  122.  
  123. it_should_redirect_to(:home)
  124. it_should_set_flash
  125. end
  126.  
  127. describe PeopleController,": Requesting /people.xml using GET while logged in" do
  128.  
  129. before(:each) do
  130. setup_person_mock
  131. end
  132.  
  133. def do_get
  134. accept("application/xml")
  135. get :index
  136. end
  137.  
  138. it "should populate people to be rendered" do
  139. do_get
  140. assigns[:people].should_not be_nil
  141. end
  142.  
  143. end
  144.  
  145. describe PeopleController,": Requesting /people/new using GET" do
  146.  
  147. before(:each) do
  148. setup_person_mock
  149. end
  150.  
  151. def do_get
  152. get :new
  153. end
  154.  
  155. it_should_be_success
  156. it_should_render(:new)
  157. end
  158.  
  159. describe PeopleController,": Requesting /people/1;edit using GET when logged in" do
  160. before(:each) do
  161. setup_person_mock(:id => 1)
  162. end
  163.  
  164. def do_get
  165. get :edit, { :id => "1" }
  166. end
  167.  
  168. it "should fail if current person doesn't match" do
  169. controller.should_receive(:current_person).at_least(1).and_return(Person.new)
  170. do_get
  171. response.should be_redirect
  172. end
  173.  
  174. it "should render edit template" do
  175. do_get
  176. response.should render_template(:edit)
  177. end
  178.  
  179. it "should assign the found Person for the view" do
  180. do_get
  181. assigns[:current_person].should equal(@person)
  182. end
  183. end
  184.  
  185. describe PeopleController,": PeopleController create (POST -> /people)" do
  186.  
  187. before(:each) do
  188. @mockperson = null_mock_model( Person, :save => true)
  189. @mockconfirmation = null_mock_model( PasswordResetConfirmation)
  190. Person.stub!(:new).and_return(@mockperson)
  191. PersonNotifier.stub!(:deliver_signup_notification).and_return(true)
  192. controller.stub!(:captcha_valid?).and_return(true)
  193. end
  194.  
  195. def do_post
  196. post :create, { :person => valid_person_attributes }
  197. end
  198.  
  199. it "should create a new person" do
  200. Person.should_receive(:new).with(valid_person_attributes).and_return(@mockperson)
  201. do_post
  202. end
  203.  
  204. it "should save new person" do
  205. @mockperson.should_receive(:save).and_return(true)
  206. do_post
  207. end
  208.  
  209. it "should not send a Password Reset Confirmation" do
  210. PersonNotifier.should_not_receive(:deliver_password_reset_confirmation)
  211. do_post
  212. end
  213.  
  214. it "should send a signup notification" do
  215. PersonNotifier.should_receive(:deliver_signup_notification)
  216. do_post
  217. end
  218.  
  219. it "should re-render registration form if captcha is wrong" do
  220. controller.stub!(:captcha_valid?).and_return(false)
  221. do_post
  222. response.should render_template(:new)
  223. end
  224.  
  225.  
  226. it "should be redirect to person's home page" do
  227. do_post
  228. response.should redirect_to(person_path(@mockperson))
  229. end
  230.  
  231.  
  232. it_should_set_flash
  233.  
  234. it "should re-render new.rhtml if the save fails" do
  235. @mockperson.stub!(:save).and_return(false)
  236. do_post
  237. response.should_not be_redirect
  238. response.should render_template( :new )
  239. end
  240.  
  241. it "should set session[:person]" do
  242. do_post
  243. session[:person].should_not be_nil
  244. end
  245. end
  246.  
  247. describe PeopleController,": Requesting stuff from people we shouldn't get" do
  248.  
  249. before(:each) do
  250. setup_user_mock
  251. end
  252.  
  253. it "should not show people index" do
  254. get :index, {}
  255. flash[:notice].should eql("Requested page was not found")
  256. response.should be_redirect
  257. end
  258.  
  259. def do_method
  260. controller.should_receive(:current_person).at_least(:once).and_return(@person)
  261. Person.should_receive(:find).with("5").and_raise(ActiveRecord::RecordNotFound.new)
  262. get :show, {:id => 5}
  263. end
  264.  
  265. #it_should_behave_like "non-existant person"
  266.  
  267. # it "should redirect to homepage for non-existant people" do
  268. # get :show, { :id => "value" }
  269. # response.should render_template('people/show')
  270. # end
  271. end
  272. #
  273. # describe "PeopleController: update (PUT -> /people/1)" do
  274. #
  275. #
  276. # before(:each) do
  277. # setup_user_mock(:id => 1)
  278. # end
  279. #
  280. # def do_update
  281. # put :update, {:id => "1", :person => {:first_name => "Bill"}}
  282. # end
  283. #
  284. # it "should find the current person" do
  285. # Person.should_receive(:find).and_return(@person)
  286. # do_update
  287. # end
  288. #
  289. # it "should redirect because person5 is attempting to update person1" do
  290. # @person.stub!(:id).and_return(5)
  291. # do_update
  292. # response.should be_redirect
  293. # end
  294. #
  295. # it "should update attributes of the found person" do
  296. # @person.should_receive(:update_attributes).and_return(true)
  297. # do_update
  298. # end
  299. #
  300. # it "should retrieve update name" do
  301. # @person.should_receive(:name).and_return("Bill Doe")
  302. # do_update
  303. # end
  304. #
  305. # it "should set flash :notice to 'updated' message" do
  306. # do_update
  307. # flash[:notice].should match(/updated/)
  308. # end
  309. # end
  310. #
  311. # describe "PeopleController: change_email update (PUT -> /people/1?commit=Change+Email)" do
  312. #
  313. #
  314. # before(:each) do
  315. # @new_email = 'test2@testy.com'
  316. # setup_mock_user(:id => 1)
  317. # @mock_email_confirmation = null_mock_model(ChangeEmailConfirmation, :new_email => "test@testy.com")
  318. # ChangeEmailConfirmation.stub!(:new).and_return(@mock_email_confirmation)
  319. # end
  320. #
  321. # def do_update
  322. # put :update, {:id => "1", :commit => 'Change Email',
  323. # :current_user => { :email => @new_email,
  324. # :email_confirmation => @new_email } }
  325. # end
  326. #
  327. # it "should find the current person" do
  328. # Person.should_receive(:find).and_return(@person)
  329. # do_update
  330. # end
  331. #
  332. # it "should redirect" do
  333. # @session = { :person => 3 }
  334. # @person.stub!(:id).and_return(3)
  335. # do_update
  336. # response.should be_redirect
  337. # end
  338. #
  339. # it "should create a ChangeEmailConfirmation object" do
  340. # ChangeEmailConfirmation.should_receive(:new).and_return(@mock_email_confirmation)
  341. # do_update
  342. # end
  343. #
  344. # it "should save the ChangeEmailConfirmation record" do
  345. # @mock_email_confirmation.should_receive(:save).and_return(true)
  346. # do_update
  347. # end
  348. #
  349. # it "should render a confirmation page to the person" do
  350. # do_update
  351. # response.should render_template('confirm')
  352. # end
  353. #
  354. # it "should set error in flash notice on invalid email" do
  355. # controller.stub!(:valid_email?).and_return(false)
  356. # do_update
  357. # flash[:error].should match(/Could not change email./)
  358. # end
  359. #
  360. # it "should render back to edit page on invalid email" do
  361. # controller.stub!(:valid_email?).and_return(false)
  362. # do_update
  363. # response.should render_template(:edit)
  364. # end
  365. #
  366. # end
  367. #
  368. # describe "PeopleController: change_password update (PUT -> /people/1?commit=Change+Password)" do
  369. #
  370. #
  371. # before(:each) do
  372. # @new_password = 'newpassword'
  373. # setup_mock_user(:id => 1)
  374. # end
  375. #
  376. # def do_update
  377. # put :update, {:id => "1", :commit => 'Change Password',
  378. # :old_password => valid_person_attributes['password'],
  379. # :current_user => { :password => @new_password,
  380. # :password_confirmation => @new_password } }
  381. # end
  382. #
  383. # it "should succeed" do
  384. # Person.should_receive(:find).and_return(@person)
  385. # @person.should_receive(:authenticated?).and_return(true)
  386. # @person.should_receive(:update_attributes).and_return(true)
  387. # do_update
  388. # end
  389. #
  390. # it "should find the current person" do
  391. # Person.should_receive(:find).and_return(@person)
  392. # do_update
  393. # end
  394. #
  395. # it "should redirect" do
  396. # @session = { :person => 3 }
  397. # @person.stub!(:id).and_return(3)
  398. # do_update
  399. # response.should be_redirect
  400. # end
  401. #
  402. # it "should check to make sure that the old password entered matches the person's password" do
  403. # @person.should_receive(:authenticated?).and_return(true)
  404. # do_update
  405. # end
  406. #
  407. # it "with commit = 'Change Password' should assign password and password_confirmation in Person model" do
  408. # @person.should_receive(:update_attributes).and_return(true)
  409. # do_update
  410. # end
  411. #
  412. # it "should return to the edit page" do
  413. # do_update
  414. # response.should be_redirect
  415. # end
  416. #
  417. # it "should set flash to success message" do
  418. # do_update
  419. # flash[:notice].should match(/password has been changed/i)
  420. # end
  421. #
  422. # it "should set error in flash notice with blank password" do
  423. # put :update, {:id => "1", :commit => 'Change Password',
  424. # :old_password => '',
  425. # :current_user => { :password => @new_password,
  426. # :password_confirmation => @new_password } }
  427. # flash[:error].should match(/Please enter your current password/)
  428. # end
  429. #
  430. # it "should set error in flash notice with bad old_password" do
  431. # @person.stub!(:authenticated?).and_return(false)
  432. # do_update
  433. # flash[:error].should match(/Old Password doesn't match/)
  434. # end
  435. #
  436. # it "should set error in flash notice with blank new password" do
  437. # put :update, {:id => "1", :commit => 'Change Password',
  438. # :old_password => valid_person_attributes['password'],
  439. # :current_user => { :password => '',
  440. # :password_confirmation => @new_password } }
  441. # flash[:error].should match(/New password must be entered/)
  442. # end
  443. # end
  444.  
  445. include PersonCreditcardValidateSpecHelper
  446.  
  447. describe PeopleController, ": Signing up for a new subscription" do
  448.  
  449. before(:each) do
  450. setup_person_mock
  451. Person.stub!(:purchase_subscription)
  452. end
  453.  
  454. it "should have a link from the main page that links to a subscription page" do
  455. get :purchase_subscription, {:id =>1}
  456. response.should render_template(:purchase_subscription)
  457. end
  458.  
  459. it "should redirect to success page upon positive validation" do
  460. @person.stub!(:create_paid_subscription)
  461. attributes = valid_person_creditcard_validate_attributes
  462. attributes["exp(3i)"] = nil
  463. attributes["exp(2i)"] = attributes.delete(:exp_month)
  464. attributes["exp(1i)"] = attributes.delete(:exp_year)
  465. post :purchase_subscription, {:id =>1, :person_creditcard_validate => attributes}
  466. response.should redirect_to( person_path(@person) )
  467. end
  468.  
  469. it "should re-render page with error callouts if validation fails" do
  470. post :purchase_subscription, {:id =>1, :person_creditcard_validate => {}}
  471. response.should render_template(:purchase_subscription)
  472. flash.now[:error].should match(/denied/i)
  473. end
  474.  
  475. end
  476.  
  477. describe PeopleController ,"#confirm_email: Clicking link from email before expiration (GET /confirm/email/<code>)" do
  478.  
  479. before(:each) do
  480. @confirmation_code = '123456'
  481. @email = 'test@person.com'
  482. @mock_person = mock_model(Person, :save => true, :email => @email)
  483. @mock_email_confirmation = mock_model(ChangeEmailConfirmation, :person => @mock_person, :new_email => @email)
  484. ChangeEmailConfirmation.stub!(:find_by_confirmation_code).and_return(@mock_email_confirmation)
  485. @mock_person.stub!(:change_email_confirmation).and_return(@mock_email_confirmation)
  486. @mock_person.stub!(:email=) { |val| return val }
  487. end
  488.  
  489. def do_get
  490. get :confirm_email, { :confirmation_code => @confirmation_code }
  491. end
  492.  
  493. it "should receive a confirmation_code in params (from incoming link)" do
  494. do_get
  495. params[:confirmation_code].should eql(@confirmation_code)
  496. end
  497.  
  498. it "should find a matching ChangeEmailConfirmation record matching confirmation_code" do
  499. ChangeEmailConfirmation.should_receive(:find_by_confirmation_code).with(@confirmation_code).and_return(@mock_email_confirmation)
  500. do_get
  501. end
  502.  
  503. it "should find a valid Person for the ChangeEmailConfirmation found" do
  504. @mock_email_confirmation.should_receive(:person).and_return(@mock_person)
  505. do_get
  506. end
  507.  
  508. it "should set the Person's email to the saved new email" do
  509. @mock_email_confirmation.should_receive(:new_email).and_return(@email)
  510. @mock_person.should_receive(:email=).with(@email).and_return(@email)
  511. do_get
  512. end
  513.  
  514. it "should successfully save the updated Person" do
  515. @mock_person.should_receive(:save).and_return(true)
  516. do_get
  517. end
  518.  
  519. it "should set flash to an 'email changed' message" do
  520. do_get
  521. flash[:notice].should match(/email address has been changed/i)
  522. end
  523.  
  524. it "should redirect back to the Person home page" do
  525. do_get
  526. response.should redirect_to(person_path(@mock_person.id))
  527. end
  528.  
  529. it "should redirect to /login if a matching EmailConfirmation cannot be found" do
  530. ChangeEmailConfirmation.stub!(:find_by_confirmation_code).and_return(nil)
  531. do_get
  532. response.response_code.should == 404
  533. end
  534. end
Add Comment
Please, Sign In to add comment