2016-08-07 09:36:08 -05:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class QuizControllerTest < ActionDispatch::IntegrationTest
|
|
|
|
def setup_auth candidate
|
|
|
|
post validate_candidate_url, params: { test_id: candidate.test_hash }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should require auth and redirect" do
|
|
|
|
get summary_path
|
|
|
|
assert_redirected_to login_path
|
|
|
|
|
|
|
|
get question_path
|
|
|
|
assert_redirected_to login_path
|
|
|
|
|
|
|
|
get question_path(questions(:fed1).id)
|
|
|
|
assert_redirected_to login_path
|
|
|
|
end
|
|
|
|
|
2016-08-16 17:09:40 -05:00
|
|
|
# TODO: test models/candidate.complete! == false
|
|
|
|
# test 'should gracefully fail on summary submit' do
|
|
|
|
# # if the mailers fail, should go to thank you still
|
|
|
|
# end
|
|
|
|
|
|
|
|
test "should redirect to saved on save" do
|
|
|
|
setup_auth candidates(:dawn)
|
|
|
|
qid = questions(:fed5).id
|
|
|
|
post post_answer_path, params: { save: 'Save', answer: { question_id: qid, radio: 'an option' } }
|
|
|
|
|
|
|
|
assert_redirected_to saved_path
|
|
|
|
assert session[:test_id].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should redirect to next question on next" do
|
|
|
|
setup_auth candidates(:roy)
|
|
|
|
qid = questions(:fed3).id
|
|
|
|
params = { submit: 'Next', answer: { question_id: qid, live_code: { text: 'stuff' } } }
|
|
|
|
post post_answer_path, params: params
|
|
|
|
|
|
|
|
assert_redirected_to question_path
|
|
|
|
assert session[:test_id].present?
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
|
|
|
assert assigns(:status), '@status not present'
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should get summary" do
|
|
|
|
setup_auth candidates :dawn
|
|
|
|
get summary_path
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert assigns(:quiz), '@quiz not present'
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should redirect from summary" do
|
|
|
|
setup_auth candidates :roy
|
|
|
|
get summary_path
|
|
|
|
|
|
|
|
assert_redirected_to question_path
|
2016-08-16 17:09:40 -05:00
|
|
|
end
|
|
|
|
|
2016-08-07 09:36:08 -05:00
|
|
|
test "should get flash message on bad radio response" do
|
2016-08-16 17:09:40 -05:00
|
|
|
setup_auth candidates(:dawn)
|
|
|
|
qid = questions(:fed5).id
|
2016-08-07 09:36:08 -05:00
|
|
|
post post_answer_path, params: { answer: { question_id: qid, radio: nil } }
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert session[:test_id].present?
|
2016-08-09 23:17:35 -05:00
|
|
|
assert_equal qid, flash[:error]
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-07 09:36:08 -05:00
|
|
|
end
|
2016-08-16 17:09:40 -05:00
|
|
|
|
|
|
|
test "should get flash message on bad text response" do
|
|
|
|
setup_auth candidates(:dawn)
|
|
|
|
qid = questions(:fed4).id
|
|
|
|
post post_answer_path, params: { answer: { question_id: qid, text: nil } }
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert session[:test_id].present?
|
|
|
|
assert_equal qid, flash[:error]
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-16 17:09:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should process checkbox" do
|
|
|
|
setup_auth candidates(:dawn)
|
|
|
|
qid = questions(:fed10).id
|
|
|
|
post post_answer_path, params: { answer: { question_id: qid, checkbox: 'an-option' } }
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert session[:test_id].present?
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-16 17:09:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should handle XHR update and complete progress' do
|
|
|
|
setup_auth candidates(:peggy)
|
|
|
|
qid = questions(:fed10).id
|
|
|
|
post post_answer_path, xhr: true, params: { answer: { question_id: qid, checkbox: ['an-option'] } }
|
|
|
|
|
|
|
|
assert_response :success
|
|
|
|
assert_match(/updated successfully/, JSON.parse(@response.body)['message'])
|
|
|
|
assert_equal 100, JSON.parse(@response.body)['progress']
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-16 17:09:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should handle XHR fail' do
|
|
|
|
setup_auth candidates(:peggy)
|
|
|
|
qid = questions(:fed10).id
|
|
|
|
post post_answer_path, xhr: true, params: { answer: { question_id: qid, checkbox: nil } }
|
|
|
|
|
|
|
|
assert_response 400
|
|
|
|
assert_match(/select.*answer/i, JSON.parse(@response.body).join)
|
2016-08-17 13:58:25 -05:00
|
|
|
assert assigns(:question), '@question not present'
|
|
|
|
assert assigns(:answer), '@answer not present'
|
2016-08-16 17:09:40 -05:00
|
|
|
end
|
2016-08-07 09:36:08 -05:00
|
|
|
end
|