2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
describe EmailLog do
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2014-12-31 08:55:03 -06: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 13:16:51 -06:00
|
|
|
|
2014-02-14 12:06:21 -06:00
|
|
|
let(:user) { Fabricate(:user) }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2014-02-14 12:06:21 -06:00
|
|
|
context 'after_create' do
|
2013-06-10 15:46:08 -05:00
|
|
|
context 'with user' do
|
|
|
|
it 'updates the last_emailed_at value for the user' do
|
2014-12-31 08:55:03 -06:00
|
|
|
expect {
|
2013-06-10 15:46:08 -05:00
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email)
|
|
|
|
user.reload
|
2014-12-31 08:55:03 -06:00
|
|
|
}.to change(user, :last_emailed_at)
|
2013-06-10 15:46:08 -05:00
|
|
|
end
|
2014-02-14 12:06:21 -06:00
|
|
|
|
|
|
|
it "doesn't update last_emailed_at if skipped is true" do
|
|
|
|
expect {
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email, skipped: true)
|
|
|
|
user.reload
|
|
|
|
}.to_not change { user.last_emailed_at }
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-02-14 12:06:21 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2016-03-22 23:08:34 -05:00
|
|
|
describe '#reached_max_emails?' do
|
|
|
|
it "tracks when max emails are reached" do
|
|
|
|
SiteSetting.max_emails_per_day_per_user = 2
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id, skipped: true)
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id)
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id, created_at: 3.days.ago)
|
|
|
|
|
|
|
|
expect(EmailLog.reached_max_emails?(user)).to eq(false)
|
|
|
|
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email, user_id: user.id)
|
|
|
|
|
|
|
|
expect(EmailLog.reached_max_emails?(user)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-14 12:06:21 -06:00
|
|
|
describe '#count_per_day' do
|
|
|
|
it "counts sent emails" do
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email)
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email, skipped: true)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(described_class.count_per_day(1.day.ago, Time.now).first[1]).to eq 1
|
2014-02-14 12:06:21 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-11-15 09:27:43 -06: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 12:44:26 -05:00
|
|
|
expect(user.email_logs.last_sent_email_address).to be_nil
|
2013-11-15 09:27:43 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|