Files
grecia/spec/models/newsletter_spec.rb
Bertocq bdbb32e824 Move newsletter User scope outside UserSegments
Why:

UserSegments are not only used for Newsletters or Email downloads, but
also for internal Global Notifications. We don't want to have that scope
hardcoded inside UserSegments as users that have opted-out from the
newsletter should still be recipients of global notifications.

How:

Removing the scope from the UserSegments `all_users` method that acts as
base for all the other segments. Including that `newsletter` scope only
on the places that is relevant:
* When listing recipients for a newsletter
* When downloading a listing emails that can be newsletter recipients

Also updated relevant tests
2018-02-21 11:45:38 +01:00

53 lines
1.4 KiB
Ruby

require 'rails_helper'
describe Newsletter do
let(:newsletter) { build(:newsletter) }
it "is valid" do
expect(newsletter).to be_valid
end
it 'is not valid without a subject' do
newsletter.subject = nil
expect(newsletter).not_to be_valid
end
it 'is not valid without a segment_recipient' do
newsletter.segment_recipient = nil
expect(newsletter).not_to be_valid
end
it 'is not valid with an inexistent user segment for segment_recipient' do
newsletter.segment_recipient = 'invalid_user_segment_name'
expect(newsletter).not_to be_valid
end
it 'is not valid without a from' do
newsletter.from = nil
expect(newsletter).not_to be_valid
end
it 'is not valid without a body' do
newsletter.body = nil
expect(newsletter).not_to be_valid
end
it 'validates from attribute email format' do
newsletter.from = "this_is_not_an_email"
expect(newsletter).not_to be_valid
end
describe '#list_of_recipients' do
before do
create(:user, newsletter: true, username: 'newsletter_user')
create(:user, newsletter: false)
newsletter.update(segment_recipient: 'all_users')
end
it 'returns list of recipients excluding users with disabled newsletter' do
expect(newsletter.list_of_recipients.count).to eq(1)
expect(newsletter.list_of_recipients.first.username).to eq('newsletter_user')
end
end
end