From cb05cd49144141f5bed1d1eb3602efad7c65dbdc Mon Sep 17 00:00:00 2001 From: Maciej Mensfeld Date: Tue, 5 Sep 2023 16:27:52 +0200 Subject: [PATCH] specs and cleanup --- lib/karafka/web.rb | 2 +- lib/karafka/web/app.rb | 8 +++-- lib/karafka/web/ui/base.rb | 10 +++---- .../web/ui/pro/controllers/messages_spec.rb | 30 ++++++++++++++++++- spec/spec_helper.rb | 3 ++ 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/lib/karafka/web.rb b/lib/karafka/web.rb index 0f48a9f3..102b7804 100644 --- a/lib/karafka/web.rb +++ b/lib/karafka/web.rb @@ -41,7 +41,7 @@ def enable! # Inject correct settings for the Web-UI sessions plugin based on the user configuration # We cannot configure this automatically like other Roda plugins because it requires safe # custom values provided by our user - Ui::Base.plugin(:sessions, **config.ui.sessions.to_h) + App.engine.plugin(:sessions, **config.ui.sessions.to_h) end end end diff --git a/lib/karafka/web/app.rb b/lib/karafka/web/app.rb index 6342c5ee..7acbbb02 100644 --- a/lib/karafka/web/app.rb +++ b/lib/karafka/web/app.rb @@ -8,8 +8,12 @@ class << self # @param env [Hash] Rack env # @param block [Proc] Rack block def call(env, &block) - handler = Karafka.pro? ? Ui::Pro::App : Ui::App - handler.call(env, &block) + engine.call(env, &block) + end + + # @return [Class] regular or pro Web engine + def engine + ::Karafka.pro? ? Ui::Pro::App : Ui::App end end end diff --git a/lib/karafka/web/ui/base.rb b/lib/karafka/web/ui/base.rb index f7eed60a..7e360b1c 100644 --- a/lib/karafka/web/ui/base.rb +++ b/lib/karafka/web/ui/base.rb @@ -20,6 +20,11 @@ class Base < Roda ) plugin :render_each plugin :partials + # The secret here will be reconfigured after Web UI configuration setup + # This is why we assign here a random value as it will have to be changed by the end + # user to make the Web UI work. + plugin :sessions, key: '_karafka_session', secret: SecureRandom.hex(64) + plugin :route_csrf end plugin :render, escape: true, engine: 'erb' @@ -29,11 +34,6 @@ class Base < Roda plugin :hooks plugin :flash plugin :path - # The secret here will be reconfigured after Web UI configuration setup - # This is why we assign here a random value as it will have to be changed by the end - # user to make the Web UI work. - plugin :sessions, key: '_karafka_session', secret: SecureRandom.hex(64) - plugin :route_csrf # Based on # https://github.com/sidekiq/sidekiq/blob/ae6ca119/lib/sidekiq/web/application.rb#L8 diff --git a/spec/lib/karafka/web/ui/pro/controllers/messages_spec.rb b/spec/lib/karafka/web/ui/pro/controllers/messages_spec.rb index bdefc7e0..32ddd15f 100644 --- a/spec/lib/karafka/web/ui/pro/controllers/messages_spec.rb +++ b/spec/lib/karafka/web/ui/pro/controllers/messages_spec.rb @@ -3,5 +3,33 @@ RSpec.describe_current do subject(:app) { Karafka::Web::Ui::Pro::App } - pending + let(:topic) { create_topic } + + describe '#republish' do + context 'when we want to republish message from a non-existing topic' do + before { post 'messages/non-existing/0/1/republish' } + + it do + expect(response).not_to be_ok + expect(response.status).to eq(404) + end + end + + context 'when message exists' do + let(:republished) { Karafka::Web::Ui::Models::Message.find(topic, 0, 1) } + let(:payload) { rand.to_s } + + before do + produce(topic, payload) + post "messages/#{topic}/0/0/republish" + end + + it do + expect(response.status).to eq(302) + # Taken fro referer and referer is nil in specs + expect(response.location).to eq(nil) + expect(republished.raw_payload).to eq(payload) + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8768ee0a..e6844ea8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -117,3 +117,6 @@ class App produce(TOPICS[2], fixtures_file('consumer_report.json')) Karafka::Web.enable! + +# Disable CSRF checks for RSpec +Karafka::Web::App.engine.plugin(:route_csrf, check_request_methods: [])