From ce7a6b3fc6a1e5c36021429c0d337fab71993427 Mon Sep 17 00:00:00 2001 From: Serge Koba Date: Wed, 28 Mar 2018 17:10:26 +0300 Subject: [PATCH] add tests for notifications --- Gemfile.lock | 2 +- README.md | 1 - test/dummy/config/environments/test.rb | 3 + test/dummy/db/schema.rb | 6 +- test/test_helper.rb | 1 + test/unit/notification_test.rb | 80 ++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 test/unit/notification_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index 79dd206..a871295 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - boxroom (0.0.4) + boxroom (0.0.5) acts_as_tree (~> 2.7) bulma-rails (~> 0.6.2) cells-erb (~> 0.1.0) diff --git a/README.md b/README.md index bd87334..f905e89 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,6 @@ ActionMailer::Base.default from: 'Boxroom ' The engine is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). ## Roadmap: -- tests for notifications - support s3 - batch actions - tag files diff --git a/test/dummy/config/environments/test.rb b/test/dummy/config/environments/test.rb index 8e5cbde..fdbf705 100644 --- a/test/dummy/config/environments/test.rb +++ b/test/dummy/config/environments/test.rb @@ -33,10 +33,13 @@ # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test + config.active_job.queue_adapter = :inline + config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + ActionMailer::Base.default from: 'Boxroom ' end diff --git a/test/dummy/db/schema.rb b/test/dummy/db/schema.rb index 18ab786..d23062c 100644 --- a/test/dummy/db/schema.rb +++ b/test/dummy/db/schema.rb @@ -10,13 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180316203125) do +ActiveRecord::Schema.define(version: 20180323134701) do create_table "boxroom_folders", force: :cascade do |t| t.string "name" t.integer "parent_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "notify_emails" + t.boolean "notify_create" + t.boolean "notify_update" + t.boolean "notify_remove" t.index ["parent_id"], name: "index_boxroom_folders_on_parent_id" end diff --git a/test/test_helper.rb b/test/test_helper.rb index d9ca597..d9f3dff 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -31,5 +31,6 @@ def clear_root_folder def do_cleanup DatabaseCleaner.clean FileUtils.rm_rf(Dir["#{Rails.root}/#{Boxroom.configuration.uploads_path}"]) + ActionMailer::Base.deliveries.clear end end diff --git a/test/unit/notification_test.rb b/test/unit/notification_test.rb new file mode 100644 index 0000000..d5fb11e --- /dev/null +++ b/test/unit/notification_test.rb @@ -0,0 +1,80 @@ +require 'test_helper' + +class NotificationTest < ActiveSupport::TestCase + def setup + clear_root_folder + do_cleanup + end + + test 'notify file was created in folder' do + folder = create(:folder, notify_emails: 'test@test.com') + file = create(:user_file, folder: folder) + + Boxroom::UserFile::NotifyCreate.(params: {id: file.id}) + email = ActionMailer::Base.deliveries.last + assert_nil email + + folder.update_attribute :notify_create, true + + Boxroom::UserFile::NotifyCreate.(params: {id: file.id}) + + email = ActionMailer::Base.deliveries.last + + assert email != nil + assert_equal ['test@test.com'], email.to + assert_equal I18n.t(:file_added), email.subject + assert_equal true, email.body.to_s.include?(file.attachment_file_name) + assert_equal true, email.body.to_s.include?(folder.name) + end + + test 'notify file was updated in folder' do + folder = create(:folder, notify_emails: 'test@test.com') + file = create(:user_file, folder: folder) + + Boxroom::UserFile::NotifyUpdate.(params: {id: file.id}) + email = ActionMailer::Base.deliveries.last + assert_nil email + + folder.update_attribute :notify_update, true + + Boxroom::UserFile::NotifyUpdate.(params: {id: file.id}) + + email = ActionMailer::Base.deliveries.last + + assert email != nil + assert_equal ['test@test.com'], email.to + assert_equal I18n.t(:file_updated), email.subject + assert_equal true, email.body.to_s.include?(file.attachment_file_name) + assert_equal true, email.body.to_s.include?(folder.name) + end + + test 'notify file was deleted in folder' do + folder = create(:folder, notify_emails: 'test@test.com') + file = create(:user_file, folder: folder) + + Boxroom::UserFile::NotifyRemove.(params: {id: file.id}) + email = ActionMailer::Base.deliveries.last + assert_nil email + + folder.update_attribute :notify_remove, true + + Boxroom::UserFile::NotifyRemove.(params: {id: file.id}) + + email = ActionMailer::Base.deliveries.last + + assert email != nil + assert_equal ['test@test.com'], email.to + assert_equal I18n.t(:file_removed), email.subject + assert_equal true, email.body.to_s.include?(file.attachment_file_name) + assert_equal true, email.body.to_s.include?(folder.name) + end + + test 'notification sent to many recipients' do + folder = create(:folder, notify_emails: 'test@test.com, test2@test.com', notify_create: true) + file = create(:user_file, folder: folder) + + Boxroom::UserFile::NotifyCreate.(params: {id: file.id}) + + assert_equal 2, ActionMailer::Base.deliveries.size + end +end \ No newline at end of file