FIX: Use updated_at date to denote expired invites (#8521)

This commit is contained in:
Penar Musaraj 2019-12-17 10:13:49 -05:00 committed by GitHub
parent 8106d94c05
commit ea05a68df7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 9 deletions

View File

@ -55,7 +55,7 @@ class Invite < ActiveRecord::Base
end
def expired?
created_at < SiteSetting.invite_expiry_days.days.ago
updated_at < SiteSetting.invite_expiry_days.days.ago
end
# link_valid? indicates whether the invite link can be used to log in to the site
@ -191,7 +191,7 @@ class Invite < ActiveRecord::Base
end
def self.find_pending_invites_from(inviter, offset = 0)
find_all_invites_from(inviter, offset).where('invites.user_id IS NULL').order('invites.created_at DESC')
find_all_invites_from(inviter, offset).where('invites.user_id IS NULL').order('invites.updated_at DESC')
end
def self.find_redeemed_invites_from(inviter, offset = 0)
@ -244,7 +244,7 @@ class Invite < ActiveRecord::Base
end
def self.rescind_all_expired_invites_from(user)
Invite.where('invites.user_id IS NULL AND invites.email IS NOT NULL AND invited_by_id = ? AND invites.created_at < ?',
Invite.where('invites.user_id IS NULL AND invites.email IS NOT NULL AND invited_by_id = ? AND invites.updated_at < ?',
user.id, SiteSetting.invite_expiry_days.days.ago).find_each do |invite|
invite.trash!(user)
end

View File

@ -88,7 +88,7 @@ InviteRedeemer = Struct.new(:invite, :username, :name, :password, :user_custom_f
end
def mark_invite_redeemed
count = Invite.where('id = ? AND redeemed_at IS NULL AND created_at >= ?',
count = Invite.where('id = ? AND redeemed_at IS NULL AND updated_at >= ?',
invite.id, SiteSetting.invite_expiry_days.days.ago)
.update_all('redeemed_at = CURRENT_TIMESTAMP')

View File

@ -1223,7 +1223,7 @@ en:
search: "type to search invites..."
title: "Invites"
user: "Invited User"
sent: "Sent"
sent: "Last Sent"
none: "No invites to display."
truncated:
one: "Showing the first invite."

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddIndexToInvites < ActiveRecord::Migration[6.0]
def change
add_index :invites, [:invited_by_id]
end
end

View File

@ -70,6 +70,7 @@ describe InviteRedeemer do
inviter = invite.invited_by
inviter.admin = true
user = invite_redeemer.redeem
invite.reload
expect(user.name).to eq(name)
expect(user.username).to eq(username)
@ -153,5 +154,20 @@ describe InviteRedeemer do
another_user = another_invite_redeemer.redeem
expect(another_user).to eq(nil)
end
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
end
end

View File

@ -109,7 +109,6 @@ describe Invite do
expect(new_invite.invite_key).not_to eq(invite.invite_key)
expect(new_invite.topics).to eq([topic])
end
end
context 'when adding a duplicate' do
@ -137,7 +136,7 @@ describe Invite do
it 'returns a new invite if the other has expired' do
SiteSetting.invite_expiry_days = 1
invite.update!(created_at: 2.days.ago)
invite.update!(updated_at: 2.days.ago)
new_invite = Invite.invite_by_email(
'iceking@adventuretime.ooo', inviter, topic
@ -159,6 +158,15 @@ describe Invite do
end
end
it 'resets expiry of a resent invite' do
SiteSetting.invite_expiry_days = 2
invite.update!(updated_at: 10.days.ago)
expect(invite).to be_expired
invite.resend_invite
expect(invite).not_to be_expired
end
it 'correctly marks invite emailed_status for email invites' do
expect(invite.emailed_status).to eq(Invite.emailed_status_types[:sending])
@ -214,7 +222,7 @@ describe Invite do
it 'wont redeem an expired invite' do
SiteSetting.invite_expiry_days = 10
invite.update_column(:created_at, 20.days.ago)
invite.update_column(:updated_at, 20.days.ago)
expect(invite.redeem).to be_blank
end
@ -493,7 +501,7 @@ describe Invite do
invite_1 = Fabricate(:invite, invited_by: user)
invite_2 = Fabricate(:invite, invited_by: user)
expired_invite = Fabricate(:invite, invited_by: user)
expired_invite.update!(created_at: 2.days.ago)
expired_invite.update!(updated_at: 2.days.ago)
Invite.rescind_all_expired_invites_from(user)
invite_1.reload
invite_2.reload