forked from eliotsykes/rspec-rails-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription_spec.rb
225 lines (166 loc) · 6.73 KB
/
subscription_spec.rb
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
require 'rails_helper'
RSpec.describe Subscription, :type => :model do
context "db" do
context "indexes" do
it { should have_db_index(:email).unique(true) }
it { should have_db_index(:confirmation_token).unique(true) }
end
context "columns" do
it { should have_db_column(:email).of_type(:string).with_options(limit: 100, null: false) }
it { should have_db_column(:confirmation_token).of_type(:string).with_options(limit: 100, null: false) }
it { should have_db_column(:confirmed).of_type(:boolean).with_options(default: false, null: false) }
it { should have_db_column(:start_on).of_type(:date) }
end
end
context "attributes" do
it "has email" do
expect(build(:subscription, email: "[email protected]")).to have_attributes(email: "[email protected]")
end
it "has confirmed" do
expect(build(:subscription, confirmed: true)).to have_attributes(confirmed: true)
end
it "has confirmation_token" do
expect(build(:subscription, confirmation_token: "what-a-token")).to have_attributes(confirmation_token: "what-a-token")
end
context "start_on" do
it "is an attribute" do
today = Date.today
expect(build(:subscription, start_on: Date.today)).to have_attributes(start_on: today)
end
it "defaults to today" do
now = Time.zone.now
today = now.to_date
travel_to now do
expect(build(:subscription).start_on).to eq(today)
end
end
end
end
context "validation" do
let(:subscription) { build(:subscription, confirmation_token: "token", email: "[email protected]") }
it "requires unique email" do
expect(subscription).to validate_uniqueness_of(:email)
end
it "requires email" do
expect(subscription).to validate_presence_of(:email)
end
it "requires confirmation_token" do
expect(subscription).to validate_presence_of(:confirmation_token)
end
it "requires unique confirmation_token" do
expect(subscription).to validate_uniqueness_of(:confirmation_token)
end
it "requires start_on" do
expect(subscription).to validate_presence_of(:start_on)
end
end
context "scopes" do
describe ".confirmation_overdue" do
before do
# Freeze time as confirmation_overdue scope is time-sensitive
travel_to Time.zone.now
end
after { travel_back }
it "returns unconfirmed subscriptions of age more than 3 days" do
overdue = create(:subscription, confirmed: false, created_at: (3.days + 1.second).ago)
expect(Subscription.confirmation_overdue).to match_array [overdue]
end
it "does not return unconfirmed subscriptions of age 3 days or younger" do
create(:subscription, confirmed: false, created_at: 3.days.ago)
expect(Subscription.confirmation_overdue).to be_empty
end
it "does not return confirmed subscriptions" do
create(:subscription, confirmed: true, created_at: 1.year.ago)
expect(Subscription.confirmation_overdue).to be_empty
end
end
end
describe "#to_param" do
it "uses confirmation_token as the default identifier for routes" do
subscription = build(:subscription, confirmation_token: "hello-im-a-token-123")
expect(subscription.to_param).to eq("hello-im-a-token-123")
end
end
describe ".create_and_request_confirmation(params)" do
it "creates an unconfirmed subscription with the given params" do
params = { email: "[email protected]", start_on: "2015-01-31" }
Subscription.create_and_request_confirmation(params)
subscription = Subscription.first
expect(subscription.confirmed?).to eq(false)
expect(subscription.email).to eq(params[:email])
expect(subscription.start_on).to eq(Date.new(2015, 1, 31))
end
it "saves subscription with a secure random confirmation_token" do
expect(::SecureRandom).to receive(:hex).with(32).and_call_original
subscription = Subscription.create_and_request_confirmation(email: "[email protected]")
subscription.reload
expect(subscription.confirmation_token).to match(/\A[a-z0-9]{64}\z/)
end
it "emails a confirmation request" do
expect(SubscriptionMailer)
.to receive(:send_confirmation_request!)
.with(Subscription)
Subscription.create_and_request_confirmation(email: "[email protected]")
end
it "doesn't create subscription if emailing fails" do
expect(SubscriptionMailer)
.to receive(:send_confirmation_request!)
.and_raise("Bad news, delivery failed!")
email = "[email protected]"
expect do
Subscription.create_and_request_confirmation(email: email)
end.to raise_error "Bad news, delivery failed!"
expect(Subscription.exists?).to eq(false)
end
it "raises an error if the subscription can't be created" do
blank_email = ""
expect do
Subscription.create_and_request_confirmation(email: blank_email)
end.to raise_error ActiveRecord::RecordInvalid
end
it "doesn't send an email if the subscription can't be created" do
blank_email = ""
expect do
Subscription.create_and_request_confirmation(email: blank_email)
end.to raise_error ActiveRecord::RecordInvalid
expect(ActionMailer::Base.deliveries).to be_empty
end
it "raises an error if the emailing fails" do
expect(SubscriptionMailer)
.to receive(:send_confirmation_request!)
.and_raise("Bad news, delivery failed!")
email = "[email protected]"
expect do
Subscription.create_and_request_confirmation(email: email)
end.to raise_error "Bad news, delivery failed!"
end
end
describe ".confirm(confirmation_token)" do
it "confirms the subscription matching the confirmation_token" do
token = Subscription.generate_confirmation_token
subscription = create(:subscription,
email: "[email protected]",
confirmation_token: token
)
expect(subscription.confirmed?).to eq(false)
returned_subscription = Subscription.confirm(token)
subscription.reload
expect(subscription).to eq(returned_subscription)
expect(subscription.confirmed?).to eq(true)
end
it "gracefully handles confirming an already confirmed subscription" do
subscription = create(:subscription,
email: "[email protected]",
confirmation_token: "xyz"
)
expect(subscription.confirmed?).to eq(false)
2.times { Subscription.confirm("xyz") }
expect(subscription.reload.confirmed?).to eq(true)
end
it "raises ActiveRecord::RecordNotFound if confirmation_token is unknown" do
expect do
Subscription.confirm("unknown-token")
end.to raise_error ActiveRecord::RecordNotFound
end
end
end