2023-02-22 02:55:31 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-27 17:07:59 +02:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 06:49:53 +03:00
|
|
|
RSpec.describe Invite do
|
2017-11-27 17:07:59 +02:00
|
|
|
describe '#valid_for_use?' do
|
|
|
|
it 'returns true when there are no limitations' do
|
2019-07-26 19:55:33 +03:00
|
|
|
invite = Fabricate(:invite, max_uses: nil, expires_at: nil)
|
2017-11-27 17:07:59 +02:00
|
|
|
expect(invite.valid_for_use?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when not expired' do
|
2019-07-26 19:55:33 +03:00
|
|
|
invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.from_now)
|
2017-11-27 17:07:59 +02:00
|
|
|
expect(invite.valid_for_use?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when expired' do
|
2019-07-26 19:55:33 +03:00
|
|
|
invite = Fabricate(:invite, max_uses: nil, expires_at: 1.hour.ago)
|
2017-11-27 17:07:59 +02:00
|
|
|
expect(invite.valid_for_use?).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true when uses still available' do
|
2019-07-26 19:55:33 +03:00
|
|
|
invite = Fabricate(:invite, max_uses: 250, uses: 249, expires_at: nil)
|
2017-11-27 17:07:59 +02:00
|
|
|
expect(invite.valid_for_use?).to be true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when maximum uses reached' do
|
2019-07-26 19:55:33 +03:00
|
|
|
invite = Fabricate(:invite, max_uses: 250, uses: 250, expires_at: nil)
|
|
|
|
expect(invite.valid_for_use?).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when invite creator has been disabled' do
|
|
|
|
invite = Fabricate(:invite, max_uses: nil, expires_at: nil)
|
2020-09-15 15:37:58 +03:00
|
|
|
invite.user.account.suspend!
|
2017-11-27 17:07:59 +02:00
|
|
|
expect(invite.valid_for_use?).to be false
|
|
|
|
end
|
|
|
|
end
|
2023-07-05 10:14:00 +03:00
|
|
|
|
|
|
|
context 'when th_use_invite_quota?' do
|
|
|
|
let(:max_uses) { 25 }
|
|
|
|
let(:expires_in) { 1.week.in_seconds }
|
|
|
|
let(:regular_user) { Fabricate(:user) }
|
|
|
|
let(:moderator_user) { Fabricate(:user, moderator: true) }
|
|
|
|
let(:user) { regular_user }
|
|
|
|
let(:created_at) { Time.at(0) }
|
|
|
|
let(:expires_at) { Time.at(0) + expires_in }
|
|
|
|
|
|
|
|
subject { Fabricate.build(:invite, user: user, max_uses: max_uses, created_at: created_at, expires_at: expires_at ) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_const('Invite::TH_USE_INVITE_QUOTA', true)
|
|
|
|
stub_const('Invite::TH_INVITE_MAX_USES', 25)
|
|
|
|
stub_const('Invite::TH_ACTIVE_INVITE_SLOT_QUOTA', 30)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
|
|
|
|
context 'and' do
|
|
|
|
context 'max_uses exceeds quota' do
|
|
|
|
let(:max_uses) { 26 }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'expires_in exceeds quota' do
|
|
|
|
let(:expires_in) { 1.week.in_seconds + 1 }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'multiple values exceed quota' do
|
|
|
|
let(:max_uses) { 26 }
|
|
|
|
let(:expires_in) { 86401 }
|
|
|
|
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'an unlimited use invite' do
|
|
|
|
before do
|
|
|
|
Fabricate.build(:invite, user: user).save(validate: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'too many outstanding invites' do
|
|
|
|
before do
|
|
|
|
Fabricate.build(:invite, user: user, max_uses: 6).save(validate: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.not_to be_valid }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'a moderator created the invite' do
|
|
|
|
let(:user) { moderator_user }
|
|
|
|
|
|
|
|
it { is_expected.to be_valid }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-11-27 17:07:59 +02:00
|
|
|
end
|