Skip to content

Commit

Permalink
add web specs (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam authored Jan 4, 2025
1 parent b71a542 commit 3045816
Show file tree
Hide file tree
Showing 20 changed files with 727 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ GEM
rack (>= 3.0.0, < 4)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rackup (2.2.1)
rack (>= 3)
rainbow (3.1.1)
Expand Down Expand Up @@ -166,6 +168,7 @@ DEPENDENCIES
pry-byebug (~> 3.0)
puma (~> 6.5)
rack-flash3 (~> 1.0)
rack-test (~> 2.1)
rackup (~> 2.0)
rake (~> 13.0)
rerun (~> 0.0)
Expand Down
1 change: 1 addition & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require 'sidekiq'
require 'sidekiq/web'
require 'outboxer/web'

require_relative 'app/models/application_record'
require_relative 'app/models/event'

use Rack::Session::Cookie, secret: ENV['SESSION_SECRET'], same_site: true, max_age: 86400
Expand Down
5 changes: 4 additions & 1 deletion lib/outboxer/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@

module Outboxer
class Web < Sinatra::Base
enable :sessions

use Rack::Flash

set :logger, Logger.new($stdout)
set :views, File.expand_path('../web/views', __FILE__)
set :public_folder, File.expand_path('../web/public', __FILE__)
Expand Down Expand Up @@ -523,7 +526,7 @@ def normalise_query_string(status: Messages::LIST_STATUS_DEFAULT,
time_zone: denormalised_query_params[:time_zone])

result = Messages.requeue_all(
status: denormalised_query_params[:status], older_than: Time.now.utc)
status: denormalised_query_params[:status])

message_text = result[:requeued_count] == 1 ? 'message' : 'messages'
flash[:primary] = "#{result[:requeued_count]} #{message_text} have been queued"
Expand Down
1 change: 1 addition & 0 deletions outboxer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "activerecord", ">= 7.0.8.6"

spec.add_development_dependency "rack-test", "~> 2.1"
spec.add_development_dependency "database_cleaner", "~> 2.0"
spec.add_development_dependency "dotenv", "~> 3.0"
spec.add_development_dependency "factory_bot", "~> 6.0"
Expand Down
6 changes: 3 additions & 3 deletions spec/factories/outboxer_publishers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
{
'throughput' => 1000,
'latency' => 0,
'cpu' => '10.5%',
'rss' => '40.95 MB',
'rtt' => '3.35 ms'
'cpu' => 10.5,
'rss' => 40.95,
'rtt' => 3.35
}
}
created_at { DateTime.parse("2024-11-10T00:00:00") }
Expand Down
39 changes: 39 additions & 0 deletions spec/lib/outboxer/web/home_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

require_relative "../../../../lib/outboxer/web"

RSpec.describe 'GET /', type: :request do
include Rack::Test::Methods

def app
Outboxer::Web
end

context 'when there are no publishers' do
before do
header 'Host', 'localhost'

get '/'
end

it "doesn't show any publishers" do
expect(last_response).to be_ok
expect(last_response.body).to include('There are no publishers to display')
end
end

context 'when there is a publisher' do
let!(:publisher) { create(:outboxer_publisher, :publishing, name: 'localhost:14325') }

before do
header 'Host', 'localhost'

get '/'
end

it "shows publisher" do
expect(last_response).to be_ok
expect(last_response.body).to include(publisher.name)
end
end
end
36 changes: 36 additions & 0 deletions spec/lib/outboxer/web/message/delete_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

require_relative '../../../../../app/models/application_record'
require_relative '../../../../../app/models/event'

require_relative "../../../../../lib/outboxer/web"

RSpec.describe 'POST /message/:id/delete', type: :request do
include Rack::Test::Methods

def app
Outboxer::Web
end

let!(:event) { Event.create!(id: 1, type: 'Event') }
let!(:message) do
Outboxer::Models::Message.find_by!(messageable_type: 'Event', messageable_id: event.id)
end

before do
header 'Host', 'localhost'

post "/message/#{message.id}/delete"

follow_redirect!
end

it 'deletes message' do
expect(Outboxer::Models::Message.exists?(message.id)).to be false
end

it 'redirects with flash' do
expect(last_response).to be_ok
expect(last_request.env['x-rack.flash'][:primary]).to include('was deleted')
end
end
29 changes: 29 additions & 0 deletions spec/lib/outboxer/web/message/messageable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'

require_relative '../../../../../app/models/application_record'
require_relative '../../../../../app/models/event'

require_relative "../../../../../lib/outboxer/web"

RSpec.describe 'GET /message/:id/messageable', type: :request do
include Rack::Test::Methods

def app
Outboxer::Web
end

let(:event) { Event.create!(type: 'Event') }
let!(:message) do
Outboxer::Models::Message.find_by!(messageable_type: 'Event', messageable_id: event.id)
end

before do
header 'Host', 'localhost'

get "/message/#{message.id}/messageable"
end

it 'loads the associated messageable object for a message' do
expect(last_response).to be_ok
end
end
38 changes: 38 additions & 0 deletions spec/lib/outboxer/web/message/requeue_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'

require_relative '../../../../../app/models/application_record'
require_relative '../../../../../app/models/event'

require_relative "../../../../../lib/outboxer/web"

RSpec.describe 'POST /message/:id/requeue', type: :request do
include Rack::Test::Methods

def app
Outboxer::Web
end

let!(:event) { Event.create!(id: 1, type: 'Event') }
let!(:message) do
Outboxer::Models::Message.find_by!(messageable_type: 'Event', messageable_id: event.id)
end

before do
header 'Host', 'localhost'

post "/message/#{message.id}/requeue"

follow_redirect!

message.reload
end

it 'requeues a message' do
expect(message.status).to eql('queued')
end

it 'redirects with flash' do
expect(last_response).to be_ok
expect(last_request.env['x-rack.flash'][:primary]).to include('was queued')
end
end
30 changes: 30 additions & 0 deletions spec/lib/outboxer/web/message/show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'

require_relative '../../../../../app/models/application_record'
require_relative '../../../../../app/models/event'

require_relative "../../../../../lib/outboxer/web"

RSpec.describe 'GET /message/:id', type: :request do
include Rack::Test::Methods

def app
Outboxer::Web
end

let(:event) { Event.create!(type: 'Event') }
let!(:message) do
Outboxer::Models::Message.find_by!(messageable_type: 'Event', messageable_id: event.id)
end

before do
header 'Host', 'localhost'

get "/message/#{message.id}"
end

it 'loads message' do
expect(last_response).to be_ok
expect(last_response.body).to include(message.id.to_s)
end
end
102 changes: 102 additions & 0 deletions spec/lib/outboxer/web/messages/delete_all_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require 'spec_helper'

require_relative '../../../../../app/models/application_record'
require_relative '../../../../../app/models/event'

require_relative "../../../../../lib/outboxer/web"

RSpec.describe 'POST /messages/delete_all', type: :request do
include Rack::Test::Methods

def app
Outboxer::Web
end

let!(:event_1) { Event.create!(id: 1, type: 'Event') }
let!(:message_1) do
Outboxer::Models::Message.find_by!(messageable_type: 'Event', messageable_id: event_1.id)
end

let!(:event_2) { Event.create!(id: 2, type: 'Event') }
let!(:message_2) do
Outboxer::Models::Message.find_by!(messageable_type: 'Event', messageable_id: event_2.id)
end

let!(:event_3) { Event.create!(id: 3, type: 'Event') }
let!(:message_3) do
Outboxer::Models::Message.find_by!(messageable_type: 'Event', messageable_id: event_3.id)
end

context 'when no status provided' do
before do
header 'Host', 'localhost'

post '/messages/delete_all', {
page: 1,
per_page: 10,
sort: :queued_at,
order: :desc,
time_zone: 'Australia/Sydney'
}

follow_redirect!
end

it 'deletes all messages' do
expect(Outboxer::Models::Message.count).to eq(0)
end

it 'does not delete events' do
expect(Event.count).to eq(3)
end

it 'redirects to /messages' do
expect(last_response).to be_ok
expect(last_request.url).to include(
"messages?sort=queued_at&order=desc&per_page=10&time_zone=Australia%2FSydney")
end

it 'flashes deleted messages count' do
expect(last_request.env['x-rack.flash'][:primary]).to include('3 messages have been deleted')
end
end

context 'when failed status provided' do
before do
message_2.update!(status: Outboxer::Models::Message::Status::PUBLISHING)
message_3.update!(status: Outboxer::Models::Message::Status::FAILED)

header 'Host', 'localhost'

post '/messages/delete_all', {
status: :failed,
page: 1,
per_page: 10,
sort: :queued_at,
order: :desc,
time_zone: 'Australia/Sydney'
}

follow_redirect!
end

it 'deletes failed messages' do
expect(Outboxer::Models::Message.failed.count).to eq(0)
end

it 'does not delete other messages' do
expect(Outboxer::Models::Message.queued.count).to eq(1)
expect(Outboxer::Models::Message.publishing.count).to eq(1)
end

it 'redirects to /messages' do
expect(last_response).to be_ok
expect(last_request.url).to include(
"messages?status=failed&sort=queued_at&order=desc&per_page=10&time_zone=Australia%2FSydney")
end

it 'flashes deleted messages count' do
expect(last_request.env['x-rack.flash'][:primary]).to include('1 message have been deleted')
end
end
end
Loading

0 comments on commit 3045816

Please sign in to comment.