From f2c9ac1316a01b8d7420c17748ca9123aaa59e42 Mon Sep 17 00:00:00 2001
From: Alexander Olofsson <alexander.olofsson@liu.se>
Date: Wed, 30 Nov 2022 13:35:32 +0100
Subject: [PATCH] Do proper testing

---
 foreman_notification_send.gemspec             | 30 +++++++++----------
 .../foreman_notification_send_tasks.rake      | 18 ++++++++---
 test/foreman_notification_send_test.rb        |  7 -----
 test/test_helper.rb                           | 21 -------------
 test/test_plugin_helper.rb                    | 22 ++++++++++++++
 test/unit/notification_test.rb                | 20 +++++++++++++
 6 files changed, 71 insertions(+), 47 deletions(-)
 delete mode 100644 test/foreman_notification_send_test.rb
 delete mode 100644 test/test_helper.rb
 create mode 100644 test/test_plugin_helper.rb
 create mode 100644 test/unit/notification_test.rb

diff --git a/foreman_notification_send.gemspec b/foreman_notification_send.gemspec
index c845696..b9cd73d 100644
--- a/foreman_notification_send.gemspec
+++ b/foreman_notification_send.gemspec
@@ -2,23 +2,23 @@
 
 require File.join File.expand_path('lib', __dir__), 'foreman_notification_send/version'
 
-Gem::Specification.new do |s|
-  s.name        = 'foreman_notification_send'
-  s.version     = ForemanNotificationSend::VERSION
-  s.authors     = ['Alexander Olofsson']
-  s.email       = ['alexander.olofsson@liu.se']
+Gem::Specification.new do |spec|
+  spec.name        = 'foreman_notification_send'
+  spec.version     = ForemanNotificationSend::VERSION
+  spec.authors     = ['Alexander Olofsson']
+  spec.email       = ['alexander.olofsson@liu.se']
 
-  s.homepage    = 'https://github.com/ananace/foreman_notification_send'
-  s.summary     = 'Send Foreman notifications to external systems'
-  s.description = s.summary
-  s.license     = 'GPL-3.0'
+  spec.homepage    = 'https://github.com/ananace/foreman_notification_send'
+  spec.summary     = 'Send Foreman notifications to external systems'
+  spec.description = spec.summary
+  spec.license     = 'GPL-3.0'
 
-  s.files         = Dir['{app,config,db,lib}/**/*.{rake,rb}'] + %w[LICENSE.txt README.md]
-  s.require_paths = ['lib']
+  spec.files       = Dir['{app,config,db,lib}/**/*.{rake,rb}'] + %w[LICENSE.txt README.md]
 
-  s.add_dependency 'matrix_sdk', '~> 2.6'
+  spec.add_dependency 'matrix_sdk', '~> 2.6'
 
-  s.add_development_dependency 'bundler'
-  s.add_development_dependency 'minitest'
-  s.add_development_dependency 'rake'
+  spec.add_development_dependency 'rubocop'
+  spec.add_development_dependency 'rubocop-minitest'
+  spec.add_development_dependency 'rubocop-performance'
+  spec.add_development_dependency 'rubocop-rails'
 end
diff --git a/lib/tasks/foreman_notification_send_tasks.rake b/lib/tasks/foreman_notification_send_tasks.rake
index 7bf7736..93f3625 100644
--- a/lib/tasks/foreman_notification_send_tasks.rake
+++ b/lib/tasks/foreman_notification_send_tasks.rake
@@ -1,4 +1,14 @@
-# desc "Explaining what the task does"
-# task :foreman_notification_send do
-#   # Task goes here
-# end
+# frozen_string_literal: true
+
+namespace :test do
+  desc 'Test ForemanNotificationSend'
+  Rake::TestTask.new(:foreman_notification_send) do |t|
+    test_dir = File.join(__dir__, '../..', 'test')
+    t.libs << ['test', test_dir]
+    t.pattern = "#{test_dir}/**/*_test.rb"
+    t.verbose = true
+    t.warning = false
+  end
+end
+
+Rake::Task[:test].enhance %w[test:foreman_notification_send]
diff --git a/test/foreman_notification_send_test.rb b/test/foreman_notification_send_test.rb
deleted file mode 100644
index 7e3041d..0000000
--- a/test/foreman_notification_send_test.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-require 'test_helper'
-
-class ForemanNotificationSend::Test < ActiveSupport::TestCase
-  test "truth" do
-    assert_kind_of Module, ForemanNotificationSend
-  end
-end
diff --git a/test/test_helper.rb b/test/test_helper.rb
deleted file mode 100644
index 94fec6b..0000000
--- a/test/test_helper.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-# Configure Rails Environment
-ENV["RAILS_ENV"] = "test"
-
-require_relative "../test/dummy/config/environment"
-ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)]
-require "rails/test_help"
-
-# Filter out Minitest backtrace while allowing backtrace from other libraries
-# to be shown.
-Minitest.backtrace_filter = Minitest::BacktraceFilter.new
-
-require "rails/test_unit/reporter"
-Rails::TestUnitReporter.executable = 'bin/test'
-
-# Load fixtures from the engine
-if ActiveSupport::TestCase.respond_to?(:fixture_path=)
-  ActiveSupport::TestCase.fixture_path = File.expand_path("fixtures", __dir__)
-  ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
-  ActiveSupport::TestCase.file_fixture_path = ActiveSupport::TestCase.fixture_path + "/files"
-  ActiveSupport::TestCase.fixtures :all
-end
diff --git a/test/test_plugin_helper.rb b/test/test_plugin_helper.rb
new file mode 100644
index 0000000..8e54ab1
--- /dev/null
+++ b/test/test_plugin_helper.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+# This calls the main test_helper in Foreman-core
+require 'test_helper'
+require 'database_cleaner'
+
+# Add plugin to FactoryBot's paths
+FactoryBot.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
+FactoryBot.reload
+
+# Foreman's setup doesn't handle cleaning up for Minitest::Spec
+DatabaseCleaner.strategy = :transaction
+
+class Minitest::Spec
+  before :each do
+    DatabaseCleaner.start
+  end
+
+  after :each do
+    DatabaseCleaner.clean
+  end
+end
diff --git a/test/unit/notification_test.rb b/test/unit/notification_test.rb
new file mode 100644
index 0000000..cd41699
--- /dev/null
+++ b/test/unit/notification_test.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+require 'test_plugin_helper'
+
+class NotificationTest < ActiveSupport::TestCase
+  let(:blueprint) { NotificationBlueprint.new group: 'test', level: 'info' }
+  let(:notification) { Notification.new notification_blueprint: blueprint, message: 'example' }
+
+  context 'Handling data manipulation' do
+    test 'it should serialize to valid text' do
+      assert_equal <<~MD.strip, notification.to_markdown
+        **test**:
+        ℹ️ example
+      MD
+      assert_equal <<~HTML.strip, notification.to_html
+        <b>test</b>:<br/>ℹ️ example
+      HTML
+    end
+  end
+end
-- 
GitLab