Skip to content

Commit 486b440

Browse files
committed
Test: Adds missing tests for requests controller #print_unfulfilled
1 parent 666a5f4 commit 486b440

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

spec/requests/partners/requests_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,60 @@
423423
end
424424
end
425425
end
426+
427+
describe "GET #print_unfulfilled" do
428+
let(:item1) { create(:item, name: "Good item") }
429+
let(:item2) { create(:item, name: "Crap item") }
430+
let(:partner1) { create(:partner, organization: organization) }
431+
let(:partner_user) { partner1.primary_user }
432+
let!(:pending_request) { create(:request, :with_item_requests, :pending, partner: partner1, request_items: [{ item_id: item1.id, quantity: '100' }]) }
433+
let!(:started_request) { create(:request, :with_item_requests, :started, partner: partner1, request_items: [{ item_id: item2.id, quantity: '50' }]) }
434+
let!(:discarded_request) { create(:request, :with_item_requests, :discarded, partner: partner1, request_items: [{ item_id: item2.id, quantity: '30' }]) }
435+
let!(:fulfilled_request) { create(:request, :with_item_requests, :fulfilled, partner: partner1, request_items: [{ item_id: item2.id, quantity: '20' }]) }
436+
437+
before do
438+
partner_user.add_role(Role::ORG_ADMIN, organization)
439+
sign_in(partner_user)
440+
get print_unfulfilled_requests_path(format: :pdf)
441+
end
442+
443+
it "returns a PDF file" do
444+
PDF::Reader.new(StringIO.new(response.body))
445+
expect(response.content_type).to eq('application/pdf')
446+
expect(response.headers['Content-Disposition']).to include('inline')
447+
expect(response.body.bytes[0..3]).to eq('%PDF'.bytes)
448+
end
449+
450+
it "includes only 'pending' and 'started' requests" do
451+
pdf_content = PDF::Reader.new(StringIO.new(response.body))
452+
# this is a semi-lazy check, since we're ensuring 1 page for each request. In real world,
453+
# it's possible that there could be more than 1 page per request if the request is long.
454+
455+
expect(pdf_content.page_count).to eq(2)
456+
end
457+
458+
it "calls compute_and_render with the 2 matching requests" do
459+
# Create a double for the PDF instance
460+
pdf_double = double("PicklistsPdf")
461+
462+
# Expect PicklistsPdf.new to be called with correct args and return our double
463+
expect(PicklistsPdf).to receive(:new)
464+
.with(organization, kind_of(ActiveRecord::Relation))
465+
.and_return(pdf_double)
466+
467+
# Expect compute_and_render to be called on our double and return some PDF data
468+
# We don't really care about the content, the PDF model is tested elsewhere
469+
expect(pdf_double).to receive(:compute_and_render)
470+
.and_return("fake pdf content")
471+
472+
# Make the request
473+
get print_unfulfilled_requests_path(format: :pdf)
474+
475+
# Verify the response
476+
expect(response).to be_successful
477+
expect(response.content_type).to eq("application/pdf")
478+
expect(response.headers["Content-Disposition"]).to include("inline")
479+
expect(response.body).to eq("fake pdf content")
480+
end
481+
end
426482
end

0 commit comments

Comments
 (0)