Skip to content

Commit

Permalink
specs and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mensfeld committed Sep 5, 2023
1 parent 462b655 commit cb05cd4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/karafka/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/karafka/web/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/karafka/web/ui/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
30 changes: 29 additions & 1 deletion spec/lib/karafka/web/ui/pro/controllers/messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: [])

0 comments on commit cb05cd4

Please sign in to comment.