From 14bbd301ed536472da8b6a053254aaee237e944a Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Tue, 16 Aug 2016 17:09:40 -0500 Subject: [PATCH] progress on #27: test coverage over 96% --- app/controllers/quiz_controller.rb | 6 -- app/models/candidate.rb | 3 + test/controllers/candidate_controller_test.rb | 8 +++ test/controllers/quiz_controller_test.rb | 66 ++++++++++++++++++- test/controllers/recruiter_controller_test.rb | 23 ++++++- test/controllers/review_controller_test.rb | 8 +++ test/services/skill_config_test.rb | 17 +++++ test/test_helper.rb | 5 +- 8 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 test/services/skill_config_test.rb diff --git a/app/controllers/quiz_controller.rb b/app/controllers/quiz_controller.rb index 0a8ce09..d604fed 100644 --- a/app/controllers/quiz_controller.rb +++ b/app/controllers/quiz_controller.rb @@ -109,10 +109,4 @@ class QuizController < ApplicationController saved: params.key?(:save), submitted: params.key?(:submit)) end - - def process_live_code_text - @answer.update(answer: answer_params[:live_code_text].to_h, - saved: params.key?(:save), - submitted: params.key?(:submit)) - end end diff --git a/app/models/candidate.rb b/app/models/candidate.rb index f4dfc84..8af27fa 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -37,6 +37,9 @@ class Candidate < ApplicationRecord "#{my_status.progress}%" end + # FIXME: This feels wrong here. Mail deliveries should be in controller. + # Privatize in QuizController + # also, bang methods in ruby do an action and replace, or return nil def complete! if update_attributes(completed: true) CandidateMailer.submitted(self).deliver_now diff --git a/test/controllers/candidate_controller_test.rb b/test/controllers/candidate_controller_test.rb index 4500630..d0258a1 100644 --- a/test/controllers/candidate_controller_test.rb +++ b/test/controllers/candidate_controller_test.rb @@ -55,6 +55,14 @@ class CandidateControllerTest < ActionDispatch::IntegrationTest assert_redirected_to thankyou_path end + test 'should reset session' do + setup_auth candidates(:dawn) + get thankyou_path + + assert :success + assert session[:test_id].nil? + end + test "should get summary if complete but not submitted" do setup_auth candidates(:dawn) diff --git a/test/controllers/quiz_controller_test.rb b/test/controllers/quiz_controller_test.rb index fa17727..8de42eb 100644 --- a/test/controllers/quiz_controller_test.rb +++ b/test/controllers/quiz_controller_test.rb @@ -16,13 +16,75 @@ class QuizControllerTest < ActionDispatch::IntegrationTest assert_redirected_to login_path end + # 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? + end + test "should get flash message on bad radio response" do - setup_auth candidates(:martha) - qid = questions(:fed1).id + setup_auth candidates(:dawn) + qid = questions(:fed5).id post post_answer_path, params: { answer: { question_id: qid, radio: nil } } assert_response :success assert session[:test_id].present? assert_equal qid, flash[:error] end + + 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] + 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? + 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'] + 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) + end end diff --git a/test/controllers/recruiter_controller_test.rb b/test/controllers/recruiter_controller_test.rb index 6108584..5a02765 100644 --- a/test/controllers/recruiter_controller_test.rb +++ b/test/controllers/recruiter_controller_test.rb @@ -11,6 +11,14 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest assert_response :success end + test 'should logout and reset session' do + setup_auth + get recruiter_logout_path + + assert :success + assert session[:user].nil? + end + test "should require auth or redirect" do get recruiter_url assert_redirected_to recruiter_login_path @@ -55,8 +63,8 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest end test "should create new candidate" do - # recruiter = users(:recruiter) setup_auth + assert_difference("ActionMailer::Base.deliveries.size", 2) do assert_difference("Candidate.count") do post create_candidate_path, params: { candidate: @@ -66,4 +74,17 @@ class RecruiterControllerTest < ActionDispatch::IntegrationTest assert_redirected_to recruiter_path assert flash[:notice] end + + test "should fail creation with message" do + setup_auth + + assert_difference("ActionMailer::Base.deliveries.size", 0) do + assert_difference("Candidate.count", 0) do + post create_candidate_path, params: { candidate: + { name: 'new name', email: 'test@mailinatorcom', experience: '0-3', quiz_id: quizzes(:fed).id } } + end + end + assert :success + assert_match(/failed.*save/i, flash[:error]) + end end diff --git a/test/controllers/review_controller_test.rb b/test/controllers/review_controller_test.rb index aa6c188..12e92f3 100644 --- a/test/controllers/review_controller_test.rb +++ b/test/controllers/review_controller_test.rb @@ -52,4 +52,12 @@ class ReviewControllerTest < ActionDispatch::IntegrationTest get review_test_url(candidates(:richard).test_hash) assert_response :success end + + test 'should logout and reset session' do + setup_auth + get review_logout_path + + assert :success + assert session[:user].nil? + end end diff --git a/test/services/skill_config_test.rb b/test/services/skill_config_test.rb new file mode 100644 index 0000000..e72dfb5 --- /dev/null +++ b/test/services/skill_config_test.rb @@ -0,0 +1,17 @@ +require 'test_helper' + +class SkillConfigTest < ActiveSupport::TestCase + test "verify sample file exists" do + assert File.exist? "#{Rails.root}/config/application.yml.sample" + end + + test "verify config file exists" do + assert File.exist? "#{Rails.root}/config/application.yml" + end + + test 'config can load and return proper values' do + skonfig = SkillConfig.new + + assert_equal 'localhost', skonfig.mysql_host + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 5c9b571..a65e8ac 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,8 +3,9 @@ ENV['RAILS_ENV'] ||= 'test' # https://github.com/colszowka/simplecov require 'simplecov' SimpleCov.start 'rails' do - add_group 'Models', ['app/models', 'app/validators'] - add_group 'Services & Workers', ['app/workers', 'app/services'] + add_group 'Models', %w(app/models app/validators) + add_group 'Services & Workers', %w(app/workers app/services) + add_group "Jobs", 'app/jobs' end require File.expand_path('../../config/environment', __FILE__)