Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- encoding : utf-8 -*-
- require 'spec_helper'
- describe OffersController do
- include DeviseSpecHelper
- let(:offers){ double('offers') }
- let(:offer){
- mock_model(Offer,
- :id => 1,
- :to_param => 'ABC',
- :code => 'mycode',
- :secret => 'mysecret',
- :status => 'nextstatus',
- :current_step => nil,
- :stay_length => 1,
- :save => true,
- :reviews => Review,
- :user => User.new,
- :reviews_count= => true,
- :remove_deleted_translations! => true,
- :touch => true,
- :public_lat => 10,
- :public_lng => 10,
- :city => 'Hamburg'
- )
- }
- let(:user_offer){
- mock_model(Offer,
- :to_param => 'ABC',
- :status => 'nextstatus',
- :remove_deleted_translations! => true
- )
- }
- before do
- current_user.stub(:super_admin?)
- current_user.stub(:country_admin?)
- offer.stub(:status=).with('address')
- user_offer.stub(:status=).with('address')
- offer.stub(:user=)
- user_offer.stub(:user=)
- end
- describe "GET index" do
- def default_params
- [:get, :index]
- end
- before do
- user_is_signed_in
- current_user.stub_chain(:offers, :non_deleted){ offers }
- end
- it "authenticates user" do
- should_authenticate_user(default_params)
- end
- it "calls offers on current_user" do
- controller.current_user.should_receive(:offers)
- call_action
- end
- it "assigns @offers" do
- call_action
- assigns[:offers].should == offers
- end
- it "renders index" do
- call_action
- response.should render_template('index')
- end
- end
- describe "GET new" do
- def default_params
- [:get, :new]
- end
- before do
- Offer.stub(:new){ offer }
- end
- it "deletes offer_code from cookie" do
- @request.cookies['offer_code'] = 'mycode'
- call_action
- response.cookies['offer_code'].should be_nil
- end
- it "initializes offer with status address" do
- call_action
- assigns[:offer].should == offer
- end
- describe "assigns email on offer if user present" do
- it "calls assign method" do
- controller.should_receive(:set_email_if_not_set)
- call_action
- end
- describe "if signed in it sets email" do
- before do
- user_is_signed_in
- current_user.stub(:email).and_return('peter@griffin.xx')
- end
- context "logged in as normal user" do
- before do
- current_user.stub(:admin?){ false }
- current_user.stub(:super_admin?){ false }
- offer.stub(:email)
- end
- it "and email is not set" do
- offer.should_receive(:email=).with('peter@griffin.xx')
- call_action
- end
- it "not if email has been set prior" do
- offer.stub(:email){ 'mymail' }
- offer.should_not_receive(:email=).with('peter@griffin.xx')
- call_action
- end
- end
- end
- end
- it "renders new" do
- call_action
- response.should render_template('new')
- end
- end
- describe "POST create" do
- def default_params
- [:post, :create, { :offer => {:title => 'my offer'} }]
- end
- before do
- Offer.stub(:new).with({'title' => 'my offer'}){ offer }
- offer.stub(:user=)
- offer.stub(:affiliate_code=)
- offer.stub(:set_status_to_next_step!)
- offer.stub(:locale=)
- controller.stub(:user_signed_in_as_offer_admin?)
- end
- it "sets affiliate_code" do
- @request.cookies['affiliate_code'] = 'mycode'
- offer.should_receive(:affiliate_code=).with('mycode')
- call_action
- end
- it "sets locale" do
- controller.stub(:current_locale) { 'de' }
- offer.should_receive(:locale=).with('de')
- call_action
- end
- context "when signed in" do
- before do
- user_is_signed_in
- end
- it "builds offer with given params" do
- offer.should_receive(:user=).with(current_user)
- call_action
- assigns[:offer].should == offer
- end
- end
- context "when not signed in" do
- it "creates new offer with given params" do
- Offer.should_receive(:new).with({'title' => 'my offer'})
- call_action
- assigns[:offer].should == offer
- end
- context "when save succeeds" do
- before do
- offer.stub(:set_status_to_next_step!){ true }
- end
- it "stores offer_code in cookie" do
- call_action
- response.cookies.should == {"offer_code"=>"mycode", "offer_secret" => "mysecret", "locale" => "en", "currency" => "EUR"}
- end
- end
- end
- context "when offer valid" do
- before do
- offer.stub(:set_status_to_next_step!){ true }
- offer.stub(:valid?) { true }
- # FIXME this is highly dependent on the implementation
- offer.stub(:status){ 'accommodation' }
- end
- it "call set_status_to_next_step! on offer" do
- offer.should_receive(:set_status_to_next_step!)
- call_action
- end
- it "redirects to edit step offer path" do
- call_action
- response.should redirect_to('/offers/ABC/edit/accommodation?use_application_layout=true')
- end
- end
- context "when not valid" do
- before do
- offer.stub(:set_status_to_next_step!){ false }
- offer.stub(:valid?) { false }
- end
- it "renders :new" do
- call_action
- response.should render_template('new')
- end
- it "does not set correct flash message anymore" do
- errors = %w[Gone bad]
- offer.stub(:errors) { errors }
- errors.stub(:full_messages) { errors }
- call_action
- flash[:error].should_not == 'Gone. bad.'
- end
- end
- end
- describe "GET edit" do
- let(:user_offer){ mock_model(Offer, :status => 'published', :to_param => 'ABC', :remove_deleted_translations! => true) }
- before do
- controller.stub(:set_email_if_not_set)
- end
- context "when no step given" do
- def default_params
- [:get, :edit, { :id => 'ABC' }]
- end
- context "when not signed in but offer code set in cookie" do
- before do
- @request.cookies['offer_secret'] = 'mysecret'
- @request.cookies['offer_code'] = 'ABC'
- Offer.stub(:find_by_code).with('ABC'){ offer }
- offer.stub(:valid_for_step?)
- offer.stub(:step_valid?)
- offer.stub(:previous_step){ 'address' }
- end
- it "loads offer from cookie" do
- Offer.should_receive(:find_by_code).with('ABC')
- call_action
- assigns[:offer].should == offer
- end
- it "raises exception if secret does not match" do
- offer.stub(:secret){ nil }
- lambda{ call_action }.should raise_exception("wrong secret for offer 1")
- end
- end
- context "when signed" do
- before do
- user_is_signed_in
- current_user.stub_chain(:offers, :non_deleted, :find_by_code){ user_offer }
- user_offer.stub(:valid_for_step?)
- current_user.stub(:admin?){ false }
- user_offer.stub(:step_valid?)
- user_offer.stub(:previous_step){ 'prev' }
- end
- context "as super_admin" do
- let(:offer){ mock_model(Offer, :to_param => 'ABC', :status => 'published', :remove_deleted_translations! => true) }
- before do
- current_user.stub_chain(:offers, :non_deleted, :find_by_code){ nil }
- current_user.stub(:super_admin?){ true }
- offer.stub(:valid_for_step?){ false }
- offer.stub(:step_valid?){ false }
- end
- it "loads offer by code and assigns to @offer" do
- Offer.should_receive(:find_by_code).with('ABC').and_return(offer)
- call_action
- assigns[:offer].should == offer
- end
- end
- context "as country_admin" do
- let(:offer){ mock_model(Offer, :to_param => 'ABC', :status => 'published', :remove_deleted_translations! => true) }
- before do
- current_user.stub_chain(:offers, :non_deleted, :find_by_code){ nil }
- current_user.stub(:country_admin?){ true }
- current_user.stub(:admin_countries){ ["de","en"] }
- offer.stub(:valid_for_step?){ false }
- offer.stub(:step_valid?){ false }
- end
- it "loads offer by code and assigns to @offer" do
- offers = double("Offers")
- Offer.should_receive(:where).with(:country_code_iso => ["de","en"]).and_return(offers)
- offers.should_receive(:find_by_code).with('ABC').and_return(offer)
- call_action
- assigns[:offer].should == offer
- end
- end
- context "as normal user" do
- before do
- current_user.stub(:country_admin?){ false }
- current_user.stub(:super_admin?){ false }
- end
- it "loads offer from users offers" do
- current_user.offers.non_deleted.should_receive(:find_by_code).with('ABC')
- call_action
- assigns[:offer].should == user_offer
- end
- end
- context "when offer loaded" do
- before do
- user_offer.stub(:step_valid?) { true }
- user_offer.stub(:valid_for_step?)
- user_offer.stub(:previous_step){ 'step' }
- end
- it "checks validity for offer.status" do
- user_offer.should_receive(:valid_for_step?).with('published'){ true }
- call_action
- end
- context "when valid for step" do
- before do
- user_offer.stub(:valid_for_step?).with('published'){ true }
- user_offer.stub(:previous_status).with('published'){ 'prevstatus' }
- user_offer.stub(:remove_deleted_translations!)
- user_offer.stub(:ensure_title_description!)
- end
- it "assigns step" do
- call_action
- assigns[:step].should == 'published'
- end
- it "skips account step when signed in and current step is details" do
- user_offer.stub(:status) { "details" }
- user_offer.stub(:valid_for_step?).with('details'){ true }
- call_action
- assigns[:step].should == 'details'
- end
- it "renders edit" do
- call_action
- response.should render_template('edit')
- end
- end
- context "when not valid for step" do
- before do
- user_offer.stub(:set_status_to_previous_step!)
- user_offer.stub(:valid_for_step?){ false }
- #user_offer.stub(:previous_step){ 'prevstatus' }
- end
- it "calls set_status_to_previous_step" do
- user_offer.should_receive(:set_status_to_previous_step!)
- call_action
- end
- it "redirects to previous step edit" do
- call_action
- response.should redirect_to('/offers/ABC/edit/published')
- end
- end
- context "when step not valid" do
- before do
- user_offer.stub(:step_valid?){ false }
- end
- it "redirects to previous step edit" do
- call_action
- response.should_not be_success
- end
- end
- end
- end
- end
- context "when step given" do
- def default_params
- [:get, :edit, { :id => 'ABC', :step => 'address' }]
- end
- before do
- user_is_signed_in
- current_user.stub_chain(:offers, :non_deleted, :find_by_code){ user_offer }
- user_offer.stub(:valid_for_step?){ true }
- user_offer.stub(:step_valid?){ true }
- user_offer.stub(:previous_status){ 'prevstatus' }
- user_offer.stub(:ensure_title_description!)
- end
- it "assigns step if step_valid" do
- call_action
- assigns[:step].should == 'address'
- end
- context "when step not valid" do
- before do
- user_offer.stub(:step_valid?){ false }
- end
- it "redirects to previous step edit" do
- call_action
- response.should_not be_success
- end
- end
- context "when step overview" do
- def default_params
- [:get, :edit, { :id => 'ABC', :step => "overview" }]
- end
- it "assigns step" do
- call_action
- assigns[:step].should == 'overview'
- end
- it "renders edit" do
- call_action
- response.should render_template('edit')
- end
- end
- end
- end
- describe "before_filter signup_and_in" do
- let(:user){ mock_model("User") }
- before do
- offer.stub(:attributes=)
- offer.stub(:was_acceptable_before=)
- offer.stub(:acceptable?)
- offer.stub(:published?){ false }
- offer.stub(:set_status_to_next_step!)
- offer.stub(:ensure_title_description!)
- offer.stub(:append_user)
- end
- context "signed in and access_type login" do
- context "signed in" do
- let(:offers) { [] }
- def default_params
- [:post, :update, { :id => 'ABC', :access_type => "login", :offer => {:title => 'my offer'}, :user => {:email => "big@poppa.xx", :firstname => "big", :password => "xxx"} }]
- end
- before do
- user_is_signed_in
- controller.stub(:tld){ "en" }
- @request.cookies['offer_secret'] = 'mysecret'
- @request.cookies['offer_code'] = 'ABC'
- offer.stub(:acceptable?)
- Offer.stub(:find_by_code).with('ABC'){ offer }
- current_user.stub(:offers){ offers }
- current_user.stub_chain(:offers, :non_deleted, :find_by_code){ nil }
- current_user.stub(:save){ true }
- end
- it "finds offer by cookie" do
- Offer.should_receive(:find_by_code).with("ABC"){ offer }
- call_action
- end
- it "assigns offer" do
- call_action
- assigns[:offer].should == offer
- end
- context "no offer_code and secret in cookie" do
- before do
- offers.stub_chain(:non_deleted, :find_by_code){ offer }
- @request.cookies['offer_secret'] = nil
- @request.cookies['offer_code'] = nil
- end
- it "renders offer nil error" do
- call_action
- response.code.should == "404"
- end
- end
- context "offer_code does not match secret in cookie" do
- before do
- offers.stub_chain(:non_deleted, :find_by_code){ offer }
- @request.cookies['offer_secret'] = nil
- @request.cookies['offer_code'] = "ABC"
- end
- it "renders offer nil error" do
- call_action
- response.code.should == "404"
- end
- end
- end
- end
- context "when not signed in" do
- def default_params
- [:put, :update, { :id => 'ABC', :offer => {:title => 'my offer'}, :user => {:email => "big@poppa.xx", :firstname => "big", :password => "xxx"} }]
- end
- context "new user" do
- before do
- @request.cookies['offer_secret'] = 'mysecret'
- @request.cookies['offer_code'] = 'ABC'
- Offer.stub(:find_by_code).with('ABC'){ offer }
- User.stub(:find_by_email) {false}
- User.stub(:new) {user}
- user.stub(:locale=)
- user.stub(:tld=)
- user.stub(:offers){ [] }
- end
- it "assigns correct locale and tld" do
- user.stub(:save)
- controller.stub(:current_locale) {"ru"}
- controller.stub(:tld) {"it"}
- user.should_receive(:locale=).with("ru")
- user.should_receive(:tld=).with("it")
- call_action
- end
- context "save succeed" do
- before do
- user.stub(:save){ true }
- controller.stub(:track_signup_host!)
- controller.stub(:sign_in)
- end
- it "should save user" do
- user.should_receive(:save) {true}
- call_action
- end
- it "should signin user" do
- controller.should_receive(:sign_in).with(user)
- call_action
- end
- it "sets correct flash message" do
- user.stub(:save) {true}
- controller.stub(:sign_in)
- call_action
- flash[:success].should == 'New account created.'
- end
- end
- context "save fails" do
- before do
- user.should_receive(:save) {false}
- end
- it "should render new" do
- call_action
- response.should render_template('edit')
- end
- it "sets correct flash message" do
- errors = %w[Gone bad]
- user.stub(:errors) { errors }
- errors.stub(:full_messages) { errors }
- call_action
- flash[:error].should_not == 'Gone. bad.'
- end
- end
- end
- end
- end
- describe "PUT update" do
- def default_params
- [:put, :update, { :id => 'ABC', :offer => { :title => 'my title' } }]
- end
- before do
- offer.stub(:attributes=)
- offer.stub(:was_acceptable_before=)
- offer.stub(:email) { "email" }
- offer.stub(:contact_phone) { "12345" }
- offer.stub(:published?){ false }
- offer.stub(:acceptable?)
- offer.stub(:set_status_to_next_step!)
- offer.stub(:ensure_title_description!)
- offer.stub(:append_user)
- @request.cookies['offer_secret'] = 'mysecret'
- @request.cookies['offer_code'] = 'ABC'
- Offer.stub(:find_by_code).with('ABC'){ offer }
- end
- it "sets attributes on the offer" do
- offer.should_receive(:attributes=).with('title' => 'my title')
- call_action
- end
- describe "after filter handle_tracking" do
- context "update succeeds" do
- before do
- offer.stub(:status){ "published" }
- offer.stub(:was_acceptable_before=)
- offer.stub(:was_acceptable_before)
- offer.stub(:tracked_last_steps=)
- offer.stub(:tracked_last_steps)
- offer.stub(:tracked_initial_steps=)
- offer.stub(:tracked_initial_steps)
- end
- describe "track last steps" do
- before do
- controller.stub(:track_offer_listed_if_needed)
- end
- context "not acceptable" do
- before do
- offer.stub(:acceptable?){ false }
- end
- it "does not track last steps" do
- controller.should_not_receive(:track_acceptable_offer!)
- call_action
- end
- it "does not set tracked_last_steps to true" do
- offer.should_not_receive(:tracked_last_steps=).with(true)
- call_action
- end
- end
- context "offer acceptable but was already tracked" do
- before do
- offer.stub(:acceptable?){ true }
- offer.stub(:tracked_last_steps){ true }
- end
- it "does not track last steps" do
- controller.should_not_receive(:track_acceptable_offer!)
- call_action
- end
- it "does not set tracked_last_steps to true" do
- offer.should_not_receive(:tracked_last_steps=).with(true)
- call_action
- end
- end
- context "offer acceptable and not yet tracked" do
- before do
- offer.stub(:acceptable?){ true }
- offer.stub(:tracked_last_steps){ false }
- end
- it "tracks last steps" do
- controller.should_receive(:track_acceptable_offer!)
- call_action
- end
- it "sets tracked_last_steps to true" do
- offer.should_receive(:tracked_last_steps=).with(true)
- call_action
- end
- it "saves offer" do
- offer.should_receive(:save).with(:validate => false)
- call_action
- end
- end
- end
- describe "track listed offer" do
- before do
- controller.stub(:track_offer_acceptable_if_needed)
- end
- context "user signed in" do
- before do
- user_is_signed_in
- current_user.stub_chain(:offers, :non_deleted, :find_by_code){ offer }
- end
- context "in valid status" do
- before do
- offer.stub(:status){ "published" }
- end
- context "not yet tracked initial offer" do
- before do
- offer.stub(:tracked_initial_steps){ false }
- end
- it "tracks initial steps" do
- controller.should_receive(:track_listed_offer!)
- call_action
- end
- it "sets tracked_initial_steps to true" do
- offer.should_receive(:tracked_initial_steps=).with(true)
- call_action
- end
- it "saves offer" do
- offer.should_receive(:save).with(:validate => false)
- call_action
- end
- end
- context "already tracked initial offer" do
- before do
- offer.stub(:tracked_initial_steps){ true }
- end
- it "does not track initial steps" do
- controller.should_not_receive(:track_listed_offer!)
- call_action
- end
- it "does not set tracked_initial_steps to true" do
- offer.should_not_receive(:tracked_initial_steps=).with(true)
- call_action
- end
- end
- end
- end
- end
- end
- context "update fails" do
- before do
- offer.stub(:errors){ ["an error"]}
- end
- it "does not track last steps" do
- controller.should_not_receive(:track_acceptable_offer!)
- call_action
- end
- it "does not set tracked_last_steps to true" do
- offer.should_not_receive(:tracked_last_steps=).with(true)
- call_action
- end
- it "does not track initial steps" do
- controller.should_not_receive(:track_listed_offer!)
- call_action
- end
- it "does not set tracked_initial_steps to true" do
- offer.should_not_receive(:tracked_initial_steps=).with(true)
- call_action
- end
- end
- end
- context "signed in" do
- before do
- user_is_signed_in
- current_user.stub_chain(:offers, :non_deleted, :find_by_code){ offer }
- end
- it "appends user on offer" do
- offer.should_receive(:append_user).with(current_user)
- call_action
- end
- end
- context "when amenity_list present it builds amenities" do
- def default_params
- [:put, :update, { :id => 'ABC', :offer => { :title => 'my title', :amenity_list => {'0' => 'tv', '1' => 'internet'} } }]
- end
- it "calls tags_from_params_to_taglist_in_params" do
- controller.should_receive(:tags_from_params_to_taglist_in_params)
- call_action
- end
- it "sets amenity_list correctly" do
- offer.should_receive(:attributes=).with('title' => 'my title', 'amenity_list' => 'tv,internet')
- call_action
- end
- end
- context "when update succeeds" do
- before do
- offer.stub(:set_status_to_next_step!){ true }
- offer.stub(:was_acceptable_before=)
- offer.stub(:was_acceptable_before)
- offer.stub(:tracked_last_steps=)
- offer.stub(:tracked_last_steps)
- end
- context "when offer in state published" do
- before do
- offer.stub(:published?){ true }
- offer.stub(:acceptable?){ false }
- offer.stub(:save){ true }
- offer.stub(:current_step)
- offer.stub(:user_id).and_return 2
- end
- it "redirects to offer page" do
- call_action
- response.should redirect_to('/offers/ABC/edit/overview')
- end
- it "sets success message" do
- call_action
- flash[:success].should == 'Offer was updated successfully.'
- end
- context "is acceptable now and was before" do
- it "sets success message" do
- offer.stub(:was_acceptable_before=)
- offer.stub(:was_acceptable_before){ true }
- offer.stub(:acceptable?){ true }
- call_action
- flash[:success].should include 'Offer was updated successfully.'
- end
- end
- context "is acceptable now but wasnt beofore" do
- before do
- offer.stub(:was_acceptable_before=)
- offer.stub(:was_acceptable_before){ false }
- offer.stub(:acceptable?){ true }
- end
- it "sets success message" do
- call_action
- flash[:success].should include 'Congratulations'
- end
- end
- end
- context "when not yet in last step" do
- before do
- offer.stub(:published?){ false }
- offer.stub(:next_step){ "details" }
- end
- it "proceeds to next step" do
- offer.should_receive(:set_status_to_next_step!)
- call_action
- end
- context "when step now is final" do
- before do
- offer.stub(:published?){ true }
- end
- context "when user signed in as normal user" do
- before do
- user_is_signed_in
- current_user.stub(:admin?){ false }
- current_user.stub_chain(:offers, :non_deleted, :find_by_code){ offer }
- offer.stub(:acceptable?){ false }
- end
- it "redirects to offer" do
- call_action
- response.should redirect_to('/offers/ABC/edit/overview')
- end
- it "sets correct flash message" do
- call_action
- flash[:success].should == "Offer was updated successfully."
- end
- it "sets correct flash message when offer now acceptable but wasnt before" do
- offer.stub(:acceptable?){ true }
- offer.stub(:was_acceptable_before=)
- offer.stub(:was_acceptable_before){ false }
- call_action
- flash[:success].should include "Congratulations"
- end
- it "sets correct flash message when offer not acceptable and was acceptable before" do
- offer.stub(:acceptable?){ true }
- offer.stub(:was_acceptable_before=)
- offer.stub(:was_acceptable_before){ true }
- call_action
- flash[:success].should == "Offer was updated successfully."
- end
- end
- end
- context "if step not final" do
- before do
- controller.stub(:published_now?){ false }
- end
- it "redirects_to edit page with offer status (changed in to_next_step!)" do
- call_action
- response.should redirect_to('/offers/ABC/edit/details')
- end
- end
- end
- end
- context "when update fails" do
- before do
- offer.stub(:set_status_to_next_step!){ false }
- end
- it "assigns step" do
- offer.stub(:current_step).and_return 'nextstatus'
- call_action
- assigns[:step].should == 'nextstatus'
- end
- it "renders edit" do
- call_action
- response.should render_template('edit')
- end
- it "assigns flash error when published" do
- offer.stub(:acceptable?) { true }
- offer.stub(:published?) { true }
- offer.stub(:save) { false }
- errors = %w[Gone bad]
- offer.stub(:errors) { errors }
- errors.stub(:full_messages) { errors }
- call_action
- flash[:error].should_not == "Gone. bad."
- end
- it "assigns flash error" do
- offer.stub(:published?) { false }
- offer.stub(:save) { false }
- errors = %w[Gone bad]
- offer.stub(:errors) { errors }
- errors.stub(:full_messages) { errors }
- call_action
- flash[:error].should_not == "Gone. bad."
- end
- end
- end
- describe "GET show" do
- let(:booking) {Factory.build(:booking) }
- let(:offer) {Factory.build(:offer, :id => 777, :code => "OFFERCODE123") }
- def default_params
- [:get, :show, { :id => 'ABC' }]
- end
- before do
- Offer.stub(:find_by_code).with('ABC').and_return offer
- offer.stub(:price) { 10 }
- Booking.any_instance.stub(:calculate_nightly_rate_without_extra_charges_or_fees_in_guest_currency) { 20 }
- end
- context "when offer loadable" do
- it "loads public reviews for offer" do
- review = Factory(:host_review, :offer => offer)
- call_action
- assigns[:public_reviews].should == [review]
- end
- context "booking" do
- before do
- call_action
- end
- def default_params
- [:get, :show, { :id => 'ABC' }]
- end
- it "is set" do
- assigns[:booking].should be_a(Booking)
- end
- it ".checkout_date is nil by deafault" do
- assigns[:booking].checkout_date.should be_nil
- end
- it "booking.guests is 1 by deafault" do
- assigns[:booking].guests.should == 1
- end
- context "with booking relevant attributes set in params" do
- def default_params
- [:get, :show, {
- :id => 'ABC',
- :guests => '2',
- :checkin_date => Date.tomorrow.to_s(:db),
- :checkout_date => (Date.tomorrow + 1).to_s(:db)
- }]
- end
- it "sets guests correctly" do
- call_action
- assigns[:booking].guests.should == 2
- end
- it "sets checkin_date and checkout_date properly" do
- call_action
- assigns[:booking].checkin_date.should == Date.tomorrow
- assigns[:booking].checkout_date.should == Date.tomorrow + 1
- end
- end
- context "with last_search relevant params set in params" do
- def default_params
- [:get, :show, {
- :id => 'ABC',
- :guests => '3',
- :checkin_date => Date.tomorrow.to_s(:db),
- :checkout_date => (Date.tomorrow + 1).to_s(:db)
- }]
- end
- it "sets guests correctly" do
- call_action
- assigns[:booking].guests.should == 3
- end
- it "sets checkin_date and checkout_date properly" do
- call_action
- assigns[:booking].checkin_date.should == Date.tomorrow
- assigns[:booking].checkout_date.should == Date.tomorrow + 1
- end
- end
- context "with empty last_search param" do
- it "does not raise an error" do
- lambda do
- get :show, { :id => 'ABC', :last_search => "" }
- end.should_not raise_error
- end
- end
- end
- it "loads offer" do
- call_action
- assigns[:offer].should == offer
- end
- it "renders show template" do
- call_action
- response.should render_template('show')
- end
- it "should render the offers page for a NONE published offer if parameter preview is given" do
- offer.stub(:is_public){ false }
- get :show, { :id => 'ABC', :preview => "true" }
- assigns[:offer].should == offer
- response.should be_success
- response.should render_template(:show)
- end
- it "sets @calendar_options to current month and year" do
- call_action
- assigns(:calendar_options).should be_a(Hash)
- assigns(:calendar_options)[:month].should == Date.today.month
- assigns(:calendar_options)[:year].should == Date.today.year
- end
- it "sets mbb_required flag to true" do
- offer.stub_chain(:user, :message_before_booking?) { true }
- call_action
- assigns(:mbb_required).should == true
- end
- it "sets mbb_required flag to false when message_before_booking? is nil" do
- offer.stub_chain(:user, :message_before_booking?) { nil }
- call_action
- assigns(:mbb_required).should === false
- end
- it "sets mbb_form_url " do
- offer.stub_chain(:user, :message_before_booking?) { true }
- call_action
- assigns(:mbb_form_url).should be_a(String)
- end
- end
- context "when offer not loadable" do
- let(:offer) { Factory.stub(:offer) }
- before(:each) do
- Offer.stub(:find_by_code) {}
- end
- it "redirects to search page" do
- call_action
- response.should redirect_to('/search')
- end
- it "calls Offer.reindex with the correct parameter" do
- 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
- OfferIndex.should_receive(:changed!).with(offer)
- call_action
- end
- end
- end
- describe "GET description_and_houserules_translated" do
- context "when offer is loaded" do
- before :each do
- Offer.stub(:find_by_code).with('ABC').and_return offer
- offer.should_receive(:translated_title) { "translated description de" }
- offer.should_receive(:translated_description) { "translated description de" }
- offer.should_receive(:translated_rules) { "translated houserules de"}
- xhr :get, :description_and_houserules_translated, :id => 'ABC', :locale => "de"
- end
- it "sets locale variable" do
- assigns(:locale).should == :de
- end
- it "responds with hash including translations in JSON format " do
- response.should be_successful
- parsed_response = JSON.parse(response.body)
- parsed_response['translated_description'].should == "translated description de"
- parsed_response['translated_rules'].should == "translated houserules de"
- end
- context "locale not provided" do
- it "renders error response" do
- xhr :get, :description_and_houserules_translated, :id => 'ABC'
- parsed_response = JSON.parse(response.body)
- parsed_response['error_message'].should_not be_blank
- end
- end
- end
- context "when offer not loaded" do
- it "renders error reponse" do
- Offer.stub(:find_by_code).with('ABC') { nil }
- xhr :get, :description_and_houserules_translated, :id => 'ABC', :locale => "de"
- assigns(:locale).should be_nil
- response.should be_not_found
- end
- end
- end
- describe "GET calendar" do
- let(:offer) { Factory.build(:offer, :id => 777, :code => "OFFERCODE123") }
- before :each do
- Offer.stub(:find_by_code).with('ABC').and_return offer
- end
- it "renders calendar partial" do
- xhr(:get, :calendar, { :id => "ABC"} )
- response.should be_success
- response.should render_template("offers/_calendar_v4")
- end
- it "sets year and month properly" do
- xhr(:get, :calendar, { :id => "ABC", :month => 5, :year => 2014 })
- assigns(:year).should == 2014
- assigns(:month).should == 5
- end
- it "sets default year and month properly" do
- xhr(:get, :calendar, { :id => "ABC"} )
- assigns(:year).should == Date.today.year
- assigns(:month).should == Date.today.month
- end
- end
- describe "GET similar_offers" do
- let(:offer) { Factory.build(:offer, :id => 777) }
- let(:offer2) { Factory.build(:offer, :id => 778) }
- before :each do
- Offer.stub(:find_by_code).with('ABC').and_return offer
- offer.stub(:find_similar_with_volatile_prices_ranges) { [offer2]}
- end
- it "assigns proper offer" do
- xhr(:get, :similar_offers, { :id => "ABC"} )
- assigns(:offer).should == offer
- end
- it "assigns booking" do
- xhr(:get, :similar_offers, { :id => "ABC"} )
- assigns(:booking).should be_a(Booking)
- end
- it "renders similar_offers partial" do
- xhr(:get, :similar_offers, { :id => "ABC"} )
- response.should be_success
- response.should render_template("offers/_similar_offers")
- end
- it "it does not crash on empty array" do
- offer.should_receive(:find_similar_with_volatile_prices_ranges) { [] }
- xhr(:get, :similar_offers, { :id => "ABC"} )
- response.should be_success
- response.should render_template("offers/_similar_offers")
- end
- end
- describe "DELETE destroy" do
- def default_params
- [:delete, :destroy, { :id => 'ABC' }]
- end
- before do
- user_is_signed_in
- controller.stub(:user_signed_in_as_offer_admin?)
- offer.stub(:delete)
- current_user.stub_chain(:offers, :non_deleted, :find_by_code){ offer }
- end
- it "calls delete on offer" do
- offer.should_receive(:delete)
- call_action
- end
- context "when delete fails" do
- before do
- offer.stub(:delete).and_return false
- end
- it "sets flash notice" do
- call_action
- flash[:error].should == 'Your offer could not be deleted.'
- end
- end
- context "when delete succeeds" do
- before do
- offer.stub(:delete).and_return true
- end
- it "sets flash success" do
- call_action
- flash[:success].should == 'You have deleted your offer.'
- end
- end
- it "redirects to offers index" do
- call_action
- response.should redirect_to('/user/offers')
- end
- end
- describe "favorites" do
- before do
- Offer.stub!(:find_by_code).with('ABC') { offer }
- Offer.stub!(:find_by_code).with('not_found') { nil }
- end
- describe "GET add_to_favorites with Offer not found" do
- def default_params
- [:get, :add_to_favorites, { :id => 'not_found' }]
- end
- it "renders 404" do
- request.env['HTTP_REFERER'] = nil
- offer.stub(:code){ "not_found" }
- call_action
- response.status.should == 404
- end
- end
- describe "GET add_to_favorites without referrer" do
- def default_params
- [:get, :add_to_favorites, { :id => 'ABC' }]
- end
- it "redirects to the offer" do
- request.env['HTTP_REFERER'] = nil
- offer.stub(:code){ "ABC"}
- call_action
- request.should redirect_to("/offers/ABC")
- end
- end
- describe "GET add_to_favorites" do
- def default_params
- [:get, :add_to_favorites, { :id => 'ABC' }]
- end
- before :each do
- request.env['HTTP_REFERER'] = "/offers/show/ABC"
- offer.stub(:code){ "ABC"}
- end
- it "sets last added fav offer in session" do
- call_action
- controller.session.should_not == {"offer_id"=>"ABC", "tracking"=>[:track_add_favorite_offer]}
- end
- it "calls track_add_favorite_offer!" do
- controller.should_not_receive(:track_add_favorite_offer!)
- call_action
- end
- context "user signed in" do
- before :each do
- user_is_signed_in
- end
- it "calls add_offer on a new FavoriteOffer using user" do
- favorite_offer = double("favorite_offer").as_null_object
- FavoriteOffer.should_receive(:new).with(current_user) { favorite_offer }
- call_action
- request.should redirect_to("/offers/show/ABC")
- end
- end
- context "user not signed in" do
- before :each do
- controller.stub(:session) { {:session_id => "13"} }
- end
- it "calls add_offer on a new FavoriteOffer using user" do
- favorite_offer = double("favorite_offer").as_null_object
- FavoriteOffer.should_receive(:new).with("13") { favorite_offer }
- call_action
- request.should redirect_to("/offers/show/ABC")
- end
- end
- end
- describe "GET add_to_favorites as js" do
- def default_params
- [:get, :add_to_favorites, { :id => 'ABC', :format => :js }]
- end
- it "returns a jsonp response" do
- call_action
- response.should render_template('add_to_favorites')
- end
- end
- describe "GET remove_from_favorites with Offer not found" do
- def default_params
- [:get, :remove_from_favorites, { :id => 'not_found' }]
- end
- it "renders 404" do
- request.env['HTTP_REFERER'] = nil
- offer.stub(:code){ "not_found" }
- call_action
- response.status.should == 404
- end
- end
- describe "GET remove_from_favorites without referrer" do
- def default_params
- [:get, :remove_from_favorites, { :id => 'ABC' }]
- end
- it "redirects to the offer" do
- request.env['HTTP_REFERER'] = nil
- offer.stub(:code){ "ABC"}
- call_action
- request.should redirect_to("/offers/ABC")
- end
- end
- describe "GET remove_from_favorites" do
- def default_params
- [:get, :remove_from_favorites, { :id => 'ABC' }]
- end
- before :each do
- request.env['HTTP_REFERER'] = "/offers/show/ABC"
- Offer.stub!(:find_by_code).with('ABC') { offer }
- offer.stub(:code){ "ABC"}
- end
- it "sets last removed fav offer in session" do
- call_action
- controller.session.should_not == {"offer_id"=>"ABC", "tracking"=>[:track_remove_favorite_offer]}
- end
- it "calls track_remove_favorite_offer!" do
- controller.should_not_receive(:track_remove_favorite_offer!)
- call_action
- end
- context "user signed in" do
- before :each do
- user_is_signed_in
- end
- it "calls add_offer on a new FavoriteOffer using user" do
- favorite_offer = double("favorite_offer").as_null_object
- FavoriteOffer.should_receive(:new).with(current_user) { favorite_offer }
- call_action
- request.should redirect_to("/offers/show/ABC")
- end
- end
- context "user not signed in" do
- before do
- controller.stub(:session) { {:session_id => "13"} }
- end
- it "calls add_offer on a new FavoriteOffer using user" do
- favorite_offer = double("favorite_offer").as_null_object
- FavoriteOffer.should_receive(:new).with("13") { favorite_offer }
- call_action
- request.should redirect_to("/offers/show/ABC")
- end
- end
- end
- describe "GET remove_from_favorites as js" do
- def default_params
- [:get, :remove_from_favorites, { :id => 'ABC', :format => :js }]
- end
- it "returns a jsonp response" do
- call_action
- response.should render_template('remove_from_favorites')
- end
- end
- describe "GET favorites" do
- def default_params
- [:get, :favorites, { :id => nil }]
- end
- before :each do
- Offer.stub_chain(:where, :order) { [offer, offer] }
- offer.stub(:to_json) { "offer_as_json" }
- controller.stub(:session) { {:session_id => "13"} }
- end
- it "renders partial _favorites" do
- call_action
- response.should render_template("search/_favorites")
- end
- end
- end
- describe "PUT update_visibility" do
- before do
- Offer.stub!(:find_by_code).with('ABC') { offer }
- end
- def default_params
- [:put, :update_visibility, { :id => 'ABC' }]
- end
- it "does not call toggle_visibility on offer no user given" do
- offer.should_not_receive(:toggle_visibility!)
- call_action
- response.code.should == "404"
- end
- it "it calls load_own_offer" do
- controller.should_receive(:load_own_offer)
- call_action
- end
- it "calls toggle_visibilit!" do
- user_is_signed_in
- controller.current_user.stub_chain(:offers, :non_deleted, :find_by_code) { offer }
- offer.should_receive(:toggle_visibility!)
- call_action
- response.should redirect_to offers_path(:host => request.host)
- end
- end
- end
Add Comment
Please, Sign In to add comment