subscription_spec.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. require 'rails_helper'
  2. RSpec.describe Subscription, type: :model do
  3. let(:alice) { Fabricate(:account, username: 'alice') }
  4. subject { Fabricate(:subscription, account: alice) }
  5. describe '#expired?' do
  6. it 'return true when expires_at is past' do
  7. subject.expires_at = 2.days.ago
  8. expect(subject.expired?).to be true
  9. end
  10. it 'return false when expires_at is future' do
  11. subject.expires_at = 2.days.from_now
  12. expect(subject.expired?).to be false
  13. end
  14. end
  15. describe 'lease_seconds' do
  16. it 'returns the time remaining until expiration' do
  17. datetime = 1.day.from_now
  18. subscription = Subscription.new(expires_at: datetime)
  19. travel_to(datetime - 12.hours) do
  20. expect(subscription.lease_seconds).to eq(12.hours)
  21. end
  22. end
  23. end
  24. describe 'lease_seconds=' do
  25. it 'sets expires_at to min expiration when small value is provided' do
  26. subscription = Subscription.new
  27. datetime = 1.day.from_now
  28. too_low = Subscription::MIN_EXPIRATION - 1000
  29. travel_to(datetime) do
  30. subscription.lease_seconds = too_low
  31. end
  32. expected = datetime + Subscription::MIN_EXPIRATION.seconds
  33. expect(subscription.expires_at).to be_within(1.0).of(expected)
  34. end
  35. it 'sets expires_at to value when valid value is provided' do
  36. subscription = Subscription.new
  37. datetime = 1.day.from_now
  38. valid = Subscription::MIN_EXPIRATION + 1000
  39. travel_to(datetime) do
  40. subscription.lease_seconds = valid
  41. end
  42. expected = datetime + valid.seconds
  43. expect(subscription.expires_at).to be_within(1.0).of(expected)
  44. end
  45. it 'sets expires_at to max expiration when large value is provided' do
  46. subscription = Subscription.new
  47. datetime = 1.day.from_now
  48. too_high = Subscription::MAX_EXPIRATION + 1000
  49. travel_to(datetime) do
  50. subscription.lease_seconds = too_high
  51. end
  52. expected = datetime + Subscription::MAX_EXPIRATION.seconds
  53. expect(subscription.expires_at).to be_within(1.0).of(expected)
  54. end
  55. end
  56. end