2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe EmailLog do
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it { should belong_to :user }
|
|
|
|
it { should validate_presence_of :to_address }
|
|
|
|
it { should validate_presence_of :email_type }
|
|
|
|
|
2014-02-15 02:06:21 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-02-15 02:06:21 +08:00
|
|
|
context 'after_create' do
|
2013-06-11 04:46:08 +08:00
|
|
|
context 'with user' do
|
|
|
|
it 'updates the last_emailed_at value for the user' do
|
|
|
|
lambda {
|
|
|
|
user.email_logs.create(email_type: 'blah', to_address: user.email)
|
|
|
|
user.reload
|
|
|
|
}.should change(user, :last_emailed_at)
|
|
|
|
end
|
2014-02-15 02:06:21 +08: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-06 03:16:51 +08:00
|
|
|
end
|
2014-02-15 02:06:21 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-02-15 02:06:21 +08: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)
|
|
|
|
described_class.count_per_day.first[1].should == 1
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-11-15 23:27:43 +08: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-09-25 23:44:48 +08:00
|
|
|
expect(user.email_logs.last_sent_email_address).should == nil
|
2013-11-15 23:27:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|