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')
|
2013-08-28 03:18:31 -04:00
|
|
|
end
|
2017-02-13 16:19:41 -05:00
|
|
|
|
|
|
|
it "can set the password too" do
|
|
|
|
password = 's3cure5tpasSw0rD'
|
|
|
|
user = InviteRedeemer.create_user_from_invite(Fabricate(:invite, email: 'walter.white@email.com'), 'walter', 'Walter White', password)
|
|
|
|
expect(user).to have_password
|
|
|
|
expect(user.confirm_password?(password)).to eq(true)
|
|
|
|
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
|
2013-08-28 03:18:31 -04:00
|
|
|
end
|
2016-07-12 23:58:31 -04:00
|
|
|
|
|
|
|
describe "#redeem" do
|
|
|
|
let(:invite) { Fabricate(:invite) }
|
|
|
|
let(:name) { 'john snow' }
|
|
|
|
let(:username) { 'kingofthenorth' }
|
2017-02-13 16:19:41 -05:00
|
|
|
let(:password) { 'know5nOthiNG'}
|
2016-07-12 23:58:31 -04:00
|
|
|
let(:invite_redeemer) { InviteRedeemer.new(invite, username, name) }
|
|
|
|
|
|
|
|
it "should redeem the invite" 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)
|
|
|
|
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
|
|
|
|
inviter = invite.invited_by
|
|
|
|
user = InviteRedeemer.new(invite, username, name, password).redeem
|
|
|
|
expect(user).to have_password
|
|
|
|
expect(user.confirm_password?(password)).to eq(true)
|
|
|
|
end
|
2016-07-12 23:58:31 -04:00
|
|
|
end
|
2013-08-28 03:18:31 -04:00
|
|
|
end
|