require 'spec_helper' describe LifenFhir::CommunicationRequest do context ':send' do let(:medium1) { LifenFhir::Medium.new(uuid: "valid-medium-uuid-1") } #valid_channel_uuid let(:medium2) { LifenFhir::Medium.new(uuid: "valid-medium-uuid-2") } let(:sender) { LifenFhir::Practitioner.new(uuid: "valid-sender-uuid") } #valid_sender_uuid let(:recipient1) { LifenFhir::Practitioner.new(uuid: "valid-recipient-uuid-1") } let(:recipient2) { LifenFhir::Practitioner.new(uuid: "valid-recipient-uuid-2") }#valid_recipient_uuid let(:binary) { LifenFhir::Binary.new(uuid: "valid-binary-uuid") } let(:attachment) { LifenFhir::Attachment.new(title: "Master Plan", path: File.dirname(__FILE__) + "/support/master_plan.pdf", content_type: "application/pdf") } let(:patient) { LifenFhir::Patient.new(uuid: "valid-patient-id", first_name: "Jean", last_name: "Dupond", birth_date: Date.new(2000,1,1)) } let(:invalid_patient) { LifenFhir::Patient.new(uuid: "invalid-patient-id", first_name: "Jean", last_name: "Dupond", birth_date: Date.new(2000,1,1)) } describe ':send multi-recipients with a binary' do let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient1, recipient2], medium: [medium1, medium2], binary: binary, patient: patient) } it 'works' do VCR.use_cassette "communication_request/send/valid_attributes_binary" do communication_request.send end expect(communication_request.uuid).to_not be_nil expect(communication_request.number_communications).to eq(2) end end describe ':send to patient with a binary' do let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: patient, recipients: [patient], binary: binary) } let(:wrong_communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [invalid_patient], binary: binary) } it 'works' do VCR.use_cassette "communication_request/send/patient/valid_attributes_binary" do communication_request.send end expect(communication_request.uuid).to_not be_nil expect(communication_request.number_communications).to eq(1) end it 'does not work' do VCR.use_cassette "communication_request/send/patient/invalid_attributes_binary" do expect{wrong_communication_request.send}.to raise_error LifenFhir::Error end end end describe ':send to patient and practitioner with a binary' do let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient1, patient], binary: binary, medium: [medium1]) } it 'works' do VCR.use_cassette("communication_request/send/to_patient_also", match_requests_on: [:method, :uri, :body]) do communication_request.send end expect(communication_request.uuid).to_not be_nil expect(communication_request.number_communications).to eq(2) end end describe ':send multi-recipients with an attachment' do let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient1, recipient2], medium: [medium1, medium2], attachment: attachment, patient: patient) } it 'works' do VCR.use_cassette "communication_request/send/valid_attributes" do communication_request.send end expect(communication_request.uuid).to_not be_nil expect(communication_request.number_communications).to eq(2) end end describe 'invalid medium' do let(:invalid_medium) { LifenFhir::Medium.new(uuid: "invalid-medium-uuid") } let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient1, recipient2], medium: [invalid_medium, medium2], binary: binary, patient: patient) } it 'raises an error' do VCR.use_cassette "communication_request/send/invalid_medium" do expect{ communication_request.send }.to raise_error LifenFhir::Error end end end end context ':find_by_uuid' do it 'works' do VCR.use_cassette "communication_request/find_by_uuid/valid_uuid_with_no_status" do @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("2d217345-6ce8-4412-a03d-076a37c6661a") end expect(@communication_request.status).to eq "unknown" expect(@communication_request.binary.uuid).to eq "11e7317b-d85e-5aa9-bdf4-0242ac110004" end context 'with a cancelled status' do it 'works' do VCR.use_cassette "communication_request/find_by_uuid/valid_uuid_with_a_status" do @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("2d217345-6ce8-4412-a03d-076a37c6661a") end expect(@communication_request.status).to eq "cancelled" end end context 'with invalid uuid' do it 'raises an error' do expect{ VCR.use_cassette "communication_request/find_by_uuid/invalid_uuid" do @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("invalid_uuid") end }.to raise_error LifenFhir::Error, "Error 404, Page not found, App Client, GET 'https://develop.lifen.fr/fhir/CommunicationRequest/invalid_uuid' with params '{}' and bearer 'valid_application_access******'" end end end end