forked from AdaGold/video-store-consumer-api
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrentals_controller_test.rb
More file actions
244 lines (212 loc) · 6.7 KB
/
rentals_controller_test.rb
File metadata and controls
244 lines (212 loc) · 6.7 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
require 'test_helper'
class RentalsControllerTest < ActionDispatch::IntegrationTest
describe "check-out" do
it "associates a movie with a customer" do
movie = movies(:one)
customer = customers(:two)
post check_out_url(title: movie.title), params: {
customer_id: customer.id,
due_date: Date.today + 1
}
assert_response :success
# Reload from DB
Movie.find(movie.id).customers.must_include Customer.find(customer.id)
end
it "sets the checkout_date to today" do
movie = movies(:one)
customer = customers(:two)
post check_out_url(title: movie.title), params: {
customer_id: customer.id,
due_date: Date.today + 1
}
assert_response :success
Movie.find(movie.id).rentals.last.checkout_date.must_equal Date.today
end
it "requires a valid movie title" do
post check_out_url(title: "does not exist"), params: {
customer_id: customers(:two).id,
due_date: Date.today + 1
}
assert_response :not_found
data = JSON.parse @response.body
data.must_include "errors"
data["errors"].must_include "rental"
end
it "requires a valid customer ID" do
bad_customer_id = 13371337
Customer.find_by(id: bad_customer_id).must_be_nil
post check_out_url(title: movies(:one).title), params: {
customer_id: bad_customer_id,
due_date: Date.today + 1
}
assert_response :not_found
data = JSON.parse @response.body
data.must_include "errors"
data["errors"].must_include "rental"
end
it "requires a due-date in the future" do
# Obvious case: actually in the past
post check_out_url(title: movies(:one).title), params: {
customer_id: customers(:two).id,
due_date: Date.today - 1
}
assert_response :bad_request
data = JSON.parse @response.body
data.must_include "errors"
data["errors"].must_include "due_date"
end
end
describe "check-in" do
before do
# Establish a rental
@rental = Rental.create!(
movie: movies(:one),
customer: customers(:two),
checkout_date: Date.today - 5,
due_date: Date.today + 5,
returned: nil
)
end
it "marks a rental complete" do
post check_in_url(title: @rental.movie.title), params: {
customer_id: @rental.customer.id
}
assert_response :success
@rental.reload
@rental.returned.must_equal true
end
it "requires a valid movie title" do
post check_in_url(title: "does not exist"), params: {
customer_id: @rental.customer.id
}
assert_response :not_found
data = JSON.parse @response.body
data.must_include "errors"
data["errors"].must_include "rental"
end
it "requires a valid customer ID" do
bad_customer_id = 13371337
Customer.find_by(id: bad_customer_id).must_be_nil
post check_in_url(title: @rental.movie.title), params: {
customer_id: bad_customer_id
}
assert_response :not_found
data = JSON.parse @response.body
data.must_include "errors"
data["errors"].must_include "rental"
end
it "requires there to be a rental for that customer-movie pair" do
post check_in_url(title: movies(:two).title), params: {
customer_id: customers(:three).id
}
assert_response :not_found
data = JSON.parse @response.body
data.must_include "errors"
data["errors"].must_include "rental"
end
it "requires an un-returned rental" do
@rental.returned = true
@rental.save!
post check_in_url(title: @rental.movie.title), params: {
customer_id: @rental.customer.id
}
assert_response :not_found
data = JSON.parse @response.body
data.must_include "errors"
data["errors"].must_include "rental"
end
# it "if multiple rentals match, ignores returned ones" do
# returned_rental = Rental.create!(
# movie: @rental.movie,
# customer: @rental.customer,
# checkout_date: Date.today - 5,
# due_date: @rental.due_date - 2,
# returned: true
# )
#
# post check_in_url(title: @rental.movie.title), params: {
# customer_id: @rental.customer.id
# }
# assert_response :success
#
# returned_rental.reload
# @rental.reload
#
# @rental.returned.must_equal true
# end
# it "returns the rental with the closest due_date" do
# soon_rental = Rental.create!(
# movie: @rental.movie,
# customer: @rental.customer,
# checkout_date: Date.today - 5,
# due_date: @rental.due_date - 2,
# returned: false
# )
#
# far_rental = Rental.create!(
# movie: @rental.movie,
# customer: @rental.customer,
# checkout_date: Date.today - 5,
# due_date: @rental.due_date + 10,
# returned: false
# )
#
# post check_in_url(title: @rental.movie.title), params: {
# customer_id: @rental.customer.id
# }
# assert_response :success
#
# soon_rental.reload
# @rental.reload
# far_rental.reload
#
# soon_rental.returned.must_equal true
# @rental.returned.must_equal false
# far_rental.returned.must_equal false
# end
end
describe "overdue" do
# Note that we *don't* check the actual content,
# since that is covered by the model tests.
# Instead we just check the things the controlelr
# is responsible for.
it "Returns a JSON array" do
get overdue_url
assert_response :success
@response.headers['Content-Type'].must_include 'json'
# Attempt to parse
data = JSON.parse @response.body
data.must_be_kind_of Array
end
it "Returns an empty array if no rentals overdue" do
# Make sure there's none overdue
Rental.all.each do |r|
r.returned = true
r.save!
end
get overdue_url
assert_response :success
data = JSON.parse @response.body
data.must_be_kind_of Array
data.length.must_equal 0
end
it "Returns expected fields" do
# Make sure we get something back
Rental.overdue.length.must_be :>, 0
get overdue_url
assert_response :success
data = JSON.parse @response.body
data.must_be_kind_of Array
data.length.must_equal Rental.overdue.length
data.each do |rental|
rental.must_be_kind_of Hash
rental.must_include "title"
rental.must_include "customer_id"
rental.must_include "name"
rental.must_include "postal_code"
rental.must_include "checkout_date"
rental.must_include "due_date"
end
end
end
end