test_flickr_noapi.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import flickr_noapi
  4. from searx.testing import SearxTestCase
  5. class TestFlickrNoapiEngine(SearxTestCase):
  6. def test_build_flickr_url(self):
  7. url = flickr_noapi.build_flickr_url("uid", "pid")
  8. self.assertIn("uid", url)
  9. self.assertIn("pid", url)
  10. def test_request(self):
  11. query = 'test_query'
  12. dicto = defaultdict(dict)
  13. dicto['pageno'] = 1
  14. dicto['time_range'] = ''
  15. params = flickr_noapi.request(query, dicto)
  16. self.assertIn('url', params)
  17. self.assertIn(query, params['url'])
  18. self.assertIn('flickr.com', params['url'])
  19. def test_response(self):
  20. self.assertRaises(AttributeError, flickr_noapi.response, None)
  21. self.assertRaises(AttributeError, flickr_noapi.response, [])
  22. self.assertRaises(AttributeError, flickr_noapi.response, '')
  23. self.assertRaises(AttributeError, flickr_noapi.response, '[]')
  24. response = mock.Mock(text='"modelExport:{"legend":[],"main":{"search-photos-lite-models":[{"photos":{}}]}}')
  25. self.assertEqual(flickr_noapi.response(response), [])
  26. response = \
  27. mock.Mock(text='"modelExport:{"legend":[],"main":{"search-photos-lite-models":[{"photos":{"_data":[]}}]}}')
  28. self.assertEqual(flickr_noapi.response(response), [])
  29. # everthing is ok test
  30. json = """
  31. modelExport: {
  32. "legend": [
  33. [
  34. "search-photos-lite-models",
  35. "0",
  36. "photos",
  37. "_data",
  38. "0"
  39. ]
  40. ],
  41. "main": {
  42. "search-photos-lite-models": [
  43. {
  44. "photos": {
  45. "_data": [
  46. {
  47. "_flickrModelRegistry": "photo-lite-models",
  48. "title": "This%20is%20the%20title",
  49. "username": "Owner",
  50. "pathAlias": "klink692",
  51. "realname": "Owner",
  52. "license": 0,
  53. "ownerNsid": "59729010@N00",
  54. "canComment": false,
  55. "commentCount": 14,
  56. "faveCount": 21,
  57. "id": "14001294434",
  58. "sizes": {
  59. "c": {
  60. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_c.jpg",
  61. "width": 541,
  62. "height": 800,
  63. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_c.jpg",
  64. "key": "c"
  65. },
  66. "h": {
  67. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_761d32237a_h.jpg",
  68. "width": 1081,
  69. "height": 1600,
  70. "url": "//c4.staticflickr.com/8/7246/14001294434_761d32237a_h.jpg",
  71. "key": "h"
  72. },
  73. "k": {
  74. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_f145a2c11a_k.jpg",
  75. "width": 1383,
  76. "height": 2048,
  77. "url": "//c4.staticflickr.com/8/7246/14001294434_f145a2c11a_k.jpg",
  78. "key": "k"
  79. },
  80. "l": {
  81. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_b.jpg",
  82. "width": 692,
  83. "height": 1024,
  84. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_b.jpg",
  85. "key": "l"
  86. },
  87. "m": {
  88. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777.jpg",
  89. "width": 338,
  90. "height": 500,
  91. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777.jpg",
  92. "key": "m"
  93. },
  94. "n": {
  95. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_n.jpg",
  96. "width": 216,
  97. "height": 320,
  98. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_n.jpg",
  99. "key": "n"
  100. },
  101. "q": {
  102. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_q.jpg",
  103. "width": 150,
  104. "height": 150,
  105. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_q.jpg",
  106. "key": "q"
  107. },
  108. "s": {
  109. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_m.jpg",
  110. "width": 162,
  111. "height": 240,
  112. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_m.jpg",
  113. "key": "s"
  114. },
  115. "sq": {
  116. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_s.jpg",
  117. "width": 75,
  118. "height": 75,
  119. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_s.jpg",
  120. "key": "sq"
  121. },
  122. "t": {
  123. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_t.jpg",
  124. "width": 68,
  125. "height": 100,
  126. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_t.jpg",
  127. "key": "t"
  128. },
  129. "z": {
  130. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
  131. "width": 433,
  132. "height": 640,
  133. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
  134. "key": "z"
  135. }
  136. }
  137. }
  138. ]
  139. }
  140. }
  141. ]
  142. }
  143. }
  144. """
  145. # Flickr serves search results in a json block named 'modelExport' buried inside a script tag,
  146. # this json is served as a single line terminating with a comma.
  147. json = ''.join(json.split()) + ',\n'
  148. response = mock.Mock(text=json)
  149. results = flickr_noapi.response(response)
  150. self.assertEqual(type(results), list)
  151. self.assertEqual(len(results), 1)
  152. self.assertEqual(results[0]['title'], 'This is the title')
  153. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
  154. self.assertIn('k.jpg', results[0]['img_src'])
  155. self.assertIn('n.jpg', results[0]['thumbnail_src'])
  156. self.assertIn('Owner', results[0]['author'])
  157. # no n size, only the z size
  158. json = """
  159. modelExport: {
  160. "legend": [
  161. [
  162. "search-photos-lite-models",
  163. "0",
  164. "photos",
  165. "_data",
  166. "0"
  167. ]
  168. ],
  169. "main": {
  170. "search-photos-lite-models": [
  171. {
  172. "photos": {
  173. "_data": [
  174. {
  175. "_flickrModelRegistry": "photo-lite-models",
  176. "title": "This%20is%20the%20title",
  177. "username": "Owner",
  178. "pathAlias": "klink692",
  179. "realname": "Owner",
  180. "license": 0,
  181. "ownerNsid": "59729010@N00",
  182. "canComment": false,
  183. "commentCount": 14,
  184. "faveCount": 21,
  185. "id": "14001294434",
  186. "sizes": {
  187. "z": {
  188. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
  189. "width": 433,
  190. "height": 640,
  191. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
  192. "key": "z"
  193. }
  194. }
  195. }
  196. ]
  197. }
  198. }
  199. ]
  200. }
  201. }
  202. """
  203. json = ''.join(json.split()) + ',\n'
  204. response = mock.Mock(text=json)
  205. results = flickr_noapi.response(response)
  206. self.assertEqual(type(results), list)
  207. self.assertEqual(len(results), 1)
  208. self.assertEqual(results[0]['title'], 'This is the title')
  209. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
  210. self.assertIn('z.jpg', results[0]['img_src'])
  211. self.assertIn('z.jpg', results[0]['thumbnail_src'])
  212. self.assertIn('Owner', results[0]['author'])
  213. # no z or n size
  214. json = """
  215. modelExport: {
  216. "legend": [
  217. [
  218. "search-photos-lite-models",
  219. "0",
  220. "photos",
  221. "_data",
  222. "0"
  223. ]
  224. ],
  225. "main": {
  226. "search-photos-lite-models": [
  227. {
  228. "photos": {
  229. "_data": [
  230. {
  231. "_flickrModelRegistry": "photo-lite-models",
  232. "title": "This%20is%20the%20title",
  233. "username": "Owner",
  234. "pathAlias": "klink692",
  235. "realname": "Owner",
  236. "license": 0,
  237. "ownerNsid": "59729010@N00",
  238. "canComment": false,
  239. "commentCount": 14,
  240. "faveCount": 21,
  241. "id": "14001294434",
  242. "sizes": {
  243. "o": {
  244. "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_o.jpg",
  245. "width": 433,
  246. "height": 640,
  247. "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_o.jpg",
  248. "key": "o"
  249. }
  250. }
  251. }
  252. ]
  253. }
  254. }
  255. ]
  256. }
  257. }
  258. """
  259. json = ''.join(json.split()) + ',\n'
  260. response = mock.Mock(text=json)
  261. results = flickr_noapi.response(response)
  262. self.assertEqual(type(results), list)
  263. self.assertEqual(len(results), 1)
  264. self.assertEqual(results[0]['title'], 'This is the title')
  265. self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/59729010@N00/14001294434')
  266. self.assertIn('o.jpg', results[0]['img_src'])
  267. self.assertIn('o.jpg', results[0]['thumbnail_src'])
  268. self.assertIn('Owner', results[0]['author'])
  269. # no image test
  270. json = """
  271. modelExport: {
  272. "legend": [
  273. [
  274. "search-photos-lite-models",
  275. "0",
  276. "photos",
  277. "_data",
  278. "0"
  279. ]
  280. ],
  281. "main": {
  282. "search-photos-lite-models": [
  283. {
  284. "photos": {
  285. "_data": [
  286. {
  287. "_flickrModelRegistry": "photo-lite-models",
  288. "title": "This is the title",
  289. "username": "Owner",
  290. "pathAlias": "klink692",
  291. "realname": "Owner",
  292. "license": 0,
  293. "ownerNsid": "59729010@N00",
  294. "canComment": false,
  295. "commentCount": 14,
  296. "faveCount": 21,
  297. "id": "14001294434",
  298. "sizes": {
  299. }
  300. }
  301. ]
  302. }
  303. }
  304. ]
  305. }
  306. }
  307. """
  308. json = ''.join(json.split()) + ',\n'
  309. response = mock.Mock(text=json)
  310. results = flickr_noapi.response(response)
  311. self.assertEqual(type(results), list)
  312. self.assertEqual(len(results), 0)
  313. # null test
  314. json = """
  315. modelExport: {
  316. "legend": [null],
  317. "main": {
  318. "search-photos-lite-models": [
  319. {
  320. "photos": {
  321. "_data": [null]
  322. }
  323. }
  324. ]
  325. }
  326. }
  327. """
  328. json = ''.join(json.split()) + ',\n'
  329. response = mock.Mock(text=json)
  330. results = flickr_noapi.response(response)
  331. self.assertEqual(type(results), list)
  332. self.assertEqual(len(results), 0)
  333. # garbage test
  334. json = r"""
  335. {"toto":[
  336. {"id":200,"name":"Artist Name",
  337. "link":"http:\/\/www.flickr.com\/artist\/1217","type":"artist"}
  338. ]}
  339. """
  340. json = ''.join(json.split()) + ',\n'
  341. response = mock.Mock(text=json)
  342. results = flickr_noapi.response(response)
  343. self.assertEqual(type(results), list)
  344. self.assertEqual(len(results), 0)