Convert age verification controller to system specs (#36026)

This commit is contained in:
Matt Jankowski 2025-09-05 08:44:09 -04:00 committed by GitHub
parent 3efba15b3c
commit 05a655f33e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 83 additions and 40 deletions

View File

@ -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

View File

@ -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

View File

@ -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