/
eee_spec.rb
508 lines (405 loc) · 14.1 KB
/
eee_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
require File.expand_path(File.dirname(__FILE__) + '/spec_helper' )
require 'pp'
describe "eee" do
before(:all) do
RestClient.put @@db, { }
end
after(:all) do
RestClient.delete @@db
end
context "a CouchDB meal" do
before(:each) do
@date = Date.new(2009, 5, 13).to_s
@title = "Meal Title"
@permalink = @date
@meal = {
:title => @title,
:date => @date,
:serves => 4,
:summary => "meal summary",
:description => "meal description",
:menu => ["meal menu item"]
}
RestClient.put "#{@@db}/#{@permalink}",
@meal.to_json,
:content_type => 'application/json'
RestClient.stub!(:get).and_return('{"rows": [] }')
end
after(:each) do
data = RestClient.proxied_by_rspec__get "#{@@db}/#{@permalink}"
meal = JSON.parse(data)
RestClient.delete "#{@@db}/#{@permalink}?rev=#{meal['_rev']}"
end
describe "GET /" do
it "should respond OK" do
get "/"
last_response.should be_ok
end
it "should request the most recent 13 meals from CouchDB" do
RestClient.
should_receive(:get).
with(/by_date.+limit=13/).
and_return('{"rows": [] }')
get "/"
end
it "should pull back full details for the first 10 meals" do
RestClient.
stub!(:get).
and_return('{"rows": [' +
'{"id":"2009-06-10","value":["2009-06-10","Foo"]},' +
'{"id":"2009-06-09","value":["2009-06-09","Foo"]},' +
'{"id":"2009-06-08","value":["2009-06-08","Foo"]},' +
'{"id":"2009-06-07","value":["2009-06-07","Foo"]},' +
'{"id":"2009-06-06","value":["2009-06-06","Foo"]},' +
'{"id":"2009-06-05","value":["2009-06-05","Foo"]},' +
'{"id":"2009-06-04","value":["2009-06-04","Foo"]},' +
'{"id":"2009-06-03","value":["2009-06-03","Foo"]},' +
'{"id":"2009-06-02","value":["2009-06-02","Foo"]},' +
'{"id":"2009-06-01","value":["2009-06-01","Foo"]},' +
'{"id":"2009-05-31","value":["2009-05-31","Foo"]},' +
'{"id":"2009-05-30","value":["2009-05-30","Foo"]},' +
'{"id":"2009-05-29","value":["2009-05-29","Foo"]}' +
']}')
RestClient.
should_receive(:get).
with(/2009-0/).
exactly(13).times.
and_return('{"title":"foo",' +
'"date":"2009-06-17",' +
'"summary":"foo summary",' +
'"menu":[]}')
get "/"
end
end
describe "GET /main.rss" do
it "should respond OK" do
get "/main.rss"
last_response.should be_ok
end
it "should be the meals rss feed" do
get "/main.rss"
last_response.
should have_selector("channel title",
:content => "EEE Cooks: Meals")
end
it "should request the 10 most recent meals from CouchDB" do
RestClient.
should_receive(:get).
with(/by_date.+limit=10/).
and_return('{"rows": [] }')
get "/main.rss"
end
it "should pull back full details for the 10 meals" do
RestClient.
stub!(:get).
and_return('{"rows": [' +
'{"id":"2009-06-10","value":["2009-06-10","Foo"]},' +
'{"id":"2009-06-09","value":["2009-06-09","Foo"]},' +
'{"id":"2009-06-08","value":["2009-06-08","Foo"]},' +
'{"id":"2009-06-07","value":["2009-06-07","Foo"]},' +
'{"id":"2009-06-06","value":["2009-06-06","Foo"]},' +
'{"id":"2009-06-05","value":["2009-06-05","Foo"]},' +
'{"id":"2009-06-04","value":["2009-06-04","Foo"]},' +
'{"id":"2009-06-03","value":["2009-06-03","Foo"]},' +
'{"id":"2009-06-02","value":["2009-06-02","Foo"]},' +
'{"id":"2009-06-01","value":["2009-06-01","Foo"]}' +
']}')
RestClient.
should_receive(:get).
with(/2009-06-/).
exactly(10).times.
and_return('{"title":"foo",' +
'"date":"2009-06-17",' +
'"summary":"foo summary",' +
'"menu":[]}')
get "/main.rss"
end
end
describe "GET /meals/YYYY" do
it "should respond OK" do
get "/meals/2009"
last_response.should be_ok
end
it "should ask CouchDB for meal from year YYYY" do
RestClient.
should_receive(:get).
with(%r{by_date.+startkey=.+2009-00-00.+endkey=.+2009-99-99}).
and_return('{"rows": [] }')
get "/meals/2009"
end
it "should ask CouchDB how many meals from all years" do
RestClient.
should_receive(:get).
with(/meals.+count_by_year/).
and_return('{"rows": [{"key":"2008","value":3},
{"key":"2009","value":3}]}')
get "/meals/2009"
end
end
describe "GET /meals/YYYY/MM" do
it "should respond OK" do
get "/meals/2009/05"
last_response.should be_ok
end
it "should ask CouchDB for meals from year YYYY and month MM" do
RestClient.
should_receive(:get).
with(%r{by_date.+startkey=.+2009-05-00.+endkey=.+2009-05-99}).
and_return('{"rows": [] }')
get "/meals/2009/05"
end
it "should ask CouchDB how many meals from all months" do
RestClient.
should_receive(:get).
with(/meals.+count_by_month/).
and_return('{"rows": [{"key":"2009-04","value":3},
{"key":"2009-05","value":3}]}')
get "/meals/2009/05"
end
end
describe "GET /meals/YYYY/MM/DD" do
before(:each) do
RestClient.
stub!(:get).
and_return(@meal.to_json)
end
it "should respond OK" do
RestClient.
should_receive(:get).
with(/by_date/).
and_return('{"rows": [] }')
get "/meals/2009/05/13"
last_response.should be_ok
end
it "should request the meal from CouchDB" do
RestClient.
should_receive(:get).
with(/by_date/).
and_return('{"rows": [] }')
RestClient.
should_receive(:get).
with(/2009-05-13/).
and_return(@meal.to_json)
get "/meals/2009/05/13"
end
end
end
describe "a CouchDB recipe" do
before (:each) do
@date = Date.today
@title = "Recipe Title"
@permalink = @date.to_s + "-" + @title.downcase.gsub(/\W/, '-')
RestClient.put "#{@@db}/#{@permalink}",
{ :title => @title,
:date => @date }.to_json,
:content_type => 'application/json'
end
after(:each) do
data = RestClient.get "#{@@db}/#{@permalink}"
recipe = JSON.parse(data)
RestClient.delete "#{@@db}/#{@permalink}?rev=#{recipe['_rev']}"
end
describe 'GET /recipes/permalink' do
it "should respond OK" do
get "/recipes/#{@permalink}"
last_response.should be_ok
end
end
describe 'GET /recipes/:permalink/:image' do
it "should retrieve the image" do
data = RestClient.get "#{@@db}/#{@permalink}"
recipe = JSON.parse(data)
RestClient.put "#{@@db}/#{@permalink}/sample.jpg?rev=#{recipe['_rev']}",
File.read('spec/fixtures/sample.jpg'),
:content_type => 'image/jpeg'
get "/images/#{@permalink}/sample.jpg"
last_response.should be_ok
end
it "should return 404 for a non-existent image" do
get "/images/#{@permalink}/sample2.jpg"
last_response.should be_not_found
end
end
end
describe "GET /recipes/search" do
before(:each) do
self.stub!(:render)
end
it "should retrieve search results from couchdb-lucene" do
RestClient.should_receive(:get).
with(/_fti\?.*q=eggs/).
and_return('{"total_rows":1,"skip":0,"limit":20,"rows":[]}')
get "/recipes/search?q=eggs"
end
it "should not include the \"all\" field when performing fielded searches" do
RestClient.should_receive(:get).
with(/q=title:eggs/).
and_return('{"total_rows":1,"skip":0,"limit":20,"rows":[]}')
get "/recipes/search?q=title:eggs"
end
it "should have page sizes of 20 records" do
RestClient.should_receive(:get).
with(/limit=20/).
and_return('{"total_rows":1,"skip":0,"limit":20,"rows":[]}')
get "/recipes/search?q=title:eggs"
end
it "should paginate" do
RestClient.should_receive(:get).
with(/skip=20/).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')
get "/recipes/search?q=title:eggs&page=2"
end
it "should display page 1 when passing a bad page number" do
RestClient.should_receive(:get).
with(/skip=0/).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')
get "/recipes/search?q=title:eggs&page=foo"
end
it "should sort" do
RestClient.should_receive(:get).
with(/sort=title/).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')
get "/recipes/search?q=title:egg&sort=title"
end
it "should not sort when no sort field is supplied" do
RestClient.stub!(:get).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')
RestClient.should_not_receive(:get).with(/sort=/)
get "/recipes/search?q=title:egg&sort="
end
it "should reverse sort when order=desc is supplied" do
RestClient.stub!(:get).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')
RestClient.should_receive(:get).with(/sort=%5C/)
get "/recipes/search?q=title:egg&sort=sort_foo&order=desc"
end
it "should display a helpful message when no results" do
RestClient.stub!(:get).
and_return('{"total_rows":0,"skip":0,"limit":20,"rows":[]}')
get "/recipes/search?q=title:egg"
last_response.should contain("No results")
end
it "should redirect without pagination after navigating beyond the pagination window" do
RestClient.stub!(:get).
and_return('{"total_rows":1,"skip":0,"limit":20,"rows":[]}')
get "/recipes/search?q=egg&page=2"
last_response.status.should == 302
last_response.headers["Location"].should == "/recipes/search?q=egg"
end
it "should treat couchdb errors as no-results" do
RestClient.stub!(:get).
and_raise(Exception)
get "/recipes/search?q=title:egg"
last_response.should contain("No results")
end
it "should treat couchdb-lucene errors as an empty query" do
RestClient.stub!(:get).
and_raise(Exception)
get "/recipes/search?q=title:egg"
last_response.should have_selector("input[@name=query][@value='']")
end
end
describe "GET /feedback" do
it "should respond OK" do
get "/feedback"
last_response.should be_ok
end
end
describe "POST /email" do
before(:each) do
Pony.stub!(:mail)
end
it "should respond OK" do
post "/email"
last_response.should be_ok
end
it "should send us an email" do
Pony.
should_receive(:mail).
with(hash_including(:subject => "Subject"))
post "/email",
:name => "Bob",
:subject => "Subject",
:message => "Feedback message."
end
it "should include the email, name and message in the email" do
message = <<"_EOM"
From: from
Email: email
Message
_EOM
Pony.
should_receive(:mail).
with(hash_including(:body => message))
post "/email",
:name => "from",
:email => "email",
:subject => "Subject",
:message => "Message"
end
it "should include the URL (if supplied) in the email" do
message = <<"_EOM"
From: from
Email: email
Message
URL: http://example.org/
_EOM
Pony.
should_receive(:mail).
with(hash_including(:body => message))
post "/email",
:name => "from",
:email => "email",
:subject => "Subject",
:message => "Message",
:url => "http://example.org/"
end
end
end
describe "GET /recipe.rss" do
before(:each) do
RestClient.stub!(:get).and_return('{"rows": [] }')
end
it "should respond OK" do
get "/recipes.rss"
last_response.should be_ok
end
it "should be the meals rss feed" do
get "/recipes.rss"
last_response.
should have_selector("channel title",
:content => "EEE Cooks: Recipes")
end
it "should request the 10 most recent meals from CouchDB" do
RestClient.
should_receive(:get).
with(/by_date.+limit=10/).
and_return('{"rows": [] }')
get "/recipes.rss"
end
it "should pull back full details for the 10 meals" do
RestClient.
stub!(:get).
and_return('{"rows": [' +
'{"id":"2009-06-10","value":["2009-06-10","Foo"]},' +
'{"id":"2009-06-09","value":["2009-06-09","Foo"]},' +
'{"id":"2009-06-08","value":["2009-06-08","Foo"]},' +
'{"id":"2009-06-07","value":["2009-06-07","Foo"]},' +
'{"id":"2009-06-06","value":["2009-06-06","Foo"]},' +
'{"id":"2009-06-05","value":["2009-06-05","Foo"]},' +
'{"id":"2009-06-04","value":["2009-06-04","Foo"]},' +
'{"id":"2009-06-03","value":["2009-06-03","Foo"]},' +
'{"id":"2009-06-02","value":["2009-06-02","Foo"]},' +
'{"id":"2009-06-01","value":["2009-06-01","Foo"]}' +
']}')
RestClient.
should_receive(:get).
with(/2009-06-/).
exactly(10).times.
and_return('{"title":"foo",' +
'"date":"2009-06-17",' +
'"summary":"foo summary",' +
'"menu":[]}')
get "/recipes.rss"
end
end