From 00d06c3f9595fd4fd77706adda650483b4c0ea70 Mon Sep 17 00:00:00 2001 From: Kenji Okimoto Date: Mon, 30 Jul 2018 12:42:08 +0900 Subject: [PATCH] Add test/integration/dashboard_test.rb Signed-off-by: Kenji Okimoto --- test/integration/dashboard_test.rb | 48 ++++++++++++++++++++++++++++++ test/support/login_macro.rb | 12 ++++++++ test/support/stub_daemon.rb | 13 ++++++++ test/test_helper.rb | 20 +++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 test/integration/dashboard_test.rb create mode 100644 test/support/login_macro.rb create mode 100644 test/support/stub_daemon.rb diff --git a/test/integration/dashboard_test.rb b/test/integration/dashboard_test.rb new file mode 100644 index 0000000..c99dc5e --- /dev/null +++ b/test/integration/dashboard_test.rb @@ -0,0 +1,48 @@ +require "test_helper" + +class DashboardTest < ActionDispatch::IntegrationTest + setup do + login_with(FactoryBot.build(:user)) + end + + test "no configuration" do + visit("/") + within("h1") do + assert_equal("fluentd", text) + end + assert do + find_link(I18n.t('terms.setup', target: 'fluentd')) + end + assert do + find_link(I18n.t('terms.setup', target: 'td-agent')) + end + end + + test "fluentd is stop" do + stub_daemon + visit("/") + assert do + page.has_css?('h1', text: I18n.t('fluentd.show.page_title')) + end + assert do + page.has_css?('h4', text: I18n.t('fluentd.common.stopped')) + end + assert do + page.has_css?('h4', text: I18n.t('fluentd.common.fluentd_info')) + end + end + + test "fluentd is running" do + stub_daemon(running: true) + visit("/") + assert do + page.has_css?('h1', text: I18n.t('fluentd.show.page_title')) + end + assert do + page.has_css?('h4', text: I18n.t('fluentd.common.running')) + end + assert do + page.has_css?('h4', text: I18n.t('fluentd.common.fluentd_info')) + end + end +end diff --git a/test/support/login_macro.rb b/test/support/login_macro.rb new file mode 100644 index 0000000..dd3fcf6 --- /dev/null +++ b/test/support/login_macro.rb @@ -0,0 +1,12 @@ +module LoginMacro + def login_with(user) + visit("/sessions/new") + within("form") do + fill_in("session_name", :with => user.name) + fill_in("session_password", :with => user.password) + end + click_button(I18n.t("terms.sign_in")) + end +end + +ActionDispatch::IntegrationTest.include(LoginMacro) diff --git a/test/support/stub_daemon.rb b/test/support/stub_daemon.rb new file mode 100644 index 0000000..25e88e2 --- /dev/null +++ b/test/support/stub_daemon.rb @@ -0,0 +1,13 @@ +module StubDaemon + def stub_daemon(running: false) + daemon = FactoryBot.build(:fluentd, variant: "td-agent") + stub(Fluentd).instance { daemon } + any_instance_of(Fluentd::Agent::TdAgent) do |object| + stub(object).detached_command { true } + stub(object).running? { running } + end + daemon.agent.config_write("") + end +end + +ActionDispatch::IntegrationTest.include(StubDaemon) diff --git a/test/test_helper.rb b/test/test_helper.rb index 99cf40d..c1bbc02 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,10 +2,12 @@ ENV['RAILS_ENV'] ||= 'test' require_relative '../config/environment' require 'test/unit/rails/test_help' require 'test/unit/rr' +require 'test/unit/capybara' require 'webmock/test_unit' WebMock.disable_net_connect!(allow_localhost: true) + module FixturePath def fixture_path(fixture_name) Rails.root.join("test/fixtures", fixture_name).to_s @@ -20,3 +22,21 @@ class ActiveSupport::TestCase include FixturePath extend FixturePath end + +Capybara.register_driver :selenium do |app| + Capybara::Selenium::Driver.new(app, + browser: :chrome, + desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome( + chrome_options: { + args: %w(headless disable-gpu window-size=1920,1080), + }, + ) + ) +end + +Capybara.javascript_driver = :selenium +require "capybara-screenshot/testunit" + +Dir[Rails.root.join("test/support/**/*.rb")].each do |path| + require(path) +end