sml-flickr 1.0.9
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.
- data/History.txt +68 -0
- data/LICENSE +20 -0
- data/README.txt +74 -0
- data/TODO +6 -0
- data/lib/flickr.rb +745 -0
- data/test/test_flickr.rb +1188 -0
- metadata +87 -0
data/test/test_flickr.rb
ADDED
@@ -0,0 +1,1188 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/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_parse_photos_response_into_flickr_photo_collection
|
150
|
+
f = flickr_client
|
151
|
+
f.expects(:request).returns(dummy_photos_response)
|
152
|
+
assert_kind_of Flickr::PhotoCollection, f.photos_request('some_method')
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_should_store_pagination_info_in_photo_collection
|
156
|
+
f = flickr_client
|
157
|
+
f.expects(:request).returns(dummy_photos_response)
|
158
|
+
photos = f.photos_request('some_method')
|
159
|
+
|
160
|
+
assert_equal "3", photos.page
|
161
|
+
assert_equal "5", photos.pages
|
162
|
+
assert_equal "10", photos.perpage
|
163
|
+
assert_equal "42", photos.total
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_should_return_collection_of_photos
|
167
|
+
f = flickr_client
|
168
|
+
f.expects(:request).returns(dummy_photos_response)
|
169
|
+
photos = f.photos_request('some_method')
|
170
|
+
assert_equal 2, photos.size
|
171
|
+
assert_kind_of Flickr::Photo, photos.first
|
172
|
+
assert_equal "foo123", photos.first.id
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_should_work_with_single_result
|
176
|
+
f = flickr_client
|
177
|
+
f.expects(:request).returns(dummy_single_photo_response)
|
178
|
+
photos = f.photos_request('some_method')
|
179
|
+
assert_equal 1, photos.size
|
180
|
+
assert_kind_of Flickr::Photo, photos.first
|
181
|
+
assert_equal "foo123", photos.first.id
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_should_work_with_empty_result
|
185
|
+
f = flickr_client
|
186
|
+
f.expects(:request).returns(dummy_zero_photo_response)
|
187
|
+
photos = f.photos_request('some_method')
|
188
|
+
assert_equal [], photos
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_should_generate_login_url
|
192
|
+
f = flickr_client
|
193
|
+
f.expects(:signature_from).with('api_key' => 'some_api_key', 'perms' => 'write').returns('validsignature')
|
194
|
+
assert_equal 'http://flickr.com/services/auth/?api_key=some_api_key&perms=write&api_sig=validsignature', f.login_url('write')
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_should_get_token_from_frob
|
198
|
+
f = flickr_client
|
199
|
+
f.expects(:request).with('auth.getToken',:frob => 'some_frob').returns({'auth' => {'token' => 'some_auth_token', 'user' => {}}})
|
200
|
+
|
201
|
+
auth_token = f.get_token_from('some_frob')
|
202
|
+
assert_equal 'some_auth_token', auth_token
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_should_store_auth_token_in_client
|
206
|
+
f = flickr_client
|
207
|
+
f.expects(:request).returns({'auth' => {'token' => 'some_auth_token','user' => {}}})
|
208
|
+
f.get_token_from('some_frob')
|
209
|
+
assert_equal 'some_auth_token', f.auth_token
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_should_store_authenticated_user_details_in_client
|
213
|
+
f = flickr_client
|
214
|
+
f.expects(:request).returns({ 'auth' => { 'token' => 'some_auth_token',
|
215
|
+
'user' => { 'nsid' => 'foo123',
|
216
|
+
'username' => 'some_user', 'fullname' => 'Some User'}}})
|
217
|
+
f.get_token_from('some_frob')
|
218
|
+
assert_kind_of Flickr::User, user = f.user
|
219
|
+
assert_equal 'foo123', user.id
|
220
|
+
assert_equal 'some_user', user.username
|
221
|
+
assert_equal 'Some User', user.name
|
222
|
+
assert_equal f, user.client
|
223
|
+
end
|
224
|
+
|
225
|
+
# photos method tests
|
226
|
+
def test_should_get_recent_photos_if_no_params_for_photos
|
227
|
+
f = flickr_client
|
228
|
+
f.expects(:photos_search)
|
229
|
+
f.photos
|
230
|
+
end
|
231
|
+
|
232
|
+
# photos_search method tests
|
233
|
+
def test_should_search_photos
|
234
|
+
f = authenticated_flickr_client
|
235
|
+
f.expects(:request).with('photos.search', anything).returns(dummy_photos_response)
|
236
|
+
photos = f.photos_search
|
237
|
+
assert_kind_of Flickr::Photo, photos.first
|
238
|
+
end
|
239
|
+
|
240
|
+
# users method tests
|
241
|
+
def test_should_find_user_from_email
|
242
|
+
f = flickr_client
|
243
|
+
f.expects(:request).with('people.findByEmail', anything).returns(dummy_user_response)
|
244
|
+
assert_kind_of Flickr::User, user = f.users("email@test.com")
|
245
|
+
assert_equal "12037949632@N01", user.id
|
246
|
+
assert_equal "Stewart", user.username
|
247
|
+
end
|
248
|
+
|
249
|
+
def test_should_find_user_from_username_if_fails_to_get_from_email
|
250
|
+
f = flickr_client
|
251
|
+
f.expects(:request).with('people.findByEmail', anything).raises
|
252
|
+
f.expects(:request).with('people.findByUsername', anything).returns(dummy_user_response)
|
253
|
+
assert_kind_of Flickr::User, f.users("email@test.com")
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_should_pass_on_flickr_client_when_finding_user
|
257
|
+
f = flickr_client
|
258
|
+
f.stubs(:request).returns(dummy_user_response)
|
259
|
+
user = f.users("email@test.com")
|
260
|
+
assert_equal f, user.client
|
261
|
+
end
|
262
|
+
|
263
|
+
# groups method tests
|
264
|
+
def test_should_search_for_given_group
|
265
|
+
f = flickr_client
|
266
|
+
f.expects(:request).with("groups.search", {"text" => "foo"}).returns(dummy_groups_response)
|
267
|
+
f.groups("foo")
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_should_search_for_given_group_with_additional_params
|
271
|
+
f = flickr_client
|
272
|
+
f.expects(:request).with("groups.search", {"text" => "foo", "per_page" => "1"}).returns(dummy_groups_response)
|
273
|
+
f.groups("foo", "per_page" => "1")
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_should_instantiate_groups_from_search_response
|
277
|
+
f = flickr_client
|
278
|
+
f.stubs(:request).returns(dummy_groups_response)
|
279
|
+
assert_kind_of Array, groups = f.groups("foo")
|
280
|
+
assert_kind_of Flickr::Group, group = groups.first
|
281
|
+
assert_equal "group1", group.id
|
282
|
+
assert_equal "Group One", group.name
|
283
|
+
assert_equal "0", group.eighteenplus
|
284
|
+
assert_equal f, group.client
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_should_instantiate_groups_from_search_response_with_single_group_returned
|
288
|
+
f = flickr_client
|
289
|
+
f.stubs(:request).returns(dummy_single_group_response)
|
290
|
+
assert_kind_of Array, groups = f.groups("foo")
|
291
|
+
assert_equal 1, groups.size
|
292
|
+
assert_equal "group1", groups.first.id
|
293
|
+
end
|
294
|
+
|
295
|
+
# ##### DIRECT MODE
|
296
|
+
#
|
297
|
+
# def test_test_echo
|
298
|
+
# assert_equal @f.test_echo['stat'], 'ok'
|
299
|
+
# end
|
300
|
+
# def test_test_login
|
301
|
+
# assert_equal @f.test_login['stat'], 'ok'
|
302
|
+
# end
|
303
|
+
#
|
304
|
+
#
|
305
|
+
# ##### BASICS
|
306
|
+
#
|
307
|
+
# def test_login
|
308
|
+
# assert_equal @username, @f.user.getInfo.username
|
309
|
+
# end
|
310
|
+
#
|
311
|
+
# def test_find_by_url
|
312
|
+
# assert_equal @group_id, @f.find_by_url(@group_url).getInfo.id # find group by URL
|
313
|
+
# assert_equal @user_id, @f.find_by_url(@user_url).getInfo.id # find user by URL
|
314
|
+
# end
|
315
|
+
#
|
316
|
+
# def test_licenses
|
317
|
+
# assert_kind_of Array, @f.licenses # find all licenses
|
318
|
+
# end
|
319
|
+
#
|
320
|
+
|
321
|
+
#
|
322
|
+
# Flickr#photos tests
|
323
|
+
#
|
324
|
+
|
325
|
+
|
326
|
+
# ##### Flickr::User tests
|
327
|
+
#
|
328
|
+
def test_should_instantiate_user
|
329
|
+
user = new_user
|
330
|
+
assert_equal 'foo123', user.id
|
331
|
+
assert_equal 'some_user', user.username
|
332
|
+
assert_equal 'bar', user.instance_variable_get(:@foo) # should collect all other params up and store as instance variables
|
333
|
+
end
|
334
|
+
|
335
|
+
def test_should_instantiate_new_user_with_old_api
|
336
|
+
Flickr.any_instance.stubs(:login) # stub logging in
|
337
|
+
user = Flickr::User.new('foo123',
|
338
|
+
'some_user',
|
339
|
+
'email@test.com', # email irrelevant since Flickr API no longer supports authentication in this way
|
340
|
+
'password', # password irrelevant since Flickr API no longer supports authentication in this way
|
341
|
+
'bar456')
|
342
|
+
assert_equal 'foo123', user.id
|
343
|
+
assert_equal 'some_user', user.username
|
344
|
+
assert_equal 'email@test.com', user.instance_variable_get(:@email)
|
345
|
+
assert_equal 'password', user.instance_variable_get(:@password)
|
346
|
+
assert_equal 'bar456', user.client.api_key
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_should_instantiate_new_client_when_instantiating_user_if_no_client_passed_in_params
|
350
|
+
f = flickr_client
|
351
|
+
Flickr.expects(:new).returns(f)
|
352
|
+
user = new_user( 'api_key' => 'an_api_key' )
|
353
|
+
assert_equal f, user.client
|
354
|
+
end
|
355
|
+
|
356
|
+
def test_should_not_instantiate_new_client_when_instantiating_user_if_client_passed_in_params
|
357
|
+
f = flickr_client
|
358
|
+
Flickr.expects(:new).never
|
359
|
+
user = new_user( 'client' => f )
|
360
|
+
assert_equal f, user.client
|
361
|
+
end
|
362
|
+
|
363
|
+
def test_should_not_instantiate_client_if_no_api_key_passed
|
364
|
+
Flickr.expects(:new).never
|
365
|
+
user = new_user
|
366
|
+
assert_nil user.client
|
367
|
+
end
|
368
|
+
|
369
|
+
def test_should_build_url_for_users_profile_page_using_user_id
|
370
|
+
Flickr.any_instance.expects(:http_get).never
|
371
|
+
assert_equal "http://www.flickr.com/people/foo123/", new_user.url
|
372
|
+
end
|
373
|
+
|
374
|
+
def test_should_build_url_for_users_photos_page_using_photosurl
|
375
|
+
f = flickr_client
|
376
|
+
f.expects(:people_getInfo).with(anything).returns({'person' => {
|
377
|
+
'photosurl' => 'http://www.flickr.com/photos/killer_bob/',
|
378
|
+
'photos' => {'count' => 0}
|
379
|
+
}})
|
380
|
+
assert_equal "http://www.flickr.com/photos/killer_bob/", new_user('client' => f).photos_url
|
381
|
+
end
|
382
|
+
|
383
|
+
def test_should_get_pretty_url_for_users_profile_page
|
384
|
+
f = flickr_client
|
385
|
+
f.expects(:urls_getUserProfile).returns({"user" => {"nsid" => "bar456", "url" => "http://www.flickr.com/people/killer_bob/"}})
|
386
|
+
|
387
|
+
assert_equal "http://www.flickr.com/people/killer_bob/", new_user( 'client' => f ).pretty_url
|
388
|
+
end
|
389
|
+
|
390
|
+
def test_should_cache_pretty_url_for_users_profile_page
|
391
|
+
f = flickr_client
|
392
|
+
user = new_user( 'client' => f )
|
393
|
+
f.expects(:urls_getUserProfile).returns({"user" => {"nsid" => "bar456", "url" => "http://www.flickr.com/people/killer_bob/"}}) # expects only one call
|
394
|
+
|
395
|
+
user.pretty_url
|
396
|
+
user.pretty_url
|
397
|
+
end
|
398
|
+
|
399
|
+
def test_should_get_users_public_groups
|
400
|
+
f = flickr_client
|
401
|
+
f.expects(:request).with("people.getPublicGroups", anything).returns(dummy_groups_response)
|
402
|
+
new_user( 'client' => f ).groups
|
403
|
+
end
|
404
|
+
|
405
|
+
def test_should_instantiate_users_public_groups
|
406
|
+
f = flickr_client
|
407
|
+
f.stubs(:request).returns(dummy_groups_response)
|
408
|
+
user = new_user( 'client' => f )
|
409
|
+
|
410
|
+
groups = user.groups
|
411
|
+
assert_equal 2, groups.size
|
412
|
+
assert_kind_of Flickr::Group, group = groups.first
|
413
|
+
assert_equal "group1", group.id
|
414
|
+
assert_equal "Group One", group.name
|
415
|
+
assert_equal "0", group.eighteenplus
|
416
|
+
assert_equal f, group.client
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_should_instantiate_users_public_groups_when_only_one_returned
|
420
|
+
f = flickr_client
|
421
|
+
f.stubs(:request).returns(dummy_single_group_response)
|
422
|
+
user = new_user( 'client' => f )
|
423
|
+
groups = user.groups
|
424
|
+
assert_equal 1, groups.size
|
425
|
+
end
|
426
|
+
|
427
|
+
def test_should_get_users_tags
|
428
|
+
f = flickr_client
|
429
|
+
user = new_user( 'client' => f )
|
430
|
+
f.expects(:tags_getListUser).with('user_id'=>user.id).returns({"who"=>{"tags"=>{"tag"=>["offf08", "ruby", "rubyonrails", "timoteo", "wbs", "webreakstuff"]}, "id"=>"9259187@N05"}, "stat"=>"ok"})
|
431
|
+
tags = user.tags
|
432
|
+
assert_kind_of Array, tags
|
433
|
+
assert_equal tags, ["offf08", "ruby", "rubyonrails", "timoteo", "wbs", "webreakstuff"]
|
434
|
+
end
|
435
|
+
|
436
|
+
def test_should_get_users_popular_tags
|
437
|
+
f = flickr_client
|
438
|
+
user = new_user( 'client' => f )
|
439
|
+
f.expects(:tags_getListUserPopular).with('user_id' => user.id).with(anything).returns({"who"=>{"tags"=>{"tag"=>[{"content"=>"design", "count"=>"94"}, {"content"=>"offf08", "count"=>"94"}, {"content"=>"ruby", "count"=>"3"}, {"content"=>"rubyonrails", "count"=>"3"}, {"content"=>"wbs", "count"=>"3"}, {"content"=>"webreakstuff", "count"=>"97"}]}, "id"=>"9259187@N05"}, "stat"=>"ok"})
|
440
|
+
pop_tags = user.popular_tags
|
441
|
+
assert_kind_of Array, pop_tags
|
442
|
+
assert_equal pop_tags, [{"tag"=>"design", "count"=>"94"}, {"tag"=>"offf08", "count"=>"94"}, {"tag"=>"ruby", "count"=>"3"}, {"tag"=>"rubyonrails", "count"=>"3"}, {"tag"=>"wbs", "count"=>"3"}, {"tag"=>"webreakstuff", "count"=>"97"}]
|
443
|
+
end
|
444
|
+
|
445
|
+
# def test_getInfo
|
446
|
+
# @u.getInfo
|
447
|
+
# assert_equal @username, @u.username
|
448
|
+
# end
|
449
|
+
#
|
450
|
+
# def test_groups
|
451
|
+
# assert_kind_of Flickr::Group, @u.groups.first # public groups
|
452
|
+
# end
|
453
|
+
#
|
454
|
+
#
|
455
|
+
# def test_contacts
|
456
|
+
# assert_kind_of Flickr::User, @u.contacts.first # public contacts
|
457
|
+
# end
|
458
|
+
#
|
459
|
+
# def test_favorites
|
460
|
+
# assert_kind_of Flickr::Photo, @u.favorites.first # public favorites
|
461
|
+
# end
|
462
|
+
#
|
463
|
+
# def test_photosets
|
464
|
+
# assert_kind_of Flickr::Photoset, @u.photosets.first # public photosets
|
465
|
+
# end
|
466
|
+
#
|
467
|
+
# def test_contactsPhotos
|
468
|
+
# assert_kind_of Flickr::Photo, @u.contactsPhotos.first # contacts' favorites
|
469
|
+
# end
|
470
|
+
|
471
|
+
# User#photos tests
|
472
|
+
|
473
|
+
def test_should_get_users_public_photos
|
474
|
+
client = mock
|
475
|
+
client.expects(:photos_request).with('people.getPublicPhotos', {'user_id' => 'some_id'}).returns([new_photo, new_photo])
|
476
|
+
Flickr.expects(:new).at_least_once.returns(client)
|
477
|
+
|
478
|
+
user = Flickr::User.new("some_id", "some_user", nil, nil, "some_api_key")
|
479
|
+
|
480
|
+
photos = user.photos
|
481
|
+
assert_equal 2, photos.size
|
482
|
+
assert_kind_of Flickr::Photo, photos.first
|
483
|
+
end
|
484
|
+
|
485
|
+
def test_should_instantiate_favorite_photos_with_id_and_all_params_returned_by_query
|
486
|
+
client = mock
|
487
|
+
client.expects(:photos_request).with('favorites.getPublicList', {'user_id' => 'some_id'})
|
488
|
+
Flickr.expects(:new).at_least_once.returns(client)
|
489
|
+
user = Flickr::User.new("some_id", "some_user", nil, nil, "some_api_key")
|
490
|
+
user.favorites
|
491
|
+
end
|
492
|
+
|
493
|
+
def test_should_instantiate_contacts_photos_with_id_and_all_params_returned_by_query
|
494
|
+
client = mock
|
495
|
+
client.expects(:photos_request).with('photos.getContactsPublicPhotos', {'user_id' => 'some_id'})
|
496
|
+
Flickr.expects(:new).at_least_once.returns(client)
|
497
|
+
user = Flickr::User.new('some_id', "some_user", nil, nil, "some_api_key")
|
498
|
+
user.contactsPhotos
|
499
|
+
end
|
500
|
+
|
501
|
+
# ##### Flickr::Photo tests
|
502
|
+
|
503
|
+
def test_should_initialize_photo_from_id
|
504
|
+
photo = Flickr::Photo.new("foo123")
|
505
|
+
assert_equal "foo123", photo.id
|
506
|
+
end
|
507
|
+
|
508
|
+
def test_should_save_extra_params_as_instance_variables
|
509
|
+
photo = Flickr::Photo.new('foo123', 'some_api_key', { 'key1' => 'value1', 'key2' => 'value2'})
|
510
|
+
assert_equal 'value1', photo.instance_variable_get(:@key1)
|
511
|
+
assert_equal 'value2', photo.instance_variable_get(:@key2)
|
512
|
+
end
|
513
|
+
|
514
|
+
def test_should_be_able_to_access_instance_variables_through_hash_like_interface
|
515
|
+
photo = Flickr::Photo.new
|
516
|
+
photo.instance_variable_set(:@key1, 'value1')
|
517
|
+
assert_equal 'value1', photo['key1']
|
518
|
+
assert_equal 'value1', photo[:key1]
|
519
|
+
assert_nil photo[:key2]
|
520
|
+
assert_nil photo['key2']
|
521
|
+
end
|
522
|
+
|
523
|
+
def test_should_get_and_store_other_info_for_photo
|
524
|
+
Flickr.any_instance.stubs(:http_get).returns(photo_info_xml_response)
|
525
|
+
photo = Flickr::Photo.new('foo123', 'some_api_key')
|
526
|
+
|
527
|
+
assert_equal "1964 120 amazon estate", photo.title # calling #title method triggers getting of info
|
528
|
+
assert_equal "1964 120 amazon estate", photo.instance_variable_get(:@title)
|
529
|
+
assert_equal "3142", photo.instance_variable_get(:@server)
|
530
|
+
assert_equal "ae75bd3111", photo.instance_variable_get(:@secret)
|
531
|
+
assert_equal "4", photo.instance_variable_get(:@farm)
|
532
|
+
assert_equal "1204145093", photo.instance_variable_get(:@dateuploaded)
|
533
|
+
assert_equal "photo", photo.instance_variable_get(:@media)
|
534
|
+
assert_equal "0", photo.instance_variable_get(:@isfavorite)
|
535
|
+
assert_equal "0", photo.instance_variable_get(:@license)
|
536
|
+
assert_equal "0", photo.instance_variable_get(:@rotation)
|
537
|
+
assert_equal "1964 Volvo 120 amazon estate spotted in derbyshire.", photo.instance_variable_get(:@description)
|
538
|
+
assert_equal( { "w" => "50",
|
539
|
+
"x" => "10",
|
540
|
+
"y" => "10",
|
541
|
+
"authorname" => "Bees",
|
542
|
+
"author" => "12037949754@N01",
|
543
|
+
"id" => "313",
|
544
|
+
"content" => "foo",
|
545
|
+
"h" => "50" }, photo.instance_variable_get(:@notes))
|
546
|
+
assert_equal "http://www.flickr.com/photos/rootes_arrow/2296968304/", photo.instance_variable_get(:@url)
|
547
|
+
assert_equal [ { "id" => "9377979-2296968304-2228", "author" => "9383319@N05", "raw" => "volvo", "machine_tag" => "0", "content" => "volvo" },
|
548
|
+
{ "id" => "9377979-2296968304-2229", "author" => "9383319@N06", "raw" => "amazon", "machine_tag" => "0", "content" => "amazon"
|
549
|
+
} ], photo.instance_variable_get(:@tags)
|
550
|
+
assert_equal "1", photo.instance_variable_get(:@comments)
|
551
|
+
assert_kind_of Flickr::User, owner = photo.instance_variable_get(:@owner)
|
552
|
+
assert_equal "Rootes_arrow_1725", owner.username
|
553
|
+
end
|
554
|
+
|
555
|
+
def test_should_get_and_other_info_for_photo_when_some_attributes_missing
|
556
|
+
Flickr.any_instance.stubs(:http_get).returns(sparse_photo_info_xml_response)
|
557
|
+
photo = Flickr::Photo.new('foo123', 'some_api_key')
|
558
|
+
|
559
|
+
assert_equal "1964 120 amazon estate", photo.title # calling #title method triggers getting of info
|
560
|
+
assert_equal "1964 120 amazon estate", photo.instance_variable_get(:@title)
|
561
|
+
assert_equal( {}, photo.instance_variable_get(:@description))
|
562
|
+
assert_nil photo.instance_variable_get(:@notes)
|
563
|
+
assert_nil photo.instance_variable_get(:@tags)
|
564
|
+
assert_equal "1", photo.instance_variable_get(:@comments)
|
565
|
+
end
|
566
|
+
|
567
|
+
def test_should_not_get_info_more_than_once
|
568
|
+
Flickr.any_instance.expects(:http_get).returns(photo_info_xml_response) # expects only one call
|
569
|
+
photo = Flickr::Photo.new('foo123', 'some_api_key')
|
570
|
+
|
571
|
+
photo.description # calling #description method triggers getting of info
|
572
|
+
photo.instance_variable_set(:@description, nil) # set description to nil
|
573
|
+
photo.description # call #description method again
|
574
|
+
end
|
575
|
+
|
576
|
+
#
|
577
|
+
# owner tests
|
578
|
+
def test_should_return_owner_when_flickr_user
|
579
|
+
user = Flickr::User.new
|
580
|
+
photo = new_photo("owner" => user)
|
581
|
+
|
582
|
+
assert_equal user, photo.owner
|
583
|
+
end
|
584
|
+
|
585
|
+
def test_should_get_info_on_owner_if_not_known
|
586
|
+
photo = new_photo("owner" => nil)
|
587
|
+
# stubbing private methods causes problems so we mock client method, which is what Photo#getInfo users to make API call
|
588
|
+
Flickr.any_instance.expects(:photos_getInfo).returns('photo' => { 'owner'=>{'nsid'=>'abc123', 'username'=>'SomeUserName', 'realname'=>"", 'location'=>''},
|
589
|
+
'notes' => {}, 'tags' => {}, 'urls' => {'url' => {'content' => 'http://prettyurl'}}})
|
590
|
+
|
591
|
+
owner = photo.owner
|
592
|
+
assert_kind_of Flickr::User, owner
|
593
|
+
assert_equal 'abc123', owner.id
|
594
|
+
assert_equal 'SomeUserName', owner.username
|
595
|
+
end
|
596
|
+
|
597
|
+
def test_should_instantiate_flickr_user_from_owner_id_if_we_have_it
|
598
|
+
photo = Flickr::Photo.new
|
599
|
+
photo.instance_variable_set(:@owner, "some_user_id")
|
600
|
+
Flickr.any_instance.expects(:photos_getInfo).never
|
601
|
+
|
602
|
+
user = photo.owner
|
603
|
+
assert_kind_of Flickr::User, user
|
604
|
+
assert_equal "some_user_id", user.id
|
605
|
+
end
|
606
|
+
|
607
|
+
def test_should_cache_owner_when_instantiated
|
608
|
+
user = Flickr::User.new
|
609
|
+
photo = Flickr::Photo.new
|
610
|
+
photo.instance_variable_set(:@owner, "some_user_id")
|
611
|
+
Flickr::User.expects(:new).returns(user)
|
612
|
+
|
613
|
+
photo.owner
|
614
|
+
photo.owner # call twice but mock expects only one call
|
615
|
+
end
|
616
|
+
|
617
|
+
#
|
618
|
+
# image_source_uri_from_self tests
|
619
|
+
def test_should_build_image_source_uri_from_self
|
620
|
+
assert_equal "http://farm1.static.flickr.com/2/1418878_1e92283336.jpg",
|
621
|
+
new_photo.send(:image_source_uri_from_self) # no size specified
|
622
|
+
end
|
623
|
+
|
624
|
+
def test_should_build_image_source_uri_from_self_for_given_size
|
625
|
+
assert_equal "http://farm1.static.flickr.com/2/1418878_1e92283336_m.jpg",
|
626
|
+
new_photo.send(:image_source_uri_from_self, "Small") # size specified
|
627
|
+
end
|
628
|
+
|
629
|
+
def test_should_build_image_source_uri_from_self_for_default_size_when_explicitly_asked_for
|
630
|
+
assert_equal "http://farm1.static.flickr.com/2/1418878_1e92283336.jpg",
|
631
|
+
new_photo.send(:image_source_uri_from_self, "Medium") # medium size specified -- the default
|
632
|
+
end
|
633
|
+
|
634
|
+
def test_should_build_image_source_uri_from_self_for_default_size_when_unknown_size_asked_for
|
635
|
+
assert_equal "http://farm1.static.flickr.com/2/1418878_1e92283336.jpg",
|
636
|
+
new_photo.send(:image_source_uri_from_self, "Dummy") # bad size specified
|
637
|
+
end
|
638
|
+
|
639
|
+
def test_should_return_nil_for_image_source_uri_if_no_attributes
|
640
|
+
assert_nil Flickr::Photo.new.send(:image_source_uri_from_self)
|
641
|
+
end
|
642
|
+
|
643
|
+
def test_should_return_nil_for_image_source_uri_if_missing_required_attributes
|
644
|
+
assert_nil Flickr::Photo.new("1418878", nil, "farm" => "1").send(:image_source_uri_from_self)
|
645
|
+
end
|
646
|
+
|
647
|
+
def test_image_source_uri_from_self_should_normalize_size
|
648
|
+
photo = new_photo
|
649
|
+
assert_equal photo.send(:image_source_uri_from_self, 'Large'),
|
650
|
+
photo.send(:image_source_uri_from_self, :large)
|
651
|
+
end
|
652
|
+
|
653
|
+
def test_uri_for_photo_from_self_should_normalize_size
|
654
|
+
photo = new_photo
|
655
|
+
assert_equal photo.send(:uri_for_photo_from_self, 'Large'),
|
656
|
+
photo.send(:uri_for_photo_from_self, :large)
|
657
|
+
end
|
658
|
+
|
659
|
+
def test_should_get_source_uri_by_building_from_self_if_possible
|
660
|
+
photo = Flickr::Photo.new
|
661
|
+
photo.expects(:image_source_uri_from_self).with('Medium').returns(true) # return any non-false-evaluating value so that sizes method isn't called
|
662
|
+
photo.source
|
663
|
+
end
|
664
|
+
|
665
|
+
def test_should_get_source_uri_by_building_from_self_if_possible_requesting_source_for_given_size
|
666
|
+
photo = Flickr::Photo.new
|
667
|
+
photo.expects(:image_source_uri_from_self).with('Large').returns(true) # return any non-false-evaluating value so that sizes method isn't called
|
668
|
+
photo.source('Large')
|
669
|
+
end
|
670
|
+
|
671
|
+
def test_should_get_source_uri_by_calling_sizes_method_if_no_luck_building_uri
|
672
|
+
photo = Flickr::Photo.new
|
673
|
+
photo.stubs(:image_source_uri_from_self) # ...and hence returns nil
|
674
|
+
photo.expects(:sizes).with('Medium').returns({})
|
675
|
+
photo.source
|
676
|
+
end
|
677
|
+
|
678
|
+
def test_should_build_uri_for_photo_from_self
|
679
|
+
assert_equal "http://www.flickr.com/photos/abc123/1418878", new_photo.send(:uri_for_photo_from_self)
|
680
|
+
end
|
681
|
+
|
682
|
+
def test_should_build_uri_for_photo_from_self_when_owner_is_a_string
|
683
|
+
assert_equal "http://www.flickr.com/photos/789user321/1418878", new_photo('owner' => "789user321").send(:uri_for_photo_from_self)
|
684
|
+
end
|
685
|
+
|
686
|
+
def test_should_build_uri_for_photo_from_self_for_given_size
|
687
|
+
assert_equal "http://www.flickr.com/photos/abc123/1418878/sizes/s/", new_photo.send(:uri_for_photo_from_self, "Small")
|
688
|
+
end
|
689
|
+
|
690
|
+
def test_should_build_uri_for_photo_from_self_with_unknown_size
|
691
|
+
assert_equal "http://www.flickr.com/photos/abc123/1418878", new_photo.send(:uri_for_photo_from_self, "Dummy")
|
692
|
+
end
|
693
|
+
|
694
|
+
def test_should_return_nil_for_uri_for_photo_when_no_user_id
|
695
|
+
assert_nil Flickr::Photo.new("1418878", nil).send(:uri_for_photo_from_self)
|
696
|
+
end
|
697
|
+
|
698
|
+
def test_should_return_nil_for_uri_for_photo_when_no_photo_id
|
699
|
+
assert_nil Flickr::Photo.new.send(:uri_for_photo_from_self)
|
700
|
+
end
|
701
|
+
|
702
|
+
def test_should_get_uri_for_photo_flickr_page
|
703
|
+
photo = new_photo
|
704
|
+
assert_equal "http://www.flickr.com/photos/abc123/1418878", photo.url
|
705
|
+
end
|
706
|
+
|
707
|
+
def test_should_return_main_page_for_photo_flickr_page_when_medium_size_requested_as_per_previous_version
|
708
|
+
assert_equal "http://www.flickr.com/photos/abc123/1418878", new_photo.url('Medium')
|
709
|
+
end
|
710
|
+
|
711
|
+
def test_should_call_size_url_if_url_given_a_size
|
712
|
+
photo = new_photo
|
713
|
+
photo.expects(:size_url).with('Large')
|
714
|
+
photo.url('Large')
|
715
|
+
end
|
716
|
+
|
717
|
+
def test_should_get_flickr_page_uri_by_building_from_self_if_possible_requesting_source_for_given_size
|
718
|
+
photo = new_photo
|
719
|
+
photo.expects(:uri_for_photo_from_self).with('Large').returns(true) # return any non-false-evaluating value so that sizes method isn't called
|
720
|
+
photo.size_url('Large')
|
721
|
+
end
|
722
|
+
|
723
|
+
def test_should_get_flickr_page_uri_by_calling_sizes_method_if_no_luck_building_uri
|
724
|
+
photo = new_photo
|
725
|
+
photo.stubs(:uri_for_photo_from_self) # ...and hence returns nil
|
726
|
+
photo.expects(:sizes).with('Large').returns({})
|
727
|
+
photo.size_url('Large')
|
728
|
+
end
|
729
|
+
|
730
|
+
def test_should_allow_size_to_be_lowercase_or_symbol
|
731
|
+
photo = new_photo
|
732
|
+
assert_equal photo.normalize_size('Small'), 'Small'
|
733
|
+
assert_equal photo.normalize_size('small'), 'Small'
|
734
|
+
assert_equal photo.normalize_size(:small), 'Small'
|
735
|
+
assert_equal photo.normalize_size(:Small), 'Small'
|
736
|
+
assert_equal photo.normalize_size('smAlL'), 'Small'
|
737
|
+
|
738
|
+
assert_equal photo.normalize_size(""), ""
|
739
|
+
assert_nil photo.normalize_size(nil)
|
740
|
+
end
|
741
|
+
|
742
|
+
def test_size_url_should_normalize_size
|
743
|
+
photo = new_photo
|
744
|
+
assert_equal photo.size_url('Large'), photo.size_url(:large)
|
745
|
+
end
|
746
|
+
|
747
|
+
def test_url_should_normalize_size
|
748
|
+
photo = new_photo
|
749
|
+
assert_equal photo.url('Medium'), photo.url(:medium)
|
750
|
+
assert_equal photo.url('Small'), photo.url('small')
|
751
|
+
end
|
752
|
+
|
753
|
+
def test_source_should_normalize_size
|
754
|
+
photo = new_photo
|
755
|
+
assert_equal photo.source('Large'), photo.source(:large)
|
756
|
+
end
|
757
|
+
|
758
|
+
def test_sizes_should_normalize_size
|
759
|
+
sizes = {'sizes' => {'size' => [{'label' => 'Small'}, {'label' => 'Large'}]}}
|
760
|
+
photo = new_photo
|
761
|
+
photo.client.expects(:photos_getSizes).at_least_once.returns(sizes)
|
762
|
+
assert_equal photo.sizes('Large'), photo.sizes(:large)
|
763
|
+
end
|
764
|
+
|
765
|
+
# Photo#context tests
|
766
|
+
def test_should_call_photos_getContext_to_get_context_photos
|
767
|
+
Flickr.any_instance.expects(:photos_getContext).returns({'prevphoto' => {}, 'nextphoto' => {}})
|
768
|
+
new_photo.context
|
769
|
+
end
|
770
|
+
|
771
|
+
def test_should_instantiate_context_photos_with_id_and_all_params_returned_by_query
|
772
|
+
photo = new_photo
|
773
|
+
Flickr.any_instance.expects(:photos_getContext).returns({ 'prevphoto' => {'id' => '123', 'key_1' => 'value_1' },
|
774
|
+
'nextphoto' => {'id' => '456', 'key_2' => 'value_2'}})
|
775
|
+
Flickr::Photo.expects(:new).with("123", "foo123", { "key_1" => "value_1"})
|
776
|
+
Flickr::Photo.expects(:new).with("456", "foo123", { "key_2" => "value_2"})
|
777
|
+
|
778
|
+
photo.context
|
779
|
+
end
|
780
|
+
|
781
|
+
def test_should_not_instantiate_context_photos_with_id_of_0
|
782
|
+
photo = new_photo
|
783
|
+
Flickr.any_instance.expects(:photos_getContext).returns({ 'prevphoto' => {'id' => '123', 'key_1' => 'value_1' },
|
784
|
+
'nextphoto' => {'id' => '0', 'key_2' => 'value_2'}})
|
785
|
+
Flickr::Photo.expects(:new).with("123", anything, anything)
|
786
|
+
Flickr::Photo.expects(:new).with("0", anything, anything).never
|
787
|
+
|
788
|
+
photo.context
|
789
|
+
end
|
790
|
+
|
791
|
+
def test_should_tell_if_it_is_vertical_based_on_the_height_and_width
|
792
|
+
v, h = Flickr::Photo.new, Flickr::Photo.new
|
793
|
+
v.expects(:sizes).with('Medium').returns({"height"=>"480", "width"=>"360"})
|
794
|
+
h.expects(:sizes).with('Medium').returns({"height"=>"360", "width"=>"480"})
|
795
|
+
assert v.vertical?
|
796
|
+
assert !h.vertical?
|
797
|
+
end
|
798
|
+
|
799
|
+
# ##### Flickr::Group tests
|
800
|
+
#
|
801
|
+
def test_should_instantiate_group_from_id
|
802
|
+
group = Flickr::Group.new("group1")
|
803
|
+
assert_equal "group1", group.id
|
804
|
+
end
|
805
|
+
|
806
|
+
# tests old api for instantiating groups
|
807
|
+
def test_should_instantiate_group_from_id_and_api_key
|
808
|
+
f = flickr_client
|
809
|
+
Flickr.expects(:new).with("some_api_key").returns(f)
|
810
|
+
group = Flickr::Group.new("group1", "some_api_key")
|
811
|
+
assert_equal f, group.client
|
812
|
+
end
|
813
|
+
|
814
|
+
# new api for instantiating groups
|
815
|
+
def test_should_instantiate_group_from_params_hash
|
816
|
+
group = Flickr::Group.new("id" => "group1", "name" => "Group One", "eighteenplus" => "1", "foo" => "bar")
|
817
|
+
assert_equal "group1", group.id
|
818
|
+
assert_equal "Group One", group.name
|
819
|
+
assert_equal "1", group.eighteenplus
|
820
|
+
assert_equal "bar", group.instance_variable_get(:@foo)
|
821
|
+
end
|
822
|
+
|
823
|
+
def test_should_use_flickr_client_passed_in_params_hash_when_instantiating_group
|
824
|
+
f = flickr_client
|
825
|
+
Flickr.expects(:new).never
|
826
|
+
group = Flickr::Group.new("id" => "group1", "name" => "Group One", "client" => f)
|
827
|
+
assert_equal f, group.client
|
828
|
+
end
|
829
|
+
|
830
|
+
def test_should_provide_id_name_eighteenplus_description_members_online_privacy_reader_methods_for_group
|
831
|
+
g = Flickr::Group.new
|
832
|
+
%w(id name eighteenplus description members online privacy).each do |m|
|
833
|
+
g.instance_variable_set("@#{m}", "foo_#{m}")
|
834
|
+
assert_equal "foo_#{m}", g.send(m)
|
835
|
+
end
|
836
|
+
end
|
837
|
+
|
838
|
+
# def test_should_initialize_photo_from_id
|
839
|
+
# photo = Flickr::Photo.new("foo123")
|
840
|
+
# assert_equal "foo123", photo.id
|
841
|
+
# end
|
842
|
+
#
|
843
|
+
# def test_should_save_extra_params_as_instance_variables
|
844
|
+
# photo = Flickr::Photo.new('foo123', 'some_api_key', { 'key1' => 'value1', 'key2' => 'value2'})
|
845
|
+
# assert_equal 'value1', photo.instance_variable_get(:@key1)
|
846
|
+
# assert_equal 'value2', photo.instance_variable_get(:@key2)
|
847
|
+
# end
|
848
|
+
#
|
849
|
+
# def test_should_be_able_to_access_instance_variables_through_hash_like_interface
|
850
|
+
# photo = Flickr::Photo.new
|
851
|
+
# photo.instance_variable_set(:@key1, 'value1')
|
852
|
+
# assert_equal 'value1', photo['key1']
|
853
|
+
# assert_equal 'value1', photo[:key1]
|
854
|
+
# assert_nil photo[:key2]
|
855
|
+
# assert_nil photo['key2']
|
856
|
+
# end
|
857
|
+
|
858
|
+
## PHOTOSETS
|
859
|
+
|
860
|
+
def test_should_initialize_photoset_from_id
|
861
|
+
photoset = Flickr::Photoset.new("foo123")
|
862
|
+
assert_equal "foo123", photoset.id
|
863
|
+
end
|
864
|
+
|
865
|
+
def test_should_initialize_photoset_from_id_and_api_key
|
866
|
+
photoset = Flickr::Photoset.new("foo123", "some_api_key")
|
867
|
+
assert_equal "some_api_key", photoset.instance_variable_get(:@api_key)
|
868
|
+
end
|
869
|
+
|
870
|
+
def test_should_get_photos_for_specified_photoset
|
871
|
+
Flickr.any_instance.expects(:request).with('photosets.getPhotos', {'photoset_id' => 'some_id'}).returns(dummy_photoset_photos_response)
|
872
|
+
photoset = Flickr::Photoset.new("some_id", "some_api_key")
|
873
|
+
|
874
|
+
assert_kind_of Flickr::PhotoCollection, photos = photoset.photos
|
875
|
+
assert_equal 2, photos.size
|
876
|
+
assert_kind_of Flickr::Photo, photos.first
|
877
|
+
end
|
878
|
+
|
879
|
+
def test_photoset_should_get_info_on_demand
|
880
|
+
client = mock('client')
|
881
|
+
Flickr.expects(:new).with("some_api_key").returns(client)
|
882
|
+
client.expects(:photosets_getInfo).never
|
883
|
+
client.expects(:photos_request).never
|
884
|
+
Flickr::Photoset.new('foo123', "some_api_key")
|
885
|
+
end
|
886
|
+
|
887
|
+
def test_photoset_should_get_info_just_once
|
888
|
+
client = mock('client')
|
889
|
+
Flickr.expects(:new).with("some_api_key").returns(client)
|
890
|
+
photoset_id, photos_url = 'foo123', 'http://example.com/photos/killer_bob/'
|
891
|
+
response = {
|
892
|
+
'primary' => 'primary',
|
893
|
+
'title' => 'title',
|
894
|
+
'description' => 'description',
|
895
|
+
'url' => "#{photos_url}sets/#{photoset_id}/" }
|
896
|
+
Flickr::User.expects(:new).returns(stub(:photos_url => photos_url))
|
897
|
+
photoset = Flickr::Photoset.new(photoset_id, "some_api_key")
|
898
|
+
client.expects(:photosets_getInfo).with(anything).returns({'photoset' => response })
|
899
|
+
client.expects(:photos_request).never
|
900
|
+
assert_equal response['title'], photoset.title
|
901
|
+
assert_equal response['url'], photoset.url
|
902
|
+
assert_equal response['primary'], photoset.primary
|
903
|
+
assert_equal response['description'], photoset.description
|
904
|
+
end
|
905
|
+
|
906
|
+
def test_photoset_should_get_photos_just_once
|
907
|
+
photoset_id = 'foo123'
|
908
|
+
client = mock('client')
|
909
|
+
Flickr.expects(:new).with("some_api_key").returns(client)
|
910
|
+
photoset = Flickr::Photoset.new(photoset_id, "some_api_key")
|
911
|
+
client.expects(:photosets_getInfo).never
|
912
|
+
client.expects(:photos_request).with('photosets.getPhotos',
|
913
|
+
{'photoset_id' => photoset_id}).returns([])
|
914
|
+
assert_equal [], photoset.photos
|
915
|
+
photoset.photos # photos_request should not be called again
|
916
|
+
end
|
917
|
+
|
918
|
+
def test_photoset_should_get_first_photos_just_once
|
919
|
+
photo = new_photo
|
920
|
+
photoset_id = 'foo123'
|
921
|
+
client = mock('client')
|
922
|
+
Flickr.expects(:new).with("some_api_key").returns(client)
|
923
|
+
photoset = Flickr::Photoset.new(photoset_id, "some_api_key")
|
924
|
+
client.expects(:photosets_getInfo).never
|
925
|
+
client.expects(:photos_request).with('photosets.getPhotos',
|
926
|
+
{'photoset_id' => photoset_id}).never
|
927
|
+
client.expects(:photos_request).with('photosets.getPhotos',
|
928
|
+
{'photoset_id' => photoset_id, :per_page => 1}).returns([photo])
|
929
|
+
assert_equal photo, photoset.first_photo
|
930
|
+
photoset.first_photo # photos_request should not be called again
|
931
|
+
end
|
932
|
+
|
933
|
+
|
934
|
+
# def test_photosets_editMeta
|
935
|
+
# assert_equal @f.photosets_editMeta('photoset_id'=>@photoset_id, 'title'=>@title)['stat'], 'ok'
|
936
|
+
# end
|
937
|
+
#
|
938
|
+
# def test_photosets_editPhotos
|
939
|
+
# assert_equal @f.photosets_editPhotos('photoset_id'=>@photoset_id, 'primary_photo_id'=>@photo_id, 'photo_ids'=>@photo_id)['stat'], 'ok'
|
940
|
+
# end
|
941
|
+
#
|
942
|
+
# def test_photosets_getContext
|
943
|
+
# assert_equal @f.photosets_getContext('photoset_id'=>@photoset_id, 'photo_id'=>@photo_id)['stat'], 'ok'
|
944
|
+
# end
|
945
|
+
#
|
946
|
+
# def test_photosets_getContext
|
947
|
+
# assert_equal @f.photosets_getContext('photoset_id'=>@photoset_id, 'photo_id'=>@photo_id)['stat'], 'ok'
|
948
|
+
# end
|
949
|
+
#
|
950
|
+
# def test_photosets_getInfo
|
951
|
+
# assert_equal @f.photosets_getInfo('photoset_id'=>@photoset_id)['stat'], 'ok'
|
952
|
+
# end
|
953
|
+
#
|
954
|
+
# def test_photosets_getList
|
955
|
+
# assert_equal @f.photosets_getList['stat'], 'ok'
|
956
|
+
# end
|
957
|
+
#
|
958
|
+
# def test_photosets_getPhotos
|
959
|
+
# assert_equal @f.photosets_getPhotos('photoset_id'=>@photoset_id)['stat'], 'ok'
|
960
|
+
# end
|
961
|
+
#
|
962
|
+
# def test_photosets_orderSets
|
963
|
+
# assert_equal @f.photosets_orderSets('photoset_ids'=>@photoset_id)['stat'], 'ok'
|
964
|
+
# end
|
965
|
+
|
966
|
+
def test_related_tags
|
967
|
+
f = flickr_client
|
968
|
+
tags_response = {
|
969
|
+
"tags" => {
|
970
|
+
"tag" => [ "zoo", "animal" ],
|
971
|
+
"source" => "monkey",
|
972
|
+
},
|
973
|
+
"stat" => "ok",
|
974
|
+
}
|
975
|
+
f.expects(:tags_getRelated).with('tag' => 'monkey').returns(tags_response)
|
976
|
+
assert_equal f.related_tags('monkey'), %w(zoo animal)
|
977
|
+
end
|
978
|
+
|
979
|
+
# ##### Flickr::PhotoCollection tests
|
980
|
+
#
|
981
|
+
def test_should_subclass_array_as_photo_collection
|
982
|
+
assert_equal Array, Flickr::PhotoCollection.superclass
|
983
|
+
end
|
984
|
+
|
985
|
+
def test_should_make_page_a_reader_method
|
986
|
+
assert_equal "3", dummy_photo_collection.page
|
987
|
+
end
|
988
|
+
|
989
|
+
def test_should_make_pages_a_reader_method
|
990
|
+
assert_equal "5", dummy_photo_collection.pages
|
991
|
+
end
|
992
|
+
|
993
|
+
def test_should_make_perpage_a_reader_method
|
994
|
+
assert_equal "10", dummy_photo_collection.perpage
|
995
|
+
end
|
996
|
+
|
997
|
+
def test_should_make_total_a_reader_method
|
998
|
+
assert_equal "42", dummy_photo_collection.total
|
999
|
+
end
|
1000
|
+
|
1001
|
+
def test_should_instantiate_photo_collection_from_photos_hash
|
1002
|
+
pc = Flickr::PhotoCollection.new(dummy_photos_response)
|
1003
|
+
assert_kind_of Flickr::PhotoCollection, pc
|
1004
|
+
assert_equal 2, pc.size
|
1005
|
+
assert_kind_of Flickr::Photo, pc.first
|
1006
|
+
assert_equal "foo123", pc.first["id"]
|
1007
|
+
end
|
1008
|
+
|
1009
|
+
def test_should_instantiate_photo_collection_using_given_api_key
|
1010
|
+
photo = Flickr::PhotoCollection.new(dummy_photos_response, "some_api_key").first
|
1011
|
+
assert_equal "some_api_key", photo.instance_variable_get(:@api_key)
|
1012
|
+
end
|
1013
|
+
|
1014
|
+
private
|
1015
|
+
def flickr_client
|
1016
|
+
Flickr.new("some_api_key")
|
1017
|
+
end
|
1018
|
+
|
1019
|
+
def authenticated_flickr_client
|
1020
|
+
f = Flickr.new('api_key' => 'some_api_key', 'shared_secret' => 'shared_secret_code')
|
1021
|
+
f.instance_variable_set(:@auth_token, 'some_auth_token')
|
1022
|
+
f
|
1023
|
+
end
|
1024
|
+
|
1025
|
+
def new_user(options={})
|
1026
|
+
Flickr::User.new({ 'id' => 'foo123',
|
1027
|
+
'username' => 'some_user',
|
1028
|
+
'name' => 'Some User',
|
1029
|
+
'foo' => 'bar',
|
1030
|
+
'auth_token' => 'foobar789'}.merge(options))
|
1031
|
+
|
1032
|
+
end
|
1033
|
+
def new_photo(options={})
|
1034
|
+
Flickr::Photo.new("1418878",
|
1035
|
+
"foo123",
|
1036
|
+
{ "farm" => "1",
|
1037
|
+
"server" => "2",
|
1038
|
+
"secret" => "1e92283336",
|
1039
|
+
"owner" => Flickr::User.new("abc123", "some_user", nil, nil, "some_api_key") }.merge(options))
|
1040
|
+
end
|
1041
|
+
|
1042
|
+
def dummy_photo_collection
|
1043
|
+
Flickr::PhotoCollection.new(dummy_photos_response)
|
1044
|
+
end
|
1045
|
+
|
1046
|
+
def dummy_photos_response
|
1047
|
+
{ "photos" =>
|
1048
|
+
{ "photo" =>
|
1049
|
+
[{ "id" => "foo123",
|
1050
|
+
"key1" => "value1",
|
1051
|
+
"key2" => "value2" },
|
1052
|
+
{ "id" => "bar456",
|
1053
|
+
"key3" => "value3"}],
|
1054
|
+
"page"=>"3",
|
1055
|
+
"pages"=>"5",
|
1056
|
+
"perpage"=>"10",
|
1057
|
+
"total"=>"42" } }
|
1058
|
+
end
|
1059
|
+
|
1060
|
+
def dummy_single_photo_response
|
1061
|
+
{ "photos" =>
|
1062
|
+
{ "photo" =>
|
1063
|
+
{ "id" => "foo123",
|
1064
|
+
"key1" => "value1",
|
1065
|
+
"key2" => "value2" } } }
|
1066
|
+
end
|
1067
|
+
|
1068
|
+
def dummy_zero_photo_response
|
1069
|
+
{ "photos" => { "total" => 0 } }
|
1070
|
+
end
|
1071
|
+
|
1072
|
+
def dummy_user_response
|
1073
|
+
{ "user" =>
|
1074
|
+
{ "nsid" => "12037949632@N01",
|
1075
|
+
"username" => "Stewart" }
|
1076
|
+
}
|
1077
|
+
end
|
1078
|
+
|
1079
|
+
def dummy_groups_response
|
1080
|
+
{ "groups" =>
|
1081
|
+
{ "group" =>
|
1082
|
+
[{ "nsid" => "group1",
|
1083
|
+
"name" => "Group One",
|
1084
|
+
"eighteenplus" => "0" },
|
1085
|
+
{ "nsid" => "group2",
|
1086
|
+
"name" => "Group Two",
|
1087
|
+
"eighteenplus" => "1"}] } }
|
1088
|
+
end
|
1089
|
+
|
1090
|
+
def dummy_single_group_response
|
1091
|
+
{ "groups" =>
|
1092
|
+
{ "group" =>
|
1093
|
+
{ "nsid" => "group1",
|
1094
|
+
"name" => "Group One",
|
1095
|
+
"eighteenplus" => "0" } } }
|
1096
|
+
end
|
1097
|
+
|
1098
|
+
def dummy_photoset_photos_response
|
1099
|
+
{ "photoset" =>
|
1100
|
+
{ "photo" =>
|
1101
|
+
[{ "id" => "foo123",
|
1102
|
+
"key1" => "value1",
|
1103
|
+
"key2" => "value2" },
|
1104
|
+
{ "id" => "bar456",
|
1105
|
+
"key3" => "value3"}],
|
1106
|
+
"page"=>"3",
|
1107
|
+
"pages"=>"5",
|
1108
|
+
"perpage"=>"10",
|
1109
|
+
"total"=>"42" } }
|
1110
|
+
end
|
1111
|
+
|
1112
|
+
def successful_xml_response
|
1113
|
+
<<-EOF
|
1114
|
+
<?xml version="1.0" encoding="utf-8" ?>
|
1115
|
+
<rsp stat="ok">
|
1116
|
+
<contacts page="1" pages="1" perpage="1000" total="2">
|
1117
|
+
<contact nsid="12037949629@N01" username="Eric" iconserver="1"
|
1118
|
+
realname="Eric Costello"
|
1119
|
+
friend="1" family="0" ignored="1" />
|
1120
|
+
<contact nsid="12037949631@N01" username="neb" iconserver="1"
|
1121
|
+
realname="Ben Cerveny"
|
1122
|
+
friend="0" family="0" ignored="0" />
|
1123
|
+
</contacts>
|
1124
|
+
</rsp>
|
1125
|
+
EOF
|
1126
|
+
end
|
1127
|
+
|
1128
|
+
def unsuccessful_xml_response
|
1129
|
+
<<-EOF
|
1130
|
+
<?xml version="1.0" encoding="utf-8" ?>
|
1131
|
+
<rsp stat="fail">
|
1132
|
+
<err code="[error-code]" msg="[error-message]" />
|
1133
|
+
</rsp>
|
1134
|
+
EOF
|
1135
|
+
end
|
1136
|
+
|
1137
|
+
def photo_info_xml_response
|
1138
|
+
<<-EOF
|
1139
|
+
<?xml version="1.0" encoding="utf-8" ?>
|
1140
|
+
<rsp stat="ok">
|
1141
|
+
<photo id="22527834" secret="ae75bd3111" server="3142" farm="4" dateuploaded="1204145093" isfavorite="0" license="0" rotation="0" media="photo">
|
1142
|
+
<owner nsid="9383319@N05" username="Rootes_arrow_1725" realname="John" location="U.K" />
|
1143
|
+
<title>1964 120 amazon estate</title>
|
1144
|
+
<description>1964 Volvo 120 amazon estate spotted in derbyshire.</description>
|
1145
|
+
<visibility ispublic="1" isfriend="0" isfamily="0" />
|
1146
|
+
<dates posted="1204145093" taken="2007-06-10 13:18:27" takengranularity="0" lastupdate="1204166772" />
|
1147
|
+
<editability cancomment="0" canaddmeta="0" />
|
1148
|
+
<usage candownload="0" canblog="0" canprint="0" />
|
1149
|
+
<comments>1</comments>
|
1150
|
+
<notes>
|
1151
|
+
<note id="313" author="12037949754@N01" authorname="Bees" x="10" y="10" w="50" h="50">foo</note>
|
1152
|
+
</notes>
|
1153
|
+
<tags>
|
1154
|
+
<tag id="9377979-2296968304-2228" author="9383319@N05" raw="volvo" machine_tag="0">volvo</tag>
|
1155
|
+
<tag id="9377979-2296968304-2229" author="9383319@N06" raw="amazon" machine_tag="0">amazon</tag>
|
1156
|
+
</tags>
|
1157
|
+
<urls>
|
1158
|
+
<url type="photopage">http://www.flickr.com/photos/rootes_arrow/2296968304/</url>
|
1159
|
+
</urls>
|
1160
|
+
</photo>
|
1161
|
+
</rsp>
|
1162
|
+
EOF
|
1163
|
+
end
|
1164
|
+
|
1165
|
+
def sparse_photo_info_xml_response
|
1166
|
+
<<-EOF
|
1167
|
+
<?xml version="1.0" encoding="utf-8" ?>
|
1168
|
+
<rsp stat="ok">
|
1169
|
+
<photo id="22527834" secret="ae75bd3111" server="3142" farm="4" dateuploaded="1204145093" isfavorite="0" license="0" rotation="0" media="photo">
|
1170
|
+
<owner nsid="9383319@N05" username="Rootes_arrow_1725" realname="John" location="U.K" />
|
1171
|
+
<title>1964 120 amazon estate</title>
|
1172
|
+
<description/>
|
1173
|
+
<visibility ispublic="1" isfriend="0" isfamily="0" />
|
1174
|
+
<dates posted="1204145093" taken="2007-06-10 13:18:27" takengranularity="0" lastupdate="1204166772" />
|
1175
|
+
<editability cancomment="0" canaddmeta="0" />
|
1176
|
+
<usage candownload="0" canblog="0" canprint="0" />
|
1177
|
+
<comments>1</comments>
|
1178
|
+
<notes/>
|
1179
|
+
<tags/>
|
1180
|
+
<urls>
|
1181
|
+
<url type="photopage">http://www.flickr.com/photos/rootes_arrow/2296968304/</url>
|
1182
|
+
</urls>
|
1183
|
+
</photo>
|
1184
|
+
</rsp>
|
1185
|
+
EOF
|
1186
|
+
end
|
1187
|
+
|
1188
|
+
end
|