From 05a655f33ebe3f6f591f341561469edd9142561f Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 5 Sep 2025 08:44:09 -0400 Subject: [PATCH] Convert age verification controller to system specs (#36026) --- .../auth/registrations_controller_spec.rb | 36 ------------ spec/requests/api/v1/accounts_spec.rb | 29 ++++++++-- spec/system/auth/registrations_spec.rb | 58 +++++++++++++++++++ 3 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 spec/system/auth/registrations_spec.rb diff --git a/spec/controllers/auth/registrations_controller_spec.rb b/spec/controllers/auth/registrations_controller_spec.rb index a110717166d..04c2d5dbbbb 100644 --- a/spec/controllers/auth/registrations_controller_spec.rb +++ b/spec/controllers/auth/registrations_controller_spec.rb @@ -342,42 +342,6 @@ RSpec.describe Auth::RegistrationsController do end end - context 'when age verification is enabled' do - subject { post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' }.merge(date_of_birth) } } - - before do - Setting.min_age = 16 - end - - let(:date_of_birth) { {} } - - context 'when date of birth is below age limit' do - let(:date_of_birth) { 13.years.ago.then { |date| { 'date_of_birth(1i)': date.day.to_s, 'date_of_birth(2i)': date.month.to_s, 'date_of_birth(3i)': date.year.to_s } } } - - it 'does not create user' do - subject - user = User.find_by(email: 'test@example.com') - expect(user).to be_nil - end - end - - context 'when date of birth is above age limit' do - let(:date_of_birth) { 17.years.ago.then { |date| { 'date_of_birth(1i)': date.day.to_s, 'date_of_birth(2i)': date.month.to_s, 'date_of_birth(3i)': date.year.to_s } } } - - it 'redirects to setup and creates user' do - subject - - expect(response).to redirect_to auth_setup_path - - expect(User.find_by(email: 'test@example.com')) - .to be_present - .and have_attributes( - age_verified_at: not_eq(nil) - ) - end - end - end - it_behaves_like 'registration mode based responses', :create end diff --git a/spec/requests/api/v1/accounts_spec.rb b/spec/requests/api/v1/accounts_spec.rb index a040174d385..0e64915baf3 100644 --- a/spec/requests/api/v1/accounts_spec.rb +++ b/spec/requests/api/v1/accounts_spec.rb @@ -110,9 +110,12 @@ RSpec.describe '/api/v1/accounts' do let(:date_of_birth) { 13.years.ago.strftime('%d.%m.%Y') } it 'returns http unprocessable entity' do - subject + expect { subject } + .to not_change(User, :count) + .and not_change(Account, :count) - expect(response).to have_http_status(422) + expect(response) + .to have_http_status(422) expect(response.content_type) .to start_with('application/json') end @@ -122,9 +125,27 @@ RSpec.describe '/api/v1/accounts' do let(:date_of_birth) { 17.years.ago.strftime('%d.%m.%Y') } it 'creates a user', :aggregate_failures do - subject + expect { subject } + .to change(User, :count).by(1) + .and change(Account, :count).by(1) - expect(response).to have_http_status(200) + expect(response) + .to have_http_status(200) + expect(response.content_type) + .to start_with('application/json') + end + end + + context 'when date of birth is over age limit in ISO-8601 format' do + let(:date_of_birth) { 17.years.ago.to_date.iso8601 } + + it 'creates a user', :aggregate_failures do + expect { subject } + .to change(User, :count).by(1) + .and change(Account, :count).by(1) + + expect(response) + .to have_http_status(200) expect(response.content_type) .to start_with('application/json') end diff --git a/spec/system/auth/registrations_spec.rb b/spec/system/auth/registrations_spec.rb new file mode 100644 index 00000000000..4c08bf47eea --- /dev/null +++ b/spec/system/auth/registrations_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Auth Registration' do + context 'when age verification is enabled' do + before { Setting.min_age = 16 } + + context 'when date of birth is below age limit' do + let(:date_of_birth) { 13.years.ago } + + it 'does not create user record and displays errors' do + visit new_user_registration_path + expect(page) + .to have_title(I18n.t('auth.register')) + + expect { fill_in_and_submit_form } + .to not_change(User, :count) + expect(page) + .to have_content(/error below/) + end + end + + context 'when date of birth is above age limit' do + let(:date_of_birth) { 17.years.ago } + + it 'creates user and marks as verified' do + visit new_user_registration_path + expect(page) + .to have_title(I18n.t('auth.register')) + + expect { fill_in_and_submit_form } + .to change(User, :count).by(1) + expect(User.last) + .to have_attributes(email: 'test@example.com', age_verified_at: be_present) + expect(page) + .to have_content(I18n.t('auth.setup.title')) + end + end + + def fill_in_and_submit_form + # Avoid the registration spam check + travel_to 10.seconds.from_now + + fill_in 'user_account_attributes_username', with: 'test' + fill_in 'user_email', with: 'test@example.com' + fill_in 'user_password', with: 'Test.123.Pass' + fill_in 'user_password_confirmation', with: 'Test.123.Pass' + check 'user_agreement' + + find('input[aria-label="Day"]').fill_in with: date_of_birth.day + find('input[autocomplete="bday-month"]').fill_in with: date_of_birth.month + find('input[autocomplete="bday-year"]').fill_in with: date_of_birth.year + + click_on I18n.t('auth.register') + end + end +end