reagent-flickr 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (7) hide show
  1. data/History.txt +43 -0
  2. data/LICENSE +20 -0
  3. data/README.txt +73 -0
  4. data/TODO +6 -0
  5. data/lib/flickr.rb +675 -0
  6. data/test/test_flickr.rb +892 -0
  7. metadata +66 -0
@@ -0,0 +1,892 @@
1
+ require 'rubygems'
2
+ require 'flickr'
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestFlickr < Test::Unit::TestCase
7
+
8
+ # Flickr client tests
9
+ #
10
+ # instantiation tests
11
+ def test_should_instantiate_new_flickr_client
12
+ Flickr.any_instance.stubs(:login)
13
+ flickr = Flickr.new('some_api_key', 'email@test.com', 'some_password', 'some_shared_secret')
14
+
15
+ assert_equal 'some_api_key', flickr.api_key
16
+ assert_equal 'some_shared_secret', flickr.instance_variable_get(:@shared_secret)
17
+ end
18
+
19
+ def test_should_try_to_login_using_old_api_if_email_and_password_passed
20
+ Flickr.any_instance.expects(:login).with('email@test.com', 'some_password') # checks email and password have been set
21
+ flickr = Flickr.new('some_api_key', 'email@test.com', 'some_password', 'some_shared_secret')
22
+ end
23
+
24
+ def test_should_instantiate_new_flickr_client_on_new_api
25
+ flickr = Flickr.new('api_key' => 'some_api_key', 'email' => 'email@test.com', 'password' => 'some_password', 'shared_secret' => 'some_shared_secret', 'foo' => 'bar')
26
+
27
+ assert_equal 'some_api_key', flickr.api_key
28
+ assert_equal 'some_shared_secret', flickr.instance_variable_get(:@shared_secret)
29
+ assert_nil flickr.instance_variable_get(:@foo) # should ignore other params
30
+ end
31
+
32
+ def test_should_not_try_to_login_using_old_api_when_instantiate_new_flickr_client_on_new_api
33
+ Flickr.any_instance.expects(:login).never # doesn't bother trying to login with new api -- it'll fail in any case
34
+ flickr = Flickr.new('api_key' => 'some_api_key', 'email' => 'email@test.com', 'password' => 'some_password', 'shared_secret' => 'some_shared_secret', 'foo' => 'bar')
35
+ end
36
+
37
+ # signature_from method tests
38
+ def test_should_return_signature_from_given_params
39
+ assert_equal Digest::MD5.hexdigest('shared_secret_codea_param1234xb_param5678yc_param97531t'),
40
+ authenticated_flickr_client.send(:signature_from, {:b_param => '5678y', 'c_param' => '97531t', :a_param => '1234x', :d_param => nil})
41
+ end
42
+
43
+ def test_should_return_nil_for_signature_when_no_shared_secret
44
+ assert_nil flickr_client.send(:signature_from, {:b_param => '5678y', :c_param => '97531t', :a_param => '1234x'})
45
+ end
46
+
47
+ # request_url method tests
48
+ def test_should_get_signature_for_params_when_building_url
49
+ f = authenticated_flickr_client
50
+ f.expects(:signature_from).with( 'method' => 'flickr.someMethod',
51
+ 'api_key' => 'some_api_key',
52
+ 'foo' => 'value which/needs&escaping',
53
+ 'auth_token' => 'some_auth_token').returns("foo123bar456")
54
+
55
+ url = f.send(:request_url, 'someMethod', 'foo' => 'value which/needs&escaping')
56
+ end
57
+
58
+ def test_should_build_url_from_params_with_signature
59
+ f = authenticated_flickr_client
60
+ f.stubs(:signature_from).returns("foo123bar456")
61
+
62
+ url = f.send(:request_url, 'someMethod', 'foo' => 'value which/needs&escaping')
63
+ [ "#{Flickr::HOST_URL}#{Flickr::API_PATH}",
64
+ 'api_key=some_api_key',
65
+ 'method=flickr.someMethod',
66
+ 'foo=value+which%2Fneeds%26escaping',
67
+ 'auth_token=some_auth_token',
68
+ 'api_sig=foo123bar456'].each do |kv_pair|
69
+ assert_match Regexp.new(Regexp.escape(kv_pair)), url
70
+ end
71
+ end
72
+
73
+ def test_should_build_url_from_params_when_signature_returns_nil
74
+ flickr = flickr_client
75
+ flickr.stubs(:signature_from)
76
+ assert_equal "#{Flickr::HOST_URL}#{Flickr::API_PATH}/?api_key=some_api_key&method=flickr.someMethod", flickr.send(:request_url, 'someMethod')
77
+ 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)
78
+ 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)
79
+ 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')
80
+ end
81
+
82
+ # method_missing tests
83
+ def test_should_generate_flickr_method_from_unkown_method_on_flickr_client
84
+ f = flickr_client
85
+ f.expects(:request).with('some.unknown.methodForFlickr', {})
86
+ f.some_unknown_methodForFlickr
87
+ end
88
+
89
+ # request method tests
90
+ def test_should_make_successful_request
91
+ f = flickr_client
92
+ f.expects(:http_get).with('some.url').returns(successful_xml_response)
93
+ f.expects(:request_url).with('some_method', 'foo' => 'bar').returns("some.url")
94
+
95
+ f.send(:request, 'some_method', 'foo' => 'bar') # request is protected
96
+ end
97
+
98
+ def test_should_raise_exception_on_unsuccessful_request
99
+ f = flickr_client
100
+ f.expects(:http_get).returns(unsuccessful_xml_response)
101
+
102
+ assert_raise(RuntimeError) { f.send(:request, 'some_method', 'foo' => 'bar') }
103
+ end
104
+
105
+ def test_should_parse_returned_xml_in_successful_request
106
+ f = flickr_client
107
+ f.stubs(:http_get).returns(successful_xml_response)
108
+ expected_response = { "contacts" => { "perpage" => "1000",
109
+ "contact" => [{ "nsid"=>"12037949629@N01",
110
+ "username"=>"Eric",
111
+ "ignored"=>"1",
112
+ "family"=>"0",
113
+ "friend"=>"1",
114
+ "realname"=>"Eric Costello",
115
+ "iconserver"=>"1"},
116
+ { "nsid"=>"12037949631@N01",
117
+ "username"=>"neb",
118
+ "ignored"=>"0",
119
+ "family"=>"0",
120
+ "friend"=>"0",
121
+ "realname"=>"Ben Cerveny",
122
+ "iconserver"=>"1"}],
123
+ "total" => "2",
124
+ "pages"=> "1",
125
+ "page"=>"1" },
126
+ "stat"=>"ok" }
127
+
128
+ assert_equal expected_response, f.send(:request, 'some_method', 'foo' => 'bar')
129
+ end
130
+
131
+ # photos_request tests
132
+ def test_should_pass_photos_request_params_to_request
133
+ f = flickr_client
134
+ f.expects(:request).with('flickr.method', :one => 1, :two => "2").returns(dummy_photos_response)
135
+ f.photos_request( 'flickr.method', :one => 1, :two => "2")
136
+ end
137
+
138
+ def test_should_instantiate_recent_photos_with_id_and_all_params_returned_by_flickr
139
+ f = flickr_client
140
+ f.expects(:request).returns(dummy_photos_response)
141
+ Flickr::Photo.expects(:new).with("foo123",
142
+ "some_api_key", { "key1" => "value1",
143
+ "key2" => "value2"})
144
+ Flickr::Photo.expects(:new).with("bar456",
145
+ "some_api_key", { "key3" => "value3"})
146
+ photos = f.photos_request('some_method')
147
+ end
148
+
149
+ def test_should_return_list_of_photos
150
+ f = flickr_client
151
+ f.expects(:request).returns(dummy_photos_response)
152
+ photos = f.photos_request('some_method')
153
+ assert_equal 2, photos.size
154
+ assert_kind_of Flickr::Photo, photos.first
155
+ assert_equal "foo123", photos.first.id
156
+ end
157
+
158
+ def test_should_work_with_single_result
159
+ f = flickr_client
160
+ f.expects(:request).returns(dummy_single_photo_response)
161
+ photos = f.photos_request('some_method')
162
+ assert_equal 1, photos.size
163
+ assert_kind_of Flickr::Photo, photos.first
164
+ assert_equal "foo123", photos.first.id
165
+ end
166
+
167
+ def test_should_work_with_empty_result
168
+ f = flickr_client
169
+ f.expects(:request).returns(dummy_zero_photo_response)
170
+ photos = f.photos_request('some_method')
171
+ assert_equal [], photos
172
+ end
173
+
174
+ def test_should_generate_login_url
175
+ f = flickr_client
176
+ f.expects(:signature_from).with('api_key' => 'some_api_key', 'perms' => 'write').returns('validsignature')
177
+ assert_equal 'http://flickr.com/services/auth/?api_key=some_api_key&perms=write&api_sig=validsignature', f.login_url('write')
178
+ end
179
+
180
+ def test_should_get_token_from_frob
181
+ f = flickr_client
182
+ f.expects(:request).with('auth.getToken',:frob => 'some_frob').returns({'auth' => {'token' => 'some_auth_token', 'user' => {}}})
183
+
184
+ auth_token = f.get_token_from('some_frob')
185
+ assert_equal 'some_auth_token', auth_token
186
+ end
187
+
188
+ def test_should_store_auth_token_in_client
189
+ f = flickr_client
190
+ f.expects(:request).returns({'auth' => {'token' => 'some_auth_token','user' => {}}})
191
+ f.get_token_from('some_frob')
192
+ assert_equal 'some_auth_token', f.auth_token
193
+ end
194
+
195
+ def test_should_store_authenticated_user_details_in_client
196
+ f = flickr_client
197
+ f.expects(:request).returns({ 'auth' => { 'token' => 'some_auth_token',
198
+ 'user' => { 'nsid' => 'foo123',
199
+ 'username' => 'some_user', 'fullname' => 'Some User'}}})
200
+ f.get_token_from('some_frob')
201
+ assert_kind_of Flickr::User, user = f.user
202
+ assert_equal 'foo123', user.id
203
+ assert_equal 'some_user', user.username
204
+ assert_equal 'Some User', user.name
205
+ assert_equal f, user.client
206
+ end
207
+
208
+ # photos method tests
209
+ def test_should_get_recent_photos_if_no_params_for_photos
210
+ f = flickr_client
211
+ f.expects(:photos_search)
212
+ f.photos
213
+ end
214
+
215
+ # photos_search method tests
216
+ def test_should_search_photos
217
+ f = authenticated_flickr_client
218
+ f.expects(:request).with('photos.search', anything).returns(dummy_photos_response)
219
+ photos = f.photos_search
220
+ assert_kind_of Flickr::Photo, photos.first
221
+ end
222
+
223
+ # users method tests
224
+ def test_should_find_user_from_email
225
+ f = flickr_client
226
+ f.expects(:request).with('people.findByEmail', anything).returns(dummy_user_response)
227
+ assert_kind_of Flickr::User, user = f.users("email@test.com")
228
+ assert_equal "12037949632@N01", user.id
229
+ assert_equal "Stewart", user.username
230
+ end
231
+
232
+ def test_should_find_user_from_username_if_fails_to_get_from_email
233
+ f = flickr_client
234
+ f.expects(:request).with('people.findByEmail', anything).raises
235
+ f.expects(:request).with('people.findByUsername', anything).returns(dummy_user_response)
236
+ assert_kind_of Flickr::User, f.users("email@test.com")
237
+ end
238
+
239
+ def test_should_pass_on_flickr_client_when_finding_user
240
+ f = flickr_client
241
+ f.stubs(:request).returns(dummy_user_response)
242
+ user = f.users("email@test.com")
243
+ assert_equal f, user.client
244
+ end
245
+
246
+ # groups method tests
247
+ def test_should_search_for_given_group
248
+ f = flickr_client
249
+ f.expects(:request).with("groups.search", {"text" => "foo"}).returns(dummy_groups_response)
250
+ f.groups("foo")
251
+ end
252
+
253
+ def test_should_search_for_given_group_with_additional_params
254
+ f = flickr_client
255
+ f.expects(:request).with("groups.search", {"text" => "foo", "per_page" => "1"}).returns(dummy_groups_response)
256
+ f.groups("foo", "per_page" => "1")
257
+ end
258
+
259
+ def test_should_instantiate_groups_from_search_response
260
+ f = flickr_client
261
+ f.stubs(:request).returns(dummy_groups_response)
262
+ assert_kind_of Array, groups = f.groups("foo")
263
+ assert_kind_of Flickr::Group, group = groups.first
264
+ assert_equal "group1", group.id
265
+ assert_equal "Group One", group.name
266
+ assert_equal "0", group.eighteenplus
267
+ assert_equal f, group.client
268
+ end
269
+
270
+ def test_should_instantiate_groups_from_search_response_with_single_group_returned
271
+ f = flickr_client
272
+ f.stubs(:request).returns(dummy_single_group_response)
273
+ assert_kind_of Array, groups = f.groups("foo")
274
+ assert_equal 1, groups.size
275
+ assert_equal "group1", groups.first.id
276
+ end
277
+
278
+ # ##### DIRECT MODE
279
+ #
280
+ # def test_test_echo
281
+ # assert_equal @f.test_echo['stat'], 'ok'
282
+ # end
283
+ # def test_test_login
284
+ # assert_equal @f.test_login['stat'], 'ok'
285
+ # end
286
+ #
287
+ #
288
+ # ##### BASICS
289
+ #
290
+ # def test_login
291
+ # assert_equal @username, @f.user.getInfo.username
292
+ # end
293
+ #
294
+ # def test_find_by_url
295
+ # assert_equal @group_id, @f.find_by_url(@group_url).getInfo.id # find group by URL
296
+ # assert_equal @user_id, @f.find_by_url(@user_url).getInfo.id # find user by URL
297
+ # end
298
+ #
299
+ # def test_licenses
300
+ # assert_kind_of Array, @f.licenses # find all licenses
301
+ # end
302
+ #
303
+
304
+ #
305
+ # Flickr#photos tests
306
+ #
307
+
308
+
309
+ # ##### Flickr::User tests
310
+ #
311
+ def test_should_instantiate_user
312
+ user = new_user
313
+ assert_equal 'foo123', user.id
314
+ assert_equal 'some_user', user.username
315
+ assert_equal 'bar', user.instance_variable_get(:@foo) # should collect all other params up and store as instance variables
316
+ end
317
+
318
+ def test_should_instantiate_new_user_with_old_api
319
+ Flickr.any_instance.stubs(:login) # stub logging in
320
+ user = Flickr::User.new('foo123',
321
+ 'some_user',
322
+ 'email@test.com', # email irrelevant since Flickr API no longer supports authentication in this way
323
+ 'password', # password irrelevant since Flickr API no longer supports authentication in this way
324
+ 'bar456')
325
+ assert_equal 'foo123', user.id
326
+ assert_equal 'some_user', user.username
327
+ assert_equal 'email@test.com', user.instance_variable_get(:@email)
328
+ assert_equal 'password', user.instance_variable_get(:@password)
329
+ assert_equal 'bar456', user.client.api_key
330
+ end
331
+
332
+ def test_should_instantiate_new_client_when_instantiating_user_if_no_client_passed_in_params
333
+ f = flickr_client
334
+ Flickr.expects(:new).returns(f)
335
+ user = new_user( 'api_key' => 'an_api_key' )
336
+ assert_equal f, user.client
337
+ end
338
+
339
+ def test_should_not_instantiate_new_client_when_instantiating_user_if_client_passed_in_params
340
+ f = flickr_client
341
+ Flickr.expects(:new).never
342
+ user = new_user( 'client' => f )
343
+ assert_equal f, user.client
344
+ end
345
+
346
+ def test_should_not_instantiate_client_if_no_api_key_passed
347
+ Flickr.expects(:new).never
348
+ user = new_user
349
+ assert_nil user.client
350
+ end
351
+
352
+ def test_should_get_users_public_groups
353
+ f = flickr_client
354
+ f.expects(:request).with("people.getPublicGroups", anything).returns(dummy_groups_response)
355
+ new_user( 'client' => f ).groups
356
+ end
357
+
358
+ def test_should_instantiate_users_public_groups
359
+ f = flickr_client
360
+ f.stubs(:request).returns(dummy_groups_response)
361
+ user = new_user( 'client' => f )
362
+
363
+ groups = user.groups
364
+ assert_equal 2, groups.size
365
+ assert_kind_of Flickr::Group, group = groups.first
366
+ assert_equal "group1", group.id
367
+ assert_equal "Group One", group.name
368
+ assert_equal "0", group.eighteenplus
369
+ assert_equal f, group.client
370
+ end
371
+
372
+ def test_should_instantiate_users_public_groups_when_only_one_returned
373
+ f = flickr_client
374
+ f.stubs(:request).returns(dummy_single_group_response)
375
+ user = new_user( 'client' => f )
376
+ groups = user.groups
377
+ assert_equal 1, groups.size
378
+ end
379
+ # def test_getInfo
380
+ # @u.getInfo
381
+ # assert_equal @username, @u.username
382
+ # end
383
+ #
384
+ # def test_groups
385
+ # assert_kind_of Flickr::Group, @u.groups.first # public groups
386
+ # end
387
+ #
388
+ #
389
+ # def test_contacts
390
+ # assert_kind_of Flickr::User, @u.contacts.first # public contacts
391
+ # end
392
+ #
393
+ # def test_favorites
394
+ # assert_kind_of Flickr::Photo, @u.favorites.first # public favorites
395
+ # end
396
+ #
397
+ # def test_photosets
398
+ # assert_kind_of Flickr::Photoset, @u.photosets.first # public photosets
399
+ # end
400
+ #
401
+ # def test_tags
402
+ # assert_kind_of Array, @u.tags # tags
403
+ # end
404
+ #
405
+ # def test_contactsPhotos
406
+ # assert_kind_of Flickr::Photo, @u.contactsPhotos.first # contacts' favorites
407
+ # end
408
+
409
+ # User#photos tests
410
+
411
+ def test_should_get_users_public_photos
412
+ client = mock
413
+ client.expects(:photos_request).with('people.getPublicPhotos', {'user_id' => 'some_id'}).returns([new_photo, new_photo])
414
+ Flickr.expects(:new).at_least_once.returns(client)
415
+
416
+ user = Flickr::User.new("some_id", "some_user", nil, nil, "some_api_key")
417
+
418
+ photos = user.photos
419
+ assert_equal 2, photos.size
420
+ assert_kind_of Flickr::Photo, photos.first
421
+ end
422
+
423
+ def test_should_instantiate_favorite_photos_with_id_and_all_params_returned_by_query
424
+ client = mock
425
+ client.expects(:photos_request).with('favorites.getPublicList', {'user_id' => 'some_id'})
426
+ Flickr.expects(:new).at_least_once.returns(client)
427
+ user = Flickr::User.new("some_id", "some_user", nil, nil, "some_api_key")
428
+ user.favorites
429
+ end
430
+
431
+ def test_should_instantiate_contacts_photos_with_id_and_all_params_returned_by_query
432
+ client = mock
433
+ client.expects(:photos_request).with('photos.getContactsPublicPhotos', {'user_id' => 'some_id'})
434
+ Flickr.expects(:new).at_least_once.returns(client)
435
+ user = Flickr::User.new('some_id', "some_user", nil, nil, "some_api_key")
436
+ user.contactsPhotos
437
+ end
438
+
439
+ # ##### Flickr::Photo tests
440
+
441
+ def test_should_initialize_photo_from_id
442
+ photo = Flickr::Photo.new("foo123")
443
+ assert_equal "foo123", photo.id
444
+ end
445
+
446
+ def test_should_save_extra_params_as_instance_variables
447
+ photo = Flickr::Photo.new('foo123', 'some_api_key', { 'key1' => 'value1', 'key2' => 'value2'})
448
+ assert_equal 'value1', photo.instance_variable_get(:@key1)
449
+ assert_equal 'value2', photo.instance_variable_get(:@key2)
450
+ end
451
+
452
+ def test_should_be_able_to_access_instance_variables_through_hash_like_interface
453
+ photo = Flickr::Photo.new
454
+ photo.instance_variable_set(:@key1, 'value1')
455
+ assert_equal 'value1', photo['key1']
456
+ assert_equal 'value1', photo[:key1]
457
+ assert_nil photo[:key2]
458
+ assert_nil photo['key2']
459
+ end
460
+
461
+ #
462
+ # owner tests
463
+ def test_should_return_owner_when_flickr_user
464
+ user = Flickr::User.new
465
+ photo = new_photo("owner" => user)
466
+
467
+ assert_equal user, photo.owner
468
+ end
469
+
470
+ def test_should_get_info_on_owner_if_not_known
471
+ photo = new_photo("owner" => nil)
472
+ # stubbing private methods causes problems so we mock client method, which is what Photo#getInfo users to make API call
473
+ Flickr.any_instance.expects(:photos_getInfo).returns('photo' => { 'owner'=>{'nsid'=>'abc123', 'username'=>'SomeUserName', 'realname'=>"", 'location'=>''},
474
+ 'notes' => {}, 'urls' => {'url' => {'content' => 'http://prettyurl'}}})
475
+
476
+ owner = photo.owner
477
+ assert_kind_of Flickr::User, owner
478
+ assert_equal 'abc123', owner.id
479
+ assert_equal 'SomeUserName', owner.username
480
+ end
481
+
482
+ def test_should_instantiate_flickr_user_from_owner_id_if_we_have_it
483
+ photo = Flickr::Photo.new
484
+ photo.instance_variable_set(:@owner, "some_user_id")
485
+ Flickr.any_instance.expects(:photos_getInfo).never
486
+
487
+ user = photo.owner
488
+ assert_kind_of Flickr::User, user
489
+ assert_equal "some_user_id", user.id
490
+ end
491
+
492
+ def test_should_cache_owner_when_instantiated
493
+ user = Flickr::User.new
494
+ photo = Flickr::Photo.new
495
+ photo.instance_variable_set(:@owner, "some_user_id")
496
+ Flickr::User.expects(:new).returns(user)
497
+
498
+ photo.owner
499
+ photo.owner # call twice but mock expects only one call
500
+ end
501
+
502
+ #
503
+ # image_source_uri_from_self tests
504
+ def test_should_build_image_source_uri_from_self
505
+ assert_equal "http://farm1.static.flickr.com/2/1418878_1e92283336.jpg",
506
+ new_photo.send(:image_source_uri_from_self) # no size specified
507
+ end
508
+
509
+ def test_should_build_image_source_uri_from_self_for_given_size
510
+ assert_equal "http://farm1.static.flickr.com/2/1418878_1e92283336_m.jpg",
511
+ new_photo.send(:image_source_uri_from_self, "Small") # size specified
512
+ end
513
+
514
+ def test_should_build_image_source_uri_from_self_for_default_size_when_explicitly_asked_for
515
+ assert_equal "http://farm1.static.flickr.com/2/1418878_1e92283336.jpg",
516
+ new_photo.send(:image_source_uri_from_self, "Medium") # medium size specified -- the default
517
+ end
518
+
519
+ def test_should_build_image_source_uri_from_self_for_default_size_when_unknown_size_asked_for
520
+ assert_equal "http://farm1.static.flickr.com/2/1418878_1e92283336.jpg",
521
+ new_photo.send(:image_source_uri_from_self, "Dummy") # bad size specified
522
+ end
523
+
524
+ def test_should_return_nil_for_image_source_uri_if_no_attributes
525
+ assert_nil Flickr::Photo.new.send(:image_source_uri_from_self)
526
+ end
527
+
528
+ def test_should_return_nil_for_image_source_uri_if_missing_required_attributes
529
+ assert_nil Flickr::Photo.new("1418878", nil, "farm" => "1").send(:image_source_uri_from_self)
530
+ end
531
+
532
+ def test_image_source_uri_from_self_should_normalize_size
533
+ photo = new_photo
534
+ assert_equal photo.send(:image_source_uri_from_self, 'Large'),
535
+ photo.send(:image_source_uri_from_self, :large)
536
+ end
537
+
538
+ def test_uri_for_photo_from_self_should_normalize_size
539
+ photo = new_photo
540
+ assert_equal photo.send(:uri_for_photo_from_self, 'Large'),
541
+ photo.send(:uri_for_photo_from_self, :large)
542
+ end
543
+
544
+ def test_should_get_source_uri_by_building_from_self_if_possible
545
+ photo = Flickr::Photo.new
546
+ photo.expects(:image_source_uri_from_self).with('Medium').returns(true) # return any non-false-evaluating value so that sizes method isn't called
547
+ photo.source
548
+ end
549
+
550
+ def test_should_get_source_uri_by_building_from_self_if_possible_requesting_source_for_given_size
551
+ photo = Flickr::Photo.new
552
+ photo.expects(:image_source_uri_from_self).with('Large').returns(true) # return any non-false-evaluating value so that sizes method isn't called
553
+ photo.source('Large')
554
+ end
555
+
556
+ def test_should_get_source_uri_by_calling_sizes_method_if_no_luck_building_uri
557
+ photo = Flickr::Photo.new
558
+ photo.stubs(:image_source_uri_from_self) # ...and hence returns nil
559
+ photo.expects(:sizes).with('Medium').returns({})
560
+ photo.source
561
+ end
562
+
563
+ def test_should_build_uri_for_photo_from_self
564
+ assert_equal "http://www.flickr.com/photos/abc123/1418878", new_photo.send(:uri_for_photo_from_self)
565
+ end
566
+
567
+ def test_should_build_uri_for_photo_from_self_when_owner_is_a_string
568
+ assert_equal "http://www.flickr.com/photos/789user321/1418878", new_photo('owner' => "789user321").send(:uri_for_photo_from_self)
569
+ end
570
+
571
+ def test_should_build_uri_for_photo_from_self_for_given_size
572
+ assert_equal "http://www.flickr.com/photos/abc123/1418878/sizes/s/", new_photo.send(:uri_for_photo_from_self, "Small")
573
+ end
574
+
575
+ def test_should_build_uri_for_photo_from_self_with_unknown_size
576
+ assert_equal "http://www.flickr.com/photos/abc123/1418878", new_photo.send(:uri_for_photo_from_self, "Dummy")
577
+ end
578
+
579
+ def test_should_return_nil_for_uri_for_photo_when_no_user_id
580
+ assert_nil Flickr::Photo.new("1418878", nil).send(:uri_for_photo_from_self)
581
+ end
582
+
583
+ def test_should_return_nil_for_uri_for_photo_when_no_photo_id
584
+ assert_nil Flickr::Photo.new.send(:uri_for_photo_from_self)
585
+ end
586
+
587
+ def test_should_get_uri_for_photo_flickr_page
588
+ photo = new_photo
589
+ assert_equal "http://www.flickr.com/photos/abc123/1418878", photo.url
590
+ end
591
+
592
+ def test_should_return_main_page_for_photo_flickr_page_when_medium_size_requested_as_per_previous_version
593
+ assert_equal "http://www.flickr.com/photos/abc123/1418878", new_photo.url('Medium')
594
+ end
595
+
596
+ def test_should_call_size_url_if_url_given_a_size
597
+ photo = new_photo
598
+ photo.expects(:size_url).with('Large')
599
+ photo.url('Large')
600
+ end
601
+
602
+ def test_should_get_flickr_page_uri_by_building_from_self_if_possible_requesting_source_for_given_size
603
+ photo = new_photo
604
+ photo.expects(:uri_for_photo_from_self).with('Large').returns(true) # return any non-false-evaluating value so that sizes method isn't called
605
+ photo.size_url('Large')
606
+ end
607
+
608
+ def test_should_get_flickr_page_uri_by_calling_sizes_method_if_no_luck_building_uri
609
+ photo = new_photo
610
+ photo.stubs(:uri_for_photo_from_self) # ...and hence returns nil
611
+ photo.expects(:sizes).with('Large').returns({})
612
+ photo.size_url('Large')
613
+ end
614
+
615
+ def test_should_allow_size_to_be_lowercase_or_symbol
616
+ photo = new_photo
617
+ assert_equal photo.normalize_size('Small'), 'Small'
618
+ assert_equal photo.normalize_size('small'), 'Small'
619
+ assert_equal photo.normalize_size(:small), 'Small'
620
+ assert_equal photo.normalize_size(:Small), 'Small'
621
+ assert_equal photo.normalize_size('smAlL'), 'Small'
622
+
623
+ assert_equal photo.normalize_size(""), ""
624
+ assert_nil photo.normalize_size(nil)
625
+ end
626
+
627
+ def test_size_url_should_normalize_size
628
+ photo = new_photo
629
+ assert_equal photo.size_url('Large'), photo.size_url(:large)
630
+ end
631
+
632
+ def test_url_should_normalize_size
633
+ photo = new_photo
634
+ assert_equal photo.url('Medium'), photo.url(:medium)
635
+ assert_equal photo.url('Small'), photo.url('small')
636
+ end
637
+
638
+ def test_source_should_normalize_size
639
+ photo = new_photo
640
+ assert_equal photo.source('Large'), photo.source(:large)
641
+ end
642
+
643
+ def test_sizes_should_normalize_size
644
+ sizes = {'sizes' => {'size' => [{'label' => 'Small'}, {'label' => 'Large'}]}}
645
+ photo = new_photo
646
+ photo.client.expects(:photos_getSizes).at_least_once.returns(sizes)
647
+ assert_equal photo.sizes('Large'), photo.sizes(:large)
648
+ end
649
+
650
+ # Photo#context tests
651
+ def test_should_call_photos_getContext_to_get_context_photos
652
+ Flickr.any_instance.expects(:photos_getContext).returns({'prevphoto' => {}, 'nextphoto' => {}})
653
+ new_photo.context
654
+ end
655
+
656
+ def test_should_instantiate_context_photos_with_id_and_all_params_returned_by_query
657
+ photo = new_photo
658
+ Flickr.any_instance.expects(:photos_getContext).returns({ 'prevphoto' => {'id' => '123', 'key_1' => 'value_1' },
659
+ 'nextphoto' => {'id' => '456', 'key_2' => 'value_2'}})
660
+ Flickr::Photo.expects(:new).with("123", "foo123", { "key_1" => "value_1"})
661
+ Flickr::Photo.expects(:new).with("456", "foo123", { "key_2" => "value_2"})
662
+
663
+ photo.context
664
+ end
665
+
666
+ def test_should_not_instantiate_context_photos_with_id_of_0
667
+ photo = new_photo
668
+ Flickr.any_instance.expects(:photos_getContext).returns({ 'prevphoto' => {'id' => '123', 'key_1' => 'value_1' },
669
+ 'nextphoto' => {'id' => '0', 'key_2' => 'value_2'}})
670
+ Flickr::Photo.expects(:new).with("123", anything, anything)
671
+ Flickr::Photo.expects(:new).with("0", anything, anything).never
672
+
673
+ photo.context
674
+ end
675
+
676
+ # ##### Flickr::Group tests
677
+ #
678
+ def test_should_instantiate_group_from_id
679
+ group = Flickr::Group.new("group1")
680
+ assert_equal "group1", group.id
681
+ end
682
+
683
+ # tests old api for instantiating groups
684
+ def test_should_instantiate_group_from_id_and_api_key
685
+ f = flickr_client
686
+ Flickr.expects(:new).with("some_api_key").returns(f)
687
+ group = Flickr::Group.new("group1", "some_api_key")
688
+ assert_equal f, group.client
689
+ end
690
+
691
+ # new api for instantiating groups
692
+ def test_should_instantiate_group_from_params_hash
693
+ group = Flickr::Group.new("id" => "group1", "name" => "Group One", "eighteenplus" => "1", "foo" => "bar")
694
+ assert_equal "group1", group.id
695
+ assert_equal "Group One", group.name
696
+ assert_equal "1", group.eighteenplus
697
+ assert_equal "bar", group.instance_variable_get(:@foo)
698
+ end
699
+
700
+ def test_should_use_flickr_client_passed_in_params_hash_when_instantiating_group
701
+ f = flickr_client
702
+ Flickr.expects(:new).never
703
+ group = Flickr::Group.new("id" => "group1", "name" => "Group One", "client" => f)
704
+ assert_equal f, group.client
705
+ end
706
+
707
+ def test_should_provide_id_name_eighteenplus_description_members_online_privacy_reader_methods_for_group
708
+ g = Flickr::Group.new
709
+ %w(id name eighteenplus description members online privacy).each do |m|
710
+ g.instance_variable_set("@#{m}", "foo_#{m}")
711
+ assert_equal "foo_#{m}", g.send(m)
712
+ end
713
+ end
714
+
715
+ # def test_should_initialize_photo_from_id
716
+ # photo = Flickr::Photo.new("foo123")
717
+ # assert_equal "foo123", photo.id
718
+ # end
719
+ #
720
+ # def test_should_save_extra_params_as_instance_variables
721
+ # photo = Flickr::Photo.new('foo123', 'some_api_key', { 'key1' => 'value1', 'key2' => 'value2'})
722
+ # assert_equal 'value1', photo.instance_variable_get(:@key1)
723
+ # assert_equal 'value2', photo.instance_variable_get(:@key2)
724
+ # end
725
+ #
726
+ # def test_should_be_able_to_access_instance_variables_through_hash_like_interface
727
+ # photo = Flickr::Photo.new
728
+ # photo.instance_variable_set(:@key1, 'value1')
729
+ # assert_equal 'value1', photo['key1']
730
+ # assert_equal 'value1', photo[:key1]
731
+ # assert_nil photo[:key2]
732
+ # assert_nil photo['key2']
733
+ # end
734
+
735
+ # ##### PHOTOSETS
736
+ #
737
+ # #def setup
738
+ # # super
739
+ # # @photoset = @f.photosets_create('title'=>@title, 'primary_photo_id'=>@photo_id)
740
+ # # @photoset_id = @photoset['photoset']['id']
741
+ # #end
742
+ # #def teardown
743
+ # # @f.photosets_delete('photoset_id'=>@photoset_id)
744
+ # #end
745
+ #
746
+ # def test_photosets_editMeta
747
+ # assert_equal @f.photosets_editMeta('photoset_id'=>@photoset_id, 'title'=>@title)['stat'], 'ok'
748
+ # end
749
+ #
750
+ # def test_photosets_editPhotos
751
+ # assert_equal @f.photosets_editPhotos('photoset_id'=>@photoset_id, 'primary_photo_id'=>@photo_id, 'photo_ids'=>@photo_id)['stat'], 'ok'
752
+ # end
753
+ #
754
+ # def test_photosets_getContext
755
+ # assert_equal @f.photosets_getContext('photoset_id'=>@photoset_id, 'photo_id'=>@photo_id)['stat'], 'ok'
756
+ # end
757
+ #
758
+ # def test_photosets_getContext
759
+ # assert_equal @f.photosets_getContext('photoset_id'=>@photoset_id, 'photo_id'=>@photo_id)['stat'], 'ok'
760
+ # end
761
+ #
762
+ # def test_photosets_getInfo
763
+ # assert_equal @f.photosets_getInfo('photoset_id'=>@photoset_id)['stat'], 'ok'
764
+ # end
765
+ #
766
+ # def test_photosets_getList
767
+ # assert_equal @f.photosets_getList['stat'], 'ok'
768
+ # end
769
+ #
770
+ # def test_photosets_getPhotos
771
+ # assert_equal @f.photosets_getPhotos('photoset_id'=>@photoset_id)['stat'], 'ok'
772
+ # end
773
+ #
774
+ # def test_photosets_orderSets
775
+ # assert_equal @f.photosets_orderSets('photoset_ids'=>@photoset_id)['stat'], 'ok'
776
+ # end
777
+
778
+ def test_related_tags
779
+ f = flickr_client
780
+ tags_response = {
781
+ "tags" => {
782
+ "tag" => [ "zoo", "animal" ],
783
+ "source" => "monkey",
784
+ },
785
+ "stat" => "ok",
786
+ }
787
+ f.expects(:tags_getRelated).with('tag' => 'monkey').returns(tags_response)
788
+ assert_equal f.related_tags('monkey'), %w(zoo animal)
789
+ end
790
+
791
+ private
792
+ def flickr_client
793
+ Flickr.new("some_api_key")
794
+ end
795
+
796
+ def authenticated_flickr_client
797
+ f = Flickr.new('api_key' => 'some_api_key', 'shared_secret' => 'shared_secret_code')
798
+ f.instance_variable_set(:@auth_token, 'some_auth_token')
799
+ f
800
+ end
801
+
802
+ def new_user(options={})
803
+ Flickr::User.new({ 'id' => 'foo123',
804
+ 'username' => 'some_user',
805
+ 'name' => 'Some User',
806
+ 'foo' => 'bar',
807
+ 'auth_token' => 'foobar789'}.merge(options))
808
+
809
+ end
810
+ def new_photo(options={})
811
+ Flickr::Photo.new("1418878",
812
+ "foo123",
813
+ { "farm" => "1",
814
+ "server" => "2",
815
+ "secret" => "1e92283336",
816
+ "owner" => Flickr::User.new("abc123", "some_user", nil, nil, "some_api_key") }.merge(options))
817
+ end
818
+
819
+ def dummy_photos_response
820
+ { "photos" =>
821
+ { "photo" =>
822
+ [{ "id" => "foo123",
823
+ "key1" => "value1",
824
+ "key2" => "value2" },
825
+ { "id" => "bar456",
826
+ "key3" => "value3"}] } }
827
+ end
828
+
829
+ def dummy_single_photo_response
830
+ { "photos" =>
831
+ { "photo" =>
832
+ { "id" => "foo123",
833
+ "key1" => "value1",
834
+ "key2" => "value2" } } }
835
+ end
836
+
837
+ def dummy_zero_photo_response
838
+ { "photos" => { "total" => 0 } }
839
+ end
840
+
841
+ def dummy_user_response
842
+ { "user" =>
843
+ { "nsid" => "12037949632@N01",
844
+ "username" => "Stewart" }
845
+ }
846
+ end
847
+
848
+ def dummy_groups_response
849
+ { "groups" =>
850
+ { "group" =>
851
+ [{ "nsid" => "group1",
852
+ "name" => "Group One",
853
+ "eighteenplus" => "0" },
854
+ { "nsid" => "group2",
855
+ "name" => "Group Two",
856
+ "eighteenplus" => "1"}] } }
857
+ end
858
+
859
+ def dummy_single_group_response
860
+ { "groups" =>
861
+ { "group" =>
862
+ { "nsid" => "group1",
863
+ "name" => "Group One",
864
+ "eighteenplus" => "0" } } }
865
+ end
866
+
867
+ def successful_xml_response
868
+ <<-EOF
869
+ <?xml version="1.0" encoding="utf-8" ?>
870
+ <rsp stat="ok">
871
+ <contacts page="1" pages="1" perpage="1000" total="2">
872
+ <contact nsid="12037949629@N01" username="Eric" iconserver="1"
873
+ realname="Eric Costello"
874
+ friend="1" family="0" ignored="1" />
875
+ <contact nsid="12037949631@N01" username="neb" iconserver="1"
876
+ realname="Ben Cerveny"
877
+ friend="0" family="0" ignored="0" />
878
+ </contacts>
879
+ </rsp>
880
+ EOF
881
+ end
882
+
883
+ def unsuccessful_xml_response
884
+ <<-EOF
885
+ <?xml version="1.0" encoding="utf-8" ?>
886
+ <rsp stat="fail">
887
+ <err code="[error-code]" msg="[error-message]" />
888
+ </rsp>
889
+ EOF
890
+ end
891
+
892
+ end