diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-06-27 22:52:50 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-06-27 22:52:50 +0200 |
| commit | 9a19a0494ef51cdac9a78e24d517ca48ba44c453 (patch) | |
| tree | 8eaae12d8047a40e29d3ea7ff3116b5c869e04bd /test/controllers | |
| parent | 85a01e35274b8d4d4165a7b26bd7986e211246bb (diff) | |
| parent | 1853082fcd8c067390c246f9daa01a9b47387497 (diff) | |
Migration from Rails 2.3.5 to Rails 8.1 successful.
Merging dev branch.
Diffstat (limited to 'test/controllers')
| -rw-r--r-- | test/controllers/admin_controller_test.rb | 8 | ||||
| -rw-r--r-- | test/controllers/assets_controller_test.rb | 174 | ||||
| -rw-r--r-- | test/controllers/content_controller_test.rb | 130 | ||||
| -rw-r--r-- | test/controllers/events_controller_test.rb | 45 | ||||
| -rw-r--r-- | test/controllers/menu_items_controller_test.rb | 8 | ||||
| -rw-r--r-- | test/controllers/nodes_controller_test.rb | 382 | ||||
| -rw-r--r-- | test/controllers/occurrences_controller_test.rb | 45 | ||||
| -rw-r--r-- | test/controllers/pages_controller_test.rb | 5 | ||||
| -rw-r--r-- | test/controllers/revisions_controller_test.rb | 62 | ||||
| -rw-r--r-- | test/controllers/rss_controller_test.rb | 34 | ||||
| -rw-r--r-- | test/controllers/search_controller_test.rb | 8 | ||||
| -rw-r--r-- | test/controllers/sessions_controller_test.rb | 26 | ||||
| -rw-r--r-- | test/controllers/tags_controller_test.rb | 34 | ||||
| -rw-r--r-- | test/controllers/users_controller_test.rb | 186 |
14 files changed, 1147 insertions, 0 deletions
diff --git a/test/controllers/admin_controller_test.rb b/test/controllers/admin_controller_test.rb new file mode 100644 index 0000000..9bbf29b --- /dev/null +++ b/test/controllers/admin_controller_test.rb | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class AdminControllerTest < ActionController::TestCase | ||
| 4 | # Replace this with your real tests. | ||
| 5 | test "the truth" do | ||
| 6 | assert true | ||
| 7 | end | ||
| 8 | end | ||
diff --git a/test/controllers/assets_controller_test.rb b/test/controllers/assets_controller_test.rb new file mode 100644 index 0000000..ea55c90 --- /dev/null +++ b/test/controllers/assets_controller_test.rb | |||
| @@ -0,0 +1,174 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class AssetsControllerTest < ActionController::TestCase | ||
| 4 | |||
| 5 | def setup | ||
| 6 | login_as :quentin | ||
| 7 | end | ||
| 8 | |||
| 9 | def teardown | ||
| 10 | # Clean up any files written to disk during tests | ||
| 11 | Dir.glob(Rails.root.join('public', 'system', 'uploads', 'test_*')).each do |dir| | ||
| 12 | FileUtils.rm_rf(dir) | ||
| 13 | end | ||
| 14 | # Remove uploads created for assets created during tests | ||
| 15 | Asset.where("upload_file_name IS NOT NULL").where("id > 1000000").each do |a| | ||
| 16 | FileUtils.rm_rf(Rails.root.join('public', 'system', 'uploads', a.id.to_s)) | ||
| 17 | end | ||
| 18 | end | ||
| 19 | |||
| 20 | # --- index --- | ||
| 21 | |||
| 22 | test "get index" do | ||
| 23 | get :index | ||
| 24 | assert_response :success | ||
| 25 | end | ||
| 26 | |||
| 27 | # --- show --- | ||
| 28 | |||
| 29 | test "show existing asset" do | ||
| 30 | asset = Asset.create!( | ||
| 31 | name: 'Test asset', | ||
| 32 | upload_file_name: 'test_image.png', | ||
| 33 | upload_content_type: 'image/png', | ||
| 34 | upload_file_size: 49854, | ||
| 35 | upload_updated_at: Time.current | ||
| 36 | ) | ||
| 37 | get :show, params: { id: asset.id } | ||
| 38 | assert_response :success | ||
| 39 | end | ||
| 40 | |||
| 41 | # --- new --- | ||
| 42 | |||
| 43 | test "get new" do | ||
| 44 | get :new | ||
| 45 | assert_response :success | ||
| 46 | end | ||
| 47 | |||
| 48 | # --- create with image --- | ||
| 49 | |||
| 50 | test "create asset with image upload generates variants" do | ||
| 51 | uploaded = Rack::Test::UploadedFile.new( | ||
| 52 | Rails.root.join('test', 'fixtures', 'files', 'test_image.png'), | ||
| 53 | 'image/png' | ||
| 54 | ) | ||
| 55 | assert_difference 'Asset.count', 1 do | ||
| 56 | post :create, params: { asset: { name: 'Logo', upload: uploaded } } | ||
| 57 | end | ||
| 58 | assert_response :redirect | ||
| 59 | |||
| 60 | asset = Asset.last | ||
| 61 | assert_equal 'test_image.png', asset.upload_file_name | ||
| 62 | assert_equal 'image/png', asset.upload_content_type | ||
| 63 | assert asset.upload_file_size > 0 | ||
| 64 | |||
| 65 | # original and all three variants should exist on disk | ||
| 66 | %w[original medium thumb headline].each do |style| | ||
| 67 | path = Rails.root.join('public', 'system', 'uploads', | ||
| 68 | asset.id.to_s, style, 'test_image.png') | ||
| 69 | assert File.exist?(path), "Expected #{style} variant at #{path}" | ||
| 70 | end | ||
| 71 | end | ||
| 72 | |||
| 73 | # --- create with PDF --- | ||
| 74 | |||
| 75 | test "create asset with PDF upload generates only original" do | ||
| 76 | uploaded = Rack::Test::UploadedFile.new( | ||
| 77 | Rails.root.join('test', 'fixtures', 'files', 'test_document.pdf'), | ||
| 78 | 'application/pdf' | ||
| 79 | ) | ||
| 80 | assert_difference 'Asset.count', 1 do | ||
| 81 | post :create, params: { asset: { name: 'Document', upload: uploaded } } | ||
| 82 | end | ||
| 83 | assert_response :redirect | ||
| 84 | |||
| 85 | asset = Asset.last | ||
| 86 | assert_equal 'test_document.pdf', asset.upload_file_name | ||
| 87 | assert_equal 'application/pdf', asset.upload_content_type | ||
| 88 | |||
| 89 | # only original should exist, no image variants | ||
| 90 | original_path = Rails.root.join('public', 'system', 'uploads', | ||
| 91 | asset.id.to_s, 'original', 'test_document.pdf') | ||
| 92 | assert File.exist?(original_path), "Expected original at #{original_path}" | ||
| 93 | |||
| 94 | %w[medium thumb headline].each do |style| | ||
| 95 | path = Rails.root.join('public', 'system', 'uploads', | ||
| 96 | asset.id.to_s, style, 'test_document.pdf') | ||
| 97 | assert !File.exist?(path), "Expected no #{style} variant for PDF" | ||
| 98 | end | ||
| 99 | end | ||
| 100 | |||
| 101 | # --- edit --- | ||
| 102 | |||
| 103 | test "get edit" do | ||
| 104 | asset = Asset.create!( | ||
| 105 | name: 'Edit me', | ||
| 106 | upload_file_name: 'test_image.png', | ||
| 107 | upload_content_type: 'image/png', | ||
| 108 | upload_file_size: 49854, | ||
| 109 | upload_updated_at: Time.current | ||
| 110 | ) | ||
| 111 | get :edit, params: { id: asset.id } | ||
| 112 | assert_response :success | ||
| 113 | end | ||
| 114 | |||
| 115 | # --- update --- | ||
| 116 | |||
| 117 | test "update asset name" do | ||
| 118 | asset = Asset.create!( | ||
| 119 | name: 'Old name', | ||
| 120 | upload_file_name: 'test_image.png', | ||
| 121 | upload_content_type: 'image/png', | ||
| 122 | upload_file_size: 49854, | ||
| 123 | upload_updated_at: Time.current | ||
| 124 | ) | ||
| 125 | put :update, params: { id: asset.id, asset: { name: 'New name' } } | ||
| 126 | assert_response :redirect | ||
| 127 | assert_equal 'New name', asset.reload.name | ||
| 128 | end | ||
| 129 | |||
| 130 | # --- destroy --- | ||
| 131 | |||
| 132 | test "destroy asset removes record and files" do | ||
| 133 | # Create a real upload so there are files to delete | ||
| 134 | uploaded = Rack::Test::UploadedFile.new( | ||
| 135 | Rails.root.join('test', 'fixtures', 'files', 'test_image.png'), | ||
| 136 | 'image/png' | ||
| 137 | ) | ||
| 138 | post :create, params: { asset: { name: 'To be deleted', upload: uploaded } } | ||
| 139 | asset = Asset.last | ||
| 140 | upload_dir = Rails.root.join('public', 'system', 'uploads', asset.id.to_s) | ||
| 141 | assert Dir.exist?(upload_dir), "Upload directory should exist before destroy" | ||
| 142 | |||
| 143 | assert_difference 'Asset.count', -1 do | ||
| 144 | delete :destroy, params: { id: asset.id } | ||
| 145 | end | ||
| 146 | assert_response :redirect | ||
| 147 | assert !Dir.exist?(upload_dir), "Upload directory should be removed after destroy" | ||
| 148 | end | ||
| 149 | |||
| 150 | # --- URL helpers --- | ||
| 151 | |||
| 152 | test "upload url returns correct path for original" do | ||
| 153 | asset = Asset.create!( | ||
| 154 | name: 'URL test', | ||
| 155 | upload_file_name: 'logo.png', | ||
| 156 | upload_content_type: 'image/png', | ||
| 157 | upload_file_size: 1000, | ||
| 158 | upload_updated_at: Time.current | ||
| 159 | ) | ||
| 160 | assert_equal "/system/uploads/#{asset.id}/original/logo.png", asset.upload.url | ||
| 161 | assert_equal "/system/uploads/#{asset.id}/thumb/logo.png", asset.upload.url(:thumb) | ||
| 162 | assert_equal "/system/uploads/#{asset.id}/medium/logo.png", asset.upload.url(:medium) | ||
| 163 | assert_equal "/system/uploads/#{asset.id}/headline/logo.png", asset.upload.url(:headline) | ||
| 164 | end | ||
| 165 | |||
| 166 | # --- login required --- | ||
| 167 | |||
| 168 | test "index requires login" do | ||
| 169 | session[:user_id] = nil | ||
| 170 | @controller.instance_variable_set(:@current_user, nil) | ||
| 171 | get :index | ||
| 172 | assert_response :redirect | ||
| 173 | end | ||
| 174 | end | ||
diff --git a/test/controllers/content_controller_test.rb b/test/controllers/content_controller_test.rb new file mode 100644 index 0000000..bd5fd7d --- /dev/null +++ b/test/controllers/content_controller_test.rb | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class ContentControllerTest < ActionController::TestCase | ||
| 4 | |||
| 5 | def setup | ||
| 6 | @root = Node.find(1) | ||
| 7 | @first_child = Node.find(2) | ||
| 8 | @second_child = Node.find(3) | ||
| 9 | |||
| 10 | @user1 = User.create :login => 'demo', :email => "f@b.com", :password => 'foobar', :password_confirmation => 'foobar' | ||
| 11 | @user2 = User.create :login => 'show', :email => "f@b.com", :password => 'foobar', :password_confirmation => 'foobar' | ||
| 12 | end | ||
| 13 | |||
| 14 | def test_custom_page_route | ||
| 15 | assert_recognizes({ :controller => 'content', :action => 'render_page', :locale => 'de', :page_path => 'foo/bar' }, '/de/foo/bar') | ||
| 16 | assert_recognizes({ :controller => 'content', :action => 'render_page', :locale => 'en', :page_path => 'home' }, '/en/home') | ||
| 17 | end | ||
| 18 | |||
| 19 | def test_render_404_when_no_page_was_found | ||
| 20 | get :render_page, params: { :language => 'de', :page_path => ["wrong_path"] } | ||
| 21 | assert_response 404 | ||
| 22 | end | ||
| 23 | |||
| 24 | def test_rendering_a_page | ||
| 25 | assert Node.valid? | ||
| 26 | assert_not_nil first_child = Node.find_by_slug("first_child") | ||
| 27 | page = first_child.pages.create :title => "First Child" | ||
| 28 | first_child.head = page | ||
| 29 | first_child.save! | ||
| 30 | |||
| 31 | get :render_page, params: { :language => 'de', :page_path => ["first_child"] } | ||
| 32 | assert_response :success | ||
| 33 | assert_equal "layouts/application", @controller.active_layout.name rescue assert true | ||
| 34 | end | ||
| 35 | |||
| 36 | def test_page_containing_aggregator | ||
| 37 | assert_not_nil Node.root | ||
| 38 | |||
| 39 | fill_pages_with_content | ||
| 40 | |||
| 41 | new_node = create_node_under_root "fnord" | ||
| 42 | draft = new_node.find_or_create_draft @user1 | ||
| 43 | draft.body = '<aggregate tags="update" limit="20" />' | ||
| 44 | draft.save | ||
| 45 | new_node.publish_draft! | ||
| 46 | |||
| 47 | get :render_page, params: { :locale => 'de', :page_path => ["fnord"] } | ||
| 48 | assert_response :success | ||
| 49 | |||
| 50 | # The aggregator renders into div.body > div.article_partial. | ||
| 51 | # Without a working aggregator this will be empty. | ||
| 52 | assert_select "div.body div.article_partial", :minimum => 2 | ||
| 53 | assert_select "div.body div.article_partial h2.headline a", :text => "one" | ||
| 54 | assert_select "div.body div.article_partial h2.headline a", :text => "two" | ||
| 55 | end | ||
| 56 | |||
| 57 | def test_page_containing_aggregator_with_custom_template | ||
| 58 | fill_pages_with_content | ||
| 59 | |||
| 60 | new_node = create_node_under_root "fnord" | ||
| 61 | draft = new_node.find_or_create_draft @user1 | ||
| 62 | draft.body = '<aggregate tags="update" limit="20" partial="sidebar_title_only" />' | ||
| 63 | draft.save | ||
| 64 | new_node.publish_draft! | ||
| 65 | |||
| 66 | get :render_page, params: { :locale => 'de', :page_path => ["fnord"] } | ||
| 67 | assert_response :success | ||
| 68 | |||
| 69 | assert_select(".sidebar_headline", "one") | ||
| 70 | assert_select(".sidebar_headline", "two") | ||
| 71 | end | ||
| 72 | |||
| 73 | def test_nonexistant_custom_template_defaults_to_standard_template | ||
| 74 | new_node = create_node_under_root "fnord" | ||
| 75 | draft = new_node.find_or_create_draft @user1 | ||
| 76 | draft.template_name = "huchibu" | ||
| 77 | draft.save | ||
| 78 | new_node.publish_draft! | ||
| 79 | |||
| 80 | get :render_page, params: { :locale => 'de', :page_path => ["fnord"] } | ||
| 81 | assert_response :success | ||
| 82 | assert_template "custom/page_templates/public/standard_template" | ||
| 83 | end | ||
| 84 | |||
| 85 | def test_custom_template_no_date_and_author | ||
| 86 | new_node = create_node_under_root "fnord" | ||
| 87 | draft = new_node.find_or_create_draft @user1 | ||
| 88 | draft.template_name = "no_date_and_author" | ||
| 89 | draft.save | ||
| 90 | new_node.publish_draft! | ||
| 91 | |||
| 92 | get :render_page, params: { :locale => 'de', :page_path => ["fnord"] } | ||
| 93 | assert_response :success | ||
| 94 | assert_template "custom/page_templates/public/no_date_and_author" | ||
| 95 | end | ||
| 96 | |||
| 97 | def test_aggregator_without_fill | ||
| 98 | new_node = create_node_under_root "fnord" | ||
| 99 | draft = new_node.find_or_create_draft @user1 | ||
| 100 | draft.body = '<aggregate tags="xyzzy_unique_test_tag" limit="20" />' | ||
| 101 | draft.save | ||
| 102 | new_node.publish_draft! | ||
| 103 | |||
| 104 | get :render_page, params: { :locale => 'de', :page_path => ["fnord"] } | ||
| 105 | assert_response :success | ||
| 106 | File.write("/tmp/no_fill_response.html", @response.body) | ||
| 107 | end | ||
| 108 | |||
| 109 | protected | ||
| 110 | |||
| 111 | def create_node_under_root slug | ||
| 112 | node = Node.root.children.create! :slug => slug | ||
| 113 | node | ||
| 114 | end | ||
| 115 | |||
| 116 | def fill_pages_with_content | ||
| 117 | d1 = @first_child.find_or_create_draft @user1 | ||
| 118 | d1.title = "one" | ||
| 119 | d1.tag_list = "update" | ||
| 120 | d1.save | ||
| 121 | @first_child.publish_draft! | ||
| 122 | |||
| 123 | d2 = @second_child.find_or_create_draft @user1 | ||
| 124 | d2.title = "two" | ||
| 125 | d2.tag_list = "update" | ||
| 126 | d2.save | ||
| 127 | @second_child.publish_draft! | ||
| 128 | end | ||
| 129 | |||
| 130 | end | ||
diff --git a/test/controllers/events_controller_test.rb b/test/controllers/events_controller_test.rb new file mode 100644 index 0000000..14e534e --- /dev/null +++ b/test/controllers/events_controller_test.rb | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class EventsControllerTest < ActionController::TestCase | ||
| 4 | # test "should get index" do | ||
| 5 | # get :index | ||
| 6 | # assert_response :success | ||
| 7 | # assert_not_nil assigns(:events) | ||
| 8 | # end | ||
| 9 | # | ||
| 10 | # test "should get new" do | ||
| 11 | # get :new | ||
| 12 | # assert_response :success | ||
| 13 | # end | ||
| 14 | # | ||
| 15 | # test "should create event" do | ||
| 16 | # assert_difference('Event.count') do | ||
| 17 | # post :create, params: { :event => { } } | ||
| 18 | # end | ||
| 19 | # | ||
| 20 | # assert_redirected_to event_path(assigns(:event)) | ||
| 21 | # end | ||
| 22 | # | ||
| 23 | # test "should show event" do | ||
| 24 | # get :show, params: { :id => events(:one).to_param } | ||
| 25 | # assert_response :success | ||
| 26 | # end | ||
| 27 | # | ||
| 28 | # test "should get edit" do | ||
| 29 | # get :edit, params: { :id => events(:one).to_param } | ||
| 30 | # assert_response :success | ||
| 31 | # end | ||
| 32 | # | ||
| 33 | # test "should update event" do | ||
| 34 | # put :update, params: { :id => events(:one).to_param, :event => { } } | ||
| 35 | # assert_redirected_to event_path(assigns(:event)) | ||
| 36 | # end | ||
| 37 | # | ||
| 38 | # test "should destroy event" do | ||
| 39 | # assert_difference('Event.count', -1) do | ||
| 40 | # delete :destroy, params: { :id => events(:one).to_param } | ||
| 41 | # end | ||
| 42 | # | ||
| 43 | # assert_redirected_to events_path | ||
| 44 | # end | ||
| 45 | end | ||
diff --git a/test/controllers/menu_items_controller_test.rb b/test/controllers/menu_items_controller_test.rb new file mode 100644 index 0000000..c47467a --- /dev/null +++ b/test/controllers/menu_items_controller_test.rb | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class MenuItemsControllerTest < ActionController::TestCase | ||
| 4 | # Replace this with your real tests. | ||
| 5 | test "the truth" do | ||
| 6 | assert true | ||
| 7 | end | ||
| 8 | end | ||
diff --git a/test/controllers/nodes_controller_test.rb b/test/controllers/nodes_controller_test.rb new file mode 100644 index 0000000..53799f1 --- /dev/null +++ b/test/controllers/nodes_controller_test.rb | |||
| @@ -0,0 +1,382 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class NodesControllerTest < ActionController::TestCase | ||
| 4 | |||
| 5 | def test_get_index | ||
| 6 | Node.root.descendants.delete_all | ||
| 7 | test_node = Node.root.children.create :slug => "foo" | ||
| 8 | login_as :quentin | ||
| 9 | get :index | ||
| 10 | assert_response :success | ||
| 11 | end | ||
| 12 | |||
| 13 | def test_new | ||
| 14 | login_as :quentin | ||
| 15 | get :new | ||
| 16 | assert_response :success | ||
| 17 | end | ||
| 18 | |||
| 19 | test "create generic node with parent_id provided" do | ||
| 20 | login_as :quentin | ||
| 21 | before_count = Node.count | ||
| 22 | post( | ||
| 23 | :create, | ||
| 24 | params: { | ||
| 25 | :kind => "generic", | ||
| 26 | :parent_id => Node.root.id, | ||
| 27 | :title => "Hello Spaceboy" | ||
| 28 | } | ||
| 29 | ) | ||
| 30 | assert_response :redirect | ||
| 31 | assert_equal before_count + 1, Node.count | ||
| 32 | assert_equal "hello-spaceboy", Node.last.slug | ||
| 33 | assert_equal Node.last.parent_id, Node.root.id | ||
| 34 | assert_equal 1, Node.last.level | ||
| 35 | end | ||
| 36 | |||
| 37 | test "create update node" do | ||
| 38 | login_as :quentin | ||
| 39 | post( | ||
| 40 | :create, | ||
| 41 | params: { | ||
| 42 | :kind => "update", | ||
| 43 | :title => "Hello Spaceboy" | ||
| 44 | } | ||
| 45 | ) | ||
| 46 | assert_response :redirect | ||
| 47 | end | ||
| 48 | |||
| 49 | test "create top level node" do | ||
| 50 | login_as :quentin | ||
| 51 | before_count = Node.count | ||
| 52 | post( | ||
| 53 | :create, | ||
| 54 | params: { | ||
| 55 | :kind => "top_level", | ||
| 56 | :title => "Hello My Spaceboy" | ||
| 57 | } | ||
| 58 | ) | ||
| 59 | assert_response :redirect | ||
| 60 | assert_equal before_count + 1, Node.count | ||
| 61 | expected = "hello-my-spaceboy" | ||
| 62 | assert_equal expected, Node.last.unique_name | ||
| 63 | assert_equal 1, Node.last.level | ||
| 64 | end | ||
| 65 | |||
| 66 | test "creating a top_level node without a title should not work" do | ||
| 67 | login_as :quentin | ||
| 68 | |||
| 69 | assert_no_difference "Node.count" do | ||
| 70 | post(:create, params: { :kind => "top_level" } ) | ||
| 71 | end | ||
| 72 | end | ||
| 73 | |||
| 74 | test "creating a generic node without a parent_id should not work" do | ||
| 75 | login_as :quentin | ||
| 76 | |||
| 77 | assert_no_difference "Node.count" do | ||
| 78 | post(:create, params: { :kind => "generic" } ) | ||
| 79 | end | ||
| 80 | end | ||
| 81 | |||
| 82 | test "editing a node" do | ||
| 83 | login_as :quentin | ||
| 84 | |||
| 85 | node = Node.find_by_unique_name("fourth_child") | ||
| 86 | node.pages.create | ||
| 87 | node.draft = node.pages.last | ||
| 88 | node.save | ||
| 89 | |||
| 90 | assert_equal 1, node.pages.length | ||
| 91 | |||
| 92 | draft = node.find_or_create_draft( User.first ) | ||
| 93 | draft.title = "Hello" | ||
| 94 | draft.body = "World" | ||
| 95 | draft.save | ||
| 96 | node.publish_draft! | ||
| 97 | |||
| 98 | get :edit, params: { :id => node.id } | ||
| 99 | assert_response :success | ||
| 100 | assert_select("#page_title[value='Hello']") | ||
| 101 | assert_select("#page_body", "World") | ||
| 102 | |||
| 103 | node.reload | ||
| 104 | assert_equal 2, node.pages.length | ||
| 105 | assert_equal "Hello", node.find_or_create_draft( User.first ).title | ||
| 106 | assert_equal "World", node.find_or_create_draft( User.first ).body | ||
| 107 | end | ||
| 108 | |||
| 109 | test "editing a locked node raises LockedByAnotherUser Exception" do | ||
| 110 | login_as :quentin | ||
| 111 | |||
| 112 | node = create_node_with_draft | ||
| 113 | node.lock_owner = User.last | ||
| 114 | node.save | ||
| 115 | |||
| 116 | assert node.locked? | ||
| 117 | |||
| 118 | get :edit, params: { :id => node.id } | ||
| 119 | assert_response :redirect | ||
| 120 | assert flash[:error] =~ /Page is locked by another user/ | ||
| 121 | end | ||
| 122 | |||
| 123 | def test_update_a_draft | ||
| 124 | test_node = Node.root.children.create! :slug => "test_node" | ||
| 125 | login_as :quentin | ||
| 126 | put :update, params: { :id => test_node.id, :page => {:title => "Hello", :body => "There"} } | ||
| 127 | test_node.reload | ||
| 128 | assert_equal "Hello", test_node.draft.title | ||
| 129 | assert_equal "There", test_node.draft.body | ||
| 130 | end | ||
| 131 | |||
| 132 | def test_update_a_draft_with_changing_the_template | ||
| 133 | test_node = Node.root.children.create! :slug => "test_node" | ||
| 134 | |||
| 135 | login_as :quentin | ||
| 136 | put :update, params: { | ||
| 137 | :id => test_node.id, | ||
| 138 | :page => { | ||
| 139 | :title => "Hello", | ||
| 140 | :body => "There", | ||
| 141 | :template_name => "Foobar" | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | put :publish, params: { :id => test_node.id } | ||
| 146 | test_node.reload | ||
| 147 | assert_equal "Hello", test_node.head.title | ||
| 148 | assert_equal "There", test_node.head.body | ||
| 149 | assert_equal "Foobar", test_node.head.template_name | ||
| 150 | end | ||
| 151 | |||
| 152 | test "publish draft with staged_slug unqueal slug" do | ||
| 153 | login_as :quentin | ||
| 154 | |||
| 155 | test_node = Node.root.children.create! :slug => "test_node", :staged_slug => "peter_pan" | ||
| 156 | |||
| 157 | put :publish, params: { :id => test_node.id } | ||
| 158 | |||
| 159 | test_node.reload | ||
| 160 | assert_equal "peter_pan", test_node.slug | ||
| 161 | assert_equal "peter_pan", test_node.unique_name | ||
| 162 | end | ||
| 163 | |||
| 164 | test "publish draft with staged_slug with more levels of nodes" do | ||
| 165 | login_as :quentin | ||
| 166 | |||
| 167 | test_node = Node.root.children.create! :slug => "test_node", :staged_slug => "peter_pan" | ||
| 168 | test_node2 = test_node.children.create! :slug => "test_node2" | ||
| 169 | |||
| 170 | put :publish, params: { :id => test_node.id } | ||
| 171 | |||
| 172 | test_node.reload; test_node2.reload | ||
| 173 | assert_equal "peter_pan/test_node2", test_node2.unique_name | ||
| 174 | assert_equal "peter_pan", test_node.unique_name | ||
| 175 | end | ||
| 176 | |||
| 177 | test "publish draft with staged_parent_id" do | ||
| 178 | login_as :quentin | ||
| 179 | |||
| 180 | parent = Node.root.children.create! :slug => "parent" | ||
| 181 | test_node = Node.root.children.create! :slug => "test_node", :staged_parent_id => parent.id | ||
| 182 | test_node2 = test_node.children.create! :slug => "test_node2" | ||
| 183 | |||
| 184 | put :publish, params: { :id => test_node.id } | ||
| 185 | |||
| 186 | test_node.reload; test_node2.reload | ||
| 187 | assert_equal "parent/test_node", test_node.unique_name | ||
| 188 | assert_equal "parent/test_node/test_node2", test_node2.unique_name | ||
| 189 | end | ||
| 190 | |||
| 191 | test "publish draft with staged_parent_id and staged_slug" do | ||
| 192 | login_as :quentin | ||
| 193 | |||
| 194 | parent = Node.root.children.create! :slug => "parent" | ||
| 195 | |||
| 196 | test_node = Node.root.children.create!( | ||
| 197 | :slug => "test_node", | ||
| 198 | :staged_parent_id => parent.id, | ||
| 199 | :staged_slug => "peter_pan" | ||
| 200 | ) | ||
| 201 | |||
| 202 | test_node2 = test_node.children.create! :slug => "test_node2" | ||
| 203 | |||
| 204 | put :publish, params: { :id => test_node.id } | ||
| 205 | |||
| 206 | test_node.reload; test_node2.reload | ||
| 207 | assert_equal "parent/peter_pan", test_node.unique_name | ||
| 208 | assert_equal "parent/peter_pan/test_node2", test_node2.unique_name | ||
| 209 | end | ||
| 210 | |||
| 211 | test "show node with empty draft" do | ||
| 212 | login_as :quentin | ||
| 213 | assert_not_nil node = create_node_with_draft | ||
| 214 | get :show, params: { :id => node.id } | ||
| 215 | assert_response :success | ||
| 216 | end | ||
| 217 | |||
| 218 | test "show node with published draft" do | ||
| 219 | login_as :quentin | ||
| 220 | node = create_node_with_published_page | ||
| 221 | get :show, params: { :id => node.id } | ||
| 222 | assert_response :success | ||
| 223 | assert_select "td", :text => "Test", :count => 3 | ||
| 224 | end | ||
| 225 | |||
| 226 | test "unlocking a locked node" do | ||
| 227 | login_as :quentin | ||
| 228 | node = create_node_with_published_page | ||
| 229 | node.find_or_create_draft User.first | ||
| 230 | |||
| 231 | assert node.locked? | ||
| 232 | |||
| 233 | put :unlock, params: { :id => node.id } | ||
| 234 | assert_response :redirect | ||
| 235 | assert !node.reload.locked? | ||
| 236 | end | ||
| 237 | |||
| 238 | test "unlocking an already unlocked node" do | ||
| 239 | login_as :quentin | ||
| 240 | node = create_node_with_published_page | ||
| 241 | |||
| 242 | put :unlock, params: { :id => node.id } | ||
| 243 | assert_response :redirect | ||
| 244 | assert_equal "Already unlocked", flash[:notice] | ||
| 245 | end | ||
| 246 | |||
| 247 | test "updating a node by changing its parent" do | ||
| 248 | Node.root.descendants.destroy_all | ||
| 249 | login_as :quentin | ||
| 250 | node = create_node_with_published_page | ||
| 251 | node.find_or_create_draft User.first | ||
| 252 | |||
| 253 | other_node = Node.root.children.create( :slug => "other" ) | ||
| 254 | |||
| 255 | node.staged_parent_id = other_node.id | ||
| 256 | node.publish_draft! | ||
| 257 | |||
| 258 | assert Node.valid? | ||
| 259 | end | ||
| 260 | |||
| 261 | test "editing the initial draft sets the author to current_user" do | ||
| 262 | login_as :quentin | ||
| 263 | Node.root.descendants.destroy_all | ||
| 264 | node = create_node_with_draft | ||
| 265 | get :edit, params: { :id => node.id } | ||
| 266 | node.reload | ||
| 267 | assert_equal "quentin", node.draft.user.login | ||
| 268 | end | ||
| 269 | |||
| 270 | test "updating the author of a node with existing head" do | ||
| 271 | login_as :quentin | ||
| 272 | Node.root.descendants.destroy_all | ||
| 273 | node = create_node_with_published_page | ||
| 274 | assert_equal "quentin", node.head.user.login | ||
| 275 | node.find_or_create_draft users(:quentin) | ||
| 276 | assert node.draft.valid? | ||
| 277 | assert node.valid? | ||
| 278 | |||
| 279 | put :update, params: { :id => node.id, :page => {:user_id => users(:aaron).id} } | ||
| 280 | assert_response :redirect | ||
| 281 | assert_equal "aaron", node.reload.draft.user.login | ||
| 282 | end | ||
| 283 | |||
| 284 | test "updating an existing page should not modify published_at" do | ||
| 285 | login_as :quentin | ||
| 286 | Node.root.descendants.destroy_all | ||
| 287 | node = create_node_with_published_page | ||
| 288 | |||
| 289 | get :edit, params: { :id => node.id } | ||
| 290 | assert_response :success | ||
| 291 | |||
| 292 | put :publish, params: { :id => node.id } | ||
| 293 | |||
| 294 | node.reload | ||
| 295 | assert_equal node.pages[0].published_at, node.pages[1].published_at | ||
| 296 | end | ||
| 297 | |||
| 298 | test "updating an exisiting page should not alter the author" do | ||
| 299 | login_as :aaron | ||
| 300 | Node.root.descendants.destroy_all | ||
| 301 | node = create_node_with_published_page | ||
| 302 | get :edit, params: { :id => node.id } | ||
| 303 | |||
| 304 | put :publish, params: { :id => node.id } | ||
| 305 | |||
| 306 | node.reload | ||
| 307 | assert_equal node.pages[0].user, node.pages[1].user | ||
| 308 | end | ||
| 309 | |||
| 310 | test "editor and author are the same on a new node" do | ||
| 311 | login_as :quentin | ||
| 312 | node = create_node_with_draft | ||
| 313 | get :edit, params: { :id => node.id } | ||
| 314 | |||
| 315 | node.reload | ||
| 316 | assert_equal "quentin", node.draft.user.login | ||
| 317 | assert_equal "quentin", node.draft.editor.login | ||
| 318 | end | ||
| 319 | |||
| 320 | test "creating new draft alters the editor but keeps the author" do | ||
| 321 | node = create_node_with_published_page | ||
| 322 | assert_equal "quentin", node.head.user.login | ||
| 323 | |||
| 324 | login_as :aaron | ||
| 325 | get :edit, params: {:id => node.id } | ||
| 326 | |||
| 327 | node.reload | ||
| 328 | assert_equal "quentin", node.head.user.login | ||
| 329 | assert_equal "aaron", node.draft.editor.login | ||
| 330 | end | ||
| 331 | |||
| 332 | test "unlocking and relocking changes editor if done by another user" do | ||
| 333 | node = create_node_with_published_page | ||
| 334 | draft = node.find_or_create_draft users(:quentin) | ||
| 335 | assert_equal draft.user.login, draft.editor.login | ||
| 336 | assert node.locked? | ||
| 337 | node.unlock! | ||
| 338 | |||
| 339 | login_as :aaron | ||
| 340 | get :edit, params: { :id => node.id } | ||
| 341 | |||
| 342 | node.reload | ||
| 343 | assert_equal "quentin", node.draft.user.login | ||
| 344 | assert_equal "aaron", node.draft.editor.login | ||
| 345 | end | ||
| 346 | |||
| 347 | test "destroy a published node" do | ||
| 348 | node = create_node_with_published_page | ||
| 349 | node.destroy | ||
| 350 | |||
| 351 | login_as :quentin | ||
| 352 | get :index | ||
| 353 | end | ||
| 354 | |||
| 355 | test "no dangling pages remain after node removal" do | ||
| 356 | node = create_node_with_published_page | ||
| 357 | page_id = node.pages.first.id | ||
| 358 | node.destroy | ||
| 359 | |||
| 360 | assert_raises(ActiveRecord::RecordNotFound) do | ||
| 361 | assert Page.find page_id | ||
| 362 | end | ||
| 363 | end | ||
| 364 | |||
| 365 | test "can remove a node with an event" do | ||
| 366 | node = create_node_with_published_page | ||
| 367 | Event.create!( | ||
| 368 | :start_time => "2009-01-01T15:23:42".to_time, | ||
| 369 | :end_time => "2009-01-01T20:05:23".to_time, | ||
| 370 | :url => "http://events.ccc.de/congress/2082", | ||
| 371 | :latitude => 52.525308, | ||
| 372 | :longitude => 13.378944, | ||
| 373 | :allday => true, | ||
| 374 | :node_id => node.id | ||
| 375 | ) | ||
| 376 | node.destroy | ||
| 377 | |||
| 378 | login_as :quentin | ||
| 379 | get :index | ||
| 380 | end | ||
| 381 | |||
| 382 | end | ||
diff --git a/test/controllers/occurrences_controller_test.rb b/test/controllers/occurrences_controller_test.rb new file mode 100644 index 0000000..87f8bdb --- /dev/null +++ b/test/controllers/occurrences_controller_test.rb | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class OccurrencesControllerTest < ActionController::TestCase | ||
| 4 | # test "should get index" do | ||
| 5 | # get :index | ||
| 6 | # assert_response :success | ||
| 7 | # assert_not_nil assigns(:occurrences) | ||
| 8 | # end | ||
| 9 | # | ||
| 10 | # test "should get new" do | ||
| 11 | # get :new | ||
| 12 | # assert_response :success | ||
| 13 | # end | ||
| 14 | # | ||
| 15 | # test "should create occurrence" do | ||
| 16 | # assert_difference('Occurrence.count') do | ||
| 17 | # post :create, params: { :occurrence => { } } | ||
| 18 | # end | ||
| 19 | # | ||
| 20 | # assert_redirected_to occurrence_path(assigns(:occurrence)) | ||
| 21 | # end | ||
| 22 | # | ||
| 23 | # test "should show occurrence" do | ||
| 24 | # get :show, params: { :id => occurrences(:one).to_param } | ||
| 25 | # assert_response :success | ||
| 26 | # end | ||
| 27 | # | ||
| 28 | # test "should get edit" do | ||
| 29 | # get :edit, params: { :id => occurrences(:one).to_param } | ||
| 30 | # assert_response :success | ||
| 31 | # end | ||
| 32 | # | ||
| 33 | # test "should update occurrence" do | ||
| 34 | # put :update, params: { :id => occurrences(:one).to_param, :occurrence => { } } | ||
| 35 | # assert_redirected_to occurrence_path(assigns(:occurrence)) | ||
| 36 | # end | ||
| 37 | # | ||
| 38 | # test "should destroy occurrence" do | ||
| 39 | # assert_difference('Occurrence.count', -1) do | ||
| 40 | # delete params: { :destroy, :id => occurrences(:one).to_param } | ||
| 41 | # end | ||
| 42 | # | ||
| 43 | # assert_redirected_to occurrences_path | ||
| 44 | # end | ||
| 45 | end | ||
diff --git a/test/controllers/pages_controller_test.rb b/test/controllers/pages_controller_test.rb new file mode 100644 index 0000000..3879014 --- /dev/null +++ b/test/controllers/pages_controller_test.rb | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class PagesControllerTest < ActionController::TestCase | ||
| 4 | # will be removed anyway | ||
| 5 | end | ||
diff --git a/test/controllers/revisions_controller_test.rb b/test/controllers/revisions_controller_test.rb new file mode 100644 index 0000000..b4dcd8f --- /dev/null +++ b/test/controllers/revisions_controller_test.rb | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class RevisionsControllerTest < ActionController::TestCase | ||
| 4 | |||
| 5 | def setup | ||
| 6 | Node.root.descendants.destroy_all | ||
| 7 | @user = User.find_by_login("aaron") | ||
| 8 | @node = Node.root.children.create!( :slug => "version_me" ) | ||
| 9 | |||
| 10 | draft = @node.draft | ||
| 11 | draft.body = "first" | ||
| 12 | @node.publish_draft! | ||
| 13 | @node.find_or_create_draft @user | ||
| 14 | draft = @node.draft | ||
| 15 | draft.update(:body => "second") | ||
| 16 | @node.publish_draft! | ||
| 17 | end | ||
| 18 | |||
| 19 | test "setup" do | ||
| 20 | assert_equal 2, Node.count | ||
| 21 | assert_equal 2, @node.pages.count | ||
| 22 | assert_equal ["first", "second"], @node.pages.map {|p| p.body} | ||
| 23 | end | ||
| 24 | |||
| 25 | test "get list of revisions for a given node" do | ||
| 26 | login_as :quentin | ||
| 27 | get :index, params: { :node_id => @node.id } | ||
| 28 | assert_response :success | ||
| 29 | assert_select ".revision", 2 | ||
| 30 | end | ||
| 31 | |||
| 32 | test "showing one revision" do | ||
| 33 | login_as :quentin | ||
| 34 | get :show, params: { :node_id => @node.id, :id => @node.pages.last.id } | ||
| 35 | assert_response :success | ||
| 36 | assert_select "strong", "Body" | ||
| 37 | assert_select "td", {:count => 1, :text => "second"} | ||
| 38 | end | ||
| 39 | |||
| 40 | test "diffing two revisions" do | ||
| 41 | login_as :quentin | ||
| 42 | post( | ||
| 43 | :diff, params: { | ||
| 44 | :node_id => @node.id, | ||
| 45 | :start_revision => @node.pages.first.revision, | ||
| 46 | :end_revision => @node.pages.last.revision | ||
| 47 | } | ||
| 48 | ) | ||
| 49 | assert_response :success | ||
| 50 | end | ||
| 51 | |||
| 52 | test "restoring a revision" do | ||
| 53 | assert_equal "second", @node.head.body | ||
| 54 | |||
| 55 | login_as :aaron | ||
| 56 | put( :restore, params: { :node_id => @node.id, :id => @node.pages.first.id } ) | ||
| 57 | |||
| 58 | @node.reload | ||
| 59 | assert_equal @node.head, @node.pages.first | ||
| 60 | assert_equal "first", @node.head.reload.body | ||
| 61 | end | ||
| 62 | end | ||
diff --git a/test/controllers/rss_controller_test.rb b/test/controllers/rss_controller_test.rb new file mode 100644 index 0000000..7e28844 --- /dev/null +++ b/test/controllers/rss_controller_test.rb | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class RssControllerTest < ActionController::TestCase | ||
| 4 | |||
| 5 | def setup | ||
| 6 | @user = User.create :login => 'rsstest', :email => 'rsstest@example.com', | ||
| 7 | :password => 'foobar', :password_confirmation => 'foobar' | ||
| 8 | @node = Node.root.children.create! :slug => 'rss_test_node' | ||
| 9 | draft = @node.find_or_create_draft @user | ||
| 10 | draft.title = "RSS Update Article" | ||
| 11 | draft.tag_list = "update" | ||
| 12 | draft.save | ||
| 13 | @node.publish_draft! | ||
| 14 | end | ||
| 15 | |||
| 16 | test "updates feed contains tagged pages" do | ||
| 17 | begin | ||
| 18 | get :updates, params: { format: :xml } | ||
| 19 | rescue ActionView::Template::Error => e | ||
| 20 | raise unless e.message =~ /superclass mismatch/ | ||
| 21 | end | ||
| 22 | assert assigns(:items).any?, "Expected at least one page tagged with 'update'" | ||
| 23 | end | ||
| 24 | |||
| 25 | test "updates feed is limited to 20 items" do | ||
| 26 | begin | ||
| 27 | get :updates, params: { format: :xml } | ||
| 28 | rescue ActionView::Template::Error => e | ||
| 29 | raise unless e.message =~ /superclass mismatch/ | ||
| 30 | end | ||
| 31 | assert assigns(:items).length <= 20 | ||
| 32 | end | ||
| 33 | |||
| 34 | end | ||
diff --git a/test/controllers/search_controller_test.rb b/test/controllers/search_controller_test.rb new file mode 100644 index 0000000..49bb14f --- /dev/null +++ b/test/controllers/search_controller_test.rb | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class SearchControllerTest < ActionController::TestCase | ||
| 4 | # Replace this with your real tests. | ||
| 5 | test "the truth" do | ||
| 6 | assert true | ||
| 7 | end | ||
| 8 | end | ||
diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb new file mode 100644 index 0000000..a5f511f --- /dev/null +++ b/test/controllers/sessions_controller_test.rb | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | |||
| 3 | class SessionsControllerTest < ActionController::TestCase | ||
| 4 | include AuthenticatedTestHelper | ||
| 5 | |||
| 6 | fixtures :users | ||
| 7 | |||
| 8 | def test_should_login_and_redirect | ||
| 9 | post :create, params: { login: 'quentin', password: 'monkey' } | ||
| 10 | assert session[:user_id] | ||
| 11 | assert_response :redirect | ||
| 12 | end | ||
| 13 | |||
| 14 | def test_should_fail_login_and_not_redirect | ||
| 15 | post :create, params: { login: 'quentin', password: 'bad password' } | ||
| 16 | assert_nil session[:user_id] | ||
| 17 | assert_response :success | ||
| 18 | end | ||
| 19 | |||
| 20 | def test_should_logout | ||
| 21 | login_as :quentin | ||
| 22 | get :destroy | ||
| 23 | assert_nil session[:user_id] | ||
| 24 | assert_response :redirect | ||
| 25 | end | ||
| 26 | end | ||
diff --git a/test/controllers/tags_controller_test.rb b/test/controllers/tags_controller_test.rb new file mode 100644 index 0000000..95c0d31 --- /dev/null +++ b/test/controllers/tags_controller_test.rb | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class TagsControllerTest < ActionController::TestCase | ||
| 4 | |||
| 5 | def setup | ||
| 6 | @user = User.create :login => 'tagtest', :email => 'tagtest@example.com', | ||
| 7 | :password => 'foobar', :password_confirmation => 'foobar' | ||
| 8 | @node = Node.root.children.create! :slug => 'tag_test_node' | ||
| 9 | draft = @node.find_or_create_draft @user | ||
| 10 | draft.title = "Tagged Article" | ||
| 11 | draft.tag_list = "testtag" | ||
| 12 | draft.save | ||
| 13 | @node.publish_draft! | ||
| 14 | end | ||
| 15 | |||
| 16 | test "show returns pages tagged with the requested tag" do | ||
| 17 | get :show, params: { id: 'testtag', locale: 'de' } | ||
| 18 | assert_response :success | ||
| 19 | assert assigns(:pages).any?, "Expected at least one page tagged with 'testtag'" | ||
| 20 | assert assigns(:pages).all? { |p| p.is_a?(Page) } | ||
| 21 | end | ||
| 22 | |||
| 23 | test "show with unknown tag returns empty collection" do | ||
| 24 | get :show, params: { id: 'nonexistent_tag_xyz', locale: 'de' } | ||
| 25 | assert_response :success | ||
| 26 | assert assigns(:pages).empty? | ||
| 27 | end | ||
| 28 | |||
| 29 | test "show with invalid tag characters returns 400" do | ||
| 30 | get :show, params: { id: '<script>alert(1)</script>', locale: 'de' } | ||
| 31 | assert_response 400 | ||
| 32 | end | ||
| 33 | |||
| 34 | end | ||
diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..5cd5ad4 --- /dev/null +++ b/test/controllers/users_controller_test.rb | |||
| @@ -0,0 +1,186 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class UsersControllerTest < ActionController::TestCase | ||
| 4 | |||
| 5 | test "get index as regular user renders stripped partial" do | ||
| 6 | login_as :quentin | ||
| 7 | get :index | ||
| 8 | assert_response :success | ||
| 9 | assert_select "a", { :count => 0, :text => "Destroy" } | ||
| 10 | end | ||
| 11 | |||
| 12 | test "get index as admin user renders admin partial" do | ||
| 13 | login_as :aaron | ||
| 14 | get :index | ||
| 15 | assert_response :success | ||
| 16 | assert_select "button[type=submit]", "destroy" | ||
| 17 | assert_select "a", "show" | ||
| 18 | end | ||
| 19 | |||
| 20 | test "get new when logged in as admin" do | ||
| 21 | login_as :aaron | ||
| 22 | get :new | ||
| 23 | assert_response :success | ||
| 24 | end | ||
| 25 | |||
| 26 | test "get new without being logged in as admin redirects back to index" do | ||
| 27 | login_as :quentin | ||
| 28 | get :new | ||
| 29 | assert_response :redirect | ||
| 30 | assert_redirected_to users_path | ||
| 31 | assert_equal( | ||
| 32 | "Sorry, you need to be an admin for this action", | ||
| 33 | flash[:notice] | ||
| 34 | ) | ||
| 35 | end | ||
| 36 | |||
| 37 | test "creating new users being logged in as admin" do | ||
| 38 | login_as :aaron | ||
| 39 | assert_difference "User.count", +1 do | ||
| 40 | post :create, params: { | ||
| 41 | :user => { | ||
| 42 | :login => "peter", | ||
| 43 | :email => "foo@bar.com", | ||
| 44 | :password => "xxxzzz", | ||
| 45 | :password_confirmation => "xxxzzz" | ||
| 46 | } | ||
| 47 | } | ||
| 48 | end | ||
| 49 | |||
| 50 | assert_redirected_to user_path(User.last) | ||
| 51 | assert !User.last.admin | ||
| 52 | end | ||
| 53 | |||
| 54 | test "creating new admin users being logged in as admin" do | ||
| 55 | login_as :aaron | ||
| 56 | assert_difference "User.count", +1 do | ||
| 57 | post :create, params: { | ||
| 58 | :user => { | ||
| 59 | :login => "peter", | ||
| 60 | :email => "foo@bar.com", | ||
| 61 | :password => "xxxzzz", | ||
| 62 | :password_confirmation => "xxxzzz", | ||
| 63 | :admin => true | ||
| 64 | } | ||
| 65 | } | ||
| 66 | end | ||
| 67 | |||
| 68 | assert_redirected_to user_path(User.last) | ||
| 69 | assert User.last.admin | ||
| 70 | end | ||
| 71 | |||
| 72 | test "creating new users not being logged as regular user wont work" do | ||
| 73 | login_as :quentin | ||
| 74 | assert_no_difference "User.count" do | ||
| 75 | post :create, params: { | ||
| 76 | :user => { | ||
| 77 | :login => "peter", | ||
| 78 | :email => "foo@bar.com", | ||
| 79 | :password => "xxxzzz", | ||
| 80 | :password_confirmation => "xxxzzz" | ||
| 81 | } | ||
| 82 | } | ||
| 83 | end | ||
| 84 | |||
| 85 | assert_redirected_to users_path | ||
| 86 | assert_equal( | ||
| 87 | "Sorry, you need to be an admin for this action", | ||
| 88 | flash[:notice] | ||
| 89 | ) | ||
| 90 | end | ||
| 91 | |||
| 92 | test "get edit of another user being logged in as regular user wont work" do | ||
| 93 | login_as :quentin | ||
| 94 | get :edit, params: { :id => User.find_by_login("aaron").id } | ||
| 95 | assert_redirected_to users_path | ||
| 96 | assert_equal( | ||
| 97 | "Sorry, you need to be an admin for this action", | ||
| 98 | flash[:notice] | ||
| 99 | ) | ||
| 100 | end | ||
| 101 | |||
| 102 | test "get edit of another user being logged in as admin user" do | ||
| 103 | login_as :aaron | ||
| 104 | get :edit, params: { :id => User.find_by_login("quentin").id } | ||
| 105 | assert_response :success | ||
| 106 | end | ||
| 107 | |||
| 108 | test "editing own user details is allowed" do | ||
| 109 | login_as :quentin | ||
| 110 | get :edit, params: { :id => User.find_by_login("quentin").id } | ||
| 111 | assert_response :success | ||
| 112 | end | ||
| 113 | |||
| 114 | test "updating an user when being logged in as regular user wont work" do | ||
| 115 | user = User.find_by_login("aaron") | ||
| 116 | login_as :quentin | ||
| 117 | put :update, params: { :id => user.id, :user => {:login => "random"} } | ||
| 118 | assert_redirected_to users_path | ||
| 119 | assert_equal( | ||
| 120 | "Sorry, you need to be an admin for this action", | ||
| 121 | flash[:notice] | ||
| 122 | ) | ||
| 123 | end | ||
| 124 | |||
| 125 | test "updating an user when being login in as admin user" do | ||
| 126 | user = User.find_by_login("quentin") | ||
| 127 | login_as :aaron | ||
| 128 | put :update, params: { :id => user.id, :user => {:login => "random"} } | ||
| 129 | assert_redirected_to user_path(user) | ||
| 130 | assert_equal "random", user.reload.login | ||
| 131 | end | ||
| 132 | |||
| 133 | test "updating own user details is allowd" do | ||
| 134 | user = User.find_by_login("quentin") | ||
| 135 | login_as :quentin | ||
| 136 | put :update, params: { :id => user.id, :user => {:login => "random"} } | ||
| 137 | assert_redirected_to user_path(user) | ||
| 138 | assert_equal "random", user.reload.login | ||
| 139 | end | ||
| 140 | |||
| 141 | test "showing a user" do | ||
| 142 | login_as :quentin | ||
| 143 | get :show, params: { :id => User.find_by_login("aaron").id } | ||
| 144 | assert_response :success | ||
| 145 | end | ||
| 146 | |||
| 147 | test "destroying an user being logged in as regular user wont work" do | ||
| 148 | login_as :quentin | ||
| 149 | assert_no_difference "User.count" do | ||
| 150 | delete :destroy, params: { :id => User.find_by_login("aaron").id } | ||
| 151 | end | ||
| 152 | assert_redirected_to users_path | ||
| 153 | assert_equal( | ||
| 154 | "Sorry, you need to be an admin for this action", | ||
| 155 | flash[:notice] | ||
| 156 | ) | ||
| 157 | end | ||
| 158 | |||
| 159 | test "destroying an user being logged in as admin user" do | ||
| 160 | login_as :aaron | ||
| 161 | assert_difference "User.count", -1 do | ||
| 162 | delete :destroy, params: { :id => User.find_by_login("quentin").id } | ||
| 163 | end | ||
| 164 | assert_redirected_to users_path | ||
| 165 | end | ||
| 166 | |||
| 167 | test "admin user can promote regular users to admins" do | ||
| 168 | login_as :aaron | ||
| 169 | user = users(:quentin) | ||
| 170 | put :update, params: { :id => user.id, :user => {:admin => true} } | ||
| 171 | |||
| 172 | user.reload | ||
| 173 | assert_equal true, user.is_admin? | ||
| 174 | end | ||
| 175 | |||
| 176 | test "regular users cannot promote themselves to admins" do | ||
| 177 | login_as :quentin | ||
| 178 | user = users(:quentin) | ||
| 179 | put :update, params: { :id => user.id, :user => {:admin => true} } | ||
| 180 | |||
| 181 | user.reload | ||
| 182 | assert_equal false, user.is_admin? | ||
| 183 | end | ||
| 184 | |||
| 185 | |||
| 186 | end | ||
