2013-02-05 14:16:51 -05:00
require 'spec_helper'
describe Invite do
it { should validate_presence_of :invited_by_id }
2013-06-25 22:15:41 +08: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
invite . should_not be_valid
end
it " should not allow a user to invite themselves " do
2014-09-25 17:44:48 +02:00
invite . email_already_exists . should == true
2013-02-05 14:16:51 -05:00
end
end
2014-10-09 20:14:15 +05:30
context 'email validators' do
let ( :coding_horror ) { Fabricate ( :coding_horror ) }
let ( :invite ) { Invite . create ( email : " test@mailinator.com " , invited_by : coding_horror ) }
it " should not allow an invite with blacklisted email " do
invite . should_not be_valid
end
it " should allow an invite with non-blacklisted email " do
invite = Fabricate ( :invite , email : " test@mail.com " , invited_by : coding_horror )
invite . should be_valid
end
end
2013-02-05 14:16:51 -05:00
context '#create' do
context 'saved' do
subject { Fabricate ( :invite ) }
2014-10-29 16:06:50 +01:00
it " works " do
subject . invite_key . should be_present
subject . email_already_exists . should == false
end
2013-02-05 14:16:51 -05:00
it 'should store a lower case version of the email' do
2013-06-25 22:15:41 +08:00
subject . email . should == 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 19:42:20 +03:00
2013-02-05 14:16:51 -05:00
context 'email' do
it 'enqueues a job to email the invite' do
Jobs . expects ( :enqueue ) . with ( :invite_email , has_key ( :invite_id ) )
2013-06-25 22:15:41 +08:00
topic . invite_by_email ( inviter , iceking )
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
2013-06-25 22:15:41 +08:00
invite = topic . invite_by_email ( inviter , iceking )
2013-02-05 14:16:51 -05:00
invite . destroy
2013-06-25 22:15:41 +08:00
invite = topic . invite_by_email ( inviter , iceking )
2013-02-05 14:16:51 -05:00
invite . should be_present
2013-02-25 19:42:20 +03:00
end
2013-02-05 14:16:51 -05:00
end
context 'after created' do
before do
2013-06-25 22:15:41 +08:00
@invite = topic . invite_by_email ( inviter , iceking )
2013-02-05 14:16:51 -05:00
end
it 'belongs to the topic' do
topic . invites . should == [ @invite ]
@invite . topics . should == [ topic ]
end
context 'when added by another user' do
let ( :coding_horror ) { Fabricate ( :coding_horror ) }
2013-06-25 22:15:41 +08:00
let ( :new_invite ) { topic . invite_by_email ( coding_horror , iceking ) }
2013-02-05 14:16:51 -05:00
it 'returns a different invite' do
new_invite . should_not == @invite
new_invite . invite_key . should_not == @invite . invite_key
new_invite . topics . should == [ topic ]
end
2014-05-08 16:45:49 +10:00
2013-02-05 14:16:51 -05:00
end
context 'when adding a duplicate' do
it 'returns the original invite' do
topic . invite_by_email ( inviter , 'iceking@adventuretime.ooo' ) . should == @invite
2013-04-15 02:20:33 +02:00
topic . invite_by_email ( inviter , 'iceking@ADVENTURETIME.ooo' ) . should == @invite
2014-07-14 10:16:24 -04:00
topic . invite_by_email ( inviter , 'ICEKING@adventuretime.ooo' ) . should == @invite
2013-02-05 14:16:51 -05:00
end
2014-01-21 15:13:55 -05:00
it 'returns a new invite if the other has expired' do
SiteSetting . stubs ( :invite_expiry_days ) . returns ( 1 )
@invite . created_at = 2 . days . ago
@invite . save
new_invite = topic . invite_by_email ( inviter , 'iceking@adventuretime.ooo' )
new_invite . should_not == @invite
new_invite . should_not be_expired
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
2014-05-08 16:45:49 +10:00
@new_invite = another_topic . invite_by_email ( inviter , iceking )
2013-02-05 14:16:51 -05:00
@new_invite . should == @invite
another_topic . invites . should == [ @invite ]
@invite . topics . should =~ [ topic , another_topic ]
end
2014-05-08 16:45:49 +10:00
2013-02-25 19:42:20 +03:00
end
2013-02-05 14:16:51 -05:00
end
end
end
context 'an existing user' do
2014-09-11 17:39:20 +10: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 ) }
let! ( :invite ) { topic . invite_by_email ( topic . user , coding_horror . email ) }
2014-09-11 17:39:20 +10:00
it " works " do
# doesn't create an invite
2013-02-05 14:16:51 -05:00
invite . should be_blank
2014-09-11 17:39:20 +10:00
# gives the user permission to access the topic
2014-09-25 17:44:48 +02:00
topic . allowed_users . include? ( coding_horror ) . should == true
2013-02-05 14:16:51 -05:00
end
2014-09-11 17:39:20 +10:00
2013-02-05 14:16:51 -05:00
end
context '.redeem' do
let ( :invite ) { Fabricate ( :invite ) }
it 'creates a notification for the invitee' do
lambda { invite . redeem } . should change ( Notification , :count )
end
it 'wont redeem an expired invite' do
SiteSetting . expects ( :invite_expiry_days ) . returns ( 10 )
invite . update_column ( :created_at , 20 . days . ago )
invite . redeem . should be_blank
end
it 'wont redeem a deleted invite' do
invite . destroy
invite . redeem . should be_blank
end
2014-01-21 16:53:46 -05:00
it " won't redeem an invalidated invite " do
invite . invalidated_at = 1 . day . ago
invite . redeem . should be_blank
end
2014-10-10 21:17:52 +05:30
context 'enqueues a job to email "set password" instructions' do
it 'does not enqueue an email if sso is enabled' do
SiteSetting . stubs ( :enable_sso ) . returns ( true )
Jobs . expects ( :enqueue ) . with ( :invite_password_instructions_email , has_key ( :username ) ) . never
invite . redeem
end
it 'does not enqueue an email if local login is disabled' do
SiteSetting . stubs ( :enable_local_logins ) . returns ( false )
Jobs . expects ( :enqueue ) . with ( :invite_password_instructions_email , has_key ( :username ) ) . never
invite . redeem
end
it 'does not enqueue an email if the user has already set password' do
user = Fabricate ( :user , email : invite . email , password_hash : " 7af7805c9ee3697ed1a83d5e3cb5a3a431d140933a87fdcdc5a42aeef9337f81 " )
Jobs . expects ( :enqueue ) . with ( :invite_password_instructions_email , has_key ( :username ) ) . never
invite . redeem
end
it 'enqueues an email if all conditions are satisfied' do
Jobs . expects ( :enqueue ) . with ( :invite_password_instructions_email , has_key ( :username ) )
invite . redeem
end
end
2014-05-08 16:45:49 +10: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
user . groups . count . should == 1
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
2014-09-05 15:20:39 +10:00
SiteSetting . stubs ( :default_invitee_trust_level ) . returns ( TrustLevel [ 3 ] )
invite . redeem . trust_level . should == TrustLevel [ 3 ]
2013-02-05 14:16:51 -05:00
end
end
2013-07-11 11:21:39 +10:00
context 'inviting when must_approve_users? is enabled' do
2014-01-17 11:11:42 -05:00
it 'correctly activates accounts' do
2013-07-11 11:21:39 +10:00
SiteSetting . stubs ( :must_approve_users ) . returns ( true )
user = invite . redeem
user . approved? . should == true
end
end
2013-02-05 14:16:51 -05:00
context 'simple invite' do
2013-02-25 19:42:20 +03:00
2013-02-05 14:16:51 -05:00
let! ( :user ) { invite . redeem }
2013-07-11 11:21:39 +10:00
it 'works correctly' do
2014-09-25 17:44:48 +02:00
user . is_a? ( User ) . should == true
user . send_welcome_message . should == true
2013-02-05 14:16:51 -05:00
user . trust_level . should == SiteSetting . default_invitee_trust_level
end
context 'after redeeming' do
before do
invite . reload
end
2013-07-11 11:21:39 +10:00
it 'works correctly' do
# has set the user_id attribute
2013-02-05 14:16:51 -05:00
invite . user . should == user
2013-07-11 11:21:39 +10:00
# returns true for redeemed
2013-02-05 14:16:51 -05:00
invite . should be_redeemed
end
2013-07-11 11:21:39 +10:00
2013-02-05 14:16:51 -05:00
context 'again' do
2014-07-01 12:52:52 -04:00
context " without a passthrough " do
before do
SiteSetting . invite_passthrough_hours = 0
end
it 'will not redeem twice' do
invite . redeem . should be_blank
end
2013-02-05 14:16:51 -05:00
end
2014-07-01 12:52:52 -04:00
context " with a passthrough " do
before do
SiteSetting . invite_passthrough_hours = 1
end
it 'will not redeem twice' do
invite . redeem . should be_present
2014-09-25 17:44:48 +02:00
invite . redeem . send_welcome_message . should == false
2014-07-01 12:52:52 -04:00
end
end
2013-02-05 14:16:51 -05:00
end
end
end
context 'invited to topics' do
let! ( :topic ) { Fabricate ( :private_message_topic ) }
2014-05-09 11:45:18 +10:00
let! ( :invite ) {
topic . invite ( topic . user , 'jake@adventuretime.ooo' )
}
2013-02-05 14:16:51 -05:00
context 'redeem topic invite' do
it 'adds the user to the topic_users' do
2014-05-09 11:45:18 +10:00
user = invite . redeem
topic . reload
2014-09-25 17:44:48 +02:00
topic . allowed_users . include? ( user ) . should == true
Guardian . new ( user ) . can_see? ( topic ) . should == true
2013-02-05 14:16:51 -05:00
end
2014-05-09 11:45:18 +10:00
2013-02-05 14:16:51 -05:00
end
context 'invited by another user to the same topic' do
2014-05-06 14:41:59 +01:00
let ( :coding_horror ) { User . find_by ( username : " CodingHorror " ) }
2013-02-05 14:16:51 -05:00
let! ( :another_invite ) { topic . invite ( coding_horror , 'jake@adventuretime.ooo' ) }
let! ( :user ) { invite . redeem }
it 'adds the user to the topic_users' do
2014-05-09 11:45:18 +10:00
topic . reload
2014-09-25 17:44:48 +02:00
topic . allowed_users . include? ( user ) . should == true
2013-02-05 14:16:51 -05:00
end
end
context 'invited by another user to a different topic' do
let! ( :another_invite ) { another_topic . invite ( coding_horror , 'jake@adventuretime.ooo' ) }
let! ( :user ) { invite . redeem }
2014-05-09 11:45:18 +10:00
let ( :coding_horror ) { User . find_by ( username : " CodingHorror " ) }
2014-09-11 17:39:20 +10:00
let ( :another_topic ) { Fabricate ( :topic , category_id : nil , archetype : " private_message " , user : coding_horror ) }
2014-05-09 11:45:18 +10:00
2013-02-05 14:16:51 -05:00
it 'adds the user to the topic_users of the first topic' do
2014-09-25 17:44:48 +02:00
topic . allowed_users . include? ( user ) . should == true
another_topic . allowed_users . include? ( user ) . should == true
2013-02-05 14:16:51 -05:00
another_invite . reload
another_invite . should_not be_redeemed
end
context 'if they redeem the other invite afterwards' do
it 'returns the same user' do
2014-05-09 11:45:18 +10:00
result = another_invite . redeem
result . should == user
2013-02-05 14:16:51 -05:00
another_invite . reload
another_invite . should be_redeemed
2013-02-25 19:42:20 +03:00
end
2013-02-05 14:16:51 -05:00
end
end
2013-02-25 19:42:20 +03:00
end
2013-02-05 14:16:51 -05:00
end
2013-11-08 11:11:41 -08: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 16:45:49 +10:00
Fabricate ( :invite )
2013-11-08 11:11:41 -08:00
invites = Invite . find_all_invites_from ( user )
expect ( invites ) . to be_empty
end
end
end
describe '.find_redeemed_invites_from' do
it 'returns redeemed invites only' do
inviter = Fabricate ( :user )
2014-05-08 16:45:49 +10:00
Fabricate (
2013-11-08 11:11:41 -08:00
:invite ,
invited_by : inviter ,
user_id : nil ,
email : 'pending@example.com'
)
2014-05-08 16:45:49 +10:00
2013-11-08 11:11:41 -08:00
redeemed_invite = Fabricate (
:invite ,
invited_by : inviter ,
user_id : 123 ,
email : 'redeemed@example.com'
)
invites = Invite . find_redeemed_invites_from ( inviter )
2014-11-07 06:05:44 -08:00
invites . size . should == 1
2013-11-08 11:11:41 -08:00
expect ( invites . first ) . to eq redeemed_invite
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
subject . should == nil
end
it 'sets the matching invite to be invalid' do
invite = Fabricate ( :invite , invited_by : Fabricate ( :user ) , user_id : nil , email : email )
subject . should == invite
subject . link_valid? . should == false
subject . should be_valid
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' )
result . should == invite
result . link_valid? . should == false
result . should be_valid
end
end
2014-07-05 01:23:41 +05:30
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
result = Invite . redeem_from_email ( user . email )
invite . reload
invite . should be_redeemed
end
it 'does not redeem the invite if email does not match' do
result = Invite . redeem_from_email ( 'test24@example.com' )
invite . reload
invite . should_not be_redeemed
end
end
2014-07-14 21:26:26 +05:30
describe '.redeem_from_token' 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 token' do
result = Invite . redeem_from_token ( invite . invite_key , user . email )
invite . reload
invite . should be_redeemed
end
it 'does not redeem the invite if token does not match' do
result = Invite . redeem_from_token ( " bae0071f995bb4b6f756e80b383778b5 " , user . email )
invite . reload
invite . should_not be_redeemed
end
end
2013-02-05 14:16:51 -05:00
end