@ -6,8 +6,12 @@ RSpec.describe AttemptsController, type: :controller do
let ( :schedule ) { Schedule . new }
let ( :schedule ) { Schedule . new }
let ( :school ) { School . create! ( name : 'School' ) }
let ( :school ) { School . create! ( name : 'School' ) }
let ( :recipient ) { school . recipients . create! ( name : 'Recipient' , phone : '+11231231234' ) }
let ( :recipient ) { school . recipients . create! ( name : 'Recipient' , phone : '+11231231234' ) }
let ( :recipient_schedule ) { RecipientSchedule . new }
let ( :recipient_schedule ) { RecipientSchedule . new }
let ( :recipient2 ) { school . recipients . create! ( name : 'Recipient2' , phone : '+12342342345' ) }
let ( :recipient_schedule2 ) { RecipientSchedule . new }
let ( :category ) { Category . create! ( name : 'Category' ) }
let ( :category ) { Category . create! ( name : 'Category' ) }
let ( :question ) { create_questions ( 1 , category ) . first }
let ( :question ) { create_questions ( 1 , category ) . first }
let! ( :first_attempt ) {
let! ( :first_attempt ) {
@ -26,6 +30,14 @@ RSpec.describe AttemptsController, type: :controller do
question : question
question : question
)
)
}
}
let! ( :attempt2 ) {
Attempt . create (
schedule : schedule ,
recipient : recipient2 ,
recipient_schedule : recipient_schedule2 ,
question : question
)
}
describe " POST # twilio " do
describe " POST # twilio " do
@ -34,8 +46,15 @@ RSpec.describe AttemptsController, type: :controller do
{ 'MessageSid' = > 'ewuefhwieuhfweiuhfewiuhf' , 'AccountSid' = > 'wefiuwhefuwehfuwefinwefw' , 'MessagingServiceSid' = > 'efwneufhwuefhweiufhiuewhf' , 'From' = > '+11231231234' , 'To' = > '2223334444' , 'Body' = > '3' , 'NumMedia' = > '0' }
{ 'MessageSid' = > 'ewuefhwieuhfweiuhfewiuhf' , 'AccountSid' = > 'wefiuwhefuwehfuwefinwefw' , 'MessagingServiceSid' = > 'efwneufhwuefhweiufhiuewhf' , 'From' = > '+11231231234' , 'To' = > '2223334444' , 'Body' = > '3' , 'NumMedia' = > '0' }
}
}
it " updates the last attempt by recipient phone number " do
before :each do
post :twilio , params : twilio_attributes
post :twilio , params : twilio_attributes
end
it 'creates the first attempt with response for the question' do
expect ( attempt . question . attempts . for_school ( school ) . with_response . count ) . to eq ( 1 )
end
it " updates the last attempt by recipient phone number " do
attempt . reload
attempt . reload
expect ( attempt . answer_index ) . to eq ( 3 )
expect ( attempt . answer_index ) . to eq ( 3 )
expect ( attempt . twilio_details ) . to eq ( twilio_attributes . with_indifferent_access . to_yaml )
expect ( attempt . twilio_details ) . to eq ( twilio_attributes . with_indifferent_access . to_yaml )
@ -47,10 +66,32 @@ RSpec.describe AttemptsController, type: :controller do
end
end
it " sends back a message " do
it " sends back a message " do
post :twilio , params : twilio_attributes
expect ( response . body ) . to eq " We've registered your response of \" Option 0:1 C \" . You are the first person to respond to this question. Once more people have responded you will be able to see all responses at http://test.host/schools/school/categories/category "
expect ( response . body ) . to eq " " " We \' ve registered your response of \" Option 0:1 C \" .
end
To see how others responded to the same question please visit
http : / / test . host / schools / school / categories / category " " "
context " with second response " do
let ( :twilio_attributes2 ) {
{ 'MessageSid' = > 'fwefwefewfewfasfsdfdf' , 'AccountSid' = > 'wefwegdbvcbrtnrn' , 'MessagingServiceSid' = > 'dfvdfvegbdfb' , 'From' = > '+12342342345' , 'To' = > '2223334444' , 'Body' = > '4' , 'NumMedia' = > '0' }
}
before :each do
post :twilio , params : twilio_attributes2
end
it 'creates the second attempt with response for the question' do
expect ( attempt . question . attempts . for_school ( school ) . with_response . count ) . to eq ( 2 )
end
it " updates the attempt from the second recipient " do
attempt2 . reload
expect ( attempt2 . answer_index ) . to eq ( 4 )
expect ( attempt2 . twilio_details ) . to eq ( twilio_attributes2 . with_indifferent_access . to_yaml )
expect ( attempt2 . responded_at ) . to be_present
end
it " sends back a message " do
expect ( response . body ) . to eq " We've registered your response of \" Option 0:1 D \" . 2 people have responded to this question so far. To see all responses visit http://test.host/schools/school/categories/category "
end
end
end
end
end