|
431 | 431 | end |
432 | 432 | end |
433 | 433 |
|
| 434 | + describe "GET #print_unfulfilled" do |
| 435 | + let(:item1) { create(:item, name: "Good item") } |
| 436 | + let(:item2) { create(:item, name: "Crap item") } |
| 437 | + let(:partner1) { create(:partner, organization: organization) } |
| 438 | + let(:partner_user) { partner1.primary_user } |
| 439 | + let!(:pending_request) { create(:request, :with_item_requests, :pending, partner: partner1, request_items: [{ item_id: item1.id, quantity: '100' }]) } |
| 440 | + let!(:started_request) { create(:request, :with_item_requests, :started, partner: partner1, request_items: [{ item_id: item2.id, quantity: '50' }]) } |
| 441 | + let!(:discarded_request) { create(:request, :with_item_requests, :discarded, partner: partner1, request_items: [{ item_id: item2.id, quantity: '30' }]) } |
| 442 | + let!(:fulfilled_request) { create(:request, :with_item_requests, :fulfilled, partner: partner1, request_items: [{ item_id: item2.id, quantity: '20' }]) } |
| 443 | + |
| 444 | + before do |
| 445 | + partner_user.add_role(Role::ORG_ADMIN, organization) |
| 446 | + sign_in(partner_user) |
| 447 | + get print_unfulfilled_requests_path(format: :pdf) |
| 448 | + end |
| 449 | + |
| 450 | + it "returns a PDF file" do |
| 451 | + PDF::Reader.new(StringIO.new(response.body)) |
| 452 | + expect(response.content_type).to eq('application/pdf') |
| 453 | + expect(response.headers['Content-Disposition']).to include('inline') |
| 454 | + expect(response.body.bytes[0..3]).to eq('%PDF'.bytes) |
| 455 | + end |
| 456 | + |
| 457 | + it "includes only 'pending' and 'started' requests" do |
| 458 | + pdf_content = PDF::Reader.new(StringIO.new(response.body)) |
| 459 | + # this is a semi-lazy check, since we're ensuring 1 page for each request. In real world, |
| 460 | + # it's possible that there could be more than 1 page per request if the request is long. |
| 461 | + |
| 462 | + expect(pdf_content.page_count).to eq(2) |
| 463 | + end |
| 464 | + |
| 465 | + it "calls compute_and_render with the 2 matching requests" do |
| 466 | + # Create a double for the PDF instance |
| 467 | + pdf_double = double("PicklistsPdf") |
| 468 | + |
| 469 | + # Expect PicklistsPdf.new to be called with correct args and return our double |
| 470 | + expect(PicklistsPdf).to receive(:new) |
| 471 | + .with(organization, kind_of(ActiveRecord::Relation)) |
| 472 | + .and_return(pdf_double) |
| 473 | + |
| 474 | + # Expect compute_and_render to be called on our double and return some PDF data |
| 475 | + # We don't really care about the content, the PDF model is tested elsewhere |
| 476 | + expect(pdf_double).to receive(:compute_and_render) |
| 477 | + .and_return("fake pdf content") |
| 478 | + |
| 479 | + # Make the request |
| 480 | + get print_unfulfilled_requests_path(format: :pdf) |
| 481 | + |
| 482 | + # Verify the response |
| 483 | + expect(response).to be_successful |
| 484 | + expect(response.content_type).to eq("application/pdf") |
| 485 | + expect(response.headers["Content-Disposition"]).to include("inline") |
| 486 | + expect(response.body).to eq("fake pdf content") |
| 487 | + end |
| 488 | + end |
| 489 | + |
| 490 | + describe "GET #print_picklist" do |
| 491 | + let(:organization) { create(:organization) } |
| 492 | + let(:partner) { create(:partner, organization: organization) } |
| 493 | + let(:partner_user) { partner.primary_user } |
| 494 | + let(:org_admin) { create(:organization_admin, organization: organization) } |
| 495 | + let(:request) { create(:request, :with_item_requests, organization: organization, partner: partner, partner_user: org_admin) } |
| 496 | + |
| 497 | + before do |
| 498 | + sign_in(org_admin) |
| 499 | + end |
| 500 | + |
| 501 | + it "generates a PDF for a single request" do |
| 502 | + # Create a double for the PDF instance |
| 503 | + pdf_double = double("PicklistsPdf") |
| 504 | + |
| 505 | + # Expect PicklistsPdf.new to be called with correct args and return our double |
| 506 | + expect(PicklistsPdf).to receive(:new) |
| 507 | + .with(organization, [request]) |
| 508 | + .and_return(pdf_double) |
| 509 | + |
| 510 | + # Expect compute_and_render to be called on our double and return some PDF data |
| 511 | + expect(pdf_double).to receive(:compute_and_render) |
| 512 | + .and_return("fake pdf content") |
| 513 | + |
| 514 | + # Make the request |
| 515 | + get print_picklist_request_path(request, format: :pdf) |
| 516 | + |
| 517 | + # Verify the response |
| 518 | + expect(response).to be_successful |
| 519 | + expect(response.content_type).to eq("application/pdf") |
| 520 | + expect(response.headers["Content-Disposition"]).to include("inline") |
| 521 | + expect(response.headers["Content-Disposition"]).to include("Picklists_") |
| 522 | + expect(response.body).to eq("fake pdf content") |
| 523 | + end |
| 524 | + |
| 525 | + it "includes correct associations in the query" do |
| 526 | + pdf_double = double("PicklistsPdf", compute_and_render: "pdf content") |
| 527 | + |
| 528 | + expect(PicklistsPdf).to receive(:new) do |org, requests| |
| 529 | + # Verify the request includes the necessary associations |
| 530 | + expect(requests.first.association(:item_requests)).to be_loaded |
| 531 | + expect(requests.first.association(:partner)).to be_loaded |
| 532 | + expect(requests.first.partner.association(:profile)).to be_loaded |
| 533 | + pdf_double |
| 534 | + end |
| 535 | + |
| 536 | + get print_picklist_request_path(request, format: :pdf) |
| 537 | + end |
| 538 | + end |
| 539 | + |
434 | 540 | describe 'POST #validate' do |
435 | 541 | it 'should handle missing CSRF gracefully' do |
436 | 542 | sign_in(partner_user) |
|
0 commit comments