diff --git a/Gemfile b/Gemfile index c9000af5205adb6928681a2875af3ad5a221ebad..17d57a4b9ee0be3a214f03e7ba78cc242fffb6e1 100644 --- a/Gemfile +++ b/Gemfile @@ -201,11 +201,6 @@ group :test, :ci do gem 'faker', '~> 1.0.0' gem 'minitest', require: false - gem 'mocha', '~> 1.1', require: false - # - # mocha note: mocha must be loaded after the things it needs to patch. - # so, we skip the 'require' here, and do it later. - # ## ## INTEGRATION TESTS diff --git a/Gemfile.lock b/Gemfile.lock index a90d576221315602f46f4497ba39d9ec4807163b..2eb3998b40d6b794b8bdab483f108ca4fab5fc04 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -136,7 +136,6 @@ GEM mail-gpg (0.3.1) gpgme (~> 2.0, >= 2.0.2) mail (~> 2.5, >= 2.5.3) - metaclass (0.0.4) middleware (0.1.0) mime-types (3.1) mime-types-data (~> 3.2015) @@ -144,8 +143,6 @@ GEM mini_mime (1.0.0) mini_portile2 (2.3.0) minitest (5.10.3) - mocha (1.2.1) - metaclass (~> 0.0.1) mysql2 (0.3.21) nokogiri (1.8.1) mini_portile2 (~> 2.3.0) @@ -274,7 +271,6 @@ DEPENDENCIES mail-gpg mime-types minitest - mocha (~> 1.1) mysql2 (~> 0.3.18) phantomjs-binaries (~> 2.1.1) poltergeist (~> 1.5) diff --git a/extensions/pages/asset_page/test/unit/asset_page_test.rb b/extensions/pages/asset_page/test/unit/asset_page_test.rb index 0a374d437b89b03000fe19a998535b5faaa65dc5..f828dbf58bdf6c31494795bc82769f18e3f94fac 100644 --- a/extensions/pages/asset_page/test/unit/asset_page_test.rb +++ b/extensions/pages/asset_page/test/unit/asset_page_test.rb @@ -1,6 +1,7 @@ require 'test_helper' class AssetPageTest < ActiveSupport::TestCase + # fixes fixture_file_upload for Rails 2.3 include ActionDispatch::TestProcess @@ -20,20 +21,20 @@ class AssetPageTest < ActiveSupport::TestCase assert_equal @asset, @page.data @asset.reload assert @asset.page_terms - assert '1', @page.data.page_terms.media + assert "1", @page.data.page_terms.media end def test_asset_page_access assert File.exist?(@asset.private_filename) assert !File.exist?(@asset.public_filename), - format('public file "%s" should NOT exist', @asset.public_filename) + 'public file "%s" should NOT exist' % @asset.public_filename end def test_asset_page_public_access @page.public = true @page.save assert File.exist?(@asset.public_filename), - format('public file "%s" not created when page became public', @asset.public_filename) + 'public file "%s" not created when page became public' % @asset.public_filename end def test_asset_page_unpublished @@ -42,13 +43,16 @@ class AssetPageTest < ActiveSupport::TestCase @page.public = false @page.save assert !File.exist?(@asset.public_filename), - format('public file "%s" still present after page was hidden', @asset.public_filename) + 'public file "%s" still present after page was hidden' % @asset.public_filename end def test_symlinks_untouched_on_unrelated_updates - @asset.expects(:update_access).never + def @asset.update_access + raise Minitest::Assertion, 'update_access should not get called...' + end @page.title = 'new title' @page.save + assert_equal 'new title', @page.reload.title end protected diff --git a/test/functional/assets_controller_test.rb b/test/functional/assets_controller_test.rb index 606a3862ef4905a0b942e862f57fd434ce4e5380..4197cbdd0bf3fd2ceadd8b61542d26f774854206 100644 --- a/test/functional/assets_controller_test.rb +++ b/test/functional/assets_controller_test.rb @@ -1,19 +1,31 @@ -require_relative 'test_helper' +require 'test_helper' class AssetsControllerTest < ActionController::TestCase + include AssetTestHelper + + def setup + super + setup_assets + end + + def teardown + teardown_assets + super + end + def test_get_permissions - image_assets_are_private - asset = FactoryBot.create :image_asset + page = FactoryBot.create :page + asset = FactoryBot.create :image_asset, parent_page: page assert_permission_denied do get :show, id: asset.id, path: asset.basename end end def test_get_with_escaped_chars - image_assets_are_private - @controller.stubs(:authorized?).returns(true) asset = FactoryBot.create :image_asset get :show, id: asset.id, path: asset.basename + '\xF3' + assert_response :redirect + get :show, id: asset.id, path: asset.basename + '\xF3' assert_response :success end @@ -30,13 +42,11 @@ class AssetsControllerTest < ActionController::TestCase end def test_thumbnail_get - image_assets_are_private asset = FactoryBot.create :image_asset - @controller.stubs(:authorized?).returns(true) - @controller.expects(:private_filename).returns(asset.private_filename) - get :show, id: asset.id, path: asset.basename - @controller.expects(:private_filename).returns(thumbnail(asset.private_filename)) get :show, id: asset.id, path: thumbnail(asset.basename) + assert_response :redirect + get :show, id: asset.id, path: thumbnail(asset.basename) + assert_response :success end def test_destroy @@ -57,7 +67,4 @@ class AssetsControllerTest < ActionController::TestCase path.sub(/#{ext}$/, "_small#{ext}") end - def image_assets_are_private - Asset::Image.any_instance.stubs(:public?).returns(false) - end end diff --git a/test/functional/me/destroys_controller_test.rb b/test/functional/me/destroys_controller_test.rb index e244398481f9c074d978279dd063248c887bc9ae..b077d5368bce826243f89d1a3428ea80556c2e44 100644 --- a/test/functional/me/destroys_controller_test.rb +++ b/test/functional/me/destroys_controller_test.rb @@ -27,17 +27,19 @@ class Me::DestroysControllerTest < ActionController::TestCase end def test_notification - notification_mock(:user_destroyed, username: @user.name) - .expects(:create_notices_for) - .with(@user.friends) - - login_as @user - post :update, scrub_name: 1 + expecting_notifications :user_destroyed, to: @user.friends do + login_as @user + post :update, scrub_name: 1 + end end - def notification_mock(*args) - mock('notification').tap do |mock| - Notification.expects(:new).with(*args).returns mock - end + def expecting_notifications(event, to:, &block) + notification_mock = Minitest::Mock.new + notification_mock.expect :create_notices_for, nil, [to] + method_mock = Minitest::Mock.new + method_mock.expect :call, notification_mock, [event, Hash] + Notification.stub :new, method_mock, &block + method_mock.verify + notification_mock.verify end end diff --git a/test/functional/me/pages_controller_test.rb b/test/functional/me/pages_controller_test.rb index 61f576c7dc13e1066b63e8471508620cd6e6327f..d9854e20c8cbc7d640aaac56d80b8b0801ffb4e1 100644 --- a/test/functional/me/pages_controller_test.rb +++ b/test/functional/me/pages_controller_test.rb @@ -44,9 +44,10 @@ class Me::PagesControllerTest < ActionController::TestCase title = 'VeryLongTitleWithNoSpaceThatWillBeFarTooLongToFitIntoTheTableColumnAndInTurnBreakTheLayoutUnlessItIsBrokenUsingHiddenHyphens' expected = json_escape('VeryLongTitleWithNoS­paceThatWillBeFarToo­LongToFitIntoTheTabl­eColumnAndInTurnBrea­kTheLayoutUnlessItIs­BrokenUsingHiddenHyp­hens') page = FactoryBot.build :wiki_page, title: title, owner: users(:blue) - Page.expects(:paginate_by_path).returns([page]) - login_as users(:blue) - xhr :get, :index + Page.stub :paginate_by_path, [page] do + login_as users(:blue) + xhr :get, :index + end assert_response :success assert assigns(:pages).include?(page) assert response.body.include?(expected), "Expected #{response.body} to include #{expected}." diff --git a/test/integration/asset_media_test.rb b/test/integration/asset_media_test.rb index f4f314b89c3d762940a72ecaa660fa8534e85f00..dd16555f1bb045f6b7540714371412a9bab6460c 100644 --- a/test/integration/asset_media_test.rb +++ b/test/integration/asset_media_test.rb @@ -146,7 +146,8 @@ class Asset_Media_Test < ActiveSupport::TestCase def test_binary asset = Asset.create_from_params uploaded_data: upload_data('raw_file.bin') assert_equal Asset, asset.class, 'asset should be an Asset' - assert_equal 'Asset', asset.versions.earliest.versioned_type, 'version should by of type Asset' + assert_equal 'Asset', asset.versions.earliest.versioned_type, + 'version should by of type Asset' end def test_failure_on_corrupted_file @@ -157,28 +158,6 @@ class Asset_Media_Test < ActiveSupport::TestCase end end - def test_failure - failing = mock - failing.stubs(:run).returns false - transmogrifier_for(input_type: 'image/jpeg').times(5).returns failing - asset = Asset.create_from_params uploaded_data: upload_data('photo.jpg') - asset.generate_thumbnails - asset.thumbnails.each do |thumb| - assert_equal true, thumb.failure?, 'generating the thumbnail should have failed' - end - end - - def test_success - failing = mock - failing.stubs(:run).returns true - transmogrifier_for(input_type: 'image/jpeg').times(5).returns failing - asset = Asset.create_from_params uploaded_data: upload_data('photo.jpg') - asset.generate_thumbnails - asset.thumbnails.each do |thumb| - assert_equal true, thumb.failure?, 'generating the thumbnail should have failed' - end - end - # we currently do not have a xcf transmogrifier def test_no_thumbs_for_xcf asset = Asset.create_from_params uploaded_data: upload_data('image.xcf') diff --git a/test/integration/visibility_test.rb b/test/integration/visibility_test.rb index 07f78f90a25ab951f36a63fdaf747dfb79ce2d9a..8b37d84a0de603437cb13143a67e9a3419673a3d 100644 --- a/test/integration/visibility_test.rb +++ b/test/integration/visibility_test.rb @@ -1,4 +1,4 @@ -require_relative '../integration_test' +require 'integration_test' class VisibilityTest < IntegrationTest def test_hidden_is_visible_to_self @@ -42,7 +42,7 @@ class VisibilityTest < IntegrationTest visit "/#{blocking_user.login}" assert_no_content "Send Message" assert_no_content "Add To My Contacts" - end + end end def test_visible_to_friends_by_default diff --git a/test/test_helper.rb b/test/test_helper.rb index 7453d2abf1b1f2db4bd818264bf17e2f16307260..bde1b4f83439cd35721cb134ff93b53f3a013149 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -57,12 +57,6 @@ end ## some special rules for integration tests ## -# -# mocha must be required last. -# the libraries that it patches must be loaded before it is. -# -require 'mocha/mini_test' - # ActiveSupport::HashWithIndifferentAccess#convert_value calls 'class' # and 'is_a?' on all values. This happens when assembling 'assigns' in # tests. diff --git a/test/unit/asset/text_test.rb b/test/unit/asset/text_test.rb index b6225eeffeb6ad9a6d0b0533f3d9ebaa937bb16d..38f75b316688316d25947aac160bedae5be593b6 100644 --- a/test/unit/asset/text_test.rb +++ b/test/unit/asset/text_test.rb @@ -14,9 +14,8 @@ class Asset::TextTest < ActiveSupport::TestCase end def test_text_asset_creates_thumbnails - @asset = FactoryBot.build :word_asset - @asset.expects :create_thumbnail_records - @asset.save + @asset = FactoryBot.create :word_asset + assert !@asset.thumbnails.count.zero?, 'should create thumbnails' end def test_only_creates_available_thumbnails diff --git a/test/unit/asset_test.rb b/test/unit/asset_test.rb index a35d838c0e8ce3d6a3fad743380e62f83038ff4b..6ed03007e57fdd0da43642353427533dcaaebfc9 100644 --- a/test/unit/asset_test.rb +++ b/test/unit/asset_test.rb @@ -66,12 +66,6 @@ class AssetTest < ActiveSupport::TestCase 'public file should not exist' end - def test_thumbnail_generation_handled_by_thumbnails - @asset = FactoryBot.create :image_asset - @asset.thumbnails.each { |thumb| thumb.expects(:generate) } - @asset.generate_thumbnails - end - def test_build_asset asset = Asset.build(uploaded_data: upload_data('photo.jpg')) asset.valid? # running validations will load metadata diff --git a/test/unit/page/history/grant_group_access_test.rb b/test/unit/page/history/grant_group_access_test.rb index 5fc40c06590888f0aa5d9dab8e4d6e36264a9b0e..a78fc99810e7d79ca7030a672f4815d3f16780d6 100644 --- a/test/unit/page/history/grant_group_access_test.rb +++ b/test/unit/page/history/grant_group_access_test.rb @@ -7,24 +7,11 @@ class Page::History::GrantGroupAccessTest < ActiveSupport::TestCase history.description_key end - def test_translation_key_with_access - part_stub = stub access_sym: :admin, group: nil - history = Page::History::GrantGroupAccess.new(participation: part_stub) - assert_equal 'page_history_granted_group_full_access', - history.description_key - end - - def test_translation_key_without_access - history = Page::History::GrantGroupAccess.new - assert_equal 'page_history_granted_group_access', - history.description_key - end - def test_group_from_participation - group = Group.new(full_name: 'Trees') - part_stub = stub group: group, access_sym: nil - history = Page::History::GrantGroupAccess.new(participation: part_stub) - assert_equal group, history.item + history = Page::History::GrantGroupAccess.new participation: part_stub + assert_equal 'page_history_granted_group_write_access', + history.description_key + assert_equal part_stub.group, history.item assert_description_params history, user_name: 'Unknown/Deleted', item_name: 'Trees' @@ -33,4 +20,15 @@ class Page::History::GrantGroupAccessTest < ActiveSupport::TestCase def assert_description_params(history, params) assert_equal params, history.description_params end + + def part_stub + @part_stub ||= Object.new.tap do |part| + def part.group + @group ||= Group.new(full_name: 'Trees') + end + def part.access_sym + :edit + end + end + end end diff --git a/test/unit/request_model_test.rb b/test/unit/request_model_test.rb index c92a2acfebccfc8c7b4f82cb35a86d8b87283916..23acd93685081e1667cdfa58a5b7613b29155c10 100644 --- a/test/unit/request_model_test.rb +++ b/test/unit/request_model_test.rb @@ -1,42 +1,57 @@ require 'test_helper' - +# +# Request Model Test +# +# Request is an abstract class. We only use subclasses of it. +# This test aims at testing the abstract class - in particular +# to make sure permissions are checked. +# class RequestModelTest < ActiveSupport::TestCase - def test_create_checks_permissions - user = User.new - request = Request.new - request.stubs(:created_by).returns(user) - request.expects(:may_create?).with(user).returns(false) + def test_create_checks_permission + request = Request.new created_by: User.new assert !request.save + assert_includes request.errors[:base], + 'Permission Denied' end def test_mark_as_destroy_checks_permission - user = User.new - request = Request.new - request.stubs(:new_record?).returns(false) + request = Request.new created_by: User.new assert_raises PermissionDenied do - request.expects(:may_destroy?).with(user).returns(false) - request.mark! :destroy, user + request.mark! :destroy, User.new end end def test_mark_as_approved_checks_permission - user = User.new - request = Request.new state: 'pending' - request.stubs(:new_record?).returns(false) + user = FactoryBot.create :user + request = RequestICanCreate.create created_by: user, + recipient: user, + requestable: user assert_raises PermissionDenied do - request.expects(:may_approve?).with(user).returns(false) - request.mark! :approve, user + request.mark! :approve, User.new end end def test_mark_as_rejected_checks_permission - user = User.new - request = Request.new state: 'pending' - request = Request.new state: 'pending' - request.stubs(:new_record?).returns(false) + user = FactoryBot.create :user + request = RequestICanCreate.create created_by: user, + recipient: user, + requestable: user assert_raises PermissionDenied do - request.expects(:may_approve?).with(user).returns(false) - request.mark! :reject, user + request.mark! :reject, User.new + end + end + + # + # The Request base class does not allow creating requests. + # + # Subclassed need to overwrite the permissions. For some tests we + # need a persisted request - mostly because of checks against + # :new_record? in request.mark!. That's what we use this class for. + # + class RequestICanCreate < Request + protected + def may_create?(_user) + true end end end diff --git a/test/unit/request_test.rb b/test/unit/request_test.rb index 8149f7344a9d569d3c43050639b53680ff0b53f7..5a242f7b346d377d22af404216dfb73589b01acf 100644 --- a/test/unit/request_test.rb +++ b/test/unit/request_test.rb @@ -193,14 +193,20 @@ class RequestTest < ActiveSupport::TestCase end def test_success_flash_messages + rec_stub = Object.new.tap do |rec| + def rec.display_name + 'New Member' + end + end request = RequestToJoinUs.new - request.stubs(:recipient).returns(stub(display_name: 'New Member')) - assert_equal 'Invitation to Join was sent to New Member.', - request.flash_message(count: 1)[:text] - assert_equal '3 Invitations to Join were sent.', - request.flash_message(count: 3)[:text] - assert_equal '0 Invitations to Join were sent.', - request.flash_message(count: 0)[:text] + request.stub(:recipient, rec_stub) do + assert_equal 'Invitation to Join was sent to New Member.', + request.flash_message(count: 1)[:text] + assert_equal '3 Invitations to Join were sent.', + request.flash_message(count: 3)[:text] + assert_equal '0 Invitations to Join were sent.', + request.flash_message(count: 0)[:text] + end end # both invites to and from a group are visible to its members diff --git a/test/unit/requests/request_to_remove_user_test.rb b/test/unit/requests/request_to_remove_user_test.rb index 8d9dc0db95b735ee8c2e6fcd799a07deb148a74f..dd1053667e6a0e6994855418d2f35ef470afdb5e 100644 --- a/test/unit/requests/request_to_remove_user_test.rb +++ b/test/unit/requests/request_to_remove_user_test.rb @@ -12,7 +12,9 @@ class RequestToRemoveUserTest < ActiveSupport::TestCase end def test_remove_request_fails - @requester.expects(:longterm_member_of?).with(@group).returns(false) + def @requester.longterm_member_of?(group) + false + end assert_raises ActiveRecord::RecordInvalid, 'Permission Denied' do @request = RequestToRemoveUser.create! created_by: @requester, group: @group, user: @user end @@ -20,20 +22,29 @@ class RequestToRemoveUserTest < ActiveSupport::TestCase end def test_remove_succeeds - @requester.expects(:longterm_member_of?).with(@group).returns(true) - @request = RequestToRemoveUser.create! created_by: @requester, group: @group, user: @user + def @requester.longterm_member_of?(group) + true + end @approver = users(:green) - @approver.expects(:longterm_member_of?).with(@group).returns(true) + def @approver.longterm_member_of?(group) + true + end + @request = RequestToRemoveUser.create! created_by: @requester, group: @group, user: @user @request.approve_by!(@approver) assert_equal 'approved', @request.state, 'state should change' assert_removed end def test_remove_fails - @requester.expects(:longterm_member_of?).with(@group).returns(true) + def @requester.longterm_member_of?(group) + true + end + @approver = users(:green) + def @approver.longterm_member_of?(group) + false + end @request = RequestToRemoveUser.create! created_by: @requester, group: @group, user: @user @approver = users(:green) - @approver.expects(:longterm_member_of?).with(@group).returns(false) assert_raises PermissionDenied do @request.approve_by!(@approver) end diff --git a/test/unit/tracking/action_test.rb b/test/unit/tracking/action_test.rb index 1af917f073db61ddef2de61659b799e1e2c7fb09..8bb4366235d79a5a9e11a3d2653de7e420d848bf 100644 --- a/test/unit/tracking/action_test.rb +++ b/test/unit/tracking/action_test.rb @@ -2,28 +2,38 @@ require 'test_helper' class Tracking::ActionTest < ActiveSupport::TestCase def test_class_lookup - Activity::Friend.expects(:create!) - Tracking::Action.track :create_friendship + expecting_creation_of Activity::Friend do + Tracking::Action.track :create_friendship + end end - def test_key_seed - Activity::Friend.expects(:create!).with(has_key(:key)) - Tracking::Action.track :create_friendship + def test_args + expecting_creation_of Activity::Friend, with: [:key, :user] do + Tracking::Action.track :create_friendship, user: :dummy, dummy: :user + end end - def test_hand_over_args - Activity::Friend.expects(:create!).with(has_key(:user)) - Tracking::Action.track :create_friendship, user: :dummy + def test_create_multiple_records + expecting_creation_of Activity::GroupCreated do + expecting_creation_of Activity::UserCreatedGroup do + Tracking::Action.track :create_group + end + end end - def test_filter_args - Activity::Friend.expects(:create!).with(Not(has_key(:dummy))) - Tracking::Action.track :create_friendship, dummy: :user - end + protected - def test_create_multiple_records - Activity::GroupCreated.expects(:create!) - Activity::UserCreatedGroup.expects(:create!) - Tracking::Action.track :create_group + # okay... this is a bit too fancy. No idea how to simplify. + # calling this makes klass expect create! to be called. + # + # with: keys of the hash expected to be handed to create! + # only checked if present + def expecting_creation_of(klass, with: nil, &block) + method_mock = Minitest::Mock.new + method_mock.expect :call, nil do |args_hash| + !with || with.sort == args_hash.keys.sort + end + klass.stub :create!, method_mock, &block + method_mock.verify end end diff --git a/test/unit/wiki/decorator_test.rb b/test/unit/wiki/decorator_test.rb index df21940f877d43ec23e9ae7e5b0bff859c2218ec..26d68c0c3184e9010b2a24f892dc600df22fe49e 100644 --- a/test/unit/wiki/decorator_test.rb +++ b/test/unit/wiki/decorator_test.rb @@ -33,8 +33,15 @@ class Wiki::DecoratorTest < ActiveSupport::TestCase end def dummy_view - stub edit_wiki_section_link: '<a>edit</a>', - div_for: '<div></div>' + Object.new.tap do |view| + def view.edit_wiki_section_link(*args) + '<a>edit</a>' + end + + def view.div_for(*args) + '<div></div>' + end + end end def wiki_with_outside_content