public
Description: An update of Scott Raymond's insanely easy flickr library
Clone URL: git://github.com/ctagg/flickr.git
flickr / test / test_flickr.rb
100644 825 lines (710 sloc) 32.018 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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
require 'rubygems'
require 'flickr'
require 'test/unit'
require 'mocha'
 
class TestFlickr < Test::Unit::TestCase
 
  # Flickr client tests
  #
  # instantiation tests
  def test_should_instantiate_new_flickr_client
    Flickr.any_instance.stubs(:login)
    flickr = Flickr.new('some_api_key', 'email@test.com', 'some_password', 'some_shared_secret')
    
    assert_equal 'some_api_key', flickr.api_key
    assert_equal 'some_shared_secret', flickr.instance_variable_get(:@shared_secret)
  end
  
  def test_should_try_to_login_using_old_api_if_email_and_password_passed
    Flickr.any_instance.expects(:login).with('email@test.com', 'some_password') # checks email and password have been set
    flickr = Flickr.new('some_api_key', 'email@test.com', 'some_password', 'some_shared_secret')
  end
  
  def test_should_instantiate_new_flickr_client_on_new_api
    flickr = Flickr.new('api_key' => 'some_api_key', 'email' => 'email@test.com', 'password' => 'some_password', 'shared_secret' => 'some_shared_secret', 'foo' => 'bar')
    
    assert_equal 'some_api_key', flickr.api_key
    assert_equal 'some_shared_secret', flickr.instance_variable_get(:@shared_secret)
    assert_nil flickr.instance_variable_get(:@foo) # should ignore other params
  end
  
  def test_should_not_try_to_login_using_old_api_when_instantiate_new_flickr_client_on_new_api
    Flickr.any_instance.expects(:login).never # doesn't bother trying to login with new api -- it'll fail in any case
    flickr = Flickr.new('api_key' => 'some_api_key', 'email' => 'email@test.com', 'password' => 'some_password', 'shared_secret' => 'some_shared_secret', 'foo' => 'bar')
  end
  
  # signature_from method tests
  def test_should_return_signature_from_given_params
    assert_equal Digest::MD5.hexdigest('shared_secret_codea_param1234xb_param5678yc_param97531t'),
                   authenticated_flickr_client.send(:signature_from, {:b_param => '5678y', 'c_param' => '97531t', :a_param => '1234x', :d_param => nil})
  end
  
  def test_should_return_nil_for_signature_when_no_shared_secret
    assert_nil flickr_client.send(:signature_from, {:b_param => '5678y', :c_param => '97531t', :a_param => '1234x'})
  end
  
  # request_url method tests
  def test_should_get_signature_for_params_when_building_url
    f = authenticated_flickr_client
    f.expects(:signature_from).with( 'method' => 'flickr.someMethod',
                                     'api_key' => 'some_api_key',
                                     'foo' => 'value which/needs&escaping',
                                     'auth_token' => 'some_auth_token').returns("foo123bar456")
    
    url = f.send(:request_url, 'someMethod', 'foo' => 'value which/needs&escaping')
  end
 
  def test_should_build_url_from_params_with_signature
    f = authenticated_flickr_client
    f.stubs(:signature_from).returns("foo123bar456")
    
    url = f.send(:request_url, 'someMethod', 'foo' => 'value which/needs&escaping')
    [ "#{Flickr::HOST_URL}#{Flickr::API_PATH}",
      'api_key=some_api_key',
      'method=flickr.someMethod',
      'foo=value+which%2Fneeds%26escaping',
      'auth_token=some_auth_token',
      'api_sig=foo123bar456'].each do |kv_pair|
      assert_match Regexp.new(Regexp.escape(kv_pair)), url
    end
  end
 
  def test_should_build_url_from_params_when_signature_returns_nil
    flickr = flickr_client
    flickr.stubs(:signature_from)
    assert_equal "#{Flickr::HOST_URL}#{Flickr::API_PATH}/?api_key=some_api_key&method=flickr.someMethod", flickr.send(:request_url, 'someMethod')
    assert_equal "#{Flickr::HOST_URL}#{Flickr::API_PATH}/?api_key=some_api_key&method=flickr.someMethod&foo=bar", flickr.send(:request_url, 'someMethod', 'foo' => 'bar', 'foobar' => nil)
    assert_equal "#{Flickr::HOST_URL}#{Flickr::API_PATH}/?api_key=some_api_key&method=flickr.someMethod&foo=101", flickr.send(:request_url, 'someMethod', 'foo' => 101)
    assert_equal "#{Flickr::HOST_URL}#{Flickr::API_PATH}/?api_key=some_api_key&method=flickr.someMethod&foo=value+which%2Fneeds%26escaping", flickr.send(:request_url, 'someMethod', 'foo' => 'value which/needs&escaping')
  end
  
  # method_missing tests
  def test_should_generate_flickr_method_from_unkown_method_on_flickr_client
    f = flickr_client
    f.expects(:request).with('some.unknown.methodForFlickr', {})
    f.some_unknown_methodForFlickr
  end
  
  # request method tests
  def test_should_make_successful_request
    f = flickr_client
    f.expects(:http_get).with('some.url').returns(successful_xml_response)
    f.expects(:request_url).with('some_method', 'foo' => 'bar').returns("some.url")
    
    f.send(:request, 'some_method', 'foo' => 'bar') # request is protected
  end
  
  def test_should_raise_exception_on_unsuccessful_request
    f = flickr_client
    f.expects(:http_get).returns(unsuccessful_xml_response)
    
    assert_raise(RuntimeError) { f.send(:request, 'some_method', 'foo' => 'bar') }
  end
  
  def test_should_parse_returned_xml_in_successful_request
    f = flickr_client
    f.stubs(:http_get).returns(successful_xml_response)
    expected_response = { "contacts" => { "perpage" => "1000",
                                          "contact" => [{ "nsid"=>"12037949629@N01",
                                                          "username"=>"Eric",
                                                          "ignored"=>"1",
                                                          "family"=>"0",
                                                          "friend"=>"1",
                                                          "realname"=>"Eric Costello",
                                                          "iconserver"=>"1"},
                                                        { "nsid"=>"12037949631@N01",
                                                          "username"=>"neb",
                                                          "ignored"=>"0",
                                                          "family"=>"0",
                                                          "friend"=>"0",
                                                          "realname"=>"Ben Cerveny",
                                                          "iconserver"=>"1"}],
                                          "total" => "2",
                                          "pages"=> "1",
                                          "page"=>"1" },
                          "stat"=>"ok" }
    
    assert_equal expected_response, f.send(:request, 'some_method', 'foo' => 'bar')
  end
  
  def test_should_generate_login_url
    f = flickr_client
    f.expects(:signature_from).with('api_key' => 'some_api_key', 'perms' => 'write').returns('validsignature')
    assert_equal 'http://flickr.com/services/auth/?api_key=some_api_key&perms=write&api_sig=validsignature', f.login_url('write')
  end
  
  def test_should_get_token_from_frob
    f = flickr_client
    f.expects(:request).with('auth.getToken',:frob => 'some_frob').returns({'auth' => {'token' => 'some_auth_token', 'user' => {}}})
    
    auth_token = f.get_token_from('some_frob')
    assert_equal 'some_auth_token', auth_token
  end
  
  def test_should_store_auth_token_in_client
    f = flickr_client
    f.expects(:request).returns({'auth' => {'token' => 'some_auth_token','user' => {}}})
    f.get_token_from('some_frob')
    assert_equal 'some_auth_token', f.auth_token
  end
  
  def test_should_store_authenticated_user_details_in_client
    f = flickr_client
    f.expects(:request).returns({ 'auth' => { 'token' => 'some_auth_token',
                                                            'user' => { 'nsid' => 'foo123',
                                                                        'username' => 'some_user', 'fullname' => 'Some User'}}})
    f.get_token_from('some_frob')
    assert_kind_of Flickr::User, user = f.user
    assert_equal 'foo123', user.id
    assert_equal 'some_user', user.username
    assert_equal 'Some User', user.name
    assert_equal f, user.client
  end
  
  # photos method tests
  def test_should_get_recent_photos_if_no_params_for_photos
    f = flickr_client
    f.expects(:photos_getRecent).returns({"photos" => {"photo" => []}})
    f.photos
  end
  
  def test_should_instantiate_recent_photos_with_id_and_all_params_returned_by_flickr
    f = flickr_client
    f.expects(:photos_getRecent).returns(dummy_photos_response)
    Flickr::Photo.expects(:new).with("foo123",
                                     "some_api_key", { "key1" => "value1",
                                                       "key2" => "value2"})
    Flickr::Photo.expects(:new).with("bar456",
                                     "some_api_key", { "key3" => "value3"})
    f.photos
  end
  
  # photos_search method tests
  def test_should_search_photos
    f = authenticated_flickr_client
    f.expects(:request).with('photos.search', anything).returns(dummy_photos_response)
    photos = f.photos_search
    assert_kind_of Flickr::Photo, photos.first
  end
  
  # users method tests
  def test_should_find_user_from_email
    f = flickr_client
    f.expects(:request).with('people.findByEmail', anything).returns(dummy_user_response)
    assert_kind_of Flickr::User, user = f.users("email@test.com")
    assert_equal "12037949632@N01", user.id
    assert_equal "Stewart", user.username
  end
  
  def test_should_find_user_from_username_if_fails_to_get_from_email
    f = flickr_client
    f.expects(:request).with('people.findByEmail', anything).raises
    f.expects(:request).with('people.findByUsername', anything).returns(dummy_user_response)
    assert_kind_of Flickr::User, f.users("email@test.com")
  end
  
  def test_should_pass_on_flickr_client_when_finding_user
    f = flickr_client
    f.stubs(:request).returns(dummy_user_response)
    user = f.users("email@test.com")
    assert_equal f, user.client
  end
  
  # groups method tests
  def test_should_search_for_given_group
    f = flickr_client
    f.expects(:request).with("groups.search", {"text" => "foo"}).returns(dummy_groups_response)
    f.groups("foo")
  end
  
  def test_should_search_for_given_group_with_additional_params
    f = flickr_client
    f.expects(:request).with("groups.search", {"text" => "foo", "per_page" => "1"}).returns(dummy_groups_response)
    f.groups("foo", "per_page" => "1")
  end
  
  def test_should_instantiate_groups_from_search_response
    f = flickr_client
    f.stubs(:request).returns(dummy_groups_response)
    assert_kind_of Array, groups = f.groups("foo")
    assert_kind_of Flickr::Group, group = groups.first
    assert_equal "group1", group.id
    assert_equal "Group One", group.name
    assert_equal "0", group.eighteenplus
    assert_equal f, group.client
  end
  
  def test_should_instantiate_groups_from_search_response_with_single_group_returned
    f = flickr_client
    f.stubs(:request).returns(dummy_single_group_response)
    assert_kind_of Array, groups = f.groups("foo")
    assert_equal 1, groups.size
    assert_equal "group1", groups.first.id
  end
  
  # ##### DIRECT MODE
  #
  # def test_test_echo
  # assert_equal @f.test_echo['stat'], 'ok'
  # end
  # def test_test_login
  # assert_equal @f.test_login['stat'], 'ok'
  # end
  #
  #
  # ##### BASICS
  #
  # def test_login
  # assert_equal @username, @f.user.getInfo.username
  # end
  #
  # def test_find_by_url
  # assert_equal @group_id, @f.find_by_url(@group_url).getInfo.id # find group by URL
  # assert_equal @user_id, @f.find_by_url(@user_url).getInfo.id # find user by URL
  # end
  #
  # def test_licenses
  # assert_kind_of Array, @f.licenses # find all licenses
  # end
  #
  
  #
  # Flickr#photos tests
  #
  
  
  # ##### Flickr::User tests
  #
  def test_should_instantiate_user
    user = new_user
    assert_equal 'foo123', user.id
    assert_equal 'some_user', user.username
    assert_equal 'bar', user.instance_variable_get(:@foo) # should collect all other params up and store as instance variables
  end
  
  def test_should_instantiate_new_user_with_old_api
    Flickr.any_instance.stubs(:login) # stub logging in
    user = Flickr::User.new('foo123',
                            'some_user',
                            'email@test.com', # email irrelevant since Flickr API no longer supports authentication in this way
                            'password', # password irrelevant since Flickr API no longer supports authentication in this way
                            'bar456')
    assert_equal 'foo123', user.id
    assert_equal 'some_user', user.username
    assert_equal 'email@test.com', user.instance_variable_get(:@email)
    assert_equal 'password', user.instance_variable_get(:@password)
    assert_equal 'bar456', user.client.api_key
  end
  
  def test_should_instantiate_new_client_when_instantiating_user_if_no_client_passed_in_params
    f = flickr_client
    Flickr.expects(:new).returns(f)
    user = new_user( 'api_key' => 'an_api_key' )
    assert_equal f, user.client
  end
  
  def test_should_not_instantiate_new_client_when_instantiating_user_if_client_passed_in_params
    f = flickr_client
    Flickr.expects(:new).never
    user = new_user( 'client' => f )
    assert_equal f, user.client
  end
  
  def test_should_not_instantiate_client_if_no_api_key_passed
    Flickr.expects(:new).never
    user = new_user
    assert_nil user.client
  end
  
  def test_should_get_users_public_groups
    f = flickr_client
    f.expects(:request).with("people.getPublicGroups", anything).returns(dummy_groups_response)
    new_user( 'client' => f ).groups
  end
  
  def test_should_instantiate_users_public_groups
    f = flickr_client
    f.stubs(:request).returns(dummy_groups_response)
    user = new_user( 'client' => f )
 
    groups = user.groups
    assert_equal 2, groups.size
    assert_kind_of Flickr::Group, group = groups.first
    assert_equal "group1", group.id
    assert_equal "Group One", group.name
    assert_equal "0", group.eighteenplus
    assert_equal f, group.client
  end
  
  def test_should_instantiate_users_public_groups_when_only_one_returned
    f = flickr_client
    f.stubs(:request).returns(dummy_single_group_response)
    user = new_user( 'client' => f )
    groups = user.groups
    assert_equal 1, groups.size
  end
  # def test_getInfo
  # @u.getInfo
  # assert_equal @username, @u.username
  # end
  #
  # def test_groups
  # assert_kind_of Flickr::Group, @u.groups.first # public groups
  # end
  #
  #
  # def test_contacts
  # assert_kind_of Flickr::User, @u.contacts.first # public contacts
  # end
  #
  # def test_favorites
  # assert_kind_of Flickr::Photo, @u.favorites.first # public favorites
  # end
  #
  # def test_photosets
  # assert_kind_of Flickr::Photoset, @u.photosets.first # public photosets
  # end
  #
  # def test_tags
  # assert_kind_of Array, @u.tags # tags
  # end
  #
  # def test_contactsPhotos
  # assert_kind_of Flickr::Photo, @u.contactsPhotos.first # contacts' favorites
  # end
  
  # User#photos tests
  
  def test_should_get_users_public_photos
    Flickr.expects(:new).at_least_once.returns(photos_response_stubber(:people_getPublicPhotos))
    user = Flickr::User.new(nil, "some_user", nil, nil, "some_api_key")
 
    photos = user.photos
    assert_equal 2, photos.size
    assert_kind_of Flickr::Photo, photos.first
  end
  
  def test_should_get_users_public_photos_when_only_one_returned
    Flickr.expects(:new).at_least_once.returns(photos_response_stubber(:people_getPublicPhotos, dummy_single_photo_response))
    user = Flickr::User.new(nil, "some_user", nil, nil, "some_api_key")
 
    photos = user.photos
    assert_equal 1, photos.size
    assert_kind_of Flickr::Photo, photos.first
  end
  
  def test_should_instantiate_photos_with_id_and_all_params_returned_by_query_and_username
    Flickr.stubs(:new).returns(photos_response_stubber(:people_getPublicPhotos))
    user = Flickr::User.new(nil, "some_user", nil, nil, "some_api_key")
    Flickr::Photo.expects(:new).with("foo123",
                                     "some_api_key", { "key1" => "value1",
               &n