2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe InviteRedeemer do
|
2023-11-09 17:47:59 -05:00
|
|
|
fab!(:admin)
|
2013-08-28 03:18:31 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
describe "#initialize" do
|
|
|
|
fab!(:redeeming_user) { Fabricate(:user, email: "redeemer@test.com") }
|
|
|
|
|
|
|
|
context "for invite link" do
|
|
|
|
fab!(:invite) { Fabricate(:invite, email: nil) }
|
|
|
|
|
|
|
|
context "when an email is passed in without a redeeming user" do
|
|
|
|
it "uses that email for invite redemption" do
|
|
|
|
redeemer = described_class.new(invite: invite, email: "blah@test.com")
|
|
|
|
expect(redeemer.email).to eq("blah@test.com")
|
|
|
|
expect { redeemer.redeem }.to change { User.count }
|
|
|
|
expect(User.find_by_email(redeemer.email)).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when an email is passed in with a redeeming user" do
|
|
|
|
it "uses the redeeming user's email for invite redemption" do
|
|
|
|
redeemer =
|
|
|
|
described_class.new(
|
|
|
|
invite: invite,
|
|
|
|
email: "blah@test.com",
|
|
|
|
redeeming_user: redeeming_user,
|
|
|
|
)
|
|
|
|
expect(redeemer.email).to eq(redeeming_user.email)
|
|
|
|
expect { redeemer.redeem }.not_to change { User.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when an email is not passed in with a redeeming user" do
|
|
|
|
it "uses the redeeming user's email for invite redemption" do
|
|
|
|
redeemer = described_class.new(invite: invite, email: nil, redeeming_user: redeeming_user)
|
|
|
|
expect(redeemer.email).to eq(redeeming_user.email)
|
|
|
|
expect { redeemer.redeem }.not_to change { User.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when no email and no redeeming user is passed in" do
|
|
|
|
it "raises an error" do
|
|
|
|
expect {
|
|
|
|
described_class.new(invite: invite, email: nil, redeeming_user: nil)
|
|
|
|
}.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for invite with email" do
|
|
|
|
fab!(:invite) { Fabricate(:invite, email: "foobar@example.com") }
|
|
|
|
|
|
|
|
context "when an email is passed in without a redeeming user" do
|
|
|
|
it "uses that email for invite redemption" do
|
|
|
|
redeemer = described_class.new(invite: invite, email: "foobar@example.com")
|
|
|
|
expect(redeemer.email).to eq("foobar@example.com")
|
|
|
|
expect { redeemer.redeem }.to change { User.count }
|
|
|
|
expect(User.find_by_email(redeemer.email)).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when an email is passed in with a redeeming user" do
|
|
|
|
it "uses the redeeming user's email for invite redemption" do
|
|
|
|
redeemer =
|
|
|
|
described_class.new(
|
|
|
|
invite: invite,
|
|
|
|
email: "blah@test.com",
|
|
|
|
redeeming_user: redeeming_user,
|
|
|
|
)
|
|
|
|
expect(redeemer.email).to eq(redeeming_user.email)
|
|
|
|
expect { redeemer.redeem }.to raise_error(
|
|
|
|
ActiveRecord::RecordNotSaved,
|
|
|
|
I18n.t("invite.not_matching_email"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when an email is not passed in with a redeeming user" do
|
|
|
|
it "uses the invite email for invite redemption" do
|
|
|
|
redeemer = described_class.new(invite: invite, email: nil, redeeming_user: redeeming_user)
|
|
|
|
expect(redeemer.email).to eq("foobar@example.com")
|
|
|
|
expect { redeemer.redeem }.to raise_error(
|
|
|
|
ActiveRecord::RecordNotSaved,
|
|
|
|
I18n.t("invite.not_matching_email"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when no email and no redeeming user is passed in" do
|
|
|
|
it "uses the invite email for invite redemption" do
|
|
|
|
redeemer = described_class.new(invite: invite, email: nil, redeeming_user: nil)
|
|
|
|
expect(redeemer.email).to eq("foobar@example.com")
|
|
|
|
expect { redeemer.redeem }.to change { User.count }
|
|
|
|
expect(User.find_by_email(redeemer.email)).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-02 10:10:48 -04:00
|
|
|
describe ".create_user_from_invite" do
|
2013-08-28 03:18:31 -04:00
|
|
|
it "should be created correctly" do
|
2020-06-09 11:19:32 -04:00
|
|
|
invite = Fabricate(:invite, email: "walter.white@email.com")
|
|
|
|
user =
|
|
|
|
InviteRedeemer.create_user_from_invite(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: "walter",
|
|
|
|
name: "Walter White",
|
|
|
|
)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(user.username).to eq("walter")
|
|
|
|
expect(user.name).to eq("Walter White")
|
|
|
|
expect(user.email).to eq("walter.white@email.com")
|
2022-06-02 10:10:48 -04:00
|
|
|
expect(user.approved).to eq(false)
|
2021-04-14 05:15:56 -04:00
|
|
|
expect(user.active).to eq(false)
|
2013-08-28 03:18:31 -04:00
|
|
|
end
|
2017-02-13 16:19:41 -05:00
|
|
|
|
2019-04-13 03:34:25 -04:00
|
|
|
it "can set the password and ip_address" do
|
2017-02-13 16:19:41 -05:00
|
|
|
password = "s3cure5tpasSw0rD"
|
2019-04-13 03:34:25 -04:00
|
|
|
ip_address = "192.168.1.1"
|
2020-06-09 11:19:32 -04:00
|
|
|
invite = Fabricate(:invite, email: "walter.white@email.com")
|
|
|
|
user =
|
|
|
|
InviteRedeemer.create_user_from_invite(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: "walter",
|
|
|
|
name: "Walter White",
|
|
|
|
password: password,
|
|
|
|
ip_address: ip_address,
|
|
|
|
)
|
2017-02-13 16:19:41 -05:00
|
|
|
expect(user).to have_password
|
|
|
|
expect(user.confirm_password?(password)).to eq(true)
|
2022-06-02 10:10:48 -04:00
|
|
|
expect(user.approved).to eq(false)
|
2019-04-13 03:34:25 -04:00
|
|
|
expect(user.ip_address).to eq(ip_address)
|
|
|
|
expect(user.registration_ip_address).to eq(ip_address)
|
2017-02-13 16:19:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises exception with record and errors" do
|
|
|
|
error = nil
|
2020-06-09 11:19:32 -04:00
|
|
|
invite = Fabricate(:invite, email: "walter.white@email.com")
|
2017-02-13 16:19:41 -05:00
|
|
|
begin
|
2020-06-09 11:19:32 -04:00
|
|
|
InviteRedeemer.create_user_from_invite(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: "walter",
|
|
|
|
name: "Walter White",
|
|
|
|
password: "aaa",
|
|
|
|
)
|
2017-02-13 16:19:41 -05:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
error = e
|
|
|
|
end
|
|
|
|
expect(error).to be_present
|
|
|
|
expect(error.record.errors[:password]).to be_present
|
|
|
|
end
|
2018-01-19 09:29:15 -05:00
|
|
|
|
|
|
|
it "should unstage user" do
|
|
|
|
staged_user =
|
|
|
|
Fabricate(
|
|
|
|
:staged,
|
|
|
|
email: "staged@account.com",
|
|
|
|
active: true,
|
|
|
|
username: "staged1",
|
|
|
|
name: "Stage Name",
|
|
|
|
)
|
2020-06-09 11:19:32 -04:00
|
|
|
invite = Fabricate(:invite, email: "staged@account.com")
|
|
|
|
user =
|
|
|
|
InviteRedeemer.create_user_from_invite(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: "walter",
|
|
|
|
name: "Walter White",
|
|
|
|
)
|
2018-01-19 09:29:15 -05:00
|
|
|
|
|
|
|
expect(user.id).to eq(staged_user.id)
|
|
|
|
expect(user.username).to eq("walter")
|
|
|
|
expect(user.name).to eq("Walter White")
|
2018-12-10 17:24:02 -05:00
|
|
|
expect(user.staged).to eq(false)
|
2018-01-19 09:29:15 -05:00
|
|
|
expect(user.email).to eq("staged@account.com")
|
2022-06-02 10:10:48 -04:00
|
|
|
expect(user.approved).to eq(false)
|
2018-01-19 09:29:15 -05:00
|
|
|
end
|
2019-07-19 01:59:12 -04:00
|
|
|
|
2021-04-14 05:15:56 -04:00
|
|
|
it "activates user invited via email with a token" do
|
|
|
|
invite =
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
invited_by: Fabricate(:admin),
|
|
|
|
email: "walter.white@email.com",
|
|
|
|
emailed_status: Invite.emailed_status_types[:sent],
|
|
|
|
)
|
|
|
|
user =
|
|
|
|
InviteRedeemer.create_user_from_invite(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: "walter",
|
|
|
|
name: "Walter White",
|
|
|
|
email_token: invite.email_token,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(user.username).to eq("walter")
|
|
|
|
expect(user.name).to eq("Walter White")
|
|
|
|
expect(user.email).to eq("walter.white@email.com")
|
2022-06-02 10:10:48 -04:00
|
|
|
expect(user.approved).to eq(false)
|
2021-04-14 05:15:56 -04:00
|
|
|
expect(user.active).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not activate user invited via email with a wrong token" do
|
|
|
|
invite =
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
invited_by: Fabricate(:user),
|
|
|
|
email: "walter.white@email.com",
|
|
|
|
emailed_status: Invite.emailed_status_types[:sent],
|
|
|
|
)
|
|
|
|
user =
|
|
|
|
InviteRedeemer.create_user_from_invite(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: "walter",
|
|
|
|
name: "Walter White",
|
|
|
|
email_token: "wrong_token",
|
|
|
|
)
|
|
|
|
expect(user.active).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not activate user invited via email without a token" do
|
|
|
|
invite =
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
invited_by: Fabricate(:user),
|
|
|
|
email: "walter.white@email.com",
|
|
|
|
emailed_status: Invite.emailed_status_types[:sent],
|
|
|
|
)
|
|
|
|
user =
|
|
|
|
InviteRedeemer.create_user_from_invite(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: "walter",
|
|
|
|
name: "Walter White",
|
|
|
|
)
|
|
|
|
expect(user.active).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not activate user invited via links" do
|
2020-06-09 11:19:32 -04:00
|
|
|
invite =
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
email: "walter.white@email.com",
|
|
|
|
emailed_status: Invite.emailed_status_types[:not_required],
|
|
|
|
)
|
|
|
|
user =
|
|
|
|
InviteRedeemer.create_user_from_invite(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: "walter",
|
|
|
|
name: "Walter White",
|
|
|
|
)
|
|
|
|
|
2019-07-19 01:59:12 -04:00
|
|
|
expect(user.username).to eq("walter")
|
|
|
|
expect(user.name).to eq("Walter White")
|
|
|
|
expect(user.email).to eq("walter.white@email.com")
|
2021-06-07 11:57:08 -04:00
|
|
|
expect(user.approved).to eq(false)
|
2022-06-02 10:10:48 -04:00
|
|
|
expect(user.active).to eq(false)
|
2021-06-07 11:57:08 -04:00
|
|
|
end
|
2022-06-02 23:43:52 -04:00
|
|
|
|
|
|
|
it "approves and actives user when redeeming an invite with email token and SiteSetting.invite_only is enabled" do
|
|
|
|
SiteSetting.invite_only = true
|
|
|
|
Jobs.run_immediately!
|
|
|
|
|
|
|
|
invite =
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
invited_by: admin,
|
|
|
|
email: "walter.white@email.com",
|
|
|
|
emailed_status: Invite.emailed_status_types[:sent],
|
|
|
|
)
|
|
|
|
|
|
|
|
user =
|
|
|
|
InviteRedeemer.create_user_from_invite(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
email_token: invite.email_token,
|
|
|
|
username: "walter",
|
|
|
|
name: "Walter White",
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(user.name).to eq("Walter White")
|
|
|
|
expect(user.username).to eq("walter")
|
|
|
|
expect(user.email).to eq("walter.white@email.com")
|
|
|
|
expect(user.approved).to eq(true)
|
|
|
|
expect(user.active).to eq(true)
|
|
|
|
expect(ReviewableUser.count).to eq(0)
|
|
|
|
end
|
2013-08-28 03:18:31 -04:00
|
|
|
end
|
2016-07-12 23:58:31 -04:00
|
|
|
|
|
|
|
describe "#redeem" do
|
|
|
|
let(:name) { "john snow" }
|
|
|
|
let(:username) { "kingofthenorth" }
|
2017-02-13 16:19:41 -05:00
|
|
|
let(:password) { "know5nOthiNG" }
|
2020-06-09 11:19:32 -04:00
|
|
|
let(:invite_redeemer) do
|
|
|
|
InviteRedeemer.new(invite: invite, email: invite.email, username: username, name: name)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2016-07-12 23:58:31 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
context "with email" do
|
|
|
|
fab!(:invite) { Fabricate(:invite, email: "foobar@example.com") }
|
|
|
|
context "when must_approve_users setting is enabled" do
|
|
|
|
before { SiteSetting.must_approve_users = true }
|
|
|
|
|
|
|
|
it "should redeem an invite but not approve the user when invite is created by a staff user" do
|
|
|
|
inviter = invite.invited_by
|
|
|
|
inviter.update!(admin: true)
|
|
|
|
user = invite_redeemer.redeem
|
|
|
|
|
|
|
|
expect(user.name).to eq(name)
|
|
|
|
expect(user.username).to eq(username)
|
|
|
|
expect(user.invited_by).to eq(inviter)
|
|
|
|
expect(user.approved).to eq(false)
|
|
|
|
|
|
|
|
expect(inviter.notifications.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should redeem the invite but not approve the user when invite is created by a regular user" do
|
|
|
|
inviter = invite.invited_by
|
|
|
|
user = invite_redeemer.redeem
|
|
|
|
|
|
|
|
expect(user.name).to eq(name)
|
|
|
|
expect(user.username).to eq(username)
|
|
|
|
expect(user.invited_by).to eq(inviter)
|
|
|
|
expect(user.approved).to eq(false)
|
|
|
|
|
|
|
|
expect(inviter.notifications.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should redeem the invite and approve the user when user email is in auto_approve_email_domains setting" do
|
|
|
|
SiteSetting.auto_approve_email_domains = "example.com"
|
|
|
|
user = invite_redeemer.redeem
|
|
|
|
|
|
|
|
expect(user.name).to eq(name)
|
|
|
|
expect(user.username).to eq(username)
|
|
|
|
expect(user.approved).to eq(true)
|
|
|
|
expect(user.approved_by).to eq(Discourse.system_user)
|
|
|
|
end
|
2022-06-02 10:10:48 -04:00
|
|
|
end
|
2016-07-12 23:58:31 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "should redeem the invite if invited by non staff and approve if staff not required to approve" do
|
2022-06-02 10:10:48 -04:00
|
|
|
inviter = invite.invited_by
|
|
|
|
user = invite_redeemer.redeem
|
2017-03-05 08:55:21 -05:00
|
|
|
|
2022-06-02 10:10:48 -04:00
|
|
|
expect(user.name).to eq(name)
|
|
|
|
expect(user.username).to eq(username)
|
|
|
|
expect(user.invited_by).to eq(inviter)
|
|
|
|
expect(inviter.notifications.count).to eq(1)
|
2022-11-13 21:02:06 -05:00
|
|
|
expect(user.approved).to eq(false)
|
2022-06-02 10:10:48 -04:00
|
|
|
end
|
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "should delete invite if invited_by user has been removed" do
|
|
|
|
invite.invited_by.destroy!
|
|
|
|
expect { invite.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
2022-06-02 10:10:48 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "can set password" do
|
|
|
|
user =
|
|
|
|
InviteRedeemer.new(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
).redeem
|
|
|
|
expect(user).to have_password
|
|
|
|
expect(user.confirm_password?(password)).to eq(true)
|
2022-06-02 10:10:48 -04:00
|
|
|
expect(user.approved).to eq(false)
|
|
|
|
end
|
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "can set custom fields" do
|
|
|
|
required_field = Fabricate(:user_field)
|
|
|
|
optional_field = Fabricate(:user_field, required: false)
|
|
|
|
user_fields = { required_field.id.to_s => "value1", optional_field.id.to_s => "value2" }
|
|
|
|
user =
|
|
|
|
InviteRedeemer.new(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
user_custom_fields: user_fields,
|
|
|
|
).redeem
|
|
|
|
|
|
|
|
expect(user).to be_present
|
|
|
|
expect(user.custom_fields["user_field_#{required_field.id}"]).to eq("value1")
|
|
|
|
expect(user.custom_fields["user_field_#{optional_field.id}"]).to eq("value2")
|
2022-06-02 10:10:48 -04:00
|
|
|
end
|
2017-03-05 08:55:21 -05:00
|
|
|
|
2024-03-27 09:12:14 -04:00
|
|
|
it "can set custom fields with field_type confirm properly" do
|
|
|
|
optional_field_1 = Fabricate(:user_field, field_type: "confirm", required: false)
|
|
|
|
optional_field_2 = Fabricate(:user_field, field_type: "confirm", required: false)
|
|
|
|
optional_field_3 = Fabricate(:user_field, field_type: "confirm", required: false)
|
|
|
|
user_fields = {
|
|
|
|
optional_field_1.id.to_s => "false",
|
|
|
|
optional_field_2.id.to_s => "true",
|
|
|
|
optional_field_3.id.to_s => "",
|
|
|
|
}
|
|
|
|
|
|
|
|
user =
|
|
|
|
InviteRedeemer.new(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
user_custom_fields: user_fields,
|
|
|
|
).redeem
|
|
|
|
|
|
|
|
expect(user).to be_present
|
|
|
|
expect(user.custom_fields["user_field_#{optional_field_1.id}"]).to eq(nil)
|
|
|
|
expect(user.custom_fields["user_field_#{optional_field_2.id}"]).to eq("true")
|
|
|
|
expect(user.custom_fields["user_field_#{optional_field_3.id}"]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "does not add user to group if inviter does not have permissions" do
|
|
|
|
group = Fabricate(:group, grant_trust_level: 2)
|
|
|
|
InvitedGroup.create(group_id: group.id, invite_id: invite.id)
|
|
|
|
user =
|
|
|
|
InviteRedeemer.new(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
).redeem
|
2020-03-31 14:29:15 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
expect(user.group_users.count).to eq(0)
|
|
|
|
end
|
2020-06-15 05:13:56 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "adds user to group" do
|
|
|
|
group = Fabricate(:group, grant_trust_level: 2)
|
|
|
|
InvitedGroup.create(group_id: group.id, invite_id: invite.id)
|
|
|
|
group.add_owner(invite.invited_by)
|
2020-06-15 05:13:56 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
user =
|
|
|
|
InviteRedeemer.new(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
).redeem
|
2017-11-23 11:39:24 -05:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
expect(user.group_users.count).to eq(4)
|
|
|
|
expect(user.trust_level).to eq(2)
|
|
|
|
end
|
2022-11-02 18:58:12 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "adds an entry to the group logs when the invited user is added to a group" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
InvitedGroup.create(group_id: group.id, invite_id: invite.id)
|
|
|
|
group.add_owner(invite.invited_by)
|
|
|
|
|
|
|
|
GroupHistory.destroy_all
|
|
|
|
|
|
|
|
user =
|
|
|
|
InviteRedeemer.new(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
).redeem
|
|
|
|
|
|
|
|
expect(group.reload.usernames.split(",")).to include(user.username)
|
|
|
|
expect(
|
|
|
|
GroupHistory.exists?(
|
|
|
|
target_user_id: user.id,
|
|
|
|
acting_user: invite.invited_by.id,
|
|
|
|
group_id: group.id,
|
|
|
|
action: GroupHistory.actions[:add_user_to_group],
|
2023-01-09 06:18:21 -05:00
|
|
|
),
|
2022-11-13 21:02:06 -05:00
|
|
|
).to eq(true)
|
|
|
|
end
|
2022-11-02 18:58:12 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "only allows one user to be created per invite" do
|
|
|
|
user = invite_redeemer.redeem
|
|
|
|
invite.reload
|
2022-11-02 18:58:12 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
user.email = "john@example.com"
|
|
|
|
user.save!
|
2018-04-27 09:51:10 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
another_invite_redeemer =
|
|
|
|
InviteRedeemer.new(invite: invite, email: invite.email, username: username, name: name)
|
|
|
|
another_user = another_invite_redeemer.redeem
|
|
|
|
expect(another_user).to eq(nil)
|
|
|
|
end
|
2018-04-27 09:51:10 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "should correctly update the invite redeemed_at date" do
|
|
|
|
SiteSetting.invite_expiry_days = 2
|
|
|
|
invite.update!(created_at: 10.days.ago)
|
2019-12-17 10:13:49 -05:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
inviter = invite.invited_by
|
|
|
|
inviter.admin = true
|
|
|
|
user = invite_redeemer.redeem
|
|
|
|
invite.reload
|
2019-12-17 10:13:49 -05:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
expect(user.invited_by).to eq(inviter)
|
|
|
|
expect(inviter.notifications.count).to eq(1)
|
|
|
|
expect(invite.invited_users.first).to be_present
|
|
|
|
end
|
2019-12-17 10:13:49 -05:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "raises an error if the email does not match the invite email" do
|
|
|
|
redeemer =
|
|
|
|
InviteRedeemer.new(invite: invite, email: "blah@test.com", username: username, name: name)
|
|
|
|
expect { redeemer.redeem }.to raise_error(
|
|
|
|
ActiveRecord::RecordNotSaved,
|
|
|
|
I18n.t("invite.not_matching_email"),
|
|
|
|
)
|
|
|
|
end
|
2020-06-09 11:19:32 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "adds the user to the appropriate private topic and no others" do
|
|
|
|
topic1 = Fabricate(:private_message_topic)
|
|
|
|
topic2 = Fabricate(:private_message_topic)
|
|
|
|
TopicInvite.create(invite: invite, topic: topic1)
|
|
|
|
user =
|
|
|
|
InviteRedeemer.new(
|
|
|
|
invite: invite,
|
|
|
|
email: invite.email,
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
).redeem
|
|
|
|
expect(TopicAllowedUser.exists?(topic: topic1, user: user)).to eq(true)
|
|
|
|
expect(TopicAllowedUser.exists?(topic: topic2, user: user)).to eq(false)
|
|
|
|
end
|
2022-11-01 12:33:32 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
context "when a redeeming user is passed in" do
|
|
|
|
fab!(:redeeming_user) { Fabricate(:user, email: "foobar@example.com") }
|
2022-11-01 12:33:32 -04:00
|
|
|
|
2022-11-13 21:02:06 -05:00
|
|
|
it "raises an error if the email does not match the invite email" do
|
|
|
|
redeeming_user.update!(email: "foo@bar.com")
|
|
|
|
redeemer = InviteRedeemer.new(invite: invite, redeeming_user: redeeming_user)
|
|
|
|
expect { redeemer.redeem }.to raise_error(
|
|
|
|
ActiveRecord::RecordNotSaved,
|
|
|
|
I18n.t("invite.not_matching_email"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the user to the appropriate private topic and no others" do
|
|
|
|
topic1 = Fabricate(:private_message_topic)
|
|
|
|
topic2 = Fabricate(:private_message_topic)
|
|
|
|
TopicInvite.create(invite: invite, topic: topic1)
|
|
|
|
InviteRedeemer.new(invite: invite, redeeming_user: redeeming_user).redeem
|
|
|
|
expect(TopicAllowedUser.exists?(topic: topic1, user: redeeming_user)).to eq(true)
|
|
|
|
expect(TopicAllowedUser.exists?(topic: topic2, user: redeeming_user)).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not create a topic allowed user record if the invited user is already in the topic" do
|
|
|
|
topic1 = Fabricate(:private_message_topic)
|
|
|
|
TopicInvite.create(invite: invite, topic: topic1)
|
|
|
|
TopicAllowedUser.create(topic: topic1, user: redeeming_user)
|
|
|
|
expect {
|
|
|
|
InviteRedeemer.new(invite: invite, redeeming_user: redeeming_user).redeem
|
|
|
|
}.not_to change { TopicAllowedUser.count }
|
|
|
|
end
|
2022-11-01 12:33:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with domain" do
|
|
|
|
fab!(:invite) { Fabricate(:invite, email: nil, domain: "test.com") }
|
|
|
|
|
|
|
|
it "raises an error if the email domain does not match the invite domain" do
|
|
|
|
redeemer =
|
|
|
|
InviteRedeemer.new(
|
|
|
|
invite: invite,
|
|
|
|
email: "blah@somesite.com",
|
|
|
|
username: username,
|
|
|
|
name: name,
|
|
|
|
)
|
|
|
|
expect { redeemer.redeem }.to raise_error(
|
|
|
|
ActiveRecord::RecordNotSaved,
|
|
|
|
I18n.t("invite.domain_not_allowed"),
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
2022-11-01 12:33:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when a redeeming user is passed in" do
|
|
|
|
fab!(:redeeming_user) { Fabricate(:user, email: "foo@test.com") }
|
|
|
|
|
|
|
|
it "raises an error if the user's email domain does not match the invite domain" do
|
|
|
|
redeeming_user.update!(email: "foo@bar.com")
|
|
|
|
redeemer = InviteRedeemer.new(invite: invite, redeeming_user: redeeming_user)
|
|
|
|
expect { redeemer.redeem }.to raise_error(
|
|
|
|
ActiveRecord::RecordNotSaved,
|
|
|
|
I18n.t("invite.domain_not_allowed"),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with invite_link" do
|
2021-01-20 03:50:02 -05:00
|
|
|
fab!(:invite_link) do
|
|
|
|
Fabricate(
|
|
|
|
:invite,
|
|
|
|
email: nil,
|
|
|
|
max_redemptions_allowed: 5,
|
|
|
|
expires_at: 1.month.from_now,
|
|
|
|
emailed_status: Invite.emailed_status_types[:not_required],
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2020-06-09 11:19:32 -04:00
|
|
|
let(:invite_redeemer) { InviteRedeemer.new(invite: invite_link, email: "foo@example.com") }
|
|
|
|
|
|
|
|
it "works as expected" do
|
|
|
|
user = invite_redeemer.redeem
|
|
|
|
invite_link.reload
|
|
|
|
|
|
|
|
expect(user.send_welcome_message).to eq(true)
|
|
|
|
expect(user.trust_level).to eq(SiteSetting.default_invitee_trust_level)
|
|
|
|
expect(user.active).to eq(false)
|
|
|
|
expect(invite_link.redemption_count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2024-02-27 05:24:20 -05:00
|
|
|
it "raises an error if email has already been invited" do
|
2022-11-01 12:33:32 -04:00
|
|
|
invite_redeemer.redeem
|
2020-06-09 11:19:32 -04:00
|
|
|
invite_link.reload
|
|
|
|
|
|
|
|
another_invite_redeemer = InviteRedeemer.new(invite: invite_link, email: "foo@example.com")
|
2024-02-27 05:24:20 -05:00
|
|
|
expect { another_invite_redeemer.redeem }.to raise_error(
|
|
|
|
Invite::UserExists,
|
|
|
|
I18n.t("invite.existing_user_already_redemeed"),
|
|
|
|
)
|
2020-06-09 11:19:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should redeem the invite if InvitedUser record does not exists for email" do
|
2022-11-01 12:33:32 -04:00
|
|
|
invite_redeemer.redeem
|
2020-06-09 11:19:32 -04:00
|
|
|
invite_link.reload
|
|
|
|
|
|
|
|
another_invite_redeemer = InviteRedeemer.new(invite: invite_link, email: "bar@example.com")
|
|
|
|
another_user = another_invite_redeemer.redeem
|
|
|
|
expect(another_user.is_a?(User)).to eq(true)
|
|
|
|
end
|
2019-12-17 10:13:49 -05:00
|
|
|
|
2022-11-01 12:33:32 -04:00
|
|
|
it "raises an error if the email is already being used by an existing user" do
|
|
|
|
Fabricate(:user, email: "foo@example.com")
|
|
|
|
expect { invite_redeemer.redeem }.to raise_error(
|
|
|
|
ActiveRecord::RecordInvalid,
|
|
|
|
/Primary email has already been taken/,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when a redeeming user is passed in" do
|
|
|
|
fab!(:redeeming_user) { Fabricate(:user, email: "foo@example.com") }
|
|
|
|
|
|
|
|
it "does not create a new user" do
|
|
|
|
expect do
|
|
|
|
InviteRedeemer.new(invite: invite_link, redeeming_user: redeeming_user).redeem
|
|
|
|
end.not_to change { User.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-12 23:58:31 -04:00
|
|
|
end
|
2013-08-28 03:18:31 -04:00
|
|
|
end
|