1

My sidekiq gui needs to authenticate a user for admin rights before allowing access.

Is there a way to password protect and test the sidekiq gui controller directly? Instead of a feature test and instead of needing redis or mock redis?

Routes

  if Rails.env.development?
    mount Sidekiq::Web, at: :sidekiq
  else
    # https://stackoverflow.com/questions/12265421/how-can-i-password-protect-my-sidekiq-route-i-e-require-authentication-for-th
    authenticate :user, ->(u) { u.admin? } do
      mount Sidekiq::Web => "/sidekiq"
    end
  end

RSPEC

# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Sidekiq::Web", type: :feature do
  context "with admin user" do
    let(:user) { create(:user, :admin) }

    before { sign_in(user) }

    specify "can able to access sidekiq GUI" do
      visit sidekiq_web_path

      within "div#navbar-menu" do
        expect(page).to have_link("Dashboard")
      end
    end
  end

  context "with normal user" do
    let(:user) { create(:user) }

    before { sign_in(user) }

    it "can not able to access sidekiq GUI" do
      expect {
        visit(sidekiq_web_path)
        expect(page).to have_link("Dashboard")
      }.to raise_error(ActionController::RoutingError)
    end
  end
end

Test Results:

Failures:

  1) Sidekiq::Web with admin user can able to access sidekiq GUI

     Failure/Error: raise CannotConnectError, "Error connecting to Redis on #{location} (#{error.class})"

     Redis::CannotConnectError:

       Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)

     # /app/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:362:in `rescue in establish_connection'

     # /app/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:343:in `establish_connection'

     # /app/vendor/bundle/ruby/2.7.0/gems/redis-4.2.2/lib/redis/client.rb:107:in `block in connect'
user2012677
  • 5,465
  • 6
  • 51
  • 113

1 Answers1

0

What is causing the errors on Heroku? Tests wouldn't run on Heroku so not sure how it is related to his issue.

Are you testing the built-in Sidekiq Web dashboard? This should already be tested, so not sure if a test is needed.

What is the test environment? Why doesn't it have Redis?

One way to work around the issue which seems to match your use case is https://github.com/sds/mock_redis .

B Seven
  • 44,484
  • 66
  • 240
  • 385
  • I am testing the authentication of the mounted sidekiq. On CircleCI I needed to add Redis to my testing environment, but all I really want to do is verify that Sidekiq is not exposed to the public, I was hoping a simple route test would do, but would not figure it out. any recommendaitons to have simple routes test? – user2012677 Feb 19 '22 at 18:03
  • Tests need redis in test environment to run in heroku. – user2012677 Feb 19 '22 at 18:24
  • You are running tests on Heroku? – B Seven Feb 19 '22 at 18:46
  • Yes, all rspec test get run before deployment on Heroku, I also do it locally and CircleCI. – user2012677 Feb 19 '22 at 19:36
  • I don't think the tests would run on Heroku... – B Seven Feb 19 '22 at 19:39
  • I added redis to my test environment, and it does work, but It's not ideal. I would really like to find another solution just testing the route only. Not a full feature test. – user2012677 Feb 19 '22 at 21:47
  • I gather that by testing the route, you mean a controller spec. I guess it's possible that it may not require Redis... – B Seven Feb 20 '22 at 01:05