Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app/services/sessions/hca_login_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,20 @@ def hca_age_attestation(user, fields)
return nil if fields[:birthday].nil?

age = age_from_birthday(fields[:birthday])
age >= 13 && age <= 18 ? :teen : :ineligible
return :teen if age >= 13 && age <= 18

if user.persisted? && user.age_attestation_teen_13_18?
signup_age = age_on_date(fields[:birthday], user.created_at.to_date)
return :teen if signup_age >= 13 && signup_age <= 18
end

:ineligible
end

def age_on_date(birthday, date)
age = date.year - birthday.year
age -= 1 if date < birthday + age.years
age
end

def age_ineligible_result(user, is_new_user, guest_collision)
Expand Down
48 changes: 48 additions & 0 deletions test/services/sessions/hca_login_service_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "test_helper"

class Sessions::HCALoginServiceTest < ActiveSupport::TestCase
setup do
@service = Sessions::HCALoginService.new(auth: nil, current_user: nil)
end

test "hca_age_attestation returns teen for user who was 18 at signup and is now 19" do
user = users(:one)
signup_date = Date.current - 1.year
birthday = Date.current - 19.years

user.update_columns(age_attestation: "teen_13_18", created_at: signup_date.to_time)

result = @service.send(:hca_age_attestation, user, { ysws_eligible: false, birthday: birthday })

assert_equal :teen, result, "user who was 18 at signup should not be blocked after turning 19"
end

test "hca_age_attestation returns ineligible for user who was always over 18 at signup" do
user = users(:one)
birthday = Date.current - 26.years

user.update_columns(age_attestation: "teen_13_18", created_at: Time.current)

result = @service.send(:hca_age_attestation, user, { ysws_eligible: false, birthday: birthday })

assert_equal :ineligible, result
end

test "hca_age_attestation returns teen when ysws_eligible is true regardless of age" do
user = users(:one)
birthday = Date.current - 26.years

result = @service.send(:hca_age_attestation, user, { ysws_eligible: true, birthday: birthday })

assert_equal :teen, result
end

test "hca_age_attestation returns ineligible for new user with birthday over 18" do
user = User.new

birthday = Date.current - 26.years
result = @service.send(:hca_age_attestation, user, { ysws_eligible: false, birthday: birthday })

assert_equal :ineligible, result
end
end