PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/test/functional/group_controller_test.rb

https://github.com/noubani/crabgrass
Ruby | 539 lines | 11 code | 6 blank | 522 comment | 0 complexity | 99fce63f7c93a568fbeaa444034223f0 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0
  1. require File.dirname(__FILE__) + '/../test_helper'
  2. #require 'group_controller'
  3. #showlog
  4. # Re-raise errors caught by the controller.
  5. #class GroupController; def rescue_action(e) raise e end; end
  6. class GroupControllerTest < Test::Unit::TestCase
  7. # fixtures :groups, :group_settings, :users, :memberships, :profiles, :pages,
  8. # :group_participations, :user_participations, :tasks, :page_terms, :sites,
  9. # :federatings
  10. include UrlHelper
  11. def setup
  12. # @controller = GroupController.new
  13. @request = ActionController::TestRequest.new
  14. @response = ActionController::TestResponse.new
  15. end
  16. def teardown
  17. disable_site_testing
  18. end
  19. =begin
  20. def test_show_when_logged_in
  21. login_as :red
  22. # show a group you belong to
  23. get :show, :id => groups(:rainbow).name
  24. assert_response :success
  25. # assert_template 'show'
  26. assert_not_nil assigns(:group)
  27. assert assigns(:group).valid?
  28. assert_not_nil assigns(:access)
  29. assert_equal :private, assigns(:access), "blue should have access to private group information for :rainbow"
  30. #show a committee you belong to
  31. get :show, :id => groups(:warm).name
  32. assert_response :success
  33. # assert_template 'show'
  34. assert assigns(:group).valid?
  35. # show a public group you don't belong to
  36. get :show, :id => groups(:public_group).name
  37. assert_response :success
  38. # assert_template 'show'
  39. assert_not_nil assigns(:group)
  40. assert assigns(:group).valid?
  41. assert_not_nil assigns(:access)
  42. assert_equal :public, assigns(:access), "blue should only have access to public group information for :public_group"
  43. # show nothing for a private group you don't belong to
  44. get :show, :id => groups(:private_group).name
  45. assert_response :missing
  46. # assert_template 'show_nothing'
  47. end
  48. def test_show_committees_when_logged_in
  49. login_as :blue
  50. # show a group you belong to
  51. get :show, :id => groups(:public_group).name
  52. assert_response :success
  53. # assert_template 'show'
  54. assert_equal :private, assigns(:access), "should have private access to public group"
  55. assert_equal 2, assigns(:committees).length, "should show 2 committee"
  56. end
  57. def test_show_public_when_not_logged_in
  58. get :show, :id => groups(:public_group).name
  59. assert_response :success
  60. # assert_template 'show'
  61. assert_equal :public, assigns(:access), "should have public access to public group"
  62. assert_equal 1, assigns(:committees).length, "should show 1 committee"
  63. get :show, :id => groups(:public_committee).name
  64. assert_response :success
  65. # assert_template 'show'
  66. assert_equal :public, assigns(:access), "should have public access to public committee of public group"
  67. end
  68. def test_show_private_when_not_logged_in
  69. get :show, :id => groups(:private_group).name
  70. assert_response 401
  71. assert_nil assigns(:access), "should have no access to private group"
  72. get :show, :id => groups(:warm).name
  73. assert_response 401
  74. assert_nil assigns(:access), "should have no access to private committee"
  75. get :show, :id => groups(:private_committee).name
  76. assert_response 401
  77. assert_nil assigns(:access), "should have no access to private committee of public group"
  78. end
  79. def test_visualize
  80. end
  81. def test_archive_logged_in
  82. login_as :red
  83. get :archive, :id => groups(:rainbow).name
  84. assert_response :success, 'logged in, member of group should succeed'
  85. assert assigns(:group).valid?
  86. assert_not_nil assigns(:months)
  87. assert assigns(:months).length > 0, "should have some months"
  88. get :archive, :id => groups(:public_group).name
  89. assert_response :success, 'public group, logged in, should be found'
  90. assert assigns(:group).valid?
  91. get :archive, :id => groups(:public_group).name, :path => 'month/1/year/2008'
  92. assert_response :success
  93. get :archive, :id => groups(:private_group).name
  94. end
  95. def test_archive_not_logged_in
  96. get :archive, :id => groups(:public_group).name
  97. assert_response :success
  98. # assert_template 'archive'
  99. get :archive, :id => groups(:private_group).name
  100. # assert_template 'show_nothing'
  101. end
  102. def test_search
  103. login_as :blue
  104. get :search, :id => groups(:rainbow).name
  105. assert_response :success
  106. assert_not_nil assigns(:pages)
  107. assert assigns(:pages).length > 0, "should have some search results"
  108. get :search, :id => groups(:rainbow).name, :path => 'type/discussion'
  109. assert_response :success
  110. assert_not_nil assigns(:pages)
  111. assert assigns(:pages).length > 0, "should have some search results when filter for discussions"
  112. post :search, :id => groups(:rainbow).name, :search => {:text => "e", :type => "", :person => "", :month => "", :year => "", :pending => "", :starred => ""}
  113. assert_response :redirect
  114. assert_redirected_to 'group/search/rainbow/text/e'
  115. assert_not_nil assigns(:pages)
  116. assert assigns(:pages).length > 0, "should have some search results when filter for text"
  117. end
  118. def test_search_pagination
  119. blue = users(:blue)
  120. rainbow = groups(:rainbow)
  121. # we need enough pages to test pagination
  122. 30.times {|i| Page.create!(:title => "page #{i}", :user => blue, :share_with => rainbow, :access => :view)}
  123. login_as :blue
  124. get :search, :id => rainbow.name, :path => ["descending", "updated_at"]
  125. assert_response :success
  126. assert_select 'div.pagination' do |es|
  127. assert_select 'a', {:text => "2"} do |as|
  128. as.each do |a|
  129. assert_equal "/group/search/rainbow/descending/updated_at?page=2", a.attributes["href"]
  130. end
  131. end
  132. end
  133. end
  134. def test_search_when_not_logged_in
  135. get :search, :id => groups(:public_group).name
  136. assert_response :success
  137. post :search, :id => groups(:public_group).name, :search => {:text => "e", :type => "", :person => "", :month => "", :year => "", :pending => "", :starred => ""}
  138. assert_response :redirect
  139. assert_redirected_to "group/search/#{groups(:public_group).name}/text/e"
  140. end
  141. def test_trash
  142. login_as :red
  143. get :trash, :id => groups(:rainbow).name
  144. assert_response :success
  145. assert_not_nil assigns(:pages)
  146. assert assigns(:pages).length > 0, "rainbow should have some page in the trash."
  147. get :trash, :id => groups(:rainbow).name, :path => 'type/discussion'
  148. assert_response :success
  149. assert_not_nil assigns(:pages)
  150. assert assigns(:pages).length > 0, "rainbow should have some discussion in the trash"
  151. post :trash, :id => groups(:rainbow).name, :search => {:text => "e", :type => "", :person => "", :month => "", :year => "", :pending => "", :starred => ""}
  152. assert_response :redirect
  153. assert_redirected_to 'group/trash/rainbow/text/e'
  154. assert_not_nil assigns(:pages)
  155. assert assigns(:pages).length > 0, "should have some search results when filter for text"
  156. end
  157. def test_trash_not_allowed
  158. login_as :kangaroo
  159. get :trash, :id => groups(:private_group).name
  160. assert_response :missing
  161. assert_equal nil, assigns(:pages)
  162. post :trash, :id => groups(:private_group).name, :search => {:text => "e", :type => "", :person => "", :month => "", :year => "", :pending => "", :starred => ""}
  163. assert_response :missing
  164. assert_equal nil, assigns(:pages)
  165. end
  166. def test_trash_undelete
  167. login_as :red
  168. get :trash, :id => groups(:rainbow).name
  169. assert_response :success
  170. assert assigns(:pages).any?, "should find a deleted page"
  171. id = assigns(:pages).first.id
  172. assert_equal id, 207, "expecting page 207 as deleted page for rainbow"
  173. post :update_trash, :page_checked=>{"207"=>"checked"}, :path=>[], :undelete=>"Undelete", :id => groups(:rainbow).name
  174. assert_response :redirect
  175. assert_redirected_to 'group/trash/rainbow'
  176. get :trash
  177. assert_response :success
  178. assert assigns(:pages).empty?, "should not find a deleted page after undeleting"
  179. end
  180. def test_tags
  181. login_as :blue
  182. get :tags, :id => groups(:rainbow).name
  183. assert_response :success
  184. assert_not_nil assigns(:pages)
  185. end
  186. def test_tags_not_allowed
  187. login_as :kangaroo
  188. get :tags, :id => groups(:private_group).name
  189. assert_response :missing
  190. assert_equal nil, assigns(:pages)
  191. end
  192. def test_tags_sql_inject
  193. login_as :blue
  194. get :tags, :id => groups(:rainbow).name, :path => "'))#"
  195. assert_response :success
  196. assert_equal [], assigns(:pages)
  197. end
  198. def test_tasks
  199. login_as :blue
  200. get :tasks, :id => groups(:rainbow).name
  201. assert_response :success
  202. assert_not_nil assigns(:pages)
  203. assert_not_nil assigns(:task_lists)
  204. assert assigns(:pages).length > 0, "should find some tasks"
  205. end
  206. def test_tasks_not_allowed
  207. login_as :kangaroo
  208. get :tasks, :id => groups(:private_group).name
  209. assert_response :missing
  210. assert_equal nil, assigns(:pages)
  211. end
  212. def test_edit
  213. login_as :blue
  214. get :edit, :id => groups(:rainbow).name
  215. assert_response :success
  216. assert_not_nil assigns(:group)
  217. assert assigns(:group).valid?
  218. new_name = "not-rainbow"
  219. new_full_name = "not a rainbow"
  220. new_summary = "new summary"
  221. group = Group.find(groups(:rainbow).id)
  222. post :update, :id => groups(:rainbow).name, :group => {
  223. :name => new_name,
  224. :full_name => new_full_name,
  225. :summary => new_summary
  226. }
  227. assert_response :redirect
  228. assert_redirected_to :action => 'edit', :id => groups(:rainbow)
  229. group.reload
  230. assert_equal new_full_name, group.full_name, "full name should now be '#{new_full_name}'"
  231. assert_equal new_name, group.name, "group name should now be '#{new_name}'"
  232. assert_equal new_summary, group.summary, "summary should now be '#{new_summary}'"
  233. # a sneaky hacker attack to watch out for
  234. g = Group.create! :name => 'hack-committee', :full_name => "hacker!", :summary => ""
  235. #Site.default.network.add_group! g unless Site.default.network.nil?
  236. assert_not_nil Group.find_by_name('hack-committee')
  237. post :edit, :id => 'hack-committee', :group => {:parent_id => groups(:rainbow).id}
  238. assert_nil Group.find_by_name('hack-committee').parent
  239. end
  240. def test_update
  241. login_as :blue
  242. post :update, :id => groups(:rainbow).name
  243. assert_response :redirect
  244. assert_redirected_to :action => 'edit', :id => groups(:rainbow).name
  245. # try changing the visibility settings
  246. post :update, :id => groups(:private_group).name,
  247. :group => { :publicly_visible_group => "1",
  248. :publicly_visible_members => "1",
  249. :publicly_visible_committees => "1",
  250. :accept_new_membership_requests => "1" }
  251. groups(:private_group).reload
  252. assert_equal true, groups(:private_group).publicly_visible_group,
  253. "private group should be public now"
  254. assert_equal true, groups(:private_group).publicly_visible_committees,
  255. "private group should have public committees now"
  256. assert_equal true, groups(:private_group).publicly_visible_members,
  257. "private group should have public membership now"
  258. assert_equal true, groups(:private_group).accept_new_membership_requests,
  259. "private group should accept new membership requests"
  260. # make sure changing back works, too
  261. post :update, :id => groups(:private_group).name,
  262. :group => { :publicly_visible_group => "0",
  263. :publicly_visible_members => "0",
  264. :publicly_visible_committees => "0",
  265. :accept_new_membership_requests => "0" }
  266. groups(:private_group).reload
  267. assert_equal false, groups(:private_group).publicly_visible_group,
  268. "private group should be private again"
  269. assert_equal false, groups(:private_group).publicly_visible_committees,
  270. "private group should not have public committees now"
  271. assert_equal false, groups(:private_group).publicly_visible_members,
  272. "private group should not have public membership now"
  273. assert_equal false, groups(:private_group).accept_new_membership_requests,
  274. "private group should not accept new membership requests"
  275. # try a sneaky hacker attack
  276. g = Group.create! :name => 'hack-committee', :full_name => "hacker!", :summary => ""
  277. #Site.default.network.add_group! g unless Site.default.network.nil?
  278. assert_not_nil Group.find_by_name('hack-committee')
  279. post :update, :id => 'hack-committee', :group => {:parent_id => groups(:rainbow).id}
  280. assert_nil Group.find_by_name('hack-committee').parent
  281. end
  282. def test_edit_tools
  283. login_as :blue
  284. post :edit_tools, :id => groups(:rainbow).name, :DiscussionPage => "on", :MessagePage => "on", :WikiPage => "on"
  285. groups(:rainbow).reload
  286. assert_equal true, groups(:rainbow).group_setting.allowed_tools.include?("DiscussionPage"),
  287. "group should have Discussion page allowed"
  288. assert_equal true, groups(:rainbow).group_setting.allowed_tools.include?("MessagePage"),
  289. "group should have Message page allowed"
  290. assert_equal true, groups(:rainbow).group_setting.allowed_tools.include?("WikiPage"),
  291. "group should have Wiki page allowed"
  292. assert_equal false, groups(:rainbow).group_setting.allowed_tools.include?("AssetPage")
  293. "group should not have Asset page allowed"
  294. end
  295. def test_destroy
  296. login_as :gerrard
  297. assert_no_difference 'Group.count', "need to be only member to destroy a group" do
  298. post :destroy, :id => groups(:true_levellers).id
  299. end
  300. group_name = 'short-lived-group'
  301. group = Group.create! :name => group_name
  302. group.add_user! users(:gerrard)
  303. assert_difference 'Group.count', -1, "should delete newly created group" do
  304. post :destroy, :id => group_name
  305. assert_redirected_to :controller => 'groups'
  306. end
  307. end
  308. def test_login_required
  309. [:create, :edit, :destroy, :update,
  310. :edit_featured_content, :feature_content, :update_featured_pages
  311. ].each do |action|
  312. assert_requires_login(nil, @request.host) do |c|
  313. c.get action, :id => groups(:public_group).name
  314. end
  315. end
  316. # should we test unlogged-in stuff on a private group?
  317. # [:create, :edit, :destroy, :update].each do |action|
  318. # get action, :id => groups(:private_group).name
  319. # assert_template 'not_found'
  320. # end
  321. end
  322. def test_member_of_committee_but_not_of_group_cannot_access_group_pages
  323. # enable_site_testing
  324. User.current = nil
  325. g = Group.create :name => 'riseup'
  326. c = Committee.create :name => 'outreach', :parent => g
  327. g.add_committee!(c)
  328. u = User.create! :login => 'user', :password => 'password', :password_confirmation => 'password'
  329. assert u.id
  330. c.add_user! u
  331. c.save
  332. u.reload
  333. group_page = DiscussionPage.create :title => 'a group page', :public => false
  334. group_page.add(g, :access => :admin)
  335. group_page.save
  336. committee_page = DiscussionPage.create :title => 'a committee page', :public => false, :group => c
  337. committee_page.add(c, :access => :admin)
  338. committee_page.save
  339. @controller.stubs(:current_user).returns(u)
  340. @controller.stubs(:logged_in?).returns(true)
  341. @controller.instance_variable_set(:@group, c)
  342. assert u.may_admin?(c)
  343. assert @controller.may?(:group,:admin)
  344. get :show
  345. assert_response :success
  346. assert_select "td.date", "Today"
  347. assert_select "a[href=?]", @controller.page_url(committee_page)
  348. @controller.instance_variable_set(:@group, g)
  349. get :show
  350. assert_select "a[href=?]", @controller.page_url(group_page), false
  351. end
  352. def test_edit_featured_content
  353. login_as :blue
  354. get :edit_featured_content, :mode => "expired", :id => groups(:animals).name
  355. assert_select "tr.even", false, "No content should be expired so far."
  356. get :edit_featured_content, :id => groups(:animals).name
  357. assert_select "tr.even", false, "No content should be featured so far."
  358. get :edit_featured_content, :mode => "unfeatured", :id => groups(:animals).name
  359. assert_select "tr.even"
  360. tr = css_select("tr.even").first
  361. id_input = css_select(tr, "input#featured_content_id").first
  362. id = id_input.attributes["value"]
  363. feature_me = Page.find(id)
  364. assert feature_me.valid?
  365. get :feature_content, :id => groups(:animals).name, :featured_content => {
  366. :id => feature_me.id,
  367. :expires => Time.now + 1.year,
  368. :mode => "feature"
  369. }
  370. get :edit_featured_content, :id => groups(:animals).name
  371. assert_select "tr.even"
  372. tr = css_select("tr.even").first
  373. id_input = css_select(tr, "input#featured_content_id").first
  374. id = id_input.attributes["value"]
  375. assert_equal feature_me, Page.find(id)
  376. get :feature_content, :id => groups(:animals).name, :featured_content => {
  377. :id => feature_me.id,
  378. :mode => "unfeature"
  379. }
  380. get :edit_featured_content, :mode => "expired", :id => groups(:animals).name
  381. assert_select "tr.even", false, "No content should be expired so far."
  382. get :edit_featured_content, :id => groups(:animals).name
  383. assert_select "tr.even", false, "No content should be featured so far."
  384. get :edit_featured_content, :mode => "unfeatured", :id => groups(:animals).name
  385. assert_select "tr.even"
  386. end
  387. # tests for group & network home
  388. def test_edit_layout
  389. login_as :blue
  390. get :edit_layout, :id => groups(:rainbow).name
  391. assert_response :success
  392. @group = Group.find_by_name(groups(:rainbow).name)
  393. assert @group, 'group should exist'
  394. @network = Network.find_by_name('fau')
  395. assert @network
  396. # test to change the default order for a group and a network
  397. [@group, @network].each do |group|
  398. # by default the groups first section should be the 'group_wiki'
  399. assert_equal group.layout('section1'), 'group_wiki'
  400. # call the groups home, and check if it is in the default order
  401. get :show, :id => group.name
  402. assert_response :success
  403. assert_select '.section' do |sections|
  404. assert_select sections.first, 'div#wiki-area'
  405. end
  406. params = { :id => group.name, :section1 => 'recent_pages', :section2 => 'group_wiki', :section4 => '' }
  407. params.merge!({:section3 => 'recent_group_pages'}) if group.network?
  408. post :edit_layout, params
  409. assert_redirected_to 'group/edit/'+group.name
  410. # call the group home again, and make sure that the order changed
  411. get :show, :id => group.name
  412. assert_response :success
  413. assert_select '.section' do |sections|
  414. assert_select sections.first, 'div.page_list'
  415. end
  416. group.reload
  417. assert_equal group.layout('section1'), 'recent_pages'
  418. end
  419. end
  420. # def test_xxx
  421. # enable_site_testing do
  422. # assert true
  423. # get :show, :id => 1
  424. # debugger
  425. # assert true
  426. # end
  427. # assert true
  428. # debugger
  429. # assert true
  430. # end
  431. # TODO: test featuring already featured content, expiring features and so on.
  432. =end
  433. end