Guest User

Untitled

a guest
Oct 4th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.90 KB | None | 0 0
  1. # -*- encoding : utf-8 -*-
  2. require 'spec_helper'
  3.  
  4. describe OffersController do
  5. include DeviseSpecHelper
  6. let(:offers){ double('offers') }
  7. let(:offer){
  8. mock_model(Offer,
  9. :id => 1,
  10. :to_param => 'ABC',
  11. :code => 'mycode',
  12. :secret => 'mysecret',
  13. :status => 'nextstatus',
  14. :current_step => nil,
  15. :stay_length => 1,
  16. :save => true,
  17. :reviews => Review,
  18. :user => User.new,
  19. :reviews_count= => true,
  20. :remove_deleted_translations! => true,
  21. :touch => true,
  22. :public_lat => 10,
  23. :public_lng => 10,
  24. :city => 'Hamburg'
  25. )
  26. }
  27.  
  28. let(:user_offer){
  29. mock_model(Offer,
  30. :to_param => 'ABC',
  31. :status => 'nextstatus',
  32. :remove_deleted_translations! => true
  33. )
  34. }
  35.  
  36. before do
  37. current_user.stub(:super_admin?)
  38. current_user.stub(:country_admin?)
  39. offer.stub(:status=).with('address')
  40. user_offer.stub(:status=).with('address')
  41. offer.stub(:user=)
  42. user_offer.stub(:user=)
  43. end
  44.  
  45. describe "GET index" do
  46. def default_params
  47. [:get, :index]
  48. end
  49.  
  50. before do
  51. user_is_signed_in
  52. current_user.stub_chain(:offers, :non_deleted){ offers }
  53. end
  54.  
  55. it "authenticates user" do
  56. should_authenticate_user(default_params)
  57. end
  58.  
  59. it "calls offers on current_user" do
  60. controller.current_user.should_receive(:offers)
  61. call_action
  62. end
  63.  
  64. it "assigns @offers" do
  65. call_action
  66. assigns[:offers].should == offers
  67. end
  68.  
  69. it "renders index" do
  70. call_action
  71. response.should render_template('index')
  72. end
  73. end
  74.  
  75. describe "GET new" do
  76. def default_params
  77. [:get, :new]
  78. end
  79.  
  80. before do
  81. Offer.stub(:new){ offer }
  82. end
  83.  
  84. it "deletes offer_code from cookie" do
  85. @request.cookies['offer_code'] = 'mycode'
  86. call_action
  87. response.cookies['offer_code'].should be_nil
  88. end
  89.  
  90. it "initializes offer with status address" do
  91. call_action
  92. assigns[:offer].should == offer
  93. end
  94.  
  95. describe "assigns email on offer if user present" do
  96. it "calls assign method" do
  97. controller.should_receive(:set_email_if_not_set)
  98. call_action
  99. end
  100.  
  101. describe "if signed in it sets email" do
  102. before do
  103. user_is_signed_in
  104. current_user.stub(:email).and_return('peter@griffin.xx')
  105. end
  106.  
  107. context "logged in as normal user" do
  108. before do
  109. current_user.stub(:admin?){ false }
  110. current_user.stub(:super_admin?){ false }
  111. offer.stub(:email)
  112. end
  113.  
  114. it "and email is not set" do
  115. offer.should_receive(:email=).with('peter@griffin.xx')
  116. call_action
  117. end
  118.  
  119. it "not if email has been set prior" do
  120. offer.stub(:email){ 'mymail' }
  121. offer.should_not_receive(:email=).with('peter@griffin.xx')
  122. call_action
  123. end
  124. end
  125. end
  126. end
  127.  
  128. it "renders new" do
  129. call_action
  130. response.should render_template('new')
  131. end
  132. end
  133.  
  134. describe "POST create" do
  135. def default_params
  136. [:post, :create, { :offer => {:title => 'my offer'} }]
  137. end
  138.  
  139. before do
  140. Offer.stub(:new).with({'title' => 'my offer'}){ offer }
  141. offer.stub(:user=)
  142. offer.stub(:affiliate_code=)
  143. offer.stub(:set_status_to_next_step!)
  144. offer.stub(:locale=)
  145. controller.stub(:user_signed_in_as_offer_admin?)
  146. end
  147.  
  148. it "sets affiliate_code" do
  149. @request.cookies['affiliate_code'] = 'mycode'
  150. offer.should_receive(:affiliate_code=).with('mycode')
  151. call_action
  152. end
  153.  
  154. it "sets locale" do
  155. controller.stub(:current_locale) { 'de' }
  156. offer.should_receive(:locale=).with('de')
  157. call_action
  158. end
  159.  
  160.  
  161. context "when signed in" do
  162. before do
  163. user_is_signed_in
  164. end
  165.  
  166. it "builds offer with given params" do
  167. offer.should_receive(:user=).with(current_user)
  168. call_action
  169. assigns[:offer].should == offer
  170. end
  171. end
  172.  
  173.  
  174. context "when not signed in" do
  175. it "creates new offer with given params" do
  176. Offer.should_receive(:new).with({'title' => 'my offer'})
  177. call_action
  178. assigns[:offer].should == offer
  179. end
  180.  
  181. context "when save succeeds" do
  182. before do
  183. offer.stub(:set_status_to_next_step!){ true }
  184. end
  185.  
  186. it "stores offer_code in cookie" do
  187. call_action
  188. response.cookies.should == {"offer_code"=>"mycode", "offer_secret" => "mysecret", "locale" => "en", "currency" => "EUR"}
  189. end
  190. end
  191. end
  192.  
  193. context "when offer valid" do
  194. before do
  195. offer.stub(:set_status_to_next_step!){ true }
  196. offer.stub(:valid?) { true }
  197. # FIXME this is highly dependent on the implementation
  198. offer.stub(:status){ 'accommodation' }
  199. end
  200.  
  201. it "call set_status_to_next_step! on offer" do
  202. offer.should_receive(:set_status_to_next_step!)
  203. call_action
  204. end
  205.  
  206. it "redirects to edit step offer path" do
  207. call_action
  208. response.should redirect_to('/offers/ABC/edit/accommodation?use_application_layout=true')
  209. end
  210. end
  211.  
  212. context "when not valid" do
  213. before do
  214. offer.stub(:set_status_to_next_step!){ false }
  215. offer.stub(:valid?) { false }
  216. end
  217.  
  218. it "renders :new" do
  219. call_action
  220. response.should render_template('new')
  221. end
  222.  
  223. it "does not set correct flash message anymore" do
  224. errors = %w[Gone bad]
  225. offer.stub(:errors) { errors }
  226. errors.stub(:full_messages) { errors }
  227. call_action
  228. flash[:error].should_not == 'Gone. bad.'
  229. end
  230. end
  231. end
  232.  
  233. describe "GET edit" do
  234. let(:user_offer){ mock_model(Offer, :status => 'published', :to_param => 'ABC', :remove_deleted_translations! => true) }
  235.  
  236. before do
  237. controller.stub(:set_email_if_not_set)
  238. end
  239.  
  240. context "when no step given" do
  241. def default_params
  242. [:get, :edit, { :id => 'ABC' }]
  243. end
  244.  
  245. context "when not signed in but offer code set in cookie" do
  246. before do
  247. @request.cookies['offer_secret'] = 'mysecret'
  248. @request.cookies['offer_code'] = 'ABC'
  249. Offer.stub(:find_by_code).with('ABC'){ offer }
  250. offer.stub(:valid_for_step?)
  251. offer.stub(:step_valid?)
  252. offer.stub(:previous_step){ 'address' }
  253. end
  254.  
  255. it "loads offer from cookie" do
  256. Offer.should_receive(:find_by_code).with('ABC')
  257. call_action
  258. assigns[:offer].should == offer
  259. end
  260.  
  261. it "raises exception if secret does not match" do
  262. offer.stub(:secret){ nil }
  263. lambda{ call_action }.should raise_exception("wrong secret for offer 1")
  264. end
  265. end
  266.  
  267. context "when signed" do
  268. before do
  269. user_is_signed_in
  270. current_user.stub_chain(:offers, :non_deleted, :find_by_code){ user_offer }
  271. user_offer.stub(:valid_for_step?)
  272. current_user.stub(:admin?){ false }
  273. user_offer.stub(:step_valid?)
  274. user_offer.stub(:previous_step){ 'prev' }
  275. end
  276.  
  277. context "as super_admin" do
  278. let(:offer){ mock_model(Offer, :to_param => 'ABC', :status => 'published', :remove_deleted_translations! => true) }
  279. before do
  280. current_user.stub_chain(:offers, :non_deleted, :find_by_code){ nil }
  281. current_user.stub(:super_admin?){ true }
  282. offer.stub(:valid_for_step?){ false }
  283. offer.stub(:step_valid?){ false }
  284. end
  285.  
  286. it "loads offer by code and assigns to @offer" do
  287. Offer.should_receive(:find_by_code).with('ABC').and_return(offer)
  288. call_action
  289. assigns[:offer].should == offer
  290. end
  291. end
  292.  
  293. context "as country_admin" do
  294. let(:offer){ mock_model(Offer, :to_param => 'ABC', :status => 'published', :remove_deleted_translations! => true) }
  295. before do
  296. current_user.stub_chain(:offers, :non_deleted, :find_by_code){ nil }
  297. current_user.stub(:country_admin?){ true }
  298. current_user.stub(:admin_countries){ ["de","en"] }
  299. offer.stub(:valid_for_step?){ false }
  300. offer.stub(:step_valid?){ false }
  301. end
  302.  
  303. it "loads offer by code and assigns to @offer" do
  304. offers = double("Offers")
  305. Offer.should_receive(:where).with(:country_code_iso => ["de","en"]).and_return(offers)
  306. offers.should_receive(:find_by_code).with('ABC').and_return(offer)
  307. call_action
  308. assigns[:offer].should == offer
  309. end
  310. end
  311.  
  312. context "as normal user" do
  313. before do
  314. current_user.stub(:country_admin?){ false }
  315. current_user.stub(:super_admin?){ false }
  316. end
  317.  
  318. it "loads offer from users offers" do
  319. current_user.offers.non_deleted.should_receive(:find_by_code).with('ABC')
  320. call_action
  321. assigns[:offer].should == user_offer
  322. end
  323. end
  324.  
  325. context "when offer loaded" do
  326. before do
  327. user_offer.stub(:step_valid?) { true }
  328. user_offer.stub(:valid_for_step?)
  329. user_offer.stub(:previous_step){ 'step' }
  330. end
  331.  
  332. it "checks validity for offer.status" do
  333. user_offer.should_receive(:valid_for_step?).with('published'){ true }
  334. call_action
  335. end
  336.  
  337. context "when valid for step" do
  338. before do
  339. user_offer.stub(:valid_for_step?).with('published'){ true }
  340. user_offer.stub(:previous_status).with('published'){ 'prevstatus' }
  341. user_offer.stub(:remove_deleted_translations!)
  342. user_offer.stub(:ensure_title_description!)
  343. end
  344.  
  345. it "assigns step" do
  346. call_action
  347. assigns[:step].should == 'published'
  348. end
  349.  
  350. it "skips account step when signed in and current step is details" do
  351. user_offer.stub(:status) { "details" }
  352. user_offer.stub(:valid_for_step?).with('details'){ true }
  353. call_action
  354. assigns[:step].should == 'details'
  355. end
  356.  
  357. it "renders edit" do
  358. call_action
  359. response.should render_template('edit')
  360. end
  361. end
  362.  
  363. context "when not valid for step" do
  364. before do
  365. user_offer.stub(:set_status_to_previous_step!)
  366. user_offer.stub(:valid_for_step?){ false }
  367. #user_offer.stub(:previous_step){ 'prevstatus' }
  368. end
  369.  
  370. it "calls set_status_to_previous_step" do
  371. user_offer.should_receive(:set_status_to_previous_step!)
  372. call_action
  373. end
  374.  
  375. it "redirects to previous step edit" do
  376. call_action
  377. response.should redirect_to('/offers/ABC/edit/published')
  378. end
  379. end
  380.  
  381. context "when step not valid" do
  382. before do
  383. user_offer.stub(:step_valid?){ false }
  384. end
  385.  
  386. it "redirects to previous step edit" do
  387. call_action
  388. response.should_not be_success
  389. end
  390. end
  391. end
  392. end
  393. end
  394.  
  395. context "when step given" do
  396. def default_params
  397. [:get, :edit, { :id => 'ABC', :step => 'address' }]
  398. end
  399.  
  400. before do
  401. user_is_signed_in
  402. current_user.stub_chain(:offers, :non_deleted, :find_by_code){ user_offer }
  403. user_offer.stub(:valid_for_step?){ true }
  404. user_offer.stub(:step_valid?){ true }
  405. user_offer.stub(:previous_status){ 'prevstatus' }
  406. user_offer.stub(:ensure_title_description!)
  407. end
  408.  
  409. it "assigns step if step_valid" do
  410. call_action
  411. assigns[:step].should == 'address'
  412. end
  413.  
  414. context "when step not valid" do
  415. before do
  416. user_offer.stub(:step_valid?){ false }
  417. end
  418.  
  419. it "redirects to previous step edit" do
  420. call_action
  421. response.should_not be_success
  422. end
  423. end
  424.  
  425. context "when step overview" do
  426. def default_params
  427. [:get, :edit, { :id => 'ABC', :step => "overview" }]
  428. end
  429.  
  430. it "assigns step" do
  431. call_action
  432. assigns[:step].should == 'overview'
  433. end
  434.  
  435. it "renders edit" do
  436. call_action
  437. response.should render_template('edit')
  438. end
  439. end
  440. end
  441.  
  442. end
  443.  
  444. describe "before_filter signup_and_in" do
  445. let(:user){ mock_model("User") }
  446.  
  447. before do
  448. offer.stub(:attributes=)
  449. offer.stub(:was_acceptable_before=)
  450. offer.stub(:acceptable?)
  451. offer.stub(:published?){ false }
  452. offer.stub(:set_status_to_next_step!)
  453. offer.stub(:ensure_title_description!)
  454. offer.stub(:append_user)
  455. end
  456.  
  457. context "signed in and access_type login" do
  458. context "signed in" do
  459. let(:offers) { [] }
  460.  
  461. def default_params
  462. [:post, :update, { :id => 'ABC', :access_type => "login", :offer => {:title => 'my offer'}, :user => {:email => "big@poppa.xx", :firstname => "big", :password => "xxx"} }]
  463. end
  464.  
  465. before do
  466. user_is_signed_in
  467. controller.stub(:tld){ "en" }
  468. @request.cookies['offer_secret'] = 'mysecret'
  469. @request.cookies['offer_code'] = 'ABC'
  470. offer.stub(:acceptable?)
  471. Offer.stub(:find_by_code).with('ABC'){ offer }
  472. current_user.stub(:offers){ offers }
  473. current_user.stub_chain(:offers, :non_deleted, :find_by_code){ nil }
  474. current_user.stub(:save){ true }
  475. end
  476.  
  477. it "finds offer by cookie" do
  478. Offer.should_receive(:find_by_code).with("ABC"){ offer }
  479. call_action
  480. end
  481.  
  482. it "assigns offer" do
  483. call_action
  484. assigns[:offer].should == offer
  485. end
  486.  
  487. context "no offer_code and secret in cookie" do
  488. before do
  489. offers.stub_chain(:non_deleted, :find_by_code){ offer }
  490. @request.cookies['offer_secret'] = nil
  491. @request.cookies['offer_code'] = nil
  492. end
  493.  
  494. it "renders offer nil error" do
  495. call_action
  496. response.code.should == "404"
  497. end
  498. end
  499.  
  500. context "offer_code does not match secret in cookie" do
  501. before do
  502. offers.stub_chain(:non_deleted, :find_by_code){ offer }
  503. @request.cookies['offer_secret'] = nil
  504. @request.cookies['offer_code'] = "ABC"
  505. end
  506.  
  507. it "renders offer nil error" do
  508. call_action
  509. response.code.should == "404"
  510. end
  511. end
  512. end
  513. end
  514.  
  515. context "when not signed in" do
  516.  
  517. def default_params
  518. [:put, :update, { :id => 'ABC', :offer => {:title => 'my offer'}, :user => {:email => "big@poppa.xx", :firstname => "big", :password => "xxx"} }]
  519. end
  520.  
  521. context "new user" do
  522. before do
  523. @request.cookies['offer_secret'] = 'mysecret'
  524. @request.cookies['offer_code'] = 'ABC'
  525. Offer.stub(:find_by_code).with('ABC'){ offer }
  526. User.stub(:find_by_email) {false}
  527. User.stub(:new) {user}
  528. user.stub(:locale=)
  529. user.stub(:tld=)
  530. user.stub(:offers){ [] }
  531. end
  532.  
  533. it "assigns correct locale and tld" do
  534. user.stub(:save)
  535. controller.stub(:current_locale) {"ru"}
  536. controller.stub(:tld) {"it"}
  537. user.should_receive(:locale=).with("ru")
  538. user.should_receive(:tld=).with("it")
  539. call_action
  540. end
  541.  
  542. context "save succeed" do
  543. before do
  544. user.stub(:save){ true }
  545. controller.stub(:track_signup_host!)
  546. controller.stub(:sign_in)
  547. end
  548.  
  549. it "should save user" do
  550. user.should_receive(:save) {true}
  551. call_action
  552. end
  553.  
  554. it "should signin user" do
  555. controller.should_receive(:sign_in).with(user)
  556. call_action
  557. end
  558.  
  559. it "sets correct flash message" do
  560. user.stub(:save) {true}
  561. controller.stub(:sign_in)
  562. call_action
  563. flash[:success].should == 'New account created.'
  564. end
  565. end
  566.  
  567. context "save fails" do
  568. before do
  569. user.should_receive(:save) {false}
  570. end
  571.  
  572. it "should render new" do
  573. call_action
  574. response.should render_template('edit')
  575. end
  576.  
  577. it "sets correct flash message" do
  578. errors = %w[Gone bad]
  579. user.stub(:errors) { errors }
  580. errors.stub(:full_messages) { errors }
  581. call_action
  582. flash[:error].should_not == 'Gone. bad.'
  583. end
  584. end
  585. end
  586. end
  587. end
  588.  
  589. describe "PUT update" do
  590. def default_params
  591. [:put, :update, { :id => 'ABC', :offer => { :title => 'my title' } }]
  592. end
  593.  
  594. before do
  595. offer.stub(:attributes=)
  596. offer.stub(:was_acceptable_before=)
  597. offer.stub(:email) { "email" }
  598. offer.stub(:contact_phone) { "12345" }
  599. offer.stub(:published?){ false }
  600. offer.stub(:acceptable?)
  601. offer.stub(:set_status_to_next_step!)
  602. offer.stub(:ensure_title_description!)
  603. offer.stub(:append_user)
  604. @request.cookies['offer_secret'] = 'mysecret'
  605. @request.cookies['offer_code'] = 'ABC'
  606. Offer.stub(:find_by_code).with('ABC'){ offer }
  607. end
  608.  
  609. it "sets attributes on the offer" do
  610. offer.should_receive(:attributes=).with('title' => 'my title')
  611. call_action
  612. end
  613.  
  614. describe "after filter handle_tracking" do
  615.  
  616. context "update succeeds" do
  617.  
  618. before do
  619. offer.stub(:status){ "published" }
  620. offer.stub(:was_acceptable_before=)
  621. offer.stub(:was_acceptable_before)
  622. offer.stub(:tracked_last_steps=)
  623. offer.stub(:tracked_last_steps)
  624. offer.stub(:tracked_initial_steps=)
  625. offer.stub(:tracked_initial_steps)
  626. end
  627.  
  628. describe "track last steps" do
  629. before do
  630. controller.stub(:track_offer_listed_if_needed)
  631. end
  632.  
  633. context "not acceptable" do
  634. before do
  635. offer.stub(:acceptable?){ false }
  636. end
  637.  
  638. it "does not track last steps" do
  639. controller.should_not_receive(:track_acceptable_offer!)
  640. call_action
  641. end
  642.  
  643. it "does not set tracked_last_steps to true" do
  644. offer.should_not_receive(:tracked_last_steps=).with(true)
  645. call_action
  646. end
  647. end
  648.  
  649. context "offer acceptable but was already tracked" do
  650. before do
  651. offer.stub(:acceptable?){ true }
  652. offer.stub(:tracked_last_steps){ true }
  653. end
  654.  
  655. it "does not track last steps" do
  656. controller.should_not_receive(:track_acceptable_offer!)
  657. call_action
  658. end
  659.  
  660. it "does not set tracked_last_steps to true" do
  661. offer.should_not_receive(:tracked_last_steps=).with(true)
  662. call_action
  663. end
  664. end
  665.  
  666. context "offer acceptable and not yet tracked" do
  667. before do
  668. offer.stub(:acceptable?){ true }
  669. offer.stub(:tracked_last_steps){ false }
  670. end
  671.  
  672. it "tracks last steps" do
  673. controller.should_receive(:track_acceptable_offer!)
  674. call_action
  675. end
  676.  
  677. it "sets tracked_last_steps to true" do
  678. offer.should_receive(:tracked_last_steps=).with(true)
  679. call_action
  680. end
  681.  
  682. it "saves offer" do
  683. offer.should_receive(:save).with(:validate => false)
  684. call_action
  685. end
  686. end
  687. end
  688.  
  689. describe "track listed offer" do
  690. before do
  691. controller.stub(:track_offer_acceptable_if_needed)
  692. end
  693. context "user signed in" do
  694. before do
  695. user_is_signed_in
  696. current_user.stub_chain(:offers, :non_deleted, :find_by_code){ offer }
  697. end
  698. context "in valid status" do
  699. before do
  700. offer.stub(:status){ "published" }
  701. end
  702.  
  703. context "not yet tracked initial offer" do
  704. before do
  705. offer.stub(:tracked_initial_steps){ false }
  706. end
  707.  
  708. it "tracks initial steps" do
  709. controller.should_receive(:track_listed_offer!)
  710. call_action
  711. end
  712.  
  713. it "sets tracked_initial_steps to true" do
  714. offer.should_receive(:tracked_initial_steps=).with(true)
  715. call_action
  716. end
  717.  
  718. it "saves offer" do
  719. offer.should_receive(:save).with(:validate => false)
  720. call_action
  721. end
  722. end
  723.  
  724. context "already tracked initial offer" do
  725. before do
  726. offer.stub(:tracked_initial_steps){ true }
  727. end
  728.  
  729. it "does not track initial steps" do
  730. controller.should_not_receive(:track_listed_offer!)
  731. call_action
  732. end
  733.  
  734. it "does not set tracked_initial_steps to true" do
  735. offer.should_not_receive(:tracked_initial_steps=).with(true)
  736. call_action
  737. end
  738. end
  739. end
  740. end
  741. end
  742. end
  743.  
  744. context "update fails" do
  745.  
  746. before do
  747. offer.stub(:errors){ ["an error"]}
  748. end
  749.  
  750. it "does not track last steps" do
  751. controller.should_not_receive(:track_acceptable_offer!)
  752. call_action
  753. end
  754.  
  755. it "does not set tracked_last_steps to true" do
  756. offer.should_not_receive(:tracked_last_steps=).with(true)
  757. call_action
  758. end
  759.  
  760. it "does not track initial steps" do
  761. controller.should_not_receive(:track_listed_offer!)
  762. call_action
  763. end
  764.  
  765. it "does not set tracked_initial_steps to true" do
  766. offer.should_not_receive(:tracked_initial_steps=).with(true)
  767. call_action
  768. end
  769. end
  770.  
  771. end
  772.  
  773. context "signed in" do
  774. before do
  775. user_is_signed_in
  776. current_user.stub_chain(:offers, :non_deleted, :find_by_code){ offer }
  777. end
  778.  
  779. it "appends user on offer" do
  780. offer.should_receive(:append_user).with(current_user)
  781. call_action
  782. end
  783. end
  784.  
  785. context "when amenity_list present it builds amenities" do
  786. def default_params
  787. [:put, :update, { :id => 'ABC', :offer => { :title => 'my title', :amenity_list => {'0' => 'tv', '1' => 'internet'} } }]
  788. end
  789.  
  790. it "calls tags_from_params_to_taglist_in_params" do
  791. controller.should_receive(:tags_from_params_to_taglist_in_params)
  792. call_action
  793. end
  794.  
  795. it "sets amenity_list correctly" do
  796. offer.should_receive(:attributes=).with('title' => 'my title', 'amenity_list' => 'tv,internet')
  797. call_action
  798. end
  799.  
  800. end
  801.  
  802. context "when update succeeds" do
  803. before do
  804. offer.stub(:set_status_to_next_step!){ true }
  805. offer.stub(:was_acceptable_before=)
  806. offer.stub(:was_acceptable_before)
  807. offer.stub(:tracked_last_steps=)
  808. offer.stub(:tracked_last_steps)
  809. end
  810.  
  811. context "when offer in state published" do
  812. before do
  813. offer.stub(:published?){ true }
  814. offer.stub(:acceptable?){ false }
  815. offer.stub(:save){ true }
  816. offer.stub(:current_step)
  817. offer.stub(:user_id).and_return 2
  818. end
  819.  
  820. it "redirects to offer page" do
  821. call_action
  822. response.should redirect_to('/offers/ABC/edit/overview')
  823. end
  824.  
  825. it "sets success message" do
  826. call_action
  827. flash[:success].should == 'Offer was updated successfully.'
  828. end
  829.  
  830. context "is acceptable now and was before" do
  831. it "sets success message" do
  832. offer.stub(:was_acceptable_before=)
  833. offer.stub(:was_acceptable_before){ true }
  834. offer.stub(:acceptable?){ true }
  835. call_action
  836. flash[:success].should include 'Offer was updated successfully.'
  837. end
  838. end
  839.  
  840. context "is acceptable now but wasnt beofore" do
  841. before do
  842. offer.stub(:was_acceptable_before=)
  843. offer.stub(:was_acceptable_before){ false }
  844. offer.stub(:acceptable?){ true }
  845. end
  846.  
  847. it "sets success message" do
  848. call_action
  849. flash[:success].should include 'Congratulations'
  850. end
  851. end
  852. end
  853.  
  854. context "when not yet in last step" do
  855. before do
  856. offer.stub(:published?){ false }
  857. offer.stub(:next_step){ "details" }
  858. end
  859.  
  860. it "proceeds to next step" do
  861. offer.should_receive(:set_status_to_next_step!)
  862. call_action
  863. end
  864.  
  865. context "when step now is final" do
  866. before do
  867. offer.stub(:published?){ true }
  868. end
  869.  
  870. context "when user signed in as normal user" do
  871. before do
  872. user_is_signed_in
  873. current_user.stub(:admin?){ false }
  874. current_user.stub_chain(:offers, :non_deleted, :find_by_code){ offer }
  875. offer.stub(:acceptable?){ false }
  876. end
  877.  
  878. it "redirects to offer" do
  879. call_action
  880. response.should redirect_to('/offers/ABC/edit/overview')
  881. end
  882.  
  883. it "sets correct flash message" do
  884. call_action
  885. flash[:success].should == "Offer was updated successfully."
  886. end
  887.  
  888. it "sets correct flash message when offer now acceptable but wasnt before" do
  889. offer.stub(:acceptable?){ true }
  890. offer.stub(:was_acceptable_before=)
  891. offer.stub(:was_acceptable_before){ false }
  892. call_action
  893. flash[:success].should include "Congratulations"
  894. end
  895.  
  896. it "sets correct flash message when offer not acceptable and was acceptable before" do
  897. offer.stub(:acceptable?){ true }
  898. offer.stub(:was_acceptable_before=)
  899. offer.stub(:was_acceptable_before){ true }
  900. call_action
  901. flash[:success].should == "Offer was updated successfully."
  902. end
  903. end
  904. end
  905.  
  906. context "if step not final" do
  907. before do
  908. controller.stub(:published_now?){ false }
  909. end
  910.  
  911. it "redirects_to edit page with offer status (changed in to_next_step!)" do
  912. call_action
  913. response.should redirect_to('/offers/ABC/edit/details')
  914. end
  915. end
  916. end
  917. end
  918.  
  919. context "when update fails" do
  920. before do
  921. offer.stub(:set_status_to_next_step!){ false }
  922. end
  923.  
  924. it "assigns step" do
  925. offer.stub(:current_step).and_return 'nextstatus'
  926. call_action
  927. assigns[:step].should == 'nextstatus'
  928. end
  929.  
  930. it "renders edit" do
  931. call_action
  932. response.should render_template('edit')
  933. end
  934.  
  935. it "assigns flash error when published" do
  936. offer.stub(:acceptable?) { true }
  937. offer.stub(:published?) { true }
  938. offer.stub(:save) { false }
  939. errors = %w[Gone bad]
  940. offer.stub(:errors) { errors }
  941. errors.stub(:full_messages) { errors }
  942. call_action
  943. flash[:error].should_not == "Gone. bad."
  944. end
  945.  
  946. it "assigns flash error" do
  947. offer.stub(:published?) { false }
  948. offer.stub(:save) { false }
  949. errors = %w[Gone bad]
  950. offer.stub(:errors) { errors }
  951. errors.stub(:full_messages) { errors }
  952. call_action
  953. flash[:error].should_not == "Gone. bad."
  954. end
  955. end
  956. end
  957.  
  958. describe "GET show" do
  959. let(:booking) {Factory.build(:booking) }
  960. let(:offer) {Factory.build(:offer, :id => 777, :code => "OFFERCODE123") }
  961.  
  962. def default_params
  963. [:get, :show, { :id => 'ABC' }]
  964. end
  965.  
  966. before do
  967. Offer.stub(:find_by_code).with('ABC').and_return offer
  968. offer.stub(:price) { 10 }
  969. Booking.any_instance.stub(:calculate_nightly_rate_without_extra_charges_or_fees_in_guest_currency) { 20 }
  970. end
  971.  
  972. context "when offer loadable" do
  973. it "loads public reviews for offer" do
  974. review = Factory(:host_review, :offer => offer)
  975. call_action
  976. assigns[:public_reviews].should == [review]
  977. end
  978.  
  979. context "booking" do
  980. before do
  981. call_action
  982. end
  983.  
  984. def default_params
  985. [:get, :show, { :id => 'ABC' }]
  986. end
  987.  
  988. it "is set" do
  989. assigns[:booking].should be_a(Booking)
  990. end
  991.  
  992. it ".checkout_date is nil by deafault" do
  993. assigns[:booking].checkout_date.should be_nil
  994. end
  995.  
  996. it "booking.guests is 1 by deafault" do
  997. assigns[:booking].guests.should == 1
  998. end
  999.  
  1000. context "with booking relevant attributes set in params" do
  1001. def default_params
  1002. [:get, :show, {
  1003. :id => 'ABC',
  1004. :guests => '2',
  1005. :checkin_date => Date.tomorrow.to_s(:db),
  1006. :checkout_date => (Date.tomorrow + 1).to_s(:db)
  1007. }]
  1008. end
  1009.  
  1010. it "sets guests correctly" do
  1011. call_action
  1012. assigns[:booking].guests.should == 2
  1013. end
  1014.  
  1015. it "sets checkin_date and checkout_date properly" do
  1016. call_action
  1017. assigns[:booking].checkin_date.should == Date.tomorrow
  1018. assigns[:booking].checkout_date.should == Date.tomorrow + 1
  1019. end
  1020. end
  1021.  
  1022. context "with last_search relevant params set in params" do
  1023. def default_params
  1024. [:get, :show, {
  1025. :id => 'ABC',
  1026. :guests => '3',
  1027. :checkin_date => Date.tomorrow.to_s(:db),
  1028. :checkout_date => (Date.tomorrow + 1).to_s(:db)
  1029. }]
  1030. end
  1031.  
  1032. it "sets guests correctly" do
  1033. call_action
  1034. assigns[:booking].guests.should == 3
  1035. end
  1036.  
  1037. it "sets checkin_date and checkout_date properly" do
  1038. call_action
  1039. assigns[:booking].checkin_date.should == Date.tomorrow
  1040. assigns[:booking].checkout_date.should == Date.tomorrow + 1
  1041. end
  1042. end
  1043.  
  1044. context "with empty last_search param" do
  1045. it "does not raise an error" do
  1046. lambda do
  1047. get :show, { :id => 'ABC', :last_search => "" }
  1048. end.should_not raise_error
  1049. end
  1050. end
  1051. end
  1052.  
  1053. it "loads offer" do
  1054. call_action
  1055. assigns[:offer].should == offer
  1056. end
  1057.  
  1058. it "renders show template" do
  1059. call_action
  1060. response.should render_template('show')
  1061. end
  1062.  
  1063. it "should render the offers page for a NONE published offer if parameter preview is given" do
  1064. offer.stub(:is_public){ false }
  1065. get :show, { :id => 'ABC', :preview => "true" }
  1066. assigns[:offer].should == offer
  1067.  
  1068. response.should be_success
  1069. response.should render_template(:show)
  1070. end
  1071.  
  1072. it "sets @calendar_options to current month and year" do
  1073. call_action
  1074. assigns(:calendar_options).should be_a(Hash)
  1075. assigns(:calendar_options)[:month].should == Date.today.month
  1076. assigns(:calendar_options)[:year].should == Date.today.year
  1077. end
  1078.  
  1079. it "sets mbb_required flag to true" do
  1080. offer.stub_chain(:user, :message_before_booking?) { true }
  1081. call_action
  1082. assigns(:mbb_required).should == true
  1083. end
  1084.  
  1085. it "sets mbb_required flag to false when message_before_booking? is nil" do
  1086. offer.stub_chain(:user, :message_before_booking?) { nil }
  1087. call_action
  1088. assigns(:mbb_required).should === false
  1089. end
  1090.  
  1091. it "sets mbb_form_url " do
  1092. offer.stub_chain(:user, :message_before_booking?) { true }
  1093. call_action
  1094. assigns(:mbb_form_url).should be_a(String)
  1095. end
  1096.  
  1097. end
  1098.  
  1099. context "when offer not loadable" do
  1100. let(:offer) { Factory.stub(:offer) }
  1101.  
  1102. before(:each) do
  1103. Offer.stub(:find_by_code) {}
  1104. end
  1105.  
  1106. it "redirects to search page" do
  1107. call_action
  1108. response.should redirect_to('/search')
  1109. end
  1110.  
  1111. it "calls Offer.reindex with the correct parameter" do
  1112. Offer.should_receive(:find_by_code).with("ABC").and_return(nil, offer) # first find should not find an offer, second one should, bit of a hack
  1113. OfferIndex.should_receive(:changed!).with(offer)
  1114. call_action
  1115. end
  1116. end
  1117. end
  1118.  
  1119. describe "GET description_and_houserules_translated" do
  1120. context "when offer is loaded" do
  1121. before :each do
  1122. Offer.stub(:find_by_code).with('ABC').and_return offer
  1123. offer.should_receive(:translated_title) { "translated description de" }
  1124. offer.should_receive(:translated_description) { "translated description de" }
  1125. offer.should_receive(:translated_rules) { "translated houserules de"}
  1126.  
  1127. xhr :get, :description_and_houserules_translated, :id => 'ABC', :locale => "de"
  1128. end
  1129.  
  1130. it "sets locale variable" do
  1131. assigns(:locale).should == :de
  1132. end
  1133.  
  1134. it "responds with hash including translations in JSON format " do
  1135. response.should be_successful
  1136. parsed_response = JSON.parse(response.body)
  1137. parsed_response['translated_description'].should == "translated description de"
  1138. parsed_response['translated_rules'].should == "translated houserules de"
  1139. end
  1140.  
  1141. context "locale not provided" do
  1142. it "renders error response" do
  1143. xhr :get, :description_and_houserules_translated, :id => 'ABC'
  1144. parsed_response = JSON.parse(response.body)
  1145. parsed_response['error_message'].should_not be_blank
  1146.  
  1147. end
  1148. end
  1149. end
  1150.  
  1151. context "when offer not loaded" do
  1152. it "renders error reponse" do
  1153. Offer.stub(:find_by_code).with('ABC') { nil }
  1154. xhr :get, :description_and_houserules_translated, :id => 'ABC', :locale => "de"
  1155. assigns(:locale).should be_nil
  1156. response.should be_not_found
  1157. end
  1158. end
  1159. end
  1160.  
  1161.  
  1162. describe "GET calendar" do
  1163. let(:offer) { Factory.build(:offer, :id => 777, :code => "OFFERCODE123") }
  1164. before :each do
  1165. Offer.stub(:find_by_code).with('ABC').and_return offer
  1166. end
  1167.  
  1168. it "renders calendar partial" do
  1169. xhr(:get, :calendar, { :id => "ABC"} )
  1170. response.should be_success
  1171. response.should render_template("offers/_calendar_v4")
  1172. end
  1173.  
  1174. it "sets year and month properly" do
  1175. xhr(:get, :calendar, { :id => "ABC", :month => 5, :year => 2014 })
  1176. assigns(:year).should == 2014
  1177. assigns(:month).should == 5
  1178. end
  1179. it "sets default year and month properly" do
  1180. xhr(:get, :calendar, { :id => "ABC"} )
  1181. assigns(:year).should == Date.today.year
  1182. assigns(:month).should == Date.today.month
  1183. end
  1184. end
  1185.  
  1186. describe "GET similar_offers" do
  1187. let(:offer) { Factory.build(:offer, :id => 777) }
  1188. let(:offer2) { Factory.build(:offer, :id => 778) }
  1189.  
  1190. before :each do
  1191. Offer.stub(:find_by_code).with('ABC').and_return offer
  1192. offer.stub(:find_similar_with_volatile_prices_ranges) { [offer2]}
  1193. end
  1194. it "assigns proper offer" do
  1195. xhr(:get, :similar_offers, { :id => "ABC"} )
  1196. assigns(:offer).should == offer
  1197. end
  1198.  
  1199. it "assigns booking" do
  1200. xhr(:get, :similar_offers, { :id => "ABC"} )
  1201. assigns(:booking).should be_a(Booking)
  1202. end
  1203.  
  1204. it "renders similar_offers partial" do
  1205. xhr(:get, :similar_offers, { :id => "ABC"} )
  1206. response.should be_success
  1207. response.should render_template("offers/_similar_offers")
  1208. end
  1209. it "it does not crash on empty array" do
  1210. offer.should_receive(:find_similar_with_volatile_prices_ranges) { [] }
  1211. xhr(:get, :similar_offers, { :id => "ABC"} )
  1212. response.should be_success
  1213. response.should render_template("offers/_similar_offers")
  1214. end
  1215.  
  1216. end
  1217.  
  1218. describe "DELETE destroy" do
  1219. def default_params
  1220. [:delete, :destroy, { :id => 'ABC' }]
  1221. end
  1222.  
  1223. before do
  1224. user_is_signed_in
  1225. controller.stub(:user_signed_in_as_offer_admin?)
  1226. offer.stub(:delete)
  1227. current_user.stub_chain(:offers, :non_deleted, :find_by_code){ offer }
  1228. end
  1229.  
  1230. it "calls delete on offer" do
  1231. offer.should_receive(:delete)
  1232. call_action
  1233. end
  1234.  
  1235. context "when delete fails" do
  1236. before do
  1237. offer.stub(:delete).and_return false
  1238. end
  1239.  
  1240. it "sets flash notice" do
  1241. call_action
  1242. flash[:error].should == 'Your offer could not be deleted.'
  1243. end
  1244. end
  1245.  
  1246. context "when delete succeeds" do
  1247. before do
  1248. offer.stub(:delete).and_return true
  1249. end
  1250.  
  1251. it "sets flash success" do
  1252. call_action
  1253. flash[:success].should == 'You have deleted your offer.'
  1254. end
  1255. end
  1256.  
  1257. it "redirects to offers index" do
  1258. call_action
  1259. response.should redirect_to('/user/offers')
  1260. end
  1261. end
  1262.  
  1263. describe "favorites" do
  1264. before do
  1265. Offer.stub!(:find_by_code).with('ABC') { offer }
  1266. Offer.stub!(:find_by_code).with('not_found') { nil }
  1267. end
  1268.  
  1269. describe "GET add_to_favorites with Offer not found" do
  1270. def default_params
  1271. [:get, :add_to_favorites, { :id => 'not_found' }]
  1272. end
  1273.  
  1274. it "renders 404" do
  1275. request.env['HTTP_REFERER'] = nil
  1276. offer.stub(:code){ "not_found" }
  1277. call_action
  1278. response.status.should == 404
  1279. end
  1280. end
  1281.  
  1282. describe "GET add_to_favorites without referrer" do
  1283. def default_params
  1284. [:get, :add_to_favorites, { :id => 'ABC' }]
  1285. end
  1286.  
  1287. it "redirects to the offer" do
  1288. request.env['HTTP_REFERER'] = nil
  1289. offer.stub(:code){ "ABC"}
  1290. call_action
  1291. request.should redirect_to("/offers/ABC")
  1292. end
  1293. end
  1294.  
  1295. describe "GET add_to_favorites" do
  1296. def default_params
  1297. [:get, :add_to_favorites, { :id => 'ABC' }]
  1298. end
  1299.  
  1300. before :each do
  1301. request.env['HTTP_REFERER'] = "/offers/show/ABC"
  1302. offer.stub(:code){ "ABC"}
  1303. end
  1304.  
  1305. it "sets last added fav offer in session" do
  1306. call_action
  1307. controller.session.should_not == {"offer_id"=>"ABC", "tracking"=>[:track_add_favorite_offer]}
  1308. end
  1309.  
  1310. it "calls track_add_favorite_offer!" do
  1311. controller.should_not_receive(:track_add_favorite_offer!)
  1312. call_action
  1313. end
  1314.  
  1315. context "user signed in" do
  1316. before :each do
  1317. user_is_signed_in
  1318. end
  1319.  
  1320. it "calls add_offer on a new FavoriteOffer using user" do
  1321. favorite_offer = double("favorite_offer").as_null_object
  1322. FavoriteOffer.should_receive(:new).with(current_user) { favorite_offer }
  1323. call_action
  1324. request.should redirect_to("/offers/show/ABC")
  1325. end
  1326. end
  1327.  
  1328. context "user not signed in" do
  1329. before :each do
  1330. controller.stub(:session) { {:session_id => "13"} }
  1331. end
  1332.  
  1333. it "calls add_offer on a new FavoriteOffer using user" do
  1334. favorite_offer = double("favorite_offer").as_null_object
  1335. FavoriteOffer.should_receive(:new).with("13") { favorite_offer }
  1336. call_action
  1337. request.should redirect_to("/offers/show/ABC")
  1338. end
  1339. end
  1340. end
  1341.  
  1342. describe "GET add_to_favorites as js" do
  1343. def default_params
  1344. [:get, :add_to_favorites, { :id => 'ABC', :format => :js }]
  1345. end
  1346.  
  1347. it "returns a jsonp response" do
  1348. call_action
  1349. response.should render_template('add_to_favorites')
  1350. end
  1351. end
  1352.  
  1353. describe "GET remove_from_favorites with Offer not found" do
  1354. def default_params
  1355. [:get, :remove_from_favorites, { :id => 'not_found' }]
  1356. end
  1357.  
  1358. it "renders 404" do
  1359. request.env['HTTP_REFERER'] = nil
  1360. offer.stub(:code){ "not_found" }
  1361. call_action
  1362. response.status.should == 404
  1363. end
  1364. end
  1365.  
  1366.  
  1367. describe "GET remove_from_favorites without referrer" do
  1368. def default_params
  1369. [:get, :remove_from_favorites, { :id => 'ABC' }]
  1370. end
  1371.  
  1372. it "redirects to the offer" do
  1373. request.env['HTTP_REFERER'] = nil
  1374. offer.stub(:code){ "ABC"}
  1375. call_action
  1376. request.should redirect_to("/offers/ABC")
  1377. end
  1378. end
  1379.  
  1380. describe "GET remove_from_favorites" do
  1381. def default_params
  1382. [:get, :remove_from_favorites, { :id => 'ABC' }]
  1383. end
  1384.  
  1385. before :each do
  1386. request.env['HTTP_REFERER'] = "/offers/show/ABC"
  1387. Offer.stub!(:find_by_code).with('ABC') { offer }
  1388. offer.stub(:code){ "ABC"}
  1389. end
  1390.  
  1391. it "sets last removed fav offer in session" do
  1392. call_action
  1393. controller.session.should_not == {"offer_id"=>"ABC", "tracking"=>[:track_remove_favorite_offer]}
  1394. end
  1395.  
  1396. it "calls track_remove_favorite_offer!" do
  1397. controller.should_not_receive(:track_remove_favorite_offer!)
  1398. call_action
  1399. end
  1400.  
  1401. context "user signed in" do
  1402. before :each do
  1403. user_is_signed_in
  1404. end
  1405.  
  1406. it "calls add_offer on a new FavoriteOffer using user" do
  1407. favorite_offer = double("favorite_offer").as_null_object
  1408. FavoriteOffer.should_receive(:new).with(current_user) { favorite_offer }
  1409. call_action
  1410. request.should redirect_to("/offers/show/ABC")
  1411. end
  1412. end
  1413.  
  1414. context "user not signed in" do
  1415. before do
  1416. controller.stub(:session) { {:session_id => "13"} }
  1417. end
  1418.  
  1419. it "calls add_offer on a new FavoriteOffer using user" do
  1420. favorite_offer = double("favorite_offer").as_null_object
  1421. FavoriteOffer.should_receive(:new).with("13") { favorite_offer }
  1422. call_action
  1423. request.should redirect_to("/offers/show/ABC")
  1424. end
  1425. end
  1426. end
  1427.  
  1428. describe "GET remove_from_favorites as js" do
  1429. def default_params
  1430. [:get, :remove_from_favorites, { :id => 'ABC', :format => :js }]
  1431. end
  1432.  
  1433. it "returns a jsonp response" do
  1434. call_action
  1435. response.should render_template('remove_from_favorites')
  1436. end
  1437. end
  1438.  
  1439. describe "GET favorites" do
  1440. def default_params
  1441. [:get, :favorites, { :id => nil }]
  1442. end
  1443.  
  1444. before :each do
  1445. Offer.stub_chain(:where, :order) { [offer, offer] }
  1446. offer.stub(:to_json) { "offer_as_json" }
  1447. controller.stub(:session) { {:session_id => "13"} }
  1448. end
  1449.  
  1450. it "renders partial _favorites" do
  1451. call_action
  1452. response.should render_template("search/_favorites")
  1453. end
  1454. end
  1455. end
  1456.  
  1457. describe "PUT update_visibility" do
  1458. before do
  1459. Offer.stub!(:find_by_code).with('ABC') { offer }
  1460. end
  1461.  
  1462. def default_params
  1463. [:put, :update_visibility, { :id => 'ABC' }]
  1464. end
  1465.  
  1466. it "does not call toggle_visibility on offer no user given" do
  1467. offer.should_not_receive(:toggle_visibility!)
  1468. call_action
  1469. response.code.should == "404"
  1470. end
  1471.  
  1472. it "it calls load_own_offer" do
  1473. controller.should_receive(:load_own_offer)
  1474. call_action
  1475. end
  1476.  
  1477. it "calls toggle_visibilit!" do
  1478. user_is_signed_in
  1479. controller.current_user.stub_chain(:offers, :non_deleted, :find_by_code) { offer }
  1480. offer.should_receive(:toggle_visibility!)
  1481. call_action
  1482. response.should redirect_to offers_path(:host => request.host)
  1483. end
  1484. end
  1485. end
Add Comment
Please, Sign In to add comment