|
5 | 5 | subject { create(:notification, published: true) } |
6 | 6 | it { is_expected.to validate_presence_of(:summary) } |
7 | 7 | it { is_expected.to validate_presence_of(:notification_message) } |
| 8 | + |
| 9 | + it 'is invalid if stop_datetime is in the past' do |
| 10 | + notification = build(:notification, stop_datetime: 1.day.ago) |
| 11 | + expect(notification).not_to be_valid |
| 12 | + expect(notification.errors[:stop_datetime]).to include('must be in the future') |
| 13 | + end |
| 14 | + |
| 15 | + it 'is valid if stop_datetime is in the future' do |
| 16 | + notification = build(:notification, stop_datetime: 1.day.from_now) |
| 17 | + expect(notification).to be_valid |
| 18 | + end |
| 19 | + |
| 20 | + it 'is valid if stop_datetime is nil' do |
| 21 | + notification = build(:notification, stop_datetime: nil) |
| 22 | + expect(notification).to be_valid |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + describe 'scopes' do |
| 27 | + describe '.currently_active' do |
| 28 | + it 'includes published notifications with future stop dates' do |
| 29 | + active = create(:notification, published: true, stop_datetime: 1.hour.from_now) |
| 30 | + expect(Notification.currently_active).to include(active) |
| 31 | + end |
| 32 | + |
| 33 | + it 'includes published notifications with no stop date' do |
| 34 | + permanent = create(:notification, published: true, stop_datetime: nil) |
| 35 | + expect(Notification.currently_active).to include(permanent) |
| 36 | + end |
| 37 | + |
| 38 | + it 'excludes notifications that have passed their stop date' do |
| 39 | + # Use save(validate: false) to simulate an old record that was valid when created |
| 40 | + expired = build(:notification, published: true, stop_datetime: 1.hour.ago) |
| 41 | + expired.save(validate: false) |
| 42 | + expect(Notification.currently_active).not_to include(expired) |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe '.expire_past_due!' do |
| 48 | + it 'updates all past-due notifications to be unpublished' do |
| 49 | + expired = build(:notification, published: true, stop_datetime: 1.minute.ago) |
| 50 | + expired.save(validate: false) |
| 51 | + |
| 52 | + Notification.expire_past_due! |
| 53 | + |
| 54 | + expired.reload |
| 55 | + expect(expired.published).to be false |
| 56 | + expect(expired.unpublished_at).to be_within(1.second).of(Time.zone.now) |
| 57 | + end |
8 | 58 | end |
9 | 59 |
|
10 | 60 | describe '#unpublish!' do |
|
0 commit comments