2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
describe Invite do
|
2021-03-25 11:26:22 -05:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2022-01-18 06:38:31 -06:00
|
|
|
let(:xss_email) { "<b onmouseover=alert('wufff!')>email</b><script>alert('test');</script>@test.com" }
|
|
|
|
let(:escaped_email) { "<b onmouseover=alert('wufff!')>email</b><script>alert('test');</script>@test.com" }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'validators' do
|
|
|
|
it { is_expected.to validate_presence_of :invited_by_id }
|
|
|
|
it { is_expected.to rate_limit }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'allows invites with valid emails' do
|
|
|
|
invite = Fabricate.build(:invite, email: 'test@example.com', invited_by: user)
|
|
|
|
expect(invite).to be_valid
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2019-02-06 11:08:06 -06:00
|
|
|
|
2022-01-18 06:38:31 -06:00
|
|
|
it 'escapes the invalid email before attaching the error message' do
|
|
|
|
invite = Fabricate.build(:invite, email: xss_email)
|
|
|
|
|
2020-06-02 21:13:25 -05:00
|
|
|
expect(invite.valid?).to eq(false)
|
2022-01-18 06:38:31 -06:00
|
|
|
expect(invite.errors.full_messages).to include(I18n.t('invite.invalid_email', email: escaped_email))
|
2019-02-06 11:08:06 -06:00
|
|
|
end
|
2014-10-09 09:44:15 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'does not allow an invite with the same email as an existing user' do
|
|
|
|
invite = Fabricate.build(:invite, email: Fabricate(:user).email, invited_by: user)
|
|
|
|
expect(invite).not_to be_valid
|
|
|
|
|
|
|
|
invite = Fabricate.build(:invite, email: user.email, invited_by: user)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(invite).not_to be_valid
|
2014-10-09 09:44:15 -05:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'does not allow an invite with blocked email' do
|
|
|
|
invite = Invite.create(email: 'test@mailinator.com', invited_by: user)
|
|
|
|
expect(invite).not_to be_valid
|
2014-10-09 09:44:15 -05:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'does not allow an invalid email address' do
|
2020-06-02 21:13:25 -05:00
|
|
|
invite = Fabricate.build(:invite, email: 'asjdso')
|
|
|
|
expect(invite.valid?).to eq(false)
|
2021-04-15 06:46:32 -05:00
|
|
|
expect(invite.errors.full_messages).to include(I18n.t('invite.invalid_email', email: invite.email))
|
2020-06-02 21:13:25 -05:00
|
|
|
end
|
2021-12-12 23:39:14 -06:00
|
|
|
|
|
|
|
it 'allows only valid domains' do
|
|
|
|
invite = Fabricate.build(:invite, domain: 'example', invited_by: user)
|
|
|
|
expect(invite).not_to be_valid
|
|
|
|
|
|
|
|
invite = Fabricate.build(:invite, domain: 'example.com', invited_by: user)
|
|
|
|
expect(invite).to be_valid
|
|
|
|
end
|
2014-10-09 09:44:15 -05:00
|
|
|
end
|
|
|
|
|
2021-04-14 04:15:56 -05:00
|
|
|
context 'before_save' do
|
|
|
|
it 'regenerates the email token when email is changed' do
|
|
|
|
invite = Fabricate(:invite, email: 'test@example.com')
|
|
|
|
token = invite.email_token
|
|
|
|
|
|
|
|
invite.update!(email: 'test@example.com')
|
|
|
|
expect(invite.email_token).to eq(token)
|
|
|
|
|
|
|
|
invite.update!(email: 'test2@example.com')
|
|
|
|
expect(invite.email_token).not_to eq(token)
|
|
|
|
|
|
|
|
invite.update!(email: nil)
|
|
|
|
expect(invite.email_token).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.generate' do
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'saves an invites' do
|
|
|
|
invite = Invite.generate(user, email: 'TEST@EXAMPLE.COM')
|
|
|
|
expect(invite.invite_key).to be_present
|
|
|
|
expect(invite.email).to eq('test@example.com')
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'can succeed for staged users emails' do
|
|
|
|
Fabricate(:staged, email: 'test@example.com')
|
|
|
|
invite = Invite.generate(user, email: 'test@example.com')
|
|
|
|
expect(invite.email).to eq('test@example.com')
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'raises an error when inviting an existing user' do
|
|
|
|
expect { Invite.generate(user, email: user.email) }
|
|
|
|
.to raise_error(Invite::UserExists)
|
|
|
|
end
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2022-01-18 06:38:31 -06:00
|
|
|
it 'escapes the email_address when raising an existing user error' do
|
|
|
|
user.email = xss_email
|
|
|
|
user.save(validate: false)
|
|
|
|
|
|
|
|
expect { Invite.generate(user, email: user.email) }
|
|
|
|
.to raise_error(
|
|
|
|
Invite::UserExists,
|
|
|
|
I18n.t(
|
|
|
|
'invite.user_exists',
|
|
|
|
email: escaped_email, username: user.username, base_path: Discourse.base_path
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'via email' do
|
2021-04-14 04:15:56 -05:00
|
|
|
it 'can be created and a job is enqueued to email the invite' do
|
2021-03-25 11:26:22 -05:00
|
|
|
invite = Invite.generate(user, email: 'test@example.com')
|
2021-04-14 04:15:56 -05:00
|
|
|
expect(invite.email).to eq('test@example.com')
|
2021-03-25 11:26:22 -05:00
|
|
|
expect(invite.emailed_status).to eq(Invite.emailed_status_types[:sending])
|
2021-04-14 04:15:56 -05:00
|
|
|
expect(invite.email_token).not_to eq(nil)
|
2021-03-25 11:26:22 -05:00
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(1)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'can skip the job to email the invite' do
|
|
|
|
invite = Invite.generate(user, email: 'test@example.com', skip_email: true)
|
|
|
|
expect(invite.emailed_status).to eq(Invite.emailed_status_types[:not_required])
|
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(0)
|
2019-07-19 00:59:12 -05:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'can invite the same user after their invite was destroyed' do
|
|
|
|
Invite.generate(user, email: 'test@example.com').destroy!
|
|
|
|
invite = Invite.generate(user, email: 'test@example.com')
|
|
|
|
expect(invite).to be_present
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2021-03-25 11:26:22 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'via link' do
|
|
|
|
it 'does not enqueue a job to email the invite' do
|
|
|
|
invite = Invite.generate(user, skip_email: true)
|
|
|
|
expect(invite.emailed_status).to eq(Invite.emailed_status_types[:not_required])
|
|
|
|
expect(Jobs::InviteEmail.jobs.size).to eq(0)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'can be created' do
|
|
|
|
invite = Invite.generate(user, max_redemptions_allowed: 5)
|
|
|
|
expect(invite.max_redemptions_allowed).to eq(5)
|
|
|
|
expect(invite.expires_at.to_date).to eq(SiteSetting.invite_expiry_days.days.from_now.to_date)
|
|
|
|
expect(invite.emailed_status).to eq(Invite.emailed_status_types[:not_required])
|
|
|
|
expect(invite.is_invite_link?).to eq(true)
|
2021-04-14 04:15:56 -05:00
|
|
|
expect(invite.email_token).to eq(nil)
|
2021-03-25 11:26:22 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'checks for max_redemptions_allowed range' do
|
|
|
|
SiteSetting.invite_link_max_redemptions_limit_users = 3
|
|
|
|
expect { Invite.generate(user, max_redemptions_allowed: 4) }.to raise_error(ActiveRecord::RecordInvalid)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
SiteSetting.invite_link_max_redemptions_limit = 3
|
|
|
|
expect { Invite.generate(Fabricate(:admin), max_redemptions_allowed: 4) }.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
|
|
|
end
|
2018-12-10 16:24:02 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'when sending an invite to the same user' do
|
|
|
|
fab!(:invite) { Invite.generate(user, email: 'test@example.com') }
|
2019-12-17 09:13:49 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'returns the original invite' do
|
|
|
|
%w{test@EXAMPLE.com TEST@example.com}.each do |email|
|
|
|
|
expect(Invite.generate(user, email: email)).to eq(invite)
|
2019-12-17 09:13:49 -06:00
|
|
|
end
|
2021-03-25 11:26:22 -05:00
|
|
|
end
|
2019-12-17 09:13:49 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'updates timestamp of existing invite' do
|
|
|
|
freeze_time
|
|
|
|
invite.update!(created_at: 10.days.ago)
|
|
|
|
resend_invite = Invite.generate(user, email: 'test@example.com')
|
|
|
|
expect(resend_invite).to eq(invite)
|
|
|
|
expect(resend_invite.created_at).to eq_time(Time.zone.now)
|
|
|
|
end
|
2018-12-10 16:24:02 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'returns a new invite if the other has expired' do
|
|
|
|
SiteSetting.invite_expiry_days = 1
|
|
|
|
invite.update!(expires_at: 2.days.ago)
|
2018-12-10 16:24:02 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
new_invite = Invite.generate(user, email: 'test@example.com')
|
|
|
|
expect(new_invite).not_to eq(invite)
|
|
|
|
expect(new_invite).not_to be_expired
|
|
|
|
end
|
2018-12-10 16:24:02 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'returns a new invite when invited by a different user' do
|
|
|
|
invite = Invite.generate(user, email: 'test@example.com')
|
|
|
|
expect(invite.email).to eq('test@example.com')
|
2018-12-10 16:24:02 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
another_invite = Invite.generate(Fabricate(:user), email: 'test@example.com')
|
|
|
|
expect(another_invite.email).to eq('test@example.com')
|
2018-12-10 16:24:02 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
expect(invite.invite_key).not_to eq(another_invite.invite_key)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
2020-06-09 10:19:32 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'invite to a topic' do
|
|
|
|
fab!(:topic) { Fabricate(:topic) }
|
|
|
|
let(:invite) { Invite.generate(topic.user, email: 'test@example.com', topic: topic) }
|
2020-06-09 10:19:32 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'belongs to the topic' do
|
|
|
|
expect(topic.invites).to contain_exactly(invite)
|
|
|
|
expect(invite.topics).to contain_exactly(topic)
|
2020-06-09 10:19:32 -05:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'when adding to another topic' do
|
|
|
|
fab!(:another_topic) { Fabricate(:topic, user: topic.user) }
|
2020-06-09 10:19:32 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'should be the same invite' do
|
|
|
|
new_invite = Invite.generate(topic.user, email: 'test@example.com', topic: another_topic)
|
|
|
|
expect(invite).to eq(new_invite)
|
|
|
|
expect(invite.topics).to contain_exactly(topic, another_topic)
|
|
|
|
expect(topic.invites).to contain_exactly(invite)
|
|
|
|
expect(another_topic.invites).to contain_exactly(invite)
|
|
|
|
end
|
2020-06-09 10:19:32 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context '#redeem' do
|
|
|
|
fab!(:invite) { Fabricate(:invite) }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'works' do
|
|
|
|
user = invite.redeem
|
|
|
|
expect(invite.invited_users.map(&:user)).to contain_exactly(user)
|
|
|
|
expect(user.is_a?(User)).to eq(true)
|
|
|
|
expect(user.trust_level).to eq(SiteSetting.default_invitee_trust_level)
|
|
|
|
expect(user.send_welcome_message).to eq(true)
|
2018-01-19 08:29:15 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
expect(invite.reload.redemption_count).to eq(1)
|
|
|
|
expect(invite.redeem).to be_blank
|
2018-01-19 08:29:15 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-29 06:03:19 -05:00
|
|
|
it 'keeps custom fields' do
|
|
|
|
user_field = Fabricate(:user_field)
|
|
|
|
staged_user = Fabricate(:user, staged: true, email: invite.email)
|
|
|
|
staged_user.set_user_field(user_field.id, 'some value')
|
|
|
|
staged_user.save_custom_fields
|
|
|
|
|
|
|
|
expect(invite.redeem).to eq(staged_user)
|
|
|
|
expect(staged_user.reload.user_fields[user_field.id.to_s]).to eq('some value')
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
it 'creates a notification for the invitee' do
|
2021-03-25 11:26:22 -05:00
|
|
|
expect { invite.redeem }.to change { Notification.count }
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'does not work with expired invites' do
|
|
|
|
invite.update!(expires_at: 1.day.ago)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(invite.redeem).to be_blank
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'does not work with deleted invites' do
|
|
|
|
invite.trash!
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(invite.redeem).to be_blank
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'does not work with deleted invites' do
|
|
|
|
invite.destroy!
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(invite.redeem).to be_blank
|
2014-01-21 15:53:46 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'does not work with invalidated invites' do
|
|
|
|
invite.update(invalidated_at: 1.day.ago)
|
|
|
|
expect(invite.redeem).to be_blank
|
|
|
|
end
|
2015-03-25 11:55:18 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'deletes duplicate invite' do
|
|
|
|
another_invite = Fabricate(:invite, email: invite.email, invited_by: Fabricate(:user))
|
|
|
|
another_redeemed_invite = Fabricate(:invite, email: invite.email, invited_by: Fabricate(:user))
|
|
|
|
Fabricate(:invited_user, invite: another_redeemed_invite)
|
2015-03-25 11:55:18 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
user = invite.redeem
|
|
|
|
expect(user).not_to eq(nil)
|
|
|
|
expect(Invite.find_by(id: another_invite.id)).to eq(nil)
|
|
|
|
expect(Invite.find_by(id: another_redeemed_invite.id)).not_to eq(nil)
|
2015-03-25 11:55:18 -05:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'as a moderator' do
|
|
|
|
it 'will give the user a moderator flag' do
|
|
|
|
invite.update!(moderator: true, invited_by: Fabricate(:admin))
|
2016-09-20 12:12:00 -05:00
|
|
|
|
|
|
|
user = invite.redeem
|
|
|
|
expect(user).to be_moderator
|
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'will not give the user a moderator flag if the inviter is not staff' do
|
|
|
|
invite.update!(moderator: true)
|
2016-09-20 12:12:00 -05:00
|
|
|
|
|
|
|
user = invite.redeem
|
|
|
|
expect(user).not_to be_moderator
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'when inviting to groups' do
|
|
|
|
it 'add the user to the correct groups' do
|
2014-05-08 01:45:49 -05:00
|
|
|
group = Fabricate(:group)
|
2020-06-15 04:13:56 -05:00
|
|
|
group.add_owner(invite.invited_by)
|
2021-03-25 11:26:22 -05:00
|
|
|
invite.invited_groups.create!(group_id: group.id)
|
2014-05-08 01:45:49 -05:00
|
|
|
|
|
|
|
user = invite.redeem
|
2021-03-25 11:26:22 -05:00
|
|
|
expect(user.groups).to contain_exactly(group)
|
2014-05-08 01:45:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'activates user when must_approve_users? is enabled' do
|
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
invite.invited_by = Fabricate(:admin)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
user = invite.redeem
|
|
|
|
expect(user.approved?).to eq(true)
|
2013-07-10 20:21:39 -05:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'invite to a topic' do
|
|
|
|
fab!(:topic) { Fabricate(:private_message_topic) }
|
|
|
|
fab!(:another_topic) { Fabricate(:private_message_topic) }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
before do
|
|
|
|
invite.topic_invites.create!(topic: topic)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'adds the user to topic_users' do
|
|
|
|
invited_user = invite.redeem
|
|
|
|
expect(invited_user).not_to eq(nil)
|
|
|
|
expect(topic.reload.allowed_users.include?(invited_user)).to eq(true)
|
|
|
|
expect(Guardian.new(invited_user).can_see?(topic)).to eq(true)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
2021-03-25 11:26:22 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
describe '#redeem_from_email' do
|
|
|
|
fab!(:invite) { Fabricate(:invite, email: 'test@example.com') }
|
|
|
|
fab!(:user) { Fabricate(:user, email: invite.email) }
|
2014-05-08 20:45:18 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'redeems the invite from email' do
|
|
|
|
Invite.redeem_from_email(user.email)
|
|
|
|
expect(invite.reload).to be_redeemed
|
2013-02-25 10:42:20 -06:00
|
|
|
end
|
2020-06-09 10:19:32 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'does not redeem the invite if email does not match' do
|
|
|
|
Invite.redeem_from_email('test2@example.com')
|
|
|
|
expect(invite.reload).not_to be_redeemed
|
2020-06-09 10:19:32 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
context 'scopes' do
|
|
|
|
fab!(:inviter) { Fabricate(:user) }
|
2013-11-08 13:11:41 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
fab!(:pending_invite) { Fabricate(:invite, invited_by: inviter, email: 'pending@example.com') }
|
|
|
|
fab!(:pending_link_invite) { Fabricate(:invite, invited_by: inviter, max_redemptions_allowed: 5) }
|
|
|
|
fab!(:pending_invite_from_another_user) { Fabricate(:invite) }
|
2015-07-11 07:09:12 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
fab!(:expired_invite) { Fabricate(:invite, invited_by: inviter, email: 'expired@example.com', expires_at: 1.day.ago) }
|
2015-07-11 07:09:12 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
fab!(:redeemed_invite) { Fabricate(:invite, invited_by: inviter, email: 'redeemed@example.com') }
|
|
|
|
let!(:redeemed_invite_user) { redeemed_invite.redeem }
|
2017-11-14 04:38:54 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
fab!(:partially_redeemed_invite) { Fabricate(:invite, invited_by: inviter, email: nil, max_redemptions_allowed: 5) }
|
|
|
|
let!(:partially_redeemed_invite_user) { partially_redeemed_invite.redeem(email: 'partially_redeemed_invite@example.com') }
|
2021-03-06 05:29:35 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
fab!(:redeemed_and_expired_invite) { Fabricate(:invite, invited_by: inviter, email: 'redeemed_and_expired@example.com') }
|
|
|
|
let!(:redeemed_and_expired_invite_user) do
|
|
|
|
user = redeemed_and_expired_invite.redeem
|
|
|
|
redeemed_and_expired_invite.update!(expires_at: 1.day.ago)
|
|
|
|
user
|
2015-07-11 07:09:12 -05:00
|
|
|
end
|
2013-11-08 13:11:41 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
fab!(:partially_redeemed_and_expired_invite) { Fabricate(:invite, invited_by: inviter, email: nil, max_redemptions_allowed: 5) }
|
|
|
|
let!(:partially_redeemed_and_expired_invite_user) do
|
|
|
|
user = partially_redeemed_and_expired_invite.redeem(email: 'partially_redeemed_and_expired_invite@example.com')
|
|
|
|
partially_redeemed_and_expired_invite.update!(expires_at: 1.day.ago)
|
|
|
|
user
|
2013-11-08 13:11:41 -06:00
|
|
|
end
|
2020-06-09 10:19:32 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
describe '#pending' do
|
|
|
|
it 'returns pending invites only' do
|
|
|
|
expect(Invite.pending(inviter)).to contain_exactly(
|
|
|
|
pending_invite, pending_link_invite, partially_redeemed_invite
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-03-06 05:29:35 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
describe '#expired' do
|
|
|
|
it 'returns expired invites only' do
|
|
|
|
expect(Invite.expired(inviter)).to contain_exactly(
|
|
|
|
expired_invite, partially_redeemed_and_expired_invite
|
|
|
|
)
|
|
|
|
end
|
2021-03-06 05:29:35 -06:00
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
describe '#redeemed_users' do
|
|
|
|
it 'returns redeemed users' do
|
|
|
|
expect(Invite.redeemed_users(inviter).map(&:user)).to contain_exactly(
|
|
|
|
redeemed_invite_user, partially_redeemed_invite_user, redeemed_and_expired_invite_user, partially_redeemed_and_expired_invite_user
|
|
|
|
)
|
|
|
|
end
|
2020-06-09 10:19:32 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'returns redeemed users for trashed invites' do
|
|
|
|
[redeemed_invite, partially_redeemed_invite, redeemed_and_expired_invite, partially_redeemed_and_expired_invite].each(&:trash!)
|
2020-06-09 10:19:32 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
expect(Invite.redeemed_users(inviter).map(&:user)).to contain_exactly(
|
|
|
|
redeemed_invite_user, partially_redeemed_invite_user, redeemed_and_expired_invite_user, partially_redeemed_and_expired_invite_user
|
|
|
|
)
|
|
|
|
end
|
2020-06-09 10:19:32 -05:00
|
|
|
end
|
2013-11-08 13:11:41 -06:00
|
|
|
end
|
2014-01-21 15:53:46 -06:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
describe '#invalidate_for_email' do
|
2014-01-21 15:53:46 -06:00
|
|
|
it 'returns nil if there is no invite for the given email' do
|
2021-03-25 11:26:22 -05:00
|
|
|
invite = Invite.invalidate_for_email('test@example.com')
|
|
|
|
expect(invite).to eq(nil)
|
2014-01-21 15:53:46 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the matching invite to be invalid' do
|
2021-03-25 11:26:22 -05:00
|
|
|
invite = Fabricate(:invite, invited_by: Fabricate(:user), email: 'test@example.com')
|
|
|
|
result = Invite.invalidate_for_email('test@example.com')
|
2014-01-21 15:53:46 -06:00
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(result).to eq(invite)
|
|
|
|
expect(result.link_valid?).to eq(false)
|
2014-01-21 15:53:46 -06:00
|
|
|
end
|
2014-07-04 14:53:41 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'sets the matching invite to be invalid without being case-sensitive' do
|
|
|
|
invite = Fabricate(:invite, invited_by: Fabricate(:user), email: 'test@Example.COM')
|
|
|
|
result = Invite.invalidate_for_email('test@EXAMPLE.com')
|
2014-07-04 14:53:41 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
expect(result).to eq(invite)
|
|
|
|
expect(result.link_valid?).to eq(false)
|
2014-07-04 14:53:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
describe '#resend_email' do
|
|
|
|
fab!(:invite) { Fabricate(:invite) }
|
2019-07-19 00:59:12 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
it 'resets expiry of a resent invite' do
|
|
|
|
invite.update!(invalidated_at: 10.days.ago, expires_at: 10.days.ago)
|
|
|
|
expect(invite).to be_expired
|
2019-07-19 00:59:12 -05:00
|
|
|
|
2021-03-25 11:26:22 -05:00
|
|
|
invite.resend_invite
|
|
|
|
expect(invite).not_to be_expired
|
|
|
|
expect(invite.invalidated_at).to be_nil
|
2019-07-19 00:59:12 -05:00
|
|
|
end
|
|
|
|
end
|
2021-07-06 04:49:26 -05:00
|
|
|
|
|
|
|
describe '#warnings' do
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:invite) { Fabricate(:invite) }
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
fab!(:secured_category) do
|
|
|
|
secured_category = Fabricate(:category)
|
|
|
|
secured_category.permissions = { group.name => :full }
|
|
|
|
secured_category.save!
|
|
|
|
secured_category
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return any warnings for simple invites' do
|
|
|
|
expect(invite.warnings(admin.guardian)).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a warning if topic is private' do
|
|
|
|
topic = Fabricate(:topic, category: secured_category)
|
|
|
|
TopicInvite.create!(topic: topic, invite: invite)
|
|
|
|
|
|
|
|
expect(invite.warnings(admin.guardian)).to contain_exactly(I18n.t("invite.requires_groups", groups: group.name))
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|