PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/amexsbs/railsapp/vendor/plugins/facebooker/test/facebooker/rails/publisher_test.rb

https://bitbucket.org/AcireStudios/social-app-demo
Ruby | 538 lines | 443 code | 90 blank | 5 comment | 2 complexity | a6c965bcccacaf193681bebda34aa3db MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, MIT
  1. require File.expand_path(File.dirname(__FILE__) + '/../../rails_test_helper')
  2. module SymbolHelper
  3. def symbol_helper_loaded
  4. true
  5. end
  6. end
  7. module ModuleHelper
  8. def module_helper_loaded
  9. true
  10. end
  11. end
  12. ::RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
  13. class TestPublisher < Facebooker::Rails::Publisher
  14. helper :symbol
  15. helper ModuleHelper
  16. def action(f)
  17. send_as :action
  18. from f
  19. title "Action Title"
  20. end
  21. def templatized_action(f)
  22. send_as :templatized_action
  23. from f
  24. title_template "Templatized Action Title"
  25. end
  26. def story(to)
  27. send_as :story
  28. recipients to
  29. title 'Story Title'
  30. end
  31. def notification(to,f)
  32. send_as :notification
  33. recipients to
  34. from f
  35. fbml "Not"
  36. end
  37. def email(to,f)
  38. send_as :email
  39. recipients to
  40. from f
  41. title "Email"
  42. fbml 'text'
  43. text fbml
  44. end
  45. def render_notification(to,f)
  46. send_as :notification
  47. recipients to
  48. from f
  49. fbml render(:inline=>"<%=module_helper_loaded%>")
  50. end
  51. def profile_update(to,f)
  52. send_as :profile
  53. recipients to
  54. profile "profile"
  55. profile_action "profile_action"
  56. mobile_profile "mobile_profile"
  57. end
  58. def profile_update_with_profile_main(to,f)
  59. send_as :profile
  60. recipients to
  61. from f
  62. profile "profile"
  63. profile_action "profile_action"
  64. mobile_profile "mobile_profile"
  65. profile_main "profile_main"
  66. end
  67. def ref_update(user)
  68. send_as :ref
  69. fbml "fbml"
  70. handle "handle"
  71. end
  72. def user_action_template
  73. one_line_story_template "{*actor*} did stuff with {*friend*}"
  74. short_story_template "{*actor*} has a title {*friend*}", render(:inline=>"This is a test render")
  75. full_story_template "{*actor*} did a lot","This is the full body",:img=>{:some_params=>true}
  76. end
  77. def simple_user_action_template
  78. one_line_story_template "{*actor*} did stuff with {*friend*}"
  79. end
  80. def user_action_with_action_links_template
  81. one_line_story_template "{*actor*} did stuff with {*friend*}"
  82. short_story_template "{*actor*} has a title {*friend*}", render(:inline=>"This is a test render")
  83. full_story_template "{*actor*} did a lot","This is the full body",:img=>{:some_params=>true}
  84. action_links action_link("Source","HREF")
  85. end
  86. def user_action(user)
  87. send_as :user_action
  88. from user
  89. data :friend=>"Mike"
  90. end
  91. def user_action_with_template_id(user)
  92. send_as :user_action
  93. from user
  94. data :friend=>"Mike"
  95. template_id 4
  96. end
  97. def user_action_with_story_size(user)
  98. send_as :user_action
  99. from user
  100. story_size ONE_LINE
  101. story_size FULL
  102. story_size SHORT
  103. data :friend=>"Mike"
  104. end
  105. def user_action_no_data(user)
  106. send_as :user_action
  107. from user
  108. end
  109. def no_send_as(to)
  110. recipients to
  111. end
  112. def invalid_send_as(to)
  113. send_as :fake
  114. recipients to
  115. end
  116. def publish_post_to_own_stream(user)
  117. send_as :publish_stream
  118. from user
  119. target user
  120. attachment({:name => "Facebooker", :href => "http://www.exampple.com"})
  121. message "Posting post to own stream"
  122. action_links([{:text => "Action Link", :href => "http://www.example.com/action_link"}])
  123. end
  124. def publish_post_to_friends_stream(from, to)
  125. send_as :publish_stream
  126. from from
  127. target to
  128. attachment({:name => "Facebooker", :href => "http://www.exampple.com"})
  129. message "Posting post to friends stream"
  130. action_links([{:text => "Action Link", :href => "http://www.example.com/action_link"}])
  131. end
  132. end
  133. class Facebooker::Rails::Publisher::FacebookTemplateTest < Test::Unit::TestCase
  134. FacebookTemplate = Facebooker::Rails::Publisher::FacebookTemplate
  135. def setup
  136. super
  137. ENV['FACEBOOK_API_KEY'] = '1234567'
  138. @template = mock("facebook template")
  139. FacebookTemplate.stubs(:register).returns(@template)
  140. FacebookTemplate.clear_cache!
  141. end
  142. def test_find_or_register_calls_find_cached
  143. FacebookTemplate.expects(:find_cached).with(TestPublisher,"simple_user_action").returns(@template)
  144. assert_equal FacebookTemplate.for_class_and_method(TestPublisher,"simple_user_action"),@template
  145. end
  146. def test_find_cached_should_use_cached_if_it_exists
  147. FacebookTemplate.cache(TestPublisher,"simple_user_action",@template)
  148. assert_equal FacebookTemplate.find_cached(TestPublisher,"simple_user_action"), @template
  149. end
  150. def test_find_cached_should_call_find_in_db_if_not_in_cache
  151. FacebookTemplate.expects(:find_in_db).with(TestPublisher,"simple_user_action").returns(@template)
  152. assert_equal FacebookTemplate.find_cached(TestPublisher,"simple_user_action"), @template
  153. end
  154. def test_find_in_db_should_run_find
  155. FacebookTemplate.expects(:find_by_template_name).with("1234567: TestPublisher::simple_user_action").returns(@template)
  156. @template.stubs(:template_changed?).returns(false)
  157. assert_equal FacebookTemplate.find_in_db(TestPublisher,"simple_user_action"), @template
  158. end
  159. def test_find_in_db_should_register_if_not_found
  160. FacebookTemplate.expects(:find_by_template_name).with("1234567: TestPublisher::simple_user_action").returns(nil)
  161. FacebookTemplate.expects(:register).with(TestPublisher,"simple_user_action").returns(@template)
  162. FacebookTemplate.find_cached(TestPublisher,"simple_user_action")
  163. end
  164. def test_find_in_db_should_check_for_change_if_found
  165. FacebookTemplate.stubs(:find_by_template_name).returns(@template)
  166. FacebookTemplate.stubs(:hashed_content).returns("MY CONTENT")
  167. @template.expects(:template_changed?).with("MY CONTENT").returns(false)
  168. FacebookTemplate.find_in_db(TestPublisher,"simple_user_action")
  169. end
  170. def test_find_in_db_should_re_register_if_changed
  171. FacebookTemplate.stubs(:find_by_template_name).with("1234567: TestPublisher::simple_user_action").returns(@template)
  172. FacebookTemplate.stubs(:hashed_content).returns("MY CONTENT")
  173. @template.stubs(:template_changed?).returns(true)
  174. @template.stubs(:destroy)
  175. FacebookTemplate.expects(:register).with(TestPublisher,"simple_user_action").returns(@template)
  176. FacebookTemplate.find_in_db(TestPublisher,"simple_user_action")
  177. end
  178. end
  179. class Facebooker::Rails::Publisher::PublisherTest < Test::Unit::TestCase
  180. FacebookTemplate = Facebooker::Rails::Publisher::FacebookTemplate
  181. def setup
  182. super
  183. ENV['FACEBOOK_API_KEY'] = '1234567'
  184. ENV['FACEBOOK_SECRET_KEY'] = '7654321'
  185. @user = Facebooker::User.new
  186. @user.id=4
  187. @session = "session"
  188. @user.stubs(:session).returns(@session)
  189. end
  190. def teardown
  191. super
  192. end
  193. def test_respond_to
  194. assert TestPublisher.respond_to?(:create_action)
  195. assert TestPublisher.respond_to?(:deliver_action)
  196. assert TestPublisher.respond_to?(:register_action)
  197. end
  198. def test_create_action
  199. action=TestPublisher.create_action(@user)
  200. assert_equal Facebooker::Feed::Action,action.class
  201. assert_equal "Action Title",action.title
  202. end
  203. def test_deliver_action
  204. @user.expects(:publish_action)
  205. TestPublisher.deliver_action(@user)
  206. end
  207. def test_create_story
  208. action=TestPublisher.create_story(@user)
  209. assert_equal Facebooker::Feed::Story,action.class
  210. assert_equal "Story Title",action.title
  211. end
  212. def test_deliver_story
  213. @user.expects(:publish_story)
  214. TestPublisher.deliver_story(@user)
  215. end
  216. def test_create_notification
  217. notification=TestPublisher.create_notification(12451752,@user)
  218. assert_equal Facebooker::Rails::Publisher::Notification,notification.class
  219. assert_equal "Not",notification.fbml
  220. end
  221. def test_deliver_notification
  222. @session.expects(:send_notification)
  223. TestPublisher.deliver_notification("12451752",@user)
  224. end
  225. def test_create_email
  226. email=TestPublisher.create_email("12451752",@user)
  227. assert_equal Facebooker::Rails::Publisher::Email,email.class
  228. assert_equal "Email",email.title
  229. assert_equal "text",email.text
  230. assert_equal "text",email.fbml
  231. end
  232. def test_deliver_email
  233. @session.expects(:send_email)
  234. TestPublisher.deliver_email("12451752",@user)
  235. end
  236. def test_create_templatized_action
  237. ta=TestPublisher.create_templatized_action(@user)
  238. assert_equal Facebooker::Feed::TemplatizedAction,ta.class
  239. assert_equal "Templatized Action Title",ta.title_template
  240. end
  241. def test_deliver_templatized_action
  242. @user.expects(:publish_action)
  243. TestPublisher.deliver_templatized_action(@user)
  244. end
  245. def test_create_profile_update
  246. p=TestPublisher.create_profile_update(@user,@user)
  247. assert_equal Facebooker::Rails::Publisher::Profile,p.class
  248. assert_equal "profile",p.profile
  249. assert_equal "profile_action",p.profile_action
  250. assert_equal "mobile_profile",p.mobile_profile
  251. end
  252. def test_create_profile_update_with_profile_main
  253. p=TestPublisher.create_profile_update_with_profile_main(@user,@user)
  254. assert_equal Facebooker::Rails::Publisher::Profile,p.class
  255. assert_equal "profile",p.profile
  256. assert_equal "profile_action",p.profile_action
  257. assert_equal "mobile_profile",p.mobile_profile
  258. assert_equal "profile_main",p.profile_main
  259. end
  260. def test_deliver_profile
  261. Facebooker::User.stubs(:new).returns(@user)
  262. @user.expects(:set_profile_fbml).with('profile', 'mobile_profile', 'profile_action',nil)
  263. TestPublisher.deliver_profile_update(@user,@user)
  264. end
  265. def test_deliver_profile_with_main
  266. Facebooker::User.stubs(:new).returns(@user)
  267. @user.expects(:set_profile_fbml).with('profile', 'mobile_profile', 'profile_action','profile_main')
  268. TestPublisher.deliver_profile_update_with_profile_main(@user,@user)
  269. end
  270. def test_create_ref_update
  271. p=TestPublisher.create_ref_update(@user)
  272. assert_equal Facebooker::Rails::Publisher::Ref,p.class
  273. assert_equal "fbml",p.fbml
  274. assert_equal "handle",p.handle
  275. end
  276. def test_deliver_ref_update
  277. Facebooker::Session.stubs(:create).returns(@session)
  278. @server_cache="server_cache"
  279. @session.expects(:server_cache).returns(@server_cache)
  280. @server_cache.expects(:set_ref_handle).with("handle","fbml")
  281. TestPublisher.deliver_ref_update(@user)
  282. end
  283. def test_register_user_action
  284. Facebooker::Rails::Publisher::FacebookTemplate.expects(:register)
  285. TestPublisher.register_user_action
  286. end
  287. def test_register_should_deactivate_template_bundle_if_exists
  288. @template = mock
  289. @template.stubs(:bundle_id).returns(999)
  290. @template.stubs(:bundle_id=)
  291. @template.stubs(:save!)
  292. @session = mock
  293. @session.stubs(:register_template_bundle).returns(1000)
  294. Facebooker::Session.stubs(:create).returns(@session)
  295. Facebooker::Rails::Publisher::FacebookTemplate.stubs(:find_or_initialize_by_template_name).returns(@template)
  296. @template.expects(:deactivate)
  297. FacebookTemplate.register(TestPublisher, "simple_user_action")
  298. end
  299. def test_register_user_action_with_action_links
  300. ActionController::Base.append_view_path("./test/../../app/views")
  301. Facebooker::Rails::Publisher::FacebookTemplate.expects(:register)
  302. TestPublisher.register_user_action_with_action_links
  303. end
  304. def test_create_user_action
  305. @from_user = Facebooker::User.new
  306. @session = Facebooker::Session.new("","")
  307. @from_user.stubs(:session).returns(@session)
  308. Facebooker::Rails::Publisher::FacebookTemplate.expects(:bundle_id_for_class_and_method).
  309. with(TestPublisher,'user_action').
  310. returns(20309041537)
  311. ua = TestPublisher.create_user_action(@from_user)
  312. assert_equal "user_action", ua.template_name
  313. end
  314. def test_create_user_action_with_template_id
  315. @from_user = Facebooker::User.new
  316. @session = Facebooker::Session.new("","")
  317. @from_user.stubs(:session).returns(@session)
  318. Facebooker::Rails::Publisher::FacebookTemplate.expects(:bundle_id_for_class_and_method).
  319. with(TestPublisher,'user_action').never
  320. ua = TestPublisher.create_user_action_with_template_id(@from_user)
  321. assert_equal 4,ua.template_id
  322. end
  323. def test_publisher_user_action
  324. @from_user = Facebooker::User.new
  325. @session = Facebooker::Session.new("","")
  326. @from_user.stubs(:session).returns(@session)
  327. @session.expects(:publish_user_action).with(20309041537,{:friend=>"Mike"},nil,nil,nil)
  328. Facebooker::Rails::Publisher::FacebookTemplate.expects(:bundle_id_for_class_and_method).
  329. with(TestPublisher, 'user_action').
  330. returns(20309041537)
  331. # pseudo_template = Struct.new(:bundle_id, :content_hash).new(20309041537, '')
  332. # pseudo_template.expects(:matches_content?).returns(true)
  333. # Facebooker::Rails::Publisher::FacebookTemplate.expects(:for).returns(pseudo_template)
  334. TestPublisher.deliver_user_action(@from_user)
  335. end
  336. def test_publish_user_action_with_story_size
  337. @from_user = Facebooker::User.new
  338. @session = Facebooker::Session.new("","")
  339. @from_user.stubs(:session).returns(@session)
  340. @session.expects(:publish_user_action).with(20309041537,{:friend=>"Mike"},nil,nil,2)
  341. Facebooker::Rails::Publisher::FacebookTemplate.expects(:bundle_id_for_class_and_method).
  342. with(TestPublisher, 'user_action_with_story_size').
  343. returns(20309041537)
  344. TestPublisher.deliver_user_action_with_story_size(@from_user)
  345. end
  346. def test_publishing_user_data_no_action_gives_nil_hash
  347. @from_user = Facebooker::User.new
  348. @session = Facebooker::Session.new("","")
  349. @from_user.stubs(:session).returns(@session)
  350. @session.expects(:publish_user_action).with(20309041537,{},nil,nil,nil)
  351. Facebooker::Rails::Publisher::FacebookTemplate.stubs(:bundle_id_for_class_and_method).returns(20309041537)
  352. TestPublisher.deliver_user_action_no_data(@from_user)
  353. end
  354. def test_no_sends_as_raises
  355. assert_raises(Facebooker::Rails::Publisher::UnspecifiedBodyType) {
  356. TestPublisher.deliver_no_send_as(@user)
  357. }
  358. end
  359. def test_invalid_send_as_raises
  360. assert_raises(Facebooker::Rails::Publisher::UnknownBodyType) {
  361. TestPublisher.deliver_invalid_send_as(@user)
  362. }
  363. end
  364. def test_publish_post_to_own_stream
  365. @user = Facebooker::User.new
  366. @user.expects(:publish_to).with(@user, has_entry(:attachment=>instance_of(Hash)))
  367. TestPublisher.deliver_publish_post_to_own_stream(@user)
  368. end
  369. def test_publish_stream_sets_action_links
  370. @user = Facebooker::User.new
  371. stream_post = TestPublisher.create_publish_post_to_own_stream(@user)
  372. assert_equal [{:text => "Action Link", :href => "http://www.example.com/action_link"}],stream_post.action_links
  373. end
  374. def test_publish_stream_sets_target
  375. @user = Facebooker::User.new
  376. stream_post = TestPublisher.create_publish_post_to_own_stream(@user)
  377. assert_equal @user,stream_post.target
  378. end
  379. def test_publish_post_to_friends_stream
  380. @from_user = Facebooker::User.new
  381. @to_user = Facebooker::User.new
  382. @from_user.expects(:publish_to).with(@to_user, has_entry(:action_links=>instance_of(Array)))
  383. TestPublisher.deliver_publish_post_to_friends_stream(@from_user, @to_user)
  384. end
  385. def test_keeps_class_method_missing
  386. assert_raises(NoMethodError) {
  387. TestPublisher.fake
  388. }
  389. end
  390. def test_keeps_instance_method_missing
  391. assert_raises(NoMethodError) {
  392. TestPublisher.new.fake
  393. }
  394. end
  395. def test_image_urls
  396. Facebooker.expects(:facebook_path_prefix).returns("/mike")
  397. string_image = TestPublisher.new.image('image.png', 'raw_string')
  398. assert_equal('/images/image.png',string_image.src)
  399. assert_equal('raw_string',string_image.href)
  400. route_image = TestPublisher.new.image('image.png', {:controller => :pokes, :action => :do, :id => 1})
  401. assert_equal('http://apps.facebook.com/mike/pokes/do/1',route_image.href)
  402. end
  403. def test_image_holder_equality
  404. assert_equal TestPublisher::ImageHolder.new('image.png', 'raw_string'), TestPublisher::ImageHolder.new('image.png', 'raw_string')
  405. end
  406. def test_image_to_json_puts_src_first
  407. string_image = TestPublisher.new.image('image.png', 'raw_string')
  408. assert_equal "{\"src\":\"/images/image.png\", \"href\":\"raw_string\"}",string_image.to_json
  409. end
  410. def test_action_link
  411. assert_equal({:text=>"text", :href=>"href"}, TestPublisher.new.action_link("text","href"))
  412. end
  413. def test_default_url_options
  414. Facebooker.expects(:facebook_path_prefix).returns("/mike")
  415. assert_equal({:host=>"apps.facebook.com/mike"},TestPublisher.new.default_url_options)
  416. end
  417. def test_recipients
  418. tp=TestPublisher.new
  419. tp.recipients "a"
  420. assert_equal("a",tp.recipients)
  421. end
  422. def test_symbol_helper
  423. assert TestPublisher.new.symbol_helper_loaded
  424. end
  425. def test_module_helper
  426. assert TestPublisher.new.module_helper_loaded
  427. end
  428. def test_with_render
  429. #normally Rails would do this for us
  430. silence_warnings do
  431. if ActionController::Base.respond_to?(:append_view_path)
  432. ActionController::Base.append_view_path("./test/../../app/views")
  433. end
  434. notification=TestPublisher.create_render_notification(12451752,@user)
  435. assert_equal "true",notification.fbml
  436. end
  437. end
  438. def test_notification_as_announcement
  439. #normally Rails would do this for us
  440. silence_warnings do
  441. if ActionController::Base.respond_to?(:append_view_path)
  442. ActionController::Base.append_view_path("./test/../../app/views")
  443. end
  444. notification=TestPublisher.create_render_notification(12451752,nil)
  445. assert_equal "true",notification.fbml
  446. end
  447. end
  448. end