Skip to content

Commit

Permalink
commit specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bedrock-adam committed Jan 4, 2025
1 parent d91dbb8 commit c9d88a8
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 27 deletions.
50 changes: 29 additions & 21 deletions spec/requests/web/messages/delete_all_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@ def app
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)
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)
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)
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

Expand All @@ -34,31 +48,29 @@ def app
end

it 'does not delete events' do
expect(Event.count).to eq(2)
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')
expect(last_request.url).to include('page=1')
expect(last_request.url).to include('per_page=10')
expect(last_request.url).to include('sort=queued_at')
expect(last_request.url).to include('order=desc')
expect(last_request.url).to include('time_zone=Australia%2FSydney')
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('2 messages have been deleted')
expect(last_request.env['x-rack.flash'][:primary]).to include('3 messages have been deleted')
end
end

context 'when queued status provided' do
context 'when failed status provided' do
before do
Outboxer::Models::Message.queued.last.update!(status: 'publishing')
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: :queued,
status: :failed,
page: 1,
per_page: 10,
sort: :queued_at,
Expand All @@ -68,23 +80,19 @@ def app
follow_redirect!
end

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

it 'does not delete publishing messages' do
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')
expect(last_request.url).to include('page=1')
expect(last_request.url).to include('per_page=10')
expect(last_request.url).to include('status=queued')
expect(last_request.url).to include('sort=queued_at')
expect(last_request.url).to include('order=desc')
expect(last_request.url).to include('time_zone=Australia%2FSydney')
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
Expand Down
111 changes: 105 additions & 6 deletions spec/requests/web/messages/update_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'spec_helper'

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

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

RSpec.describe 'POST /messages/update', type: :request do
Expand All @@ -9,11 +12,107 @@ def app
Outboxer::Web
end

it 'updates selected messages and redirects with query parameters' do
ids = [1, 2] # Assuming these IDs exist in your test setup
post '/messages/update', { selected_ids: ids, action: 'requeue_by_ids' }
follow_redirect!
expect(last_response).to be_ok
expect(flash[:primary]).to include('Requeued')
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)
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)
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)
end

before do
message_2.update!(status: Outboxer::Models::Message::Status::PUBLISHING)
message_3.update!(status: Outboxer::Models::Message::Status::FAILED)

header 'Host', 'localhost'
end

context 'when action is requeue_by_ids' do
let(:ids) { [message_2.id, message_3.id] }

before do
post '/messages/update', {
selected_ids: ids,
action: 'requeue_by_ids',
status: :failed,
page: 1,
per_page: 10,
sort: :queued_at,
order: :desc,
time_zone: 'Australia/Sydney'
}

follow_redirect!
end

it 'queues selected messages' do
expect(Outboxer::Models::Message.queued.pluck(:id)).to eq([
message_1.id, message_2.id, message_3.id ])
end

it 'redirects with flash message' 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')
expect(last_request.env['x-rack.flash'][:primary]).to include('Requeued 2 messages')
end
end

context 'when action is delete_by_ids' do
let(:ids) { [message_2.id, message_3.id] }

before do
post '/messages/update', {
selected_ids: ids,
action: 'delete_by_ids',
status: :failed,
page: 1,
per_page: 10,
sort: :queued_at,
order: :desc,
time_zone: 'Australia/Sydney'
}

follow_redirect!
end

it 'deletes selected messages' do
expect(Outboxer::Models::Message.all.pluck(:id)).to eq([message_1.id])
end

it 'redirects with flash message' 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')
expect(last_request.env['x-rack.flash'][:primary]).to include('Deleted 2 messages')
end
end

context 'with invalid action' do
let(:ids) { [message_2.id, message_3.id] }

before do
post '/messages/update', {
selected_ids: ids,
action: 'invalid',
status: :failed,
page: 1,
per_page: 10,
sort: :queued_at,
order: :desc,
time_zone: 'Australia/Sydney'
}
end

it 'responds with 500 internal server error' do
expect(last_response.status).to eq(500)
end
end
end

0 comments on commit c9d88a8

Please sign in to comment.