/
server_spec.rb
563 lines (470 loc) · 15.3 KB
/
server_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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
require 'spec/spec_helper'
describe "Draw! app" do
include Rack::Test::Methods
# see https://groups.google.com/forum/#!topic/sinatrarb/qd5PaQYxs6E
# source https://gist.github.com/375973
class SessionData
def initialize(cookies)
@cookies = cookies
@data = cookies['rack.session']
if @data
@data = @data.unpack("m*").first
@data = Marshal.load(@data)
else
@data = {}
end
end
def [](key)
@data[key]
end
def []=(key, value)
@data[key] = value
session_data = Marshal.dump(@data)
session_data = [session_data].pack("m*")
@cookies.merge("rack.session=#{Rack::Utils.escape(session_data)}", URI.parse("//example.org//"))
raise "session variable not set" unless @cookies['rack.session'] == session_data
end
end
def session
SessionData.new(rack_test_session.instance_variable_get(:@rack_mock_session).cookie_jar)
end
def app
@app ||= Sinatra::Application
end
describe "POST /" do
it "should be tested"
end
describe "GET /" do
it "should respond" do
Drawing.should_receive(:all).and_return([])
get '/'
last_response.should be_ok
end
it "should create session" do
get '/'
last_response.header['Set-Cookie'].should match /rack\.session/
end
it "should set session expiration date" do
get '/'
last_response.header['Set-Cookie'].should match /expires/i
end
end
describe "GET /feed.rss" do
it "should respond" do
Drawing.should_receive(:all).and_return([])
header 'Accept', 'application/rss+xml'
get '/'
last_response.should be_ok
end
end
describe "GET /users/123" do
before :each do
@id = "123"
@user = {'uid' => "123", 'user_info' => {'first_name' => "John"}}
@drawing = {'url' => "/the/drawing.png"}
end
describe "user found" do
before(:each) do
User.should_receive(:find).with(@id).and_return(@user)
Drawing.should_receive(:all).with(:user_id => @id, :page => 0, :per_page => PER_PAGE, :host => 'example.org').and_return([@drawing])
end
it "should respond 200 if a user is found" do
get "/users/#{@id}"
last_response.should be_ok
end
it "should display user info" do
get "/users/#{@id}"
last_response.should match @user['user_info']['first_name']
end
it "should display user gallery" do
get "/users/#{@id}"
last_response.should match @user['user_info']['first_name']
end
describe "json request" do
before(:each) do
header 'Accept', 'application/json'
end
it "should respond" do
get "/users/#{@id}"
last_response.should be_ok
end
it "should respond with json if accept header is set to 'application/json'" do
get "/users/#{@id}"
last_response.header['Content-type'].should == 'application/json'
end
it "should respond with user data" do
get "/users/#{@id}"
last_response.should match /first_name/
last_response.should match /John/
last_response.should match /uid/
last_response.should match @id
end
it "should respond with user's drawings" do
get "/users/#{@id}"
last_response.should match /drawings/
last_response.should match @drawing['url'].to_s
end
it "should respond with metadata" do
get "/users/#{@id}"
last_response.should match /meta/
last_response.should match /current_page/
end
it "should not respond with user's private data" do
get "/users/#{@id}"
last_response.should_not match /token/
end
end
end
describe "user not found" do
before(:each) do
User.should_receive(:find).with(@id).and_return(nil)
end
it "should respond 404 if a user is not found" do
get "/users/#{@id}"
last_response.should be_not_found
end
it "should display 'not found'" do
get "/users/#{@id}"
last_response.should match /not found/
end
end
end
describe "GET /drawings" do
before(:each) do
header 'Accept', 'application/json'
@drawings = [
{:id => 1},
{:id => 2}
]
Drawing.should_receive(:all).and_return(@drawings)
get "/drawings"
end
it "should respond" do
last_response.should be_ok
end
it "should respond with json" do
last_response.header['Content-type'].should == 'application/json'
end
it "should respond with drawings" do
last_response.should match /drawings/
@drawings.each do |drawing|
last_response.should match drawing[:id].to_s
end
end
it "should respond with metadata" do
last_response.should match /meta/
last_response.should match /current_page/
end
end
describe "GET /drawings/123.png" do
before :each do
@id = "123.png"
@drawing = {'url' => "/the/drawing.png", :children => []}
end
describe "drawing found" do
before(:each) do
Drawing.should_receive(:find).with(@id).and_return(@drawing)
end
it "should respond 200 if a drawing is found" do
get "/drawings/#{@id}"
last_response.should be_ok
end
it "should respond with json if accept header is set to 'application/json'" do
header 'Accept', 'application/json'
get "/drawings/#{@id}"
last_response.header['Content-type'].should == 'application/json'
end
it "should display drawing" do
get "/drawings/#{@id}"
last_response.should match @drawing['url']
end
end
describe "drawing not found" do
before(:each) do
Drawing.should_receive(:find).with(@id).and_return(nil)
get "/drawings/#{@id}"
end
it "should respond 404 if a drawing is not found" do
last_response.should be_not_found
end
it "should display 'not found'" do
last_response.should match /not found/
end
end
end
describe "POST /upload" do
describe "guest user" do
it "should redirect to '/'" do
post '/upload'
last_response.should be_redirect
last_response.header['Location'].should match /\//
end
describe "API request" do
before(:each) do
header 'Accept', 'application/json'
end
it "should respond with json" do
post '/upload'
last_response.header['Content-type'].should == 'application/json'
end
it "should respond unauthorized" do
post '/upload'
last_response.status.should == 403
end
end
end
describe "authenticated user" do
before(:each) do
@user_id = "123"
@user_key = "user:#{@user_id}"
@user = {
'uid' => @user_id,
'user_info' => {
'first_name' => "John"
},
'credentials' => {
'token' => "access_token"
}
}
@png_drawing = {
'image' => {
'frame' => [[]]
}
}
@gif_drawing = {
'image' => {
'frames' => [[[]]]
}
}
end
it "should respond with json" do
session[:user] = @user_key
User.stub!(:find_by_key).with(@user_key).and_return(@user)
post '/upload', {}.to_json
last_response.header['Content-type'].should == 'application/json'
end
describe "HTML request" do
before(:each) do
session[:user] = @user_key
User.should_receive(:find_by_key).with(@user_key).and_return(@user)
end
it "should respond" do
@drawing = Object.new
@drawing.should_receive(:save).and_return({})
Drawing.should_receive(:new).and_return(@drawing)
post '/upload', @png_drawing.to_json
last_response.should be_ok
end
end
describe "API request" do
before(:each) do
header 'Accept', 'application/json'
end
it "should respond" do
User.should_receive(:find).with(@user_id).and_return(@user)
@drawing = Object.new
@drawing.should_receive(:save).and_return({})
Drawing.should_receive(:new).and_return(@drawing)
post "/upload?uid=#{@user_id}&token=#{@user['credentials']['token']}", @png_drawing.to_json
last_response.should be_ok
end
it "should respond unauthorized without auth params" do
User.should_receive(:find).with('wrong_uid').and_return(nil)
post "/upload?uid=wrong_uid&token=wrong_token", @png_drawing.to_json
last_response.status.should == 403
end
it "should respond unauthorized with wrong auth params" do
post "/upload", @png_drawing.to_json
last_response.status.should == 403
end
end
end
end
describe "GET /auth/facebook/callback" do
# #
# # GET /auth/facebook/callback
# #
# get '/auth/facebook/callback' do
# session[:user] = "user:#{request.env['omniauth.auth']['uid']}"
# @user = User.new(request.env['omniauth.auth'].merge(:key => session[:user])).save
# haml :callback
# end
before(:each) do
get '/auth/facebook/callback'
end
it "should set user auth session" do
last_request.session['user'].should match /user:/
end
it "should save authenticated user" do
pending
@user = Object.new
@user.should_receive(:save)
User.should_receive(:new).and_return(@user)
end
it "should render callback template" do
last_response.body.should match /window\.close/
end
end
describe "GET /auth/failure" do
before(:each) do
get '/auth/failure'
end
it "should show an error message" do
last_response.body.should match /There was an error trying to access to your Facebook data/
end
it "should clear session" do
last_request.session['user'].should be_nil
end
end
describe "GET /logout" do
it "should redirect" do
get '/logout'
last_response.should be_redirect
end
it "should redirect to origin if param[:origin] is set" do
get '/logout', :origin => "/origin"
last_response.should be_redirect
last_response.header['Location'].should match /\/origin/
end
it "should clear session" do
get '/logout'
last_request.session['user'].should be_nil
end
end
{
:about => 'Credits',
:privacy => 'Privacy',
:tos => 'Terms of Service'
}.each do |action, content|
describe "GET /#{action}" do
before(:each) do
get "/#{action}"
end
it "should respond" do
last_response.should be_ok
end
it "should render #{action} template" do
last_response.body.should match /#{content}/
end
end
end
describe "POST /authorize" do
before(:each) do
@client = Object.new
@selection = Object.new
@me = Object.new
@info = Object.new
@data = Object.new
@client.stub!(:selection).and_return(@selection)
@selection.stub!(:me).and_return(@me)
@me.stub!(:info!).and_return(@info)
@info.stub!(:data).and_return(@data)
@data.stub!(:id).and_return("123")
@data.stub!(:first_name).and_return("John")
@data.stub!(:last_name).and_return("Doe")
end
it "should call FB API to authorize user" do
FBGraph::Client.should_receive(:new).with(:client_id => FACEBOOK['app_id'], :secret_id => FACEBOOK['app_secret'], :token => "token").and_return(@client)
post '/authorize', {:uid => "123", :token => "token"}
end
describe "authorized user" do
before(:each) do
FBGraph::Client.stub!(:new).and_return(@client)
post '/authorize', {:uid => "123", :token => "token"}
end
it "should be successful" do
last_response.should be_ok
end
it "should respond with json" do
last_response.header['Content-type'].should == 'application/json'
end
it "should respond with user data" do
last_response.should match /first_name/
last_response.should match /John/
last_response.should match /uid/
last_response.should match /123/
end
it "should respond with user's drawings" do
last_response.should match /drawings/
end
it "should respond with metadata" do
last_response.should match /meta/
last_response.should match /current_page/
end
end
describe "invalid params" do
it "should be unauthorized if token is wrong" do
FBGraph::Client.stub!(:new).and_raise(RuntimeError)
post '/authorize', {:uid => "123", :token => "wrong_token"}
last_response.status.should == 403
end
it "should be unauthorized if uid is wrong" do
FBGraph::Client.stub!(:new).and_return(@client)
post '/authorize', {:uid => "wrong_id", :token => "token"}
last_response.status.should == 403
end
end
end
describe "DELETE /drawings/123.png" do
before(:each) do
@id = "123.png"
end
describe "guest user" do
before(:each) do
@user = nil
delete '/drawings/123.png'
end
it "should redirect to '/drawings/123.png'" do
last_response.should be_redirect
last_response.header['Location'].should match /\/drawings\/123\.png/
end
end
describe "authenticated user" do
before(:each) do
@user_id = "user:xxx"
session[:user] = @user_id
@user = {'uid' => "123", 'user_info' => {'first_name' => "John"}}
User.should_receive(:find_by_key).with(@user_id).and_return(@user)
end
describe "drawing not found" do
before(:each) do
Drawing.should_receive(:find).with(@id, :shallow => true).and_return(nil)
delete '/drawings/123.png'
end
it "should respond 404 if a drawing is not found" do
last_response.should be_not_found
end
it "should display 'not found'" do
last_response.should match /not found/
end
end
describe "drawing found" do
describe "unauthorized user" do
before(:each) do
@drawing = {}
Drawing.should_receive(:find).with(@id, :shallow => true).and_return(@drawing)
delete '/drawings/123.png'
end
it "should respond 403" do
last_response.status.should == 403
end
it "should display 'Access forbidden'" do
last_response.should match /Access forbidden/
end
end
describe "authorized user" do
before(:each) do
@drawing = {'user' => @user}
Drawing.should_receive(:find).with(@id, :shallow => true).and_return(@drawing)
Drawing.should_receive(:destroy).with(@id, @user['uid'])
delete '/drawings/123.png'
end
it "should redirect to '/'" do
last_response.should be_redirect
last_response.header['Location'].should match /\//
end
end
end
end
end
end