From 6f75c8451d5668cc1505b192f29cf2f0dda48f7e Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Wed, 3 May 2017 01:21:22 +0900 Subject: [PATCH] Fix subscription expiration condition (#2715) * Fix subscription expiration condition * dry and add spec --- app/models/subscription.rb | 4 ++++ app/views/admin/pubsubhubbub/index.html.haml | 5 ++--- spec/models/subscription_spec.rb | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 6a039cf07..6046ae4c2 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -33,6 +33,10 @@ class Subscription < ApplicationRecord (expires_at - Time.now.utc).to_i end + def expired? + Time.now.utc > expires_at + end + before_validation :set_min_expiration private diff --git a/app/views/admin/pubsubhubbub/index.html.haml b/app/views/admin/pubsubhubbub/index.html.haml index 60a0b40b0..852e84f11 100644 --- a/app/views/admin/pubsubhubbub/index.html.haml +++ b/app/views/admin/pubsubhubbub/index.html.haml @@ -11,7 +11,6 @@ %th= t('admin.pubsubhubbub.last_delivery') %tbody - @subscriptions.each do |subscription| - - expired = Time.now.utc < subscription.expires_at %tr %td %samp= subscription.account.acct @@ -20,8 +19,8 @@ %td - if subscription.confirmed? %i.fa.fa-check - %td{ style: "color: #{expired ? 'red' : 'inherit'};" } - = precede expired ? '-' : '' do + %td{ style: "color: #{subscription.expired? ? 'red' : 'inherit'};" } + = precede subscription.expired? ? '-' : '' do = time_ago_in_words(subscription.expires_at) %td - if subscription.last_successful_delivery_at? diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb index 9cb3d41ce..11fd8fadc 100644 --- a/spec/models/subscription_spec.rb +++ b/spec/models/subscription_spec.rb @@ -1,5 +1,19 @@ require 'rails_helper' RSpec.describe Subscription, type: :model do + let(:alice) { Fabricate(:account, username: 'alice') } + subject { Fabricate(:subscription, account: alice) } + + describe '#expired?' do + it 'return true when expires_at is past' do + subject.expires_at = 2.days.ago + expect(subject.expired?).to be true + end + + it 'return false when expires_at is future' do + subject.expires_at = 2.days.from_now + expect(subject.expired?).to be false + end + end end