Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'md5'
- require File.dirname(__FILE__) + '/../spec_helper'
- describe Survey, "validation" do
- before(:each) do
- @merchant = Merchant.create!(valid_merchant_params)
- end
- it "should fail if name is not specified" do
- s = Survey.create(:merchant_id => 1)
- s.should_not be_valid
- s.should have(1).error_on(:name)
- end
- it "should fail if merchant is not specified" do
- s = Survey.create(:name => "Test survey")
- s.should_not be_valid
- s.should have(1).error_on(:merchant_id)
- end
- it "should add identifier to name if name is not unique" do
- s1 = Survey.create!(:name => "test", :merchant => @merchant)
- s2 = Survey.create!(:name => "test", :merchant => @merchant)
- s2.name.should == "test(2)"
- end
- it "should have unique name only within merchant scope" do
- Survey.create(:name=>"test", :merchant_id => 1).should be_valid
- Survey.create(:name => "test", :merchant_id => 2).should be_valid
- end
- it "should have ext_id after creation" do
- Time.stub!(:now).and_return(12345)
- s = Survey.create(:merchant_id => 1, :name => "test")
- s.ext_id.should == Digest::MD5.hexdigest("--test-12345-")
- end
- end
- describe Survey, "when created" do
- before :each do
- @survey = Survey.create(:name => "Test survey", :theme_id => 3, :merchant_id => 1, :status => "draft", :created_at => 10.days.ago)
- end
- it "should be draft" do
- @survey.status.should == "draft"
- end
- it "should be editable" do
- @survey.should be_editable
- end
- end
- describe Survey do
- before(:each) do
- @survey = Survey.create(:name => "Test survey", :theme_id => 3, :merchant_id => 1, :status => "draft", :created_at => 10.days.ago)
- end
- it "should publish survey on request" do
- @survey.publish.should be_true
- @survey.status.should == 'published'
- end
- it "should allow changes to draft surveys" do
- @survey.update_attributes(:name => "Just something").should be_true
- end
- it "should not allow changes to survey if it was published" do
- @survey.publish.should be_true
- @survey.update_attributes(:name => "Just something").should be_false
- end
- it "should close survey on request" do
- @survey.close
- @survey.status.should == 'closed'
- end
- it "should not allow changes to closed surveys" do
- @survey.close.should be_true
- @survey.update_attributes(:name => "Just something").should be_false
- end
- it "should copy survey by creating new unsaved instance" do
- new_survey = @survey.make_copy(mock_model(Merchant))
- new_survey.should be_new_record
- new_survey.theme_id.should == @survey.theme_id
- new_survey.name.should == "#{@survey.name} Copy"
- end
- it "should correctly save newly copied survey" do
- new_survey = @survey.make_copy(mock_model(Merchant, :surveys_recipients_limit => 200))
- new_survey.merchant_id = 1
- new_survey.save.should be_true
- new_survey.should be_valid
- end
- it "should return state back to draft if survey was published and still have no responses" do
- @survey.publish
- @answers = mock("pseudo-answers array")
- @answers.stub!(:count).and_return(0) #imagine we have no answers yet
- @survey.stub!(:answers).and_return(@answers)
- @survey.unpublish.should be_true
- @survey.status.should == 'draft'
- end
- it "should not return back to draft if survey was published and have responses" do
- @survey.publish
- @answers = mock("pseudo-answers array")
- @answers.stub!(:count).and_return(13) #imagine we have no answers yet
- @survey.stub!(:answers).and_return(@answers)
- @survey.unpublish.should be_false
- @survey.status.should == 'published'
- end
- it "should have correct 'published?' return values" do
- @survey.published?.should be_false
- @survey.publish
- @survey.published?.should be_true
- end
- it "should delete all questions on destroy" do
- @survey.survey_questions.create!(:title => "Who da hell is Joe Shmoe?")
- @survey.survey_questions.create!(:title => "Who let the dogs out?")
- survey_questions_count = @survey.survey_questions.count
- lambda { @survey.destroy }.should change(SurveyQuestion, :count).by(-survey_questions_count)
- end
- it "should delete all answers on delete" do
- @survey.answers.create!
- @survey.answers.create!
- survey_answers_count = @survey.answers.count
- lambda { @survey.destroy }.should change(SurveyAnswer, :count).by(-survey_answers_count)
- end
- end
- describe Survey, "voting" do
- before(:each) do
- @survey = Survey.new(:name => "test survey", :merchant_id => 1)
- @survey.save!
- @survey.survey_questions << (@input_box = InputBoxQuestion.new(:title => "Input box"))
- @survey.survey_questions << (@yes_no = YesNoQuestion.new(:title => "Yes or no"))
- @survey.survey_questions << (@radio_list = RadiobuttonListQuestion.new(:title => "List of radio buttons", :vars => ["opt1", "opt2", "opt3", "opt4"]))
- @survey.survey_questions << (@checkbox_list = CheckboxListQuestion.new(:title => "List of check boxes", :vars => ['cbopt1', 'cbopt2', 'cbopt3']))
- @survey.save!
- @survey.publish
- end
- it "should accept proper vote" do
- lambda {
- @survey.vote("question_", vote_params).should_not be_nil
- }.should change(@survey.answers, :count).by(1)
- @survey.answers.first.should have(@survey.survey_questions.count).answer_options
- end
- it "should check validity of vote" do
- @survey.survey_questions.each do |q|
- q.should_receive(:accept_vote).with(vote_params["question_#{q.id}"])
- @survey.survey_questions.should_receive(:find).with(q.id).and_return(q)
- end
- @survey.vote("question_", vote_params).should_not be_nil
- end
- it "should not accept invalid vote as response" do
- answer, msg = @survey.vote("questions_", vote_params("question_#{@yes_no.id}" => 3))
- answer.should be_nil
- msg.should == "Please provide at least one response to submit this survey."
- end
- it "should create answer on vote" do
- answer, msg = @survey.vote("question_", vote_params)
- answer.should be_kind_of(SurveyAnswer)
- answer.should have(@survey.survey_questions.count).answer_options
- answer.answer_options.each{ |ao| ao.should be_valid }
- end
- [:close, :unpublish ].each do |status_change|
- it "should not allow voting for #{status_change}ed survey" do
- @survey.send(status_change)
- answer, msg = @survey.vote("question_", vote_params)
- answer.should be_nil
- msg = "Survey is not published"
- end
- end
- it "should vote if at least one question key is present" do
- answer, msg = @survey.vote("question_", "question_#{@yes_no.id}" => 1)
- answer.should be_kind_of(SurveyAnswer)
- end
- it "should not vote if no question keys are present" do
- answer, msg = @survey.vote("question_", {})
- answer.should be_nil
- msg.should == "Please provide at least one response to submit this survey."
- end
- protected
- def vote_params(opts={})
- {
- "question_#{@input_box.id}" => "Some string value",
- "question_#{@yes_no.id}" => "1",
- "question_#{@radio_list.id}" => "3",
- "question_#{@checkbox_list.id}" => {'1' => "on", '3' => "on"}
- }.merge(opts)
- end
- end
- describe Survey, "recipients limit" do
- before(:each) do
- @merchant = Merchant.new(:id => 1)
- end
- it "should set default value" do
- @survey = Survey.create!(valid_survey_params.merge(:merchant => @merchant))
- @survey.recipients_limit.should == Merchant::DEFAULT_SURVEYS_RECIPIENTS_LIMIT
- end
- it "should set merchant's surveys recepints limit value if defined" do
- @merchant.surveys_recipients_limit = 100
- @survey = Survey.create!(valid_survey_params.merge(:merchant => @merchant))
- @survey.recipients_limit.should == @merchant.surveys_recipients_limit
- end
- end
- describe Survey, "recipients_limit_exceeded?" do
- before(:each) do
- @survey = Survey.create!(valid_survey_params)
- @recipients = stub("Recipients", :size => 1)
- @survey.stub!(:recipients).and_return(@recipients)
- end
- it "should be false if amount of recipients is less then recipients_limit" do
- @survey.recipients_limit_exceeded?.should == false
- end
- it "should be true if amount of recipients is equal to recipients_limit" do
- @recipients.stub!(:size).and_return(@survey.recipients_limit)
- @survey.recipients_limit_exceeded?.should == true
- end
- it "should be true if amount of recipients is more then recipients_limit" do
- @recipients.stub!(:size).and_return(@survey.recipients_limit + 1)
- @survey.recipients_limit_exceeded?.should == true
- end
- end
- describe Survey, "copying" do
- fixtures :merchants, :surveys
- before(:each) do
- @merchant = merchants(:apple)
- @survey = Survey.new(:name => "test survey", :merchant_id => 1, :created_at => 10.minutes.ago)
- @survey.save!
- @survey.survey_questions << (@input_box = InputBoxQuestion.new(:title => "Input box"))
- @survey.survey_questions << (@yes_no = YesNoQuestion.new(:title => "Yes or no"))
- @survey.survey_questions << (@radio_list = RadiobuttonListQuestion.new(:title => "List of radio buttons", :vars => ["opt1", "opt2", "opt3", "opt4"]))
- @survey.survey_questions << (@checkbox_list = CheckboxListQuestion.new(:title => "List of check boxes", :vars => ['cbopt1', 'cbopt2', 'cbopt3']))
- @survey.publish
- @new_survey = @survey.make_copy(@merchant)
- end
- it "should create new instance of survey" do
- @new_survey.should be_new_record
- end
- it "should create new instances of questions" do
- @new_survey.should have(@survey.survey_questions.count).survey_questions
- @new_survey.survey_questions.each {|q| q.should be_new_record}
- end
- it "should correctly save instances" do
- @new_survey.save.should be_true
- end
- it "should create survey without answers" do
- @new_survey.should have(0).answers
- end
- it "should copy theme from source survey" do
- @new_survey.theme_id.should == @survey.theme_id
- end
- it "should copy survey close date" do
- @new_survey.close_at.should == @survey.close_at
- end
- it "should allow using global template as a base" do
- @survey.update_attribute("merchant_id", nil)
- new_survey = @survey.make_copy(@merchant)
- new_survey.should_not be_nil
- end
- it "should set survey state to 'draft'" do
- @new_survey.status.should == 'draft'
- end
- it "should allow using local survey as a base" do
- @survey.merchant.should_not be_nil
- @new_survey.should be_kind_of(Survey)
- end
- it "should set new created_at date" do
- new_survey = @survey.make_copy(mock_model(Merchant))
- new_survey.created_at.should_not == @survey.created_at
- end
- end
- describe Survey, "address book contacts for survey recipients managament" do
- before(:each) do
- @merchant = Merchant.create!(valid_merchant_params)
- @survey = Survey.create!(:name => "Test Survey", :merchant => @merchant)
- @contacts = []
- for i in 0..9 do
- @contacts << @merchant.address_book_contacts.create!(:name => "Contact #{i}", :email => "email_#{i}@test.com")
- end
- #email_0 is unsubscribed email
- @merchant.unsubscribe_email("email_0@test.com")
- #email_1 - 4 in survey's recipient's list
- success, created_recipients = SurveyRecipient.create_list(@survey, %w(email_1@test.com email_2@test.com email_3@test.com email_4@test.com))
- #email_1 - 2 was notified
- created_recipients[0..1].each { |recepient| recepient.update_attribute("notified", true)}
- #address book contact 1 have two emails
- @contacts[1].email_contacts.create(:address => 'email_1_2@test.com')
- end
- it "should get all recipients who not in unsubscribed list of given merchant and not in recipients list of given survey with address_book_contacts_for_add_recipients method" do
- result = @survey.address_book_contacts_for_add_recipients
- result.size.should == 5
- #contacts 0 (unsubscribed), 1-4 (already in recipients list) should be filtered
- @contacts.each_with_index { |c, i| i < 5 ? result.should_not(include(c)) : result.should(include(c)) }
- end
- it "should get all recipients who not in unsubscribed list of given merchant and is not notified recipient of given survey with address_book_contacts_for_instant_send method" do
- result = @survey.address_book_contacts_for_instant_send
- result.size.should == 7
- #contacts 0(unsubscribed) 1, 2 (notified recipients) should be filtered
- @contacts.each_with_index { |c, i| i < 3 ? result.should_not(include(c)) : result.should(include(c)) }
- end
- end
- describe Survey, "copy recipients process" do
- before(:each) do
- @merchant = Merchant.create!(valid_merchant_params)
- @survey1 = @merchant.surveys.create!(:name => "Test 1")
- @recipients = []
- for i in 0..4 do
- @recipients << @survey1.recipients.create!(:name => "Rec_#{i}", :email => "test#{i}@email.com", :merchant => @merchant)
- end
- @survey2 = @merchant.surveys.create!(:name => "Test 1")
- #recipient with email same as Rec_0 is already in survey 2 list
- @survey2.recipients << @recipients[0].clone
- #email of Rec_1 was unsubscribed
- @merchant.unsubscribe_email(@recipients[1].email)
- end
- describe "actual copying" do
- it "should get all recipients of given survey, except already present in list or unsubscribed" do
- result = @survey2.copy_recipients(@survey1)
- result.size.should == 3
- result_emails = result.collect(&:email)
- @recipients.each_with_index { |r, i| i > 1 ? result_emails.should(include(r.email)) : result_emails.should_not(include(r.email))}
- end
- it "should create recipients" do
- lambda { @survey2.copy_recipients(@survey1) }.should change(@survey2.reload.recipients, :size).by(3)
- end
- end
- describe "counting recipients for copy" do
- it "should count all recipients of given survey, except already present in list of current survey or unsubscribed" do
- #we have 5 recipients in setup:
- #1 is present in current survey's recipients list
- #1 was unsubscribed by business
- #3 are ready for copying
- @survey2.count_recipients_to_copy(@survey1).should == 3
- end
- end
- describe "finding out is survey have recipients to copy" do
- it "should return true if have recipients to copy" do
- @survey1.have_recipients_to_copy?(@survey2).should be_true
- end
- it "should return false if have no recipients to copy" do
- @survey2.have_recipients_to_copy?(@survey1).should be_false
- end
- end
- end
- describe Survey, "sending ivitations" do
- fixtures :merchants
- before(:each) do
- EmailQueue.delete_all
- @merchant = merchants(:merchant_1)
- @survey = @merchant.surveys.create!(:name => 'Service Satisfaction Survey')
- @survey.invitation = SurveyInvitation.new(:subject => 'answer please', :body => 'some text')
- @recipients = [
- SurveyRecipient.create!(:merchant => @merchant, :survey => @survey, :email => "email1@example.com"),
- SurveyRecipient.create!(:merchant => @merchant, :survey => @survey, :email => "email2@example.com")
- ]
- @queue = Survey.send_invitations(@survey, @recipients)
- end
- it "should create email queue" do
- @queue.should be_kind_of(EmailQueue)
- @queue.email_type.should == 'survey_invitation'
- end
- it "should add emails to this queue" do
- @queue.emails.size.should == 2
- end
- it "should mark recipients as notified" do
- @recipients.map(&:reload).should be_all(&:notified?)
- end
- it "should have survey name as subject" do
- @queue.subject.should == @survey.name
- end
- end
- describe Survey, "replacing questions with replace_questions(other_survey)" do
- before(:each) do
- @prev_survey = Survey.new(:merchant_id => 0)
- @prev_survey.survey_questions << YesNoQuestion.new(:title => 'Why did you choose <business>?')
- @survey = Survey.new(:merchant => Merchant.new(:name => 'Qwerty Board'))
- end
- it "should replace <business> for business name in titles for system templates" do
- @survey.replace_questions(@prev_survey)
- @survey.survey_questions.first.title.should == 'Why did you choose Qwerty Board?'
- end
- it "should NOT replace <business> in question titles for normal surveys" do
- @prev_survey.merchant_id = 1234567
- @survey.replace_questions(@prev_survey)
- @survey.survey_questions.first.title.should == 'Why did you choose <business>?'
- end
- end
- describe Survey, "creating from template with create_or_copy(merchant, copy_ext_id, params, templates)" do
- fixtures :merchants, :surveys, :survey_questions
- before(:each) do
- @templates = Survey.find(:all)
- @merchant = @templates.first.merchant
- end
- it "should return new blank survey if no ext_id to copy given" do
- new_survey = Survey.create_or_copy(@merchant, nil, nil, @templates)
- new_survey.survey_questions.should be_empty
- end
- it "should return new survey with questions from template if ext_id to copy given" do
- new_survey = Survey.create_or_copy(@merchant, @templates.first.ext_id, nil, @templates)
- new_survey.name.should match(/^#{@templates.first.name}/)
- new_survey.survey_questions.should_not be_empty
- end
- it "should return new blank survey if ext_id is invalid" do
- new_survey = Survey.create_or_copy(@merchant, nil, nil, @templates)
- new_survey.survey_questions.should be_empty
- end
- end
Add Comment
Please, Sign In to add comment