diff --git a/lib/tasks/survey.rake b/lib/tasks/survey.rake index b2d2fd97..5499980c 100644 --- a/lib/tasks/survey.rake +++ b/lib/tasks/survey.rake @@ -1,7 +1,7 @@ namespace :survey do desc 'Text all Recipients ready for an Attempt' - task :attempt_qustions => :environment do + task :attempt_questions => :environment do Schedule.active.each do |schedule| schedule.recipient_schedules.ready.each do |recipient_schedule| recipient_schedule.attempt_question diff --git a/spec/controllers/attempts_controller_spec.rb b/spec/controllers/attempts_controller_spec.rb index c42ad893..a7f938fa 100644 --- a/spec/controllers/attempts_controller_spec.rb +++ b/spec/controllers/attempts_controller_spec.rb @@ -26,7 +26,7 @@ RSpec.describe AttemptsController, type: :controller do } - describe "POS #twilio" do + describe "POST #twilio" do context "with valid params" do let(:twilio_attributes) { {'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+11231231234','To' => '2223334444','Body' => '3','NumMedia' => '0'} @@ -37,6 +37,8 @@ RSpec.describe AttemptsController, type: :controller do attempt.reload expect(attempt.answer_index).to eq(3) expect(attempt.twilio_details).to eq(twilio_attributes.with_indifferent_access.to_yaml) + expect(first_attempt.answer_index).to be_nil + expect(first_attempt.twilio_details).to be_nil end it "sends back a message" do diff --git a/spec/lib/tasks/survey_rake_spec.rb b/spec/lib/tasks/survey_rake_spec.rb index 7452f955..62445e05 100644 --- a/spec/lib/tasks/survey_rake_spec.rb +++ b/spec/lib/tasks/survey_rake_spec.rb @@ -1,26 +1,107 @@ require 'rails_helper' -describe "survey:attempt_qustions" do +describe "survey:attempt_questions" do include_context "rake" - let(:ready_recipient_schedule) { double('ready recipient schedule', attempt_question: nil) } - let(:recipient_schedules) { double("recipient schedules", ready: [ready_recipient_schedule]) } - let(:active_schedule) { double("active schedule", recipient_schedules: recipient_schedules) } - - before do - # ReportGenerator.stubs(:generate) - # UsersReport.stubs(:new => report) - # User.stubs(:all => user_records) - end - it 'should have environment as a prerequisite' do expect(subject.prerequisites).to include("environment") end - it "finds all active schedules" do - expect(ready_recipient_schedule).to receive(:attempt_question) - expect(active_schedule).to receive(:recipient_schedules) - expect(Schedule).to receive(:active).and_return([active_schedule]) - subject.invoke + describe "basic flow" do + let(:ready_recipient_schedule) { double('ready recipient schedule', attempt_question: nil) } + let(:recipient_schedules) { double("recipient schedules", ready: [ready_recipient_schedule]) } + let(:active_schedule) { double("active schedule", recipient_schedules: recipient_schedules) } + + it "finds all active schedules" do + expect(ready_recipient_schedule).to receive(:attempt_question) + expect(active_schedule).to receive(:recipient_schedules) + expect(Schedule).to receive(:active).and_return([active_schedule]) + subject.invoke + end + end + + describe "complex flow" do + + let!(:school) { School.create!(name: 'School') } + + let!(:recipients) { create_recipients(school, 3) } + let!(:recipient_list) do + school.recipient_lists.create!(name: 'Parents', recipient_ids: recipients.map(&:id).join(',')) + end + + let!(:questions) { create_questions(3) } + let!(:question_list) do + QuestionList.create!(name: 'Parent Questions', question_ids: questions.map(&:id).join(',')) + end + + let!(:schedule) do + Schedule.create!( + name: 'Parent Schedule', + recipient_list_id: recipient_list.id, + question_list: question_list, + frequency_hours: 24 * 7, + start_date: Time.new, + end_date: 1.year.from_now + ) + end + + describe 'First Attempt' do + + before :each do + Timecop.freeze + subject.invoke + end + + it 'should create the first attempt for each recipient' do + recipients.each do |recipient| + recipient.reload + expect(recipient.attempts.count).to eq(1) + attempt = recipient.attempts.first + expect(attempt.sent_at).to be_present + expect(attempt.answer_index).to be_nil + end + end + end + + describe 'Second Attempts' do + before :each do + recipients.each do |recipient| + recipient_schedule = schedule.recipient_schedules.for(recipient).first + recipient_schedule.attempt_question + end + end + + describe 'Immediate' do + before :each do + subject.invoke + end + + it 'should do nothing' do + recipients.each do |recipient| + recipient.reload + expect(recipient.attempts.count).to eq(1) + end + end + end + + describe 'A Week Later' do + before :each do + Timecop.freeze(Date.today + 8) { subject.invoke } + end + + it 'should create the second attempt for each recipient with a different question' do + recipients.each do |recipient| + recipient.reload + expect(recipient.attempts.count).to eq(2) + attempt = recipient.attempts.last + expect(attempt.sent_at).to be_present + expect(attempt.answer_index).to be_nil + + first_attempt = recipient.attempts.first + expect(first_attempt.question).to_not eq(attempt.question) + end + end + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 58310aa9..a5bff0f2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -100,6 +100,10 @@ RSpec.configure do |config| config.before(:each) do stub_const("Twilio::REST::Client", FakeSMS) end + + config.after(:each) do + FakeSMS.reset + end end @@ -113,6 +117,10 @@ class FakeSMS def initialize(_account_sid, _auth_token) end + def self.reset + self.messages = [] + end + def messages self end