2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe Invite do
|
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to validate_presence_of :invited_by_id }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-01-19 13:50:01 -05:00
|
|
|
it { is_expected.to rate_limit }
|
|
|
|
|
2013-06-25 10:15:41 -04:00
|
|
|
let(:iceking) { 'iceking@adventuretime.ooo' }
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'user validators' do
|
|
|
|
let(:coding_horror) { Fabricate(:coding_horror) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:invite) { Invite.create(email: user.email, invited_by: coding_horror) }
|
|
|
|
|
|
|
|
it "should not allow an invite with the same email as an existing user" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite).not_to be_valid
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not allow a user to invite themselves" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite.email_already_exists).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-10-09 10:44:15 -04:00
|
|
|
context 'email validators' do
|
|
|
|
let(:coding_horror) { Fabricate(:coding_horror) }
|
2019-02-06 12:08:06 -05:00
|
|
|
|
|
|
|
it "should not allow an invite with unformatted email address" do
|
|
|
|
expect {
|
|
|
|
Fabricate(:invite, email: "John Doe <john.doe@example.com>")
|
|
|
|
}.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
2014-10-09 10:44:15 -04:00
|
|
|
|
|
|
|
it "should not allow an invite with blacklisted email" do
|
2019-02-06 12:08:06 -05:00
|
|
|
invite = Invite.create(email: "test@mailinator.com", invited_by: coding_horror)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite).not_to be_valid
|
2014-10-09 10:44:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should allow an invite with non-blacklisted email" do
|
|
|
|
invite = Fabricate(:invite, email: "test@mail.com", invited_by: coding_horror)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite).to be_valid
|
2014-10-09 10:44:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context '#create' do
|
|
|
|
|
|
|
|
context 'saved' do
|
|
|
|
subject { Fabricate(:invite) }
|
2014-10-29 11:06:50 -04:00
|
|
|
|
|
|
|
it "works" do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(subject.invite_key).to be_present
|
|
|
|
expect(subject.email_already_exists).to eq(false)
|
2014-10-29 11:06:50 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'should store a lower case version of the email' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(subject.email).to eq(iceking)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'to a topic' do
|
|
|
|
let!(:topic) { Fabricate(:topic) }
|
|
|
|
let(:inviter) { topic.user }
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'email' do
|
|
|
|
it 'enqueues a job to email the invite' do
|
2018-12-05 10:43:07 -05:00
|
|
|
expect do
|
|
|
|
Invite.invite_by_email(iceking, inviter, topic)
|
|
|
|
end.to change { Jobs::InviteEmail.jobs.size }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'destroyed' do
|
|
|
|
it "can invite the same user after their invite was destroyed" do
|
2018-12-05 10:43:07 -05:00
|
|
|
Invite.invite_by_email(iceking, inviter, topic).destroy!
|
|
|
|
invite = Invite.invite_by_email(iceking, inviter, topic)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite).to be_present
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'after created' do
|
2018-12-05 10:43:07 -05:00
|
|
|
let(:invite) { Invite.invite_by_email(iceking, inviter, topic) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'belongs to the topic' do
|
2018-12-05 10:43:07 -05:00
|
|
|
expect(topic.invites).to eq([invite])
|
|
|
|
expect(invite.topics).to eq([topic])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when added by another user' do
|
|
|
|
let(:coding_horror) { Fabricate(:coding_horror) }
|
2018-12-05 10:43:07 -05:00
|
|
|
|
|
|
|
let(:new_invite) do
|
|
|
|
Invite.invite_by_email(iceking, coding_horror, topic)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'returns a different invite' do
|
2018-12-05 10:43:07 -05:00
|
|
|
expect(new_invite).not_to eq(invite)
|
|
|
|
expect(new_invite.invite_key).not_to eq(invite.invite_key)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(new_invite.topics).to eq([topic])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-05-08 02:45:49 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when adding a duplicate' do
|
|
|
|
it 'returns the original invite' do
|
2018-12-05 10:43:07 -05:00
|
|
|
%w{
|
|
|
|
iceking@adventuretime.ooo
|
|
|
|
iceking@ADVENTURETIME.ooo
|
|
|
|
ICEKING@adventuretime.ooo
|
|
|
|
}.each do |email|
|
|
|
|
expect(Invite.invite_by_email(
|
|
|
|
email, inviter, topic
|
|
|
|
)).to eq(invite)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-01-21 15:13:55 -05:00
|
|
|
|
2017-04-11 10:05:35 -04:00
|
|
|
it 'updates timestamp of existing invite' do
|
2018-12-05 10:43:07 -05:00
|
|
|
invite.update!(created_at: 10.days.ago)
|
|
|
|
|
|
|
|
resend_invite = Invite.invite_by_email(
|
|
|
|
'iceking@adventuretime.ooo', inviter, topic
|
|
|
|
)
|
|
|
|
|
2017-04-11 10:05:35 -04:00
|
|
|
expect(resend_invite.created_at).to be_within(1.minute).of(Time.zone.now)
|
|
|
|
end
|
|
|
|
|
2014-01-21 15:13:55 -05:00
|
|
|
it 'returns a new invite if the other has expired' do
|
2017-04-11 10:05:35 -04:00
|
|
|
SiteSetting.invite_expiry_days = 1
|
2018-12-05 10:43:07 -05:00
|
|
|
invite.update!(created_at: 2.days.ago)
|
|
|
|
|
|
|
|
new_invite = Invite.invite_by_email(
|
|
|
|
'iceking@adventuretime.ooo', inviter, topic
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(new_invite).not_to eq(invite)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(new_invite).not_to be_expired
|
2014-01-21 15:13:55 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when adding to another topic' do
|
|
|
|
let!(:another_topic) { Fabricate(:topic, user: topic.user) }
|
|
|
|
|
|
|
|
it 'should be the same invite' do
|
2018-12-05 10:43:07 -05:00
|
|
|
new_invite = Invite.invite_by_email(iceking, inviter, another_topic)
|
|
|
|
expect(new_invite).to eq(invite)
|
|
|
|
expect(another_topic.invites).to eq([invite])
|
|
|
|
expect(invite.topics).to match_array([topic, another_topic])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2018-12-10 17:24:02 -05:00
|
|
|
|
|
|
|
it 'correctly marks invite as sent via email' do
|
|
|
|
expect(invite.via_email).to eq(true)
|
|
|
|
|
|
|
|
Invite.invite_by_email(iceking, inviter, topic)
|
|
|
|
expect(invite.reload.via_email).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not mark invite as sent via email after generating invite link' do
|
|
|
|
expect(invite.via_email).to eq(true)
|
|
|
|
|
|
|
|
Invite.generate_invite_link(iceking, inviter, topic)
|
|
|
|
expect(invite.reload.via_email).to eq(false)
|
|
|
|
|
|
|
|
Invite.invite_by_email(iceking, inviter, topic)
|
|
|
|
expect(invite.reload.via_email).to eq(false)
|
|
|
|
|
|
|
|
Invite.generate_invite_link(iceking, inviter, topic)
|
|
|
|
expect(invite.reload.via_email).to eq(false)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'an existing user' do
|
2014-09-11 03:39:20 -04:00
|
|
|
let(:topic) { Fabricate(:topic, category_id: nil, archetype: 'private_message') }
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:coding_horror) { Fabricate(:coding_horror) }
|
|
|
|
|
2014-09-11 03:39:20 -04:00
|
|
|
it "works" do
|
2018-12-05 10:43:07 -05:00
|
|
|
expect do
|
|
|
|
Invite.invite_by_email(coding_horror.email, topic.user, topic)
|
|
|
|
end.to raise_error(Invite::UserExists)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-09-11 03:39:20 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2018-01-19 09:29:15 -05:00
|
|
|
context 'a staged user' do
|
|
|
|
it 'creates an invite for a staged user' do
|
|
|
|
Fabricate(:staged, email: 'staged@account.com')
|
|
|
|
invite = Invite.invite_by_email('staged@account.com', Fabricate(:coding_horror))
|
|
|
|
|
|
|
|
expect(invite).to be_valid
|
|
|
|
expect(invite.email).to eq('staged@account.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context '.redeem' do
|
|
|
|
|
|
|
|
let(:invite) { Fabricate(:invite) }
|
|
|
|
|
|
|
|
it 'creates a notification for the invitee' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect { invite.redeem }.to change(Notification, :count)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'wont redeem an expired invite' do
|
|
|
|
SiteSetting.expects(:invite_expiry_days).returns(10)
|
|
|
|
invite.update_column(:created_at, 20.days.ago)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite.redeem).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'wont redeem a deleted invite' do
|
|
|
|
invite.destroy
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite.redeem).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-01-21 16:53:46 -05:00
|
|
|
it "won't redeem an invalidated invite" do
|
|
|
|
invite.invalidated_at = 1.day.ago
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite.redeem).to be_blank
|
2014-01-21 16:53:46 -05:00
|
|
|
end
|
|
|
|
|
2015-03-25 12:55:18 -04:00
|
|
|
context "deletes duplicate invites" do
|
|
|
|
let(:another_user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it 'delete duplicate invite' do
|
|
|
|
another_invite = Fabricate(:invite, email: invite.email, invited_by: another_user)
|
|
|
|
invite.redeem
|
|
|
|
duplicate_invite = Invite.find_by(id: another_invite.id)
|
|
|
|
expect(duplicate_invite).to be_nil
|
|
|
|
end
|
|
|
|
|
2015-03-25 13:59:37 -04:00
|
|
|
it 'does not delete already redeemed invite' do
|
|
|
|
redeemed_invite = Fabricate(:invite, email: invite.email, invited_by: another_user, redeemed_at: 1.day.ago)
|
|
|
|
invite.redeem
|
|
|
|
used_invite = Invite.find_by(id: redeemed_invite.id)
|
|
|
|
expect(used_invite).not_to be_nil
|
|
|
|
end
|
|
|
|
|
2015-03-25 12:55:18 -04:00
|
|
|
end
|
|
|
|
|
2016-09-20 13:12:00 -04:00
|
|
|
context "as a moderator" do
|
|
|
|
it "will give the user a moderator flag" do
|
|
|
|
invite.invited_by = Fabricate(:admin)
|
|
|
|
invite.moderator = true
|
|
|
|
invite.save
|
|
|
|
|
|
|
|
user = invite.redeem
|
|
|
|
expect(user).to be_moderator
|
|
|
|
end
|
|
|
|
|
|
|
|
it "will not give the user a moderator flag if the inviter is not staff" do
|
|
|
|
invite.moderator = true
|
|
|
|
invite.save
|
|
|
|
|
|
|
|
user = invite.redeem
|
|
|
|
expect(user).not_to be_moderator
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-08 02:45:49 -04:00
|
|
|
context "when inviting to groups" do
|
|
|
|
it "add the user to the correct groups" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
invite.invited_groups.build(group_id: group.id)
|
|
|
|
invite.save
|
|
|
|
|
|
|
|
user = invite.redeem
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.groups.count).to eq(1)
|
2014-05-08 02:45:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "invite trust levels" do
|
2013-02-05 14:16:51 -05:00
|
|
|
it "returns the trust level in default_invitee_trust_level" do
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.default_invitee_trust_level = TrustLevel[3]
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite.redeem.trust_level).to eq(TrustLevel[3])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-10 21:21:39 -04:00
|
|
|
context 'inviting when must_approve_users? is enabled' do
|
2014-01-17 11:11:42 -05:00
|
|
|
it 'correctly activates accounts' do
|
2017-03-05 08:55:21 -05:00
|
|
|
invite.invited_by = Fabricate(:admin)
|
2017-07-07 02:09:14 -04:00
|
|
|
SiteSetting.must_approve_users = true
|
2013-07-10 21:21:39 -04:00
|
|
|
user = invite.redeem
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.approved?).to eq(true)
|
2013-07-10 21:21:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'simple invite' do
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
let!(:user) { invite.redeem }
|
|
|
|
|
2013-07-10 21:21:39 -04:00
|
|
|
it 'works correctly' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.is_a?(User)).to eq(true)
|
|
|
|
expect(user.send_welcome_message).to eq(true)
|
|
|
|
expect(user.trust_level).to eq(SiteSetting.default_invitee_trust_level)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'after redeeming' do
|
|
|
|
before do
|
|
|
|
invite.reload
|
|
|
|
end
|
|
|
|
|
2013-07-10 21:21:39 -04:00
|
|
|
it 'works correctly' do
|
|
|
|
# has set the user_id attribute
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite.user).to eq(user)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-10 21:21:39 -04:00
|
|
|
# returns true for redeemed
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite).to be_redeemed
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'again' do
|
2018-05-08 10:42:58 -04:00
|
|
|
it 'will not redeem twice' do
|
|
|
|
expect(invite.redeem).to be_blank
|
2014-07-01 12:52:52 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'invited to topics' do
|
2017-02-02 12:38:25 -05:00
|
|
|
let(:tl2_user) { Fabricate(:user, trust_level: 2) }
|
|
|
|
let!(:topic) { Fabricate(:private_message_topic, user: tl2_user) }
|
2018-02-25 23:46:15 -05:00
|
|
|
|
|
|
|
let!(:invite) do
|
2014-05-08 21:45:18 -04:00
|
|
|
topic.invite(topic.user, 'jake@adventuretime.ooo')
|
2018-02-25 23:46:15 -05:00
|
|
|
Invite.find_by(invited_by_id: topic.user)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
context 'redeem topic invite' do
|
|
|
|
it 'adds the user to the topic_users' do
|
2014-05-08 21:45:18 -04:00
|
|
|
user = invite.redeem
|
|
|
|
topic.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(topic.allowed_users.include?(user)).to eq(true)
|
|
|
|
expect(Guardian.new(user).can_see?(topic)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'invited by another user to the same topic' do
|
2017-02-02 12:38:25 -05:00
|
|
|
let(:another_tl2_user) { Fabricate(:user, trust_level: 2) }
|
|
|
|
let!(:another_invite) { topic.invite(another_tl2_user, 'jake@adventuretime.ooo') }
|
2013-02-05 14:16:51 -05:00
|
|
|
let!(:user) { invite.redeem }
|
|
|
|
|
|
|
|
it 'adds the user to the topic_users' do
|
2014-05-08 21:45:18 -04:00
|
|
|
topic.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(topic.allowed_users.include?(user)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'invited by another user to a different topic' do
|
|
|
|
let!(:user) { invite.redeem }
|
2017-02-02 12:38:25 -05:00
|
|
|
let(:another_tl2_user) { Fabricate(:user, trust_level: 2) }
|
|
|
|
let(:another_topic) { Fabricate(:topic, user: another_tl2_user) }
|
2014-05-08 21:45:18 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
it 'adds the user to the topic_users of the first topic' do
|
2017-02-02 12:38:25 -05:00
|
|
|
expect(another_topic.invite(another_tl2_user, user.username)).to be_truthy # invited via username
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(topic.allowed_users.include?(user)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-08 14:11:41 -05:00
|
|
|
describe '.find_all_invites_from' do
|
|
|
|
context 'with user that has invited' do
|
|
|
|
it 'returns invites' do
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
invite = Fabricate(:invite, invited_by: inviter)
|
|
|
|
|
|
|
|
invites = Invite.find_all_invites_from(inviter)
|
|
|
|
|
|
|
|
expect(invites).to include invite
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with user that has not invited' do
|
|
|
|
it 'does not return invites' do
|
|
|
|
user = Fabricate(:user)
|
2014-05-08 02:45:49 -04:00
|
|
|
Fabricate(:invite)
|
2013-11-08 14:11:41 -05:00
|
|
|
|
|
|
|
invites = Invite.find_all_invites_from(user)
|
|
|
|
|
|
|
|
expect(invites).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-11 08:09:12 -04:00
|
|
|
describe '.find_pending_invites_from' do
|
|
|
|
it 'returns pending invites only' do
|
|
|
|
inviter = Fabricate(:user)
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
invited_by: inviter,
|
|
|
|
user_id: 123,
|
|
|
|
email: 'redeemed@example.com'
|
|
|
|
)
|
|
|
|
|
|
|
|
pending_invite = Fabricate(
|
|
|
|
:invite,
|
|
|
|
invited_by: inviter,
|
|
|
|
user_id: nil,
|
|
|
|
email: 'pending@example.com'
|
|
|
|
)
|
|
|
|
|
|
|
|
invites = Invite.find_pending_invites_from(inviter)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(invites.length).to eq(1)
|
2015-07-11 08:09:12 -04:00
|
|
|
expect(invites.first).to eq pending_invite
|
2017-11-14 05:38:54 -05:00
|
|
|
|
|
|
|
expect(Invite.find_pending_invites_count(inviter)).to eq(1)
|
2015-07-11 08:09:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-08 14:11:41 -05:00
|
|
|
describe '.find_redeemed_invites_from' do
|
|
|
|
it 'returns redeemed invites only' do
|
|
|
|
inviter = Fabricate(:user)
|
2014-05-08 02:45:49 -04:00
|
|
|
Fabricate(
|
2013-11-08 14:11:41 -05:00
|
|
|
:invite,
|
|
|
|
invited_by: inviter,
|
|
|
|
user_id: nil,
|
|
|
|
email: 'pending@example.com'
|
|
|
|
)
|
2014-05-08 02:45:49 -04:00
|
|
|
|
2013-11-08 14:11:41 -05:00
|
|
|
redeemed_invite = Fabricate(
|
|
|
|
:invite,
|
|
|
|
invited_by: inviter,
|
|
|
|
user_id: 123,
|
|
|
|
email: 'redeemed@example.com'
|
|
|
|
)
|
|
|
|
|
|
|
|
invites = Invite.find_redeemed_invites_from(inviter)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(invites.length).to eq(1)
|
2013-11-08 14:11:41 -05:00
|
|
|
expect(invites.first).to eq redeemed_invite
|
2017-11-14 05:38:54 -05:00
|
|
|
|
|
|
|
expect(Invite.find_redeemed_invites_count(inviter)).to eq(1)
|
2013-11-08 14:11:41 -05:00
|
|
|
end
|
|
|
|
end
|
2014-01-21 16:53:46 -05:00
|
|
|
|
|
|
|
describe '.invalidate_for_email' do
|
|
|
|
let(:email) { 'invite.me@example.com' }
|
|
|
|
subject { described_class.invalidate_for_email(email) }
|
|
|
|
|
|
|
|
it 'returns nil if there is no invite for the given email' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(subject).to eq(nil)
|
2014-01-21 16:53:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the matching invite to be invalid' do
|
|
|
|
invite = Fabricate(:invite, invited_by: Fabricate(:user), user_id: nil, email: email)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(subject).to eq(invite)
|
|
|
|
expect(subject.link_valid?).to eq(false)
|
|
|
|
expect(subject).to be_valid
|
2014-01-21 16:53:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the matching invite to be invalid without being case-sensitive' do
|
|
|
|
invite = Fabricate(:invite, invited_by: Fabricate(:user), user_id: nil, email: 'invite.me2@Example.COM')
|
|
|
|
result = described_class.invalidate_for_email('invite.me2@EXAMPLE.com')
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(result).to eq(invite)
|
|
|
|
expect(result.link_valid?).to eq(false)
|
|
|
|
expect(result).to be_valid
|
2014-01-21 16:53:46 -05:00
|
|
|
end
|
|
|
|
end
|
2014-07-04 15:53:41 -04:00
|
|
|
|
|
|
|
describe '.redeem_from_email' do
|
|
|
|
let(:inviter) { Fabricate(:user) }
|
|
|
|
let(:invite) { Fabricate(:invite, invited_by: inviter, email: 'test@example.com', user_id: nil) }
|
|
|
|
let(:user) { Fabricate(:user, email: invite.email) }
|
|
|
|
|
|
|
|
it 'redeems the invite from email' do
|
2016-09-20 13:12:00 -04:00
|
|
|
Invite.redeem_from_email(user.email)
|
2014-07-04 15:53:41 -04:00
|
|
|
invite.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite).to be_redeemed
|
2014-07-04 15:53:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not redeem the invite if email does not match' do
|
2016-09-20 13:12:00 -04:00
|
|
|
Invite.redeem_from_email('test24@example.com')
|
2014-07-04 15:53:41 -04:00
|
|
|
invite.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(invite).not_to be_redeemed
|
2014-07-04 15:53:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-03-07 04:49:46 -05:00
|
|
|
describe '.rescind_all_expired_invites_from' do
|
|
|
|
it 'removes all expired invites sent by a user' do
|
|
|
|
SiteSetting.invite_expiry_days = 1
|
2017-06-29 10:32:07 -04:00
|
|
|
user = Fabricate(:user)
|
|
|
|
invite_1 = Fabricate(:invite, invited_by: user)
|
|
|
|
invite_2 = Fabricate(:invite, invited_by: user)
|
2019-03-07 04:49:46 -05:00
|
|
|
expired_invite = Fabricate(:invite, invited_by: user)
|
|
|
|
expired_invite.update!(created_at: 2.days.ago)
|
|
|
|
Invite.rescind_all_expired_invites_from(user)
|
2017-06-29 10:32:07 -04:00
|
|
|
invite_1.reload
|
|
|
|
invite_2.reload
|
2019-03-07 04:49:46 -05:00
|
|
|
expired_invite.reload
|
|
|
|
expect(invite_1.deleted_at).to eq(nil)
|
|
|
|
expect(invite_2.deleted_at).to eq(nil)
|
|
|
|
expect(expired_invite.deleted_at).to be_present
|
2017-06-29 10:32:07 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|