about summary refs log tree commit diff
path: root/spec/models/subscription_spec.rb
blob: b83979d13799f4d3134d2a0dfd2f62658d6daada (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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

  describe 'lease_seconds' do
    it 'returns the time remaining until expiration' do
      datetime = 1.day.from_now
      subscription = Subscription.new(expires_at: datetime)
      travel_to(datetime - 12.hours) do
        expect(subscription.lease_seconds).to eq(12.hours)
      end
    end
  end

  describe 'lease_seconds=' do
    it 'sets expires_at to min expiration when small value is provided' do
      subscription = Subscription.new
      datetime = 1.day.from_now
      too_low = Subscription::MIN_EXPIRATION - 1000
      travel_to(datetime) do
        subscription.lease_seconds = too_low
      end

      expected = datetime + Subscription::MIN_EXPIRATION.seconds
      expect(subscription.expires_at).to be_within(1.0).of(expected)
    end

    it 'sets expires_at to value when valid value is provided' do
      subscription = Subscription.new
      datetime = 1.day.from_now
      valid = Subscription::MIN_EXPIRATION + 1000
      travel_to(datetime) do
        subscription.lease_seconds = valid
      end

      expected = datetime + valid.seconds
      expect(subscription.expires_at).to be_within(1.0).of(expected)
    end

    it 'sets expires_at to max expiration when large value is provided' do
      subscription = Subscription.new
      datetime = 1.day.from_now
      too_high = Subscription::MAX_EXPIRATION + 1000
      travel_to(datetime) do
        subscription.lease_seconds = too_high
      end

      expected = datetime + Subscription::MAX_EXPIRATION.seconds
      expect(subscription.expires_at).to be_within(1.0).of(expected)
    end
  end
end