diff --git a/app/controllers/quizzes/quizzes_controller.rb b/app/controllers/quizzes/quizzes_controller.rb index c1a8e59281d7..cdf005ae32f4 100644 --- a/app/controllers/quizzes/quizzes_controller.rb +++ b/app/controllers/quizzes/quizzes_controller.rb @@ -869,9 +869,13 @@ def can_take_quiz? remote_ip: request.remote_ip, access_code: params[:access_code]) - reason = can_take.declined_reason_renders - render reason if reason - can_take.eligible? + if params[:take] + reason = can_take.declined_reason_renders + render reason if reason + can_take.eligible? + else + can_take.potentially_eligible? + end end def quiz_submission_active? diff --git a/app/models/quizzes/quiz_eligibility.rb b/app/models/quizzes/quiz_eligibility.rb index cc200bd546de..7a0ad9f9a09a 100644 --- a/app/models/quizzes/quiz_eligibility.rb +++ b/app/models/quizzes/quiz_eligibility.rb @@ -32,13 +32,17 @@ def initialize(args = {}) store_session_access_code(args[:access_code]) if args[:access_code] end - def eligible? + def potentially_eligible? return true if quiz.grants_right?(user, session, :manage) return false unless course return false if inactive_student_with_private_course? !(course_restrictions_apply? || user_restrictions_apply?) end + def eligible? + potentially_eligible? && !quiz_restrictions_apply? + end + def declined_reason_renders return :access_code if need_access_code? return :invalid_ip if invalid_ip? @@ -66,8 +70,11 @@ def course_restrictions_apply? end def user_restrictions_apply? - return true if inactive_non_admin? || need_access_code? || invalid_ip? - !quiz.grants_right?(user, session, :submit) + inactive_non_admin? || !quiz.grants_right?(user, session, :submit) + end + + def quiz_restrictions_apply? + need_access_code? || invalid_ip? end def restricted_by_date? diff --git a/spec/controllers/quizzes/quizzes_controller_spec.rb b/spec/controllers/quizzes/quizzes_controller_spec.rb index a85e3dff084c..274bbccebe2d 100644 --- a/spec/controllers/quizzes/quizzes_controller_spec.rb +++ b/spec/controllers/quizzes/quizzes_controller_spec.rb @@ -1479,7 +1479,10 @@ def logged_out_survey_with_submission(user, questions, &block) context "when access code is required but does not match" do before do @quiz.stubs(:access_code).returns("trust me. *winks*") - subject.stubs(:params).returns({:access_code => "Don't trust me. *tips hat*"}) + subject.stubs(:params).returns({ + :access_code => "Don't trust me. *tips hat*", + :take => 1 + }) end it "renders access_code template" do @@ -1503,6 +1506,7 @@ def logged_out_survey_with_submission(user, questions, &block) before do @quiz.stubs(:ip_filter).returns(true) @quiz.stubs(:valid_ip?).returns(false) + subject.stubs(:params).returns({:take => 1}) end it "renders invalid_ip" do diff --git a/spec/models/quizzes/quiz_eligibility_spec.rb b/spec/models/quizzes/quiz_eligibility_spec.rb index 0a549b202e72..1db0ffe380ea 100644 --- a/spec/models/quizzes/quiz_eligibility_spec.rb +++ b/spec/models/quizzes/quiz_eligibility_spec.rb @@ -41,26 +41,43 @@ quiz.stubs(:grants_right?) .with(anything, anything, :manage).returns(true) expect(eligibility.eligible?).to be_truthy + expect(eligibility.potentially_eligible?).to be_truthy end it "returns false if no course is provided" do eligibility.stubs(:course).returns(nil) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end it "returns false if the student is inactive" do user.stubs(:workflow_state).returns("deleted") expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end it "returns false if a user cannot read as an admin" do user.stubs(:new_record?).returns(false) course.stubs(:grants_right?).returns(false) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey + end + + it "returns false if a quiz is access code restricted (but is still potentially_eligible)" do + quiz.access_code = 'x' + expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_truthy + end + + it "returns false if a quiz is ip restricted (but is still potentially_eligible)" do + quiz.ip_filter = '1.1.1.1' + expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_truthy end it "otherwise returns true" do expect(eligibility.eligible?).to be_truthy + expect(eligibility.potentially_eligible?).to be_truthy end describe "date-based overrides" do @@ -86,6 +103,7 @@ eligibility.stubs(:course).returns(concluded_course) eligibility.stubs(:course_section).returns(concluded_section) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end it "returns false if active course because term > course" do @@ -94,6 +112,7 @@ restricted_active_course.stubs(:enrollment_term).returns(concluded_term) eligibility.stubs(:course_section).returns(concluded_section) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end it "returns true if active course overrides" do @@ -102,6 +121,7 @@ restricted_active_course.stubs(:enrollment_term).returns(concluded_term) eligibility.stubs(:course_section).returns(concluded_section) expect(eligibility.eligible?).to be_truthy + expect(eligibility.potentially_eligible?).to be_truthy end it "returns false and ignores section" do @@ -109,6 +129,7 @@ eligibility.stubs(:course).returns(concluded_course) eligibility.stubs(:course_section).returns(active_section) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end it "returns false and ignores section" do @@ -116,6 +137,7 @@ eligibility.stubs(:course).returns(concluded_course) eligibility.stubs(:course_section).returns(active_section) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end it "returns true if active section overrides" do @@ -124,6 +146,7 @@ concluded_course.stubs(:enrollment_term).returns(concluded_term) eligibility.stubs(:course_section).returns(restricted_active_section) expect(eligibility.eligible?).to be_truthy + expect(eligibility.potentially_eligible?).to be_truthy end end @@ -134,6 +157,7 @@ restricted_concluded_course.stubs(:enrollment_term).returns(active_term) eligibility.stubs(:course_section).returns(active_section) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end it "returns true if active section overrides" do @@ -141,6 +165,7 @@ eligibility.stubs(:course).returns(restricted_concluded_course) eligibility.stubs(:course_section).returns(restricted_active_section) expect(eligibility.eligible?).to be_truthy + expect(eligibility.potentially_eligible?).to be_truthy end end @@ -150,6 +175,7 @@ eligibility.stubs(:course).returns(concluded_course) eligibility.stubs(:course_section).returns(concluded_section) expect(eligibility.eligible?).to be_truthy + expect(eligibility.potentially_eligible?).to be_truthy end it "returns false if term is closed with no respect for section" do @@ -157,8 +183,11 @@ eligibility.stubs(:course).returns(concluded_course) eligibility.stubs(:course_section).returns(active_section) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey + eligibility.stubs(:course_section).returns(concluded_section) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end it "returns true if active section overrides" do @@ -166,6 +195,7 @@ eligibility.stubs(:course).returns(concluded_course) eligibility.stubs(:course_section).returns(restricted_active_section) expect(eligibility.eligible?).to be_truthy + expect(eligibility.potentially_eligible?).to be_truthy end end @@ -175,6 +205,7 @@ eligibility.stubs(:course).returns(active_course) eligibility.stubs(:course_section).returns(concluded_section) expect(eligibility.eligible?).to be_truthy + expect(eligibility.potentially_eligible?).to be_truthy end it "returns false if section overrides" do @@ -182,6 +213,7 @@ eligibility.stubs(:course).returns(active_course) eligibility.stubs(:course_section).returns(restricted_concluded_section) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end end @@ -190,6 +222,7 @@ eligibility.stubs(:course).returns(restricted_active_course) eligibility.stubs(:course_section).returns(restricted_concluded_section) expect(eligibility.eligible?).to be_falsey + expect(eligibility.potentially_eligible?).to be_falsey end end end