Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require US state during account signup and on update #58697

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions dashboard/app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def set_student_information
student_information[:us_state] = params[:user][:us_state] unless current_user.user_provided_us_state
student_information[:user_provided_us_state] = params[:user][:us_state].present? unless current_user.user_provided_us_state
student_information[:gender_student_input] = params[:user][:gender_student_input] if current_user.gender.blank?
student_information[:country_code] = params[:user][:country_code] if current_user.country_code.blank?

current_user.update(student_information) unless student_information.empty?
end
Expand Down
2 changes: 1 addition & 1 deletion dashboard/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class User < ApplicationRecord
self.gender = Policies::Gender.normalize gender
end

validate :validate_us_state, if: -> {us_state.present?}
validate :validate_us_state

before_create unless: -> {Policies::ChildAccount.compliant?(self)} do
Services::ChildAccount.lock_out(self)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- location = Geocoder.search(request.ip).try(:first)
- country_code = location&.country_code.to_s.upcase
.modal#student-information-modal{'data-backdrop' => 'static', 'data-keyboard' => 'false', style: 'display: none'}
.modal-dialog
.modal-content.no-modal-icon
Expand All @@ -19,6 +21,7 @@
.form-group
%div{style: 'display: flex;'}
= f.label :us_state, t('activerecord.attributes.user.us_state'), class: 'label-title'
= f.hidden_field :country_code, value: country_code
.state-required *
= f.select :us_state, us_state_options, {include_blank: t('school_info.select_state')}, required: true
.required-message.state-required= t('activerecord.attributes.user.required')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class SetStudentInformationTest < ActionDispatch::IntegrationTest

test "set_student_information does nothing if user age, state and gender are already set" do
User.any_instance.expects(:update).never
student = create :student, age: 18, us_state: 'AL', gender_student_input: 'he', user_provided_us_state: true
student = create :student, age: 18, us_state: 'AL', gender_student_input: 'he', user_provided_us_state: true, country_code: 'US'
assert_equal 18, student.age
assert_equal 'AL', student.us_state
assert_equal 'm', student.gender
assert_equal 'US', student.country_code

sign_in student
patch '/users/set_student_information', params: {user: {age: '20', us_state: 'AK', gender_student_input: 'They'}}
Expand All @@ -26,23 +27,26 @@ class SetStudentInformationTest < ActionDispatch::IntegrationTest
assert_equal 18, student.age
assert_equal 'AL', student.us_state
assert_equal 'm', student.gender
assert_equal 'US', student.country_code
end

test "set_student_information updates state if user has not provided state" do
student = create :student, age: 18, us_state: 'AL', gender_student_input: 'he', user_provided_us_state: false
assert_equal 18, student.age
assert_equal 'AL', student.us_state
assert_equal 'm', student.gender
assert_nil student.country_code

sign_in student
patch '/users/set_student_information', params: {user: {us_state: 'AK'}}
patch '/users/set_student_information', params: {user: {us_state: 'AK', country_code: 'US'}}
assert_response :success

student.reload
assert_equal 18, student.age
assert_equal 'AK', student.us_state
assert_equal 'm', student.gender
assert_equal true, student.user_provided_us_state
assert_equal 'US', student.country_code
end

test "set_student_information sets age if user is only shown age option" do
Expand All @@ -55,21 +59,22 @@ class SetStudentInformationTest < ActionDispatch::IntegrationTest

student.reload
assert_equal 20, student.age
assert_equal nil, student.user_provided_us_state
assert_nil student.user_provided_us_state
end

test "set_student_information sets age if user is signed in and age is blank" do
test "set_student_information sets age and state if user is signed in and age and state is blank" do
student = create :student_in_picture_section, birthday: nil
assert student.age.blank?

sign_in student
patch '/users/set_student_information', params: {user: {age: '20', us_state: 'AL', gender_student_input: 'he'}}
patch '/users/set_student_information', params: {user: {age: '20', us_state: 'AL', gender_student_input: 'he', country_code: 'US'}}
assert_response :success

student.reload
assert_equal 20, student.age
assert_equal 'AL', student.us_state
assert_equal 'm', student.gender
assert_equal 'US', student.country_code
end
end
end
18 changes: 18 additions & 0 deletions dashboard/test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5286,4 +5286,22 @@ def hide_scripts_in_sections(section1, section2)
student.update!(username: "very big husky")
end
end

test 'validate_us_state' do
# If we don't know what country they are in, we don't require US State.
create :student
create :student, country_code: "CO"
# If the student is in the US, they must tell us what US State they live in
assert_raises(ActiveRecord::RecordInvalid) do
create :student, country_code: "US"
end
assert_raises(ActiveRecord::RecordInvalid) do
create :student, country_code: "US", us_state: 'INVALID_STATE'
end
student = create :student, country_code: "US", us_state: 'CO'
assert_raises(ActiveRecord::RecordInvalid) do
student.update!(us_state: 'INVALID_STATE')
end
student.update!(us_state: 'WA')
end
end