2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-08-28 03:18:31 -04:00
|
|
|
|
|
|
|
describe InviteRedeemer do
|
|
|
|
|
2016-07-12 23:58:31 -04:00
|
|
|
describe '#create_user_from_invite' do
|
2013-08-28 03:18:31 -04:00
|
|
|
it "should be created correctly" do
|
2017-02-13 16:19:41 -05:00
|
|
|
user = InviteRedeemer.create_user_from_invite(Fabricate(:invite, email: 'walter.white@email.com'), 'walter', '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).to be_active
|
|
|
|
expect(user.email).to eq('walter.white@email.com')
|
2017-03-05 08:55:21 -05:00
|
|
|
expect(user.approved).to eq(true)
|
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'
|
|
|
|
user = InviteRedeemer.create_user_from_invite(Fabricate(:invite, email: 'walter.white@email.com'), 'walter', 'Walter White', password, nil, ip_address)
|
2017-02-13 16:19:41 -05:00
|
|
|
expect(user).to have_password
|
|
|
|
expect(user.confirm_password?(password)).to eq(true)
|
2017-03-05 08:55:21 -05:00
|
|
|
expect(user.approved).to eq(true)
|
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
|
|
|
|
begin
|
|
|
|
InviteRedeemer.create_user_from_invite(Fabricate(:invite, email: 'walter.white@email.com'), 'walter', 'Walter White', 'aaa')
|
|
|
|
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')
|
|
|
|
user = InviteRedeemer.create_user_from_invite(Fabricate(:invite, email: 'staged@account.com'), 'walter', 'Walter White')
|
|
|
|
|
|
|
|
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')
|
|
|
|
expect(user.approved).to eq(true)
|
|
|
|
end
|
2019-07-19 01:59:12 -04:00
|
|
|
|
|
|
|
it "should not activate user invited via links" do
|
|
|
|
user = InviteRedeemer.create_user_from_invite(Fabricate(:invite, email: 'walter.white@email.com', emailed_status: Invite.emailed_status_types[:not_required]), 'walter', 'Walter White')
|
|
|
|
expect(user.username).to eq('walter')
|
|
|
|
expect(user.name).to eq('Walter White')
|
|
|
|
expect(user.email).to eq('walter.white@email.com')
|
|
|
|
expect(user.approved).to eq(true)
|
|
|
|
expect(user.active).to eq(false)
|
|
|
|
end
|
2013-08-28 03:18:31 -04:00
|
|
|
end
|
2016-07-12 23:58:31 -04:00
|
|
|
|
|
|
|
describe "#redeem" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:invite) { Fabricate(:invite) }
|
2016-07-12 23:58:31 -04:00
|
|
|
let(:name) { 'john snow' }
|
|
|
|
let(:username) { 'kingofthenorth' }
|
2017-07-27 21:20:09 -04:00
|
|
|
let(:password) { 'know5nOthiNG' }
|
2016-07-12 23:58:31 -04:00
|
|
|
let(:invite_redeemer) { InviteRedeemer.new(invite, username, name) }
|
|
|
|
|
2017-03-05 08:55:21 -05:00
|
|
|
it "should redeem the invite if invited by staff" do
|
|
|
|
SiteSetting.must_approve_users = true
|
2016-07-12 23:58:31 -04:00
|
|
|
inviter = invite.invited_by
|
2017-03-05 08:55:21 -05:00
|
|
|
inviter.admin = true
|
2016-07-12 23:58:31 -04:00
|
|
|
user = invite_redeemer.redeem
|
2019-12-17 10:13:49 -05:00
|
|
|
invite.reload
|
2016-07-12 23:58:31 -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)
|
2017-03-05 08:55:21 -05:00
|
|
|
expect(user.approved).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should redeem the invite if invited by non staff but not approve" do
|
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
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(inviter.notifications.count).to eq(1)
|
|
|
|
expect(user.approved).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should redeem the invite if invited by non staff and approve if staff not required to approve" 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(inviter.notifications.count).to eq(1)
|
|
|
|
expect(user.approved).to eq(true)
|
2016-07-12 23:58:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not blow up if invited_by user has been removed" do
|
|
|
|
invite.invited_by.destroy!
|
|
|
|
invite.reload
|
|
|
|
|
|
|
|
user = invite_redeemer.redeem
|
|
|
|
|
|
|
|
expect(user.name).to eq(name)
|
|
|
|
expect(user.username).to eq(username)
|
|
|
|
expect(user.invited_by).to eq(nil)
|
|
|
|
end
|
2017-02-13 16:19:41 -05:00
|
|
|
|
|
|
|
it "can set password" do
|
|
|
|
user = InviteRedeemer.new(invite, username, name, password).redeem
|
|
|
|
expect(user).to have_password
|
|
|
|
expect(user.confirm_password?(password)).to eq(true)
|
2017-03-05 08:55:21 -05:00
|
|
|
expect(user.approved).to eq(true)
|
2017-02-13 16:19:41 -05:00
|
|
|
end
|
2017-06-08 15:10:43 -04:00
|
|
|
|
|
|
|
it "can set custom fields" do
|
|
|
|
required_field = Fabricate(:user_field)
|
2017-07-27 21:20:09 -04:00
|
|
|
optional_field = Fabricate(:user_field, required: false)
|
2017-06-08 15:10:43 -04:00
|
|
|
user_fields = {
|
|
|
|
required_field.id.to_s => 'value1',
|
|
|
|
optional_field.id.to_s => 'value2'
|
|
|
|
}
|
|
|
|
user = InviteRedeemer.new(invite, username, name, password, 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')
|
|
|
|
end
|
2017-11-23 11:39:24 -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)
|
|
|
|
user = InviteRedeemer.new(invite, username, name, password).redeem
|
|
|
|
|
|
|
|
expect(user.group_users.count).to eq(4)
|
|
|
|
expect(user.trust_level).to eq(2)
|
|
|
|
end
|
2018-04-27 09:51:10 -04:00
|
|
|
|
|
|
|
it "only allows one user to be created per invite" do
|
|
|
|
user = invite_redeemer.redeem
|
|
|
|
invite.reload
|
|
|
|
|
|
|
|
user.email = "john@example.com"
|
|
|
|
user.save!
|
|
|
|
|
|
|
|
another_invite_redeemer = InviteRedeemer.new(invite, username, name)
|
|
|
|
another_user = another_invite_redeemer.redeem
|
|
|
|
expect(another_user).to eq(nil)
|
|
|
|
end
|
2019-12-17 10:13:49 -05:00
|
|
|
|
|
|
|
it "should correctly update the invite redeemed_at date" do
|
|
|
|
SiteSetting.invite_expiry_days = 2
|
|
|
|
invite.update!(created_at: 10.days.ago)
|
|
|
|
|
|
|
|
inviter = invite.invited_by
|
|
|
|
inviter.admin = true
|
|
|
|
user = invite_redeemer.redeem
|
|
|
|
invite.reload
|
|
|
|
|
|
|
|
expect(user.invited_by).to eq(inviter)
|
|
|
|
expect(inviter.notifications.count).to eq(1)
|
|
|
|
expect(invite.redeemed_at).not_to eq(nil)
|
|
|
|
end
|
|
|
|
|
2016-07-12 23:58:31 -04:00
|
|
|
end
|
2013-08-28 03:18:31 -04:00
|
|
|
end
|