2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe EmailLog do
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
it { is_expected.to belong_to :user }
|
|
|
|
it { is_expected.to validate_presence_of :to_address }
|
|
|
|
it { is_expected.to validate_presence_of :email_type }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-02-14 13:06:21 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-04-15 01:59:01 -04:00
|
|
|
context 'unique email per post' do
|
|
|
|
it 'only allows through one email per post' do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
user = post.user
|
|
|
|
|
|
|
|
# skipped emails do not matter
|
2017-05-03 08:14:32 -04:00
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', post_id: post.id, to_address: user.email, user_id: user.id, skipped: true)
|
2016-04-15 01:59:01 -04:00
|
|
|
|
|
|
|
ran = EmailLog.unique_email_per_post(post, user) do
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(ran).to eq(true)
|
|
|
|
|
2017-05-03 08:14:32 -04:00
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', post_id: post.id, to_address: user.email, user_id: user.id)
|
2016-04-15 01:59:01 -04:00
|
|
|
|
|
|
|
ran = EmailLog.unique_email_per_post(post, user) do
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(ran).to be_falsy
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-14 13:06:21 -05:00
|
|
|
context 'after_create' do
|
2013-06-10 16:46:08 -04:00
|
|
|
context 'with user' do
|
|
|
|
it 'updates the last_emailed_at value for the user' do
|
2014-12-31 09:55:03 -05:00
|
|
|
expect {
|
2013-06-10 16:46:08 -04:00
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email)
|
|
|
|
user.reload
|
2014-12-31 09:55:03 -05:00
|
|
|
}.to change(user, :last_emailed_at)
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
2014-02-14 13:06:21 -05:00
|
|
|
|
|
|
|
it "doesn't update last_emailed_at if skipped is true" do
|
|
|
|
expect {
|
2017-05-03 08:14:32 -04:00
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', to_address: user.email, skipped: true)
|
2014-02-14 13:06:21 -05:00
|
|
|
user.reload
|
|
|
|
}.to_not change { user.last_emailed_at }
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2014-02-14 13:06:21 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-03-23 00:08:34 -04:00
|
|
|
describe '#reached_max_emails?' do
|
2017-05-03 04:03:43 -04:00
|
|
|
before do
|
2016-03-23 00:08:34 -04:00
|
|
|
SiteSetting.max_emails_per_day_per_user = 2
|
2017-05-03 08:14:32 -04:00
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', to_address: user.email, user_id: user.id, skipped: true)
|
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', to_address: user.email, user_id: user.id)
|
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', to_address: user.email, user_id: user.id, created_at: 3.days.ago)
|
2017-05-03 04:03:43 -04:00
|
|
|
end
|
2016-03-23 00:08:34 -04:00
|
|
|
|
2017-05-03 04:03:43 -04:00
|
|
|
it "tracks when max emails are reached" do
|
2016-03-23 00:08:34 -04:00
|
|
|
expect(EmailLog.reached_max_emails?(user)).to eq(false)
|
|
|
|
|
2017-05-03 08:14:32 -04:00
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', to_address: user.email, user_id: user.id)
|
2016-03-23 00:08:34 -04:00
|
|
|
expect(EmailLog.reached_max_emails?(user)).to eq(true)
|
|
|
|
end
|
2017-05-03 04:03:43 -04:00
|
|
|
|
2017-05-03 07:36:01 -04:00
|
|
|
it "returns false for critical email" do
|
2017-05-03 08:14:32 -04:00
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', to_address: user.email, user_id: user.id)
|
2017-05-03 04:03:43 -04:00
|
|
|
expect(EmailLog.reached_max_emails?(user, 'forgot_password')).to eq(false)
|
2017-05-03 07:36:01 -04:00
|
|
|
expect(EmailLog.reached_max_emails?(user, 'confirm_new_email')).to eq(false)
|
2017-05-03 04:03:43 -04:00
|
|
|
end
|
2016-03-23 00:08:34 -04:00
|
|
|
end
|
|
|
|
|
2014-02-14 13:06:21 -05:00
|
|
|
describe '#count_per_day' do
|
|
|
|
it "counts sent emails" do
|
2017-05-03 08:14:32 -04:00
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', to_address: user.email)
|
|
|
|
Fabricate(:email_log, user: user, email_type: 'blah', to_address: user.email, skipped: true)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(described_class.count_per_day(1.day.ago, Time.now).first[1]).to eq 1
|
2014-02-14 13:06:21 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
describe ".last_sent_email_address" do
|
|
|
|
context "when user's email exist in the logs" do
|
|
|
|
before do
|
|
|
|
user.email_logs.create(email_type: 'signup', to_address: user.email)
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email)
|
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it "the user's last email from the log" do
|
|
|
|
expect(user.email_logs.last_sent_email_address).to eq(user.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user's email does not exist email logs" do
|
|
|
|
it "returns nil" do
|
2014-10-31 13:44:26 -04:00
|
|
|
expect(user.email_logs.last_sent_email_address).to be_nil
|
2013-11-15 10:27:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-17 05:05:04 -04:00
|
|
|
%w{reply_key bounce_key}.each do |key|
|
|
|
|
describe "##{key}" do
|
|
|
|
it "should format the #{key} correctly" do
|
|
|
|
hex = SecureRandom.hex
|
|
|
|
email_log = Fabricate(:email_log, user: user, "#{key}": hex)
|
|
|
|
|
|
|
|
raw_key = EmailLog.where(id: email_log.id)
|
|
|
|
.pluck("#{key}::text")
|
|
|
|
.first
|
|
|
|
|
|
|
|
expect(raw_key).to_not eq(hex)
|
|
|
|
expect(raw_key.delete('-')).to eq(hex)
|
|
|
|
expect(EmailLog.find(email_log.id).send(key)).to eq(hex)
|
|
|
|
end
|
2018-07-16 08:05:54 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|