Add: #348 新規登録の上限人数 (#527)

* Add: #348 新規登録の上限人数

* Fix test

* Fix test

* Wip

* Fix test

* Add invite support

* Wip

* Fix test

* Fix test

* Fix test
This commit is contained in:
KMY(雪あすか) 2024-02-12 22:05:32 +09:00 committed by GitHub
parent d7cc6b788c
commit e317edecb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 362 additions and 6 deletions

View file

@ -280,6 +280,164 @@ RSpec.describe Auth::RegistrationsController do
end
end
context 'when max user count is set' do
subject do
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
end
let(:users_max) { 3 }
before do
Fabricate(:user)
Fabricate(:user)
Setting.registrations_mode = 'open'
Setting.registrations_limit = users_max
request.headers['Accept-Language'] = accept_language
end
it 'redirects to setup' do
subject
expect(response).to redirect_to auth_setup_path
end
it 'creates user' do
subject
user = User.find_by(email: 'test@example.com')
expect(user).to_not be_nil
expect(user.locale).to eq(accept_language)
end
context 'when limit is reached' do
let(:users_max) { 2 }
it 'does not create user' do
subject
user = User.find_by(email: 'test@example.com')
expect(user).to be_nil
end
context 'with invite' do
subject do
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', invite_code: invite.code, agreement: 'true' } }
end
let(:invite) { Fabricate(:invite) }
it 'creates user' do
subject
user = User.find_by(email: 'test@example.com')
expect(user).to_not be_nil
expect(user.locale).to eq(accept_language)
end
end
end
end
context 'when max user count per day is set' do
subject do
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
end
let(:users_max) { 2 }
let(:created_at) { Time.now.utc }
let(:precreate_users) { true }
before do
Fabricate(:user, created_at: created_at) if precreate_users
Setting.registrations_mode = 'open'
Setting.registrations_limit_per_day = users_max
request.headers['Accept-Language'] = accept_language
end
it 'creates user' do
subject
user = User.find_by(email: 'test@example.com')
expect(user).to_not be_nil
expect(user.locale).to eq(accept_language)
end
context 'when limit is reached' do
let(:users_max) { 2 }
let(:created_at) { Time.now.utc - 1.day }
let(:precreate_users) { false }
before do
travel_to Time.now.utc - 1.day
Fabricate(:user)
create_other_user
end
it 'does not create user yesterday' do
subject
user = User.find_by(email: 'test@example.com')
expect(user).to be_nil
end
it 'creates user' do
travel_to Time.now.utc + 1.day
subject
user = User.find_by(email: 'test@example.com')
expect(user).to_not be_nil
expect(user.locale).to eq(accept_language)
end
end
def create_other_user
post :create, params: { user: { account_attributes: { username: 'ohagi' }, email: 'test@ohagi.com', password: 'ohagi_must_be_tsubuan', password_confirmation: 'ohagi_must_be_tsubuan', agreement: 'true' } }
end
end
context 'when registration time range is set' do
subject do
post :create, params: { user: { account_attributes: { username: 'test' }, email: 'test@example.com', password: '12345678', password_confirmation: '12345678', agreement: 'true' } }
end
shared_examples 'registration with time' do |header, start_hour_val, end_hour_val, secondary_start_hour_val, secondary_end_hour_val, result| # rubocop:disable Metrics/ParameterLists
context header do
let(:start_hour) { start_hour_val }
let(:end_hour) { end_hour_val }
let(:secondary_start_hour) { secondary_start_hour_val }
let(:secondary_end_hour) { secondary_end_hour_val }
before do
Setting.registrations_mode = 'open'
Setting.registrations_start_hour = start_hour
Setting.registrations_end_hour = end_hour
Setting.registrations_secondary_start_hour = secondary_start_hour
Setting.registrations_secondary_end_hour = secondary_end_hour
request.headers['Accept-Language'] = accept_language
travel_to Time.now.utc.beginning_of_day + 10.hours
end
if result
it 'creates user' do
subject
user = User.find_by(email: 'test@example.com')
expect(user).to_not be_nil
expect(user.locale).to eq(accept_language)
end
else
it 'does not create user' do
subject
user = User.find_by(email: 'test@example.com')
expect(user).to be_nil
end
end
end
end
it_behaves_like 'registration with time', 'time range is not set', 0, 24, 0, 0, true
it_behaves_like 'registration with time', 'time range is set', 9, 12, 0, 0, true
it_behaves_like 'registration with time', 'time range is out of range', 12, 15, 0, 0, false
it_behaves_like 'registration with time', 'time range is invalid', 20, 15, 0, 0, true
it_behaves_like 'registration with time', 'secondary time range is set', 0, 4, 9, 12, true
it_behaves_like 'registration with time', 'secondary time range is out of range', 0, 4, 12, 15, false
it_behaves_like 'registration with time', 'secondary time range is invalid', 0, 4, 20, 15, false
it_behaves_like 'registration with time', 'both time range are invalid', 4, 0, 20, 15, true
it_behaves_like 'registration with time', 'only secondary time range is set', 0, 0, 9, 12, true
end
include_examples 'checks for enabled registrations', :create
end

View file

@ -233,6 +233,38 @@ RSpec.describe User do
expect(TriggerWebhookWorker).to_not have_received(:perform_async).with('account.approved', 'Account', user.account_id)
end
end
context 'when max user count is set' do
let(:users_max) { 3 }
before do
Fabricate(:user)
Fabricate(:user)
Setting.registrations_limit = users_max
end
it 'creates user' do
expect { subject }.to_not raise_error
expect(user.confirmed?).to be true
end
context 'with limit is reached' do
let(:users_max) { 2 }
it 'does not create user' do
expect { subject }.to raise_error Mastodon::ValidationError
expect(user.confirmed?).to be false
end
it 'but creates user when invited' do
invite = Fabricate(:invite, user: Fabricate(:user), max_uses: nil, expires_at: 1.hour.from_now)
user.update!(invite: invite)
expect { subject }.to_not raise_error
expect(user.confirmed?).to be true
end
end
end
end
end