Fix tootctl accounts create failing because of date-of-birth check (#34663)

This commit is contained in:
Claire 2025-05-12 11:28:12 +02:00 committed by GitHub
parent 3357ae9889
commit 4e2aa78a56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 7 deletions

View file

@ -112,7 +112,7 @@ class User < ApplicationRecord
validates_with RegistrationFormTimeValidator, on: :create
validates :website, absence: true, on: :create
validates :confirm_password, absence: true, on: :create
validates :date_of_birth, presence: true, date_of_birth: true, on: :create, if: -> { Setting.min_age.present? }
validates :date_of_birth, presence: true, date_of_birth: true, on: :create, if: -> { Setting.min_age.present? && !bypass_registration_checks? }
validate :validate_role_elevation
scope :account_not_suspended, -> { joins(:account).merge(Account.without_suspended) }
@ -144,7 +144,7 @@ class User < ApplicationRecord
delegate :can?, to: :role
attr_reader :invite_code, :date_of_birth
attr_writer :external, :bypass_invite_request_check, :current_account
attr_writer :external, :bypass_registration_checks, :current_account
def self.those_who_can(*any_of_privileges)
matching_role_ids = UserRole.that_can(*any_of_privileges).map(&:id)
@ -501,8 +501,8 @@ class User < ApplicationRecord
!!@external
end
def bypass_invite_request_check?
@bypass_invite_request_check
def bypass_registration_checks?
@bypass_registration_checks
end
def sanitize_role
@ -549,7 +549,7 @@ class User < ApplicationRecord
end
def invite_text_required?
Setting.require_invite_text && !open_registrations? && !invited? && !external? && !bypass_invite_request_check?
Setting.require_invite_text && !open_registrations? && !invited? && !external? && !bypass_registration_checks?
end
def trigger_webhooks

View file

@ -79,7 +79,14 @@ module Mastodon::CLI
account = Account.new(username: username)
password = SecureRandom.hex
user = User.new(email: options[:email], password: password, agreement: true, role_id: role_id, confirmed_at: options[:confirmed] ? Time.now.utc : nil, bypass_invite_request_check: true)
user = User.new(
email: options[:email],
password: password,
agreement: true,
role_id: role_id,
confirmed_at: options[:confirmed] ? Time.now.utc : nil,
bypass_registration_checks: true
)
if options[:reattach]
account = Account.find_local(username) || Account.new(username: username)

View file

@ -552,7 +552,7 @@ namespace :mastodon do
password = SecureRandom.hex(16)
owner_role = UserRole.find_by(name: 'Owner')
user = User.new(email: email, password: password, confirmed_at: Time.now.utc, account_attributes: { username: username }, bypass_invite_request_check: true, role: owner_role)
user = User.new(email: email, password: password, confirmed_at: Time.now.utc, account_attributes: { username: username }, bypass_registration_checks: true, role: owner_role)
user.save(validate: false)
user.approve!

View file

@ -70,6 +70,25 @@ RSpec.describe Mastodon::CLI::Accounts do
end
end
context 'with min_age setting' do
let(:options) { { email: 'tootctl@example.com', confirmed: true } }
before do
Setting.min_age = 42
end
it_behaves_like 'a new user with given email address and username'
it 'creates a new user with confirmed status' do
expect { subject }
.to output_results('New password')
user = User.find_by(email: options[:email])
expect(user.confirmed?).to be(true)
end
end
context 'with --confirmed option' do
let(:options) { { email: 'tootctl@example.com', confirmed: true } }