From bd47a2d10e1367b85abafb35cc52e09a35a89c41 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Feldis <5403+jbfeldis@users.noreply.github.com> Date: Fri, 31 May 2024 09:23:35 +0200 Subject: [PATCH] Review improvements on hubee storing --- app/controllers/application_controller.rb | 5 + app/controllers/claims_controller.rb | 8 +- app/interactors/hubee/clean_attachments.rb | 2 +- app/interactors/hubee/create.rb | 13 --- app/interactors/hubee/create_folder.rb | 14 ++- app/interactors/hubee/mark_folder_complete.rb | 6 +- app/interactors/hubee/prepare_attachments.rb | 2 +- app/interactors/hubee/upload_attachments.rb | 6 +- ...prepare_quotient_familial_hubee_folder.rb} | 16 +-- app/models/hubee.rb | 2 - app/models/hubee/attachment.rb | 2 +- app/models/hubee/folder.rb | 2 +- app/models/hubee/recipient.rb | 2 +- .../base_organizer.rb | 0 .../get_family_quotient.rb | 2 +- .../store_quotient_familial.rb | 2 +- .../upload_quotient_familial_to_hubee.rb | 8 ++ config/initializers/inflections.rb | 3 + lib/hubee/api.rb | 2 +- spec/factories/hubee_attachment.rb | 6 +- spec/factories/hubee_folder.rb | 6 +- spec/factories/hubee_recipient.rb | 2 +- .../hubee/clean_attachments_spec.rb | 28 ++--- spec/interactors/hubee/create_folder_spec.rb | 50 +++++++-- spec/interactors/hubee/create_spec.rb | 44 -------- .../hubee/mark_folder_complete_spec.rb | 24 ++-- .../hubee/prepare_attachments_spec.rb | 8 +- .../hubee/upload_attachments_spec.rb | 28 ++--- ...re_quotient_familial_hubee_folder_spec.rb} | 8 +- spec/lib/hubee/api_spec.rb | 105 ++---------------- spec/models/hubee/attachment_spec.rb | 8 +- spec/models/hubee/folder_spec.rb | 8 +- spec/models/hubee/recipient_spec.rb | 8 +- .../store_quotient_familial_spec.rb | 2 +- .../upload_quotient_familial_to_hubee_spec.rb | 44 ++++++++ spec/rails_helper.rb | 4 +- spec/support/provider_stubs.rb | 1 + spec/support/provider_stubs/hubee.rb | 104 +++++++++++++++++ 38 files changed, 327 insertions(+), 258 deletions(-) delete mode 100644 app/interactors/hubee/create.rb rename app/interactors/{hubee/prepare_folder.rb => prepare_quotient_familial_hubee_folder.rb} (67%) delete mode 100644 app/models/hubee.rb rename app/{interactors => organizers}/base_organizer.rb (100%) rename app/{interactors => organizers}/get_family_quotient.rb (92%) rename app/{interactors => organizers}/store_quotient_familial.rb (53%) create mode 100644 app/organizers/upload_quotient_familial_to_hubee.rb delete mode 100644 spec/interactors/hubee/create_spec.rb rename spec/interactors/{hubee/prepare_folder_spec.rb => prepare_quotient_familial_hubee_folder_spec.rb} (72%) rename spec/{interactors => organizers}/store_quotient_familial_spec.rb (87%) create mode 100644 spec/organizers/upload_quotient_familial_to_hubee_spec.rb create mode 100644 spec/support/provider_stubs.rb create mode 100644 spec/support/provider_stubs/hubee.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 03a8283b..89b2dbdd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,6 +5,11 @@ class ApplicationController < ActionController::Base def identify_user Current.user = PivotIdentity.new(auth: session_auth, recipient:) + Current.quotient_familial = quotient_familial + end + + def quotient_familial + session.fetch("quotient_familial", {}) end def recipient diff --git a/app/controllers/claims_controller.rb b/app/controllers/claims_controller.rb index 62d7fb41..c368e568 100644 --- a/app/controllers/claims_controller.rb +++ b/app/controllers/claims_controller.rb @@ -6,17 +6,17 @@ def quotient_familial result = GetFamilyQuotient.call(identity: Current.user) if result.success? - session["qf"] = result.qf - Current.quotient_familial = result.qf + session["quotient_familial"] = result.quotient_familial + Current.quotient_familial = result.quotient_familial else raise end end def send_qf - recipient = Hubee::Recipient.new(siren: "21040107100019", branch_code: "04107") + hubee_recipient = HubEE::Recipient.new(siren: "21040107100019", branch_code: "04107") - result = StoreQuotientFamilial.call(identity: Current.user, quotient_familial: Current.quotient_familial, recipient: recipient) + result = StoreQuotientFamilial.call(identity: Current.user, quotient_familial: Current.quotient_familial, recipient: hubee_recipient) if result.success? Rails.logger.debug "Noïce" diff --git a/app/interactors/hubee/clean_attachments.rb b/app/interactors/hubee/clean_attachments.rb index ea25e502..5dc20d0b 100644 --- a/app/interactors/hubee/clean_attachments.rb +++ b/app/interactors/hubee/clean_attachments.rb @@ -1,4 +1,4 @@ -class Hubee::CleanAttachments < BaseInteractor +class HubEE::CleanAttachments < BaseInteractor def call context.folder.attachments.each do |attachment| attachment.close_file diff --git a/app/interactors/hubee/create.rb b/app/interactors/hubee/create.rb deleted file mode 100644 index 65500812..00000000 --- a/app/interactors/hubee/create.rb +++ /dev/null @@ -1,13 +0,0 @@ -class Hubee::Create < BaseInteractor - before do - context.session ||= Hubee::Api.session - end - - def call - context.folder = context.session.create_folder(folder: context.folder) - end - - def rollback - context.session.delete_folder(folder_id: context.folder.id) - end -end diff --git a/app/interactors/hubee/create_folder.rb b/app/interactors/hubee/create_folder.rb index 33d5c76c..e4e85b49 100644 --- a/app/interactors/hubee/create_folder.rb +++ b/app/interactors/hubee/create_folder.rb @@ -1,5 +1,13 @@ -class Hubee - class CreateFolder < BaseOrganizer - organize PrepareFolder, PrepareAttachments, Create, UploadAttachments, MarkFolderComplete, CleanAttachments +class HubEE::CreateFolder < BaseInteractor + before do + context.session ||= HubEE::Api.session + end + + def call + context.folder = context.session.create_folder(folder: context.folder) + end + + def rollback + context.session.delete_folder(folder_id: context.folder.id) end end diff --git a/app/interactors/hubee/mark_folder_complete.rb b/app/interactors/hubee/mark_folder_complete.rb index 7b198dcd..81b6cade 100644 --- a/app/interactors/hubee/mark_folder_complete.rb +++ b/app/interactors/hubee/mark_folder_complete.rb @@ -1,4 +1,8 @@ -class Hubee::MarkFolderComplete < BaseInteractor +class HubEE::MarkFolderComplete < BaseInteractor + before do + context.session ||= HubEE::Api.session + end + def call context.session.mark_folder_complete(folder_id: context.folder.id) end diff --git a/app/interactors/hubee/prepare_attachments.rb b/app/interactors/hubee/prepare_attachments.rb index 6684da6e..acfbf031 100644 --- a/app/interactors/hubee/prepare_attachments.rb +++ b/app/interactors/hubee/prepare_attachments.rb @@ -1,4 +1,4 @@ -class Hubee::PrepareAttachments < BaseInteractor +class HubEE::PrepareAttachments < BaseInteractor def call context.folder = context.folder.with(attachments:) end diff --git a/app/interactors/hubee/upload_attachments.rb b/app/interactors/hubee/upload_attachments.rb index 524a5ae2..b00fb89d 100644 --- a/app/interactors/hubee/upload_attachments.rb +++ b/app/interactors/hubee/upload_attachments.rb @@ -1,4 +1,8 @@ -class Hubee::UploadAttachments < BaseInteractor +class HubEE::UploadAttachments < BaseInteractor + before do + context.session ||= HubEE::Api.session + end + def call context.folder.attachments.each do |attachment| context.session.upload_attachment(folder_id: context.folder.id, attachment:) diff --git a/app/interactors/hubee/prepare_folder.rb b/app/interactors/prepare_quotient_familial_hubee_folder.rb similarity index 67% rename from app/interactors/hubee/prepare_folder.rb rename to app/interactors/prepare_quotient_familial_hubee_folder.rb index 993a3a4d..eede62c0 100644 --- a/app/interactors/hubee/prepare_folder.rb +++ b/app/interactors/prepare_quotient_familial_hubee_folder.rb @@ -1,6 +1,6 @@ -class Hubee::PrepareFolder < BaseInteractor +class PrepareQuotientFamilialHubEEFolder < BaseInteractor def call - context.folder = ::Hubee::Folder.new(**folder_params) + context.folder = ::HubEE::Folder.new(**folder_params) end private @@ -10,7 +10,8 @@ def case_external_id end def external_id - # HubEE's portal view greps 13 chars after two dashes + # This random string is formatted following HubEE's requirements + # so it can display nicely in their portal. @external_id ||= "Formulaire-QF-#{SecureRandom.hex[0...13].upcase}" end @@ -19,7 +20,8 @@ def folder_params applicant: context.identity, attachments: [ json_file, - text_file, + # can't setup this file until we can upload a proper PDF + # text_file, ], cases: [ external_id: case_external_id, @@ -31,12 +33,12 @@ def folder_params end def json_file - ::Hubee::Attachment.new( + ::HubEE::Attachment.new( file_name: "FormulaireQF.json", mime_type: "application/json", recipients: [case_external_id], type: process_code, - file_content: '{"identite": "pivot"}' + file_content: '{"first_name":"David"}' ) end @@ -45,7 +47,7 @@ def process_code end def text_file - ::Hubee::Attachment.new( + ::HubEE::Attachment.new( file_name: "FormulaireQF.pdf", mime_type: "application/pdf", recipients: [case_external_id], diff --git a/app/models/hubee.rb b/app/models/hubee.rb deleted file mode 100644 index 3df4da49..00000000 --- a/app/models/hubee.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Hubee -end diff --git a/app/models/hubee/attachment.rb b/app/models/hubee/attachment.rb index b099e9d6..6589d7e0 100644 --- a/app/models/hubee/attachment.rb +++ b/app/models/hubee/attachment.rb @@ -1,4 +1,4 @@ -class Hubee +module HubEE class Attachment < Data.define(:id, :file, :file_content, :file_name, :file_size, :mime_type, :recipients, :type) def initialize(file_content:, file_name:, mime_type:, recipients:, type:, id: nil, file: nil, file_size: nil) super diff --git a/app/models/hubee/folder.rb b/app/models/hubee/folder.rb index 9ca612a1..b7e6b623 100644 --- a/app/models/hubee/folder.rb +++ b/app/models/hubee/folder.rb @@ -1,4 +1,4 @@ -class Hubee +module HubEE class Folder < Data.define(:id, :applicant, :attachments, :cases, :external_id, :process_code) def initialize(applicant:, attachments:, cases:, external_id:, process_code:, id: nil) super diff --git a/app/models/hubee/recipient.rb b/app/models/hubee/recipient.rb index 19fac7fd..56fff0f4 100644 --- a/app/models/hubee/recipient.rb +++ b/app/models/hubee/recipient.rb @@ -1,4 +1,4 @@ -class Hubee +module HubEE class Recipient < Data.define(:siren, :branch_code, :type) def initialize(siren:, branch_code:, type: "SI") super diff --git a/app/interactors/base_organizer.rb b/app/organizers/base_organizer.rb similarity index 100% rename from app/interactors/base_organizer.rb rename to app/organizers/base_organizer.rb diff --git a/app/interactors/get_family_quotient.rb b/app/organizers/get_family_quotient.rb similarity index 92% rename from app/interactors/get_family_quotient.rb rename to app/organizers/get_family_quotient.rb index 15c33da9..85949e1e 100644 --- a/app/interactors/get_family_quotient.rb +++ b/app/organizers/get_family_quotient.rb @@ -1,6 +1,6 @@ class GetFamilyQuotient < BaseInteractor def call - context.qf = { + context.quotient_familial = { regime: "CNAF", enfants: [ { diff --git a/app/interactors/store_quotient_familial.rb b/app/organizers/store_quotient_familial.rb similarity index 53% rename from app/interactors/store_quotient_familial.rb rename to app/organizers/store_quotient_familial.rb index 4cf3fa20..616bfcc5 100644 --- a/app/interactors/store_quotient_familial.rb +++ b/app/organizers/store_quotient_familial.rb @@ -1,3 +1,3 @@ class StoreQuotientFamilial < BaseOrganizer - organize Hubee::CreateFolder + organize UploadQuotientFamilialToHubEE end diff --git a/app/organizers/upload_quotient_familial_to_hubee.rb b/app/organizers/upload_quotient_familial_to_hubee.rb new file mode 100644 index 00000000..0a9b7425 --- /dev/null +++ b/app/organizers/upload_quotient_familial_to_hubee.rb @@ -0,0 +1,8 @@ +class UploadQuotientFamilialToHubEE < BaseOrganizer + organize PrepareQuotientFamilialHubEEFolder, + HubEE::PrepareAttachments, + HubEE::CreateFolder, + HubEE::UploadAttachments, + HubEE::MarkFolderComplete, + HubEE::CleanAttachments +end diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 3860f659..088c4a90 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -14,3 +14,6 @@ # ActiveSupport::Inflector.inflections(:en) do |inflect| # inflect.acronym "RESTful" # end +ActiveSupport::Inflector.inflections do |inflect| + inflect.acronym "HubEE" +end diff --git a/lib/hubee/api.rb b/lib/hubee/api.rb index 782e866c..35ac23e4 100644 --- a/lib/hubee/api.rb +++ b/lib/hubee/api.rb @@ -2,7 +2,7 @@ require "net/http" require "json" -class Hubee::Api +class HubEE::Api def self.session new end diff --git a/spec/factories/hubee_attachment.rb b/spec/factories/hubee_attachment.rb index fa84a506..daf3628f 100644 --- a/spec/factories/hubee_attachment.rb +++ b/spec/factories/hubee_attachment.rb @@ -1,11 +1,11 @@ FactoryBot.define do - factory :hubee_attachment, class: Hubee::Attachment do + factory :hubee_attachment, class: HubEE::Attachment do initialize_with { new(**attributes) } file_content { "{\"first_name\":\"David\"}" } file_name { "FormulaireQF.json" } mime_type { "application/json" } - recipients { ["external_id-01"] } + recipients { ["Formulaire-QF-ABCDEF1234567-01"] } type { "FormulaireQF" } trait :with_file do @@ -16,7 +16,7 @@ end end - file_size { file.size } + file_size { 22 } end end end diff --git a/spec/factories/hubee_folder.rb b/spec/factories/hubee_folder.rb index 8ab5884b..0b5c2a1a 100644 --- a/spec/factories/hubee_folder.rb +++ b/spec/factories/hubee_folder.rb @@ -1,11 +1,11 @@ FactoryBot.define do - factory :hubee_folder, class: Hubee::Folder do + factory :hubee_folder, class: HubEE::Folder do initialize_with { new(**attributes) } applicant { {first_name: "David", last_name: "Heinemeier Hansson"} } attachments { [build(:hubee_attachment, :with_file)] } - cases { [recipient: build(:hubee_recipient), external_id: "case_id"] } - external_id { "external_id" } + cases { [recipient: build(:hubee_recipient), external_id: "Formulaire-QF-ABCDEF1234567-01"] } + external_id { "Formulaire-QF-ABCDEF1234567" } process_code { "FormulaireQF" } end end diff --git a/spec/factories/hubee_recipient.rb b/spec/factories/hubee_recipient.rb index b9cef301..ad860e1d 100644 --- a/spec/factories/hubee_recipient.rb +++ b/spec/factories/hubee_recipient.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :hubee_recipient, class: Hubee::Recipient do + factory :hubee_recipient, class: HubEE::Recipient do initialize_with { new(**attributes) } siren { "123456789" } diff --git a/spec/interactors/hubee/clean_attachments_spec.rb b/spec/interactors/hubee/clean_attachments_spec.rb index 22b642e0..b41bb989 100644 --- a/spec/interactors/hubee/clean_attachments_spec.rb +++ b/spec/interactors/hubee/clean_attachments_spec.rb @@ -1,22 +1,22 @@ require "rails_helper" -RSpec.describe Hubee::CleanAttachments, type: :interactor do - subject(:interactor) { described_class.call(**params) } +RSpec.describe HubEE::CleanAttachments, type: :interactor do + describe ".call" do + subject(:interactor) { described_class.call(**params) } - let(:attachment) { double(Hubee::Attachment) } - let(:attachments) { [attachment] } - let(:folder) { double(Hubee::Folder, attachments: attachments) } - let(:params) do - { - folder: folder, - } - end + let(:attachment) { double(HubEE::Attachment) } + let(:attachments) { [attachment] } + let(:folder) { double(HubEE::Folder, attachments: attachments) } + let(:params) do + { + folder: folder, + } + end - before do - allow(attachment).to receive(:close_file) - end + before do + allow(attachment).to receive(:close_file) + end - describe ".call" do it "closes the attachments" do expect(attachment).to receive(:close_file) interactor diff --git a/spec/interactors/hubee/create_folder_spec.rb b/spec/interactors/hubee/create_folder_spec.rb index 62d960d2..80772ec2 100644 --- a/spec/interactors/hubee/create_folder_spec.rb +++ b/spec/interactors/hubee/create_folder_spec.rb @@ -1,18 +1,44 @@ require "rails_helper" -RSpec.describe Hubee::CreateFolder, type: :organizer do - let(:interactors) do - [ - Hubee::PrepareFolder, - Hubee::PrepareAttachments, - Hubee::Create, - Hubee::UploadAttachments, - Hubee::MarkFolderComplete, - Hubee::CleanAttachments, - ] +RSpec.describe HubEE::CreateFolder, type: :interactor do + subject(:interactor) { described_class.call(**params) } + + let(:params) do + { + folder:, + } + end + + let(:session) { double(HubEE::Api) } + + before do + allow(HubEE::Api).to receive(:session).and_return(session) end - it "creates the folder, uploads the attachments and marks the folder complete" do - expect(described_class).to organize interactors + describe ".call" do + let(:folder) { double(HubEE::Folder) } + it "creates the folder" do + expect(session).to receive(:create_folder).with(folder:) + interactor + end + end + + describe "#rollback" do + subject(:rollback) { interactor.rollback } + + let(:folder) { double(HubEE::Folder, id: folder_id) } + let(:folder_id) { "folder_id" } + let(:interactor) { described_class.new(**params) } + let(:params) do + { + folder:, + session:, + } + end + + it "deletes the folder" do + expect(session).to receive(:delete_folder).with(folder_id:) + rollback + end end end diff --git a/spec/interactors/hubee/create_spec.rb b/spec/interactors/hubee/create_spec.rb deleted file mode 100644 index c2333c10..00000000 --- a/spec/interactors/hubee/create_spec.rb +++ /dev/null @@ -1,44 +0,0 @@ -require "rails_helper" - -RSpec.describe Hubee::Create, type: :interactor do - subject(:interactor) { described_class.call(**params) } - - let(:params) do - { - folder:, - } - end - - let(:session) { double(Hubee::Api) } - - before do - allow(Hubee::Api).to receive(:session).and_return(session) - end - - describe ".call" do - let(:folder) { double(Hubee::Folder) } - it "creates the folder" do - expect(session).to receive(:create_folder).with(folder:) - interactor - end - end - - describe "#rollback" do - subject(:rollback) { interactor.rollback } - - let(:folder) { double(Hubee::Folder, id: folder_id) } - let(:folder_id) { "folder_id" } - let(:interactor) { described_class.new(**params) } - let(:params) do - { - folder:, - session:, - } - end - - it "deletes the folder" do - expect(session).to receive(:delete_folder).with(folder_id:) - rollback - end - end -end diff --git a/spec/interactors/hubee/mark_folder_complete_spec.rb b/spec/interactors/hubee/mark_folder_complete_spec.rb index 16bec761..31df87d3 100644 --- a/spec/interactors/hubee/mark_folder_complete_spec.rb +++ b/spec/interactors/hubee/mark_folder_complete_spec.rb @@ -1,19 +1,19 @@ require "rails_helper" -RSpec.describe Hubee::MarkFolderComplete, type: :interactor do - subject(:interactor) { described_class.call(**params) } +RSpec.describe HubEE::MarkFolderComplete, type: :interactor do + describe ".call" do + subject(:interactor) { described_class.call(**params) } - let(:folder) { double(Hubee::Folder, id: folder_id) } - let(:folder_id) { "folder_id" } - let(:params) do - { - folder:, - session:, - } - end - let(:session) { double(Hubee::Api) } + let(:folder) { double(HubEE::Folder, id: folder_id) } + let(:folder_id) { "folder_id" } + let(:params) do + { + folder:, + session:, + } + end + let(:session) { double(HubEE::Api) } - describe ".call" do it "marks the folder complete" do expect(session).to receive(:mark_folder_complete).with(folder_id: folder_id) interactor diff --git a/spec/interactors/hubee/prepare_attachments_spec.rb b/spec/interactors/hubee/prepare_attachments_spec.rb index 024450e2..6e753706 100644 --- a/spec/interactors/hubee/prepare_attachments_spec.rb +++ b/spec/interactors/hubee/prepare_attachments_spec.rb @@ -1,10 +1,10 @@ require "rails_helper" -RSpec.describe Hubee::PrepareAttachments, type: :interactor do +RSpec.describe HubEE::PrepareAttachments, type: :interactor do subject(:interactor) { described_class.call(**params) } let(:attachments) { [] } - let(:folder) { Hubee::Folder.new(applicant: double, cases: [], external_id: "exId", process_code: "FormulaireQF", attachments:) } + let(:folder) { HubEE::Folder.new(applicant: double, cases: [], external_id: "exId", process_code: "FormulaireQF", attachments:) } let(:params) do { folder:, @@ -14,7 +14,7 @@ describe ".call" do let(:attachments) do [ - Hubee::Attachment.new(file_name: "FormulaireQF.json", mime_type: "application/json", type: "FormulaireQF", file_content: "content", recipients: []), + HubEE::Attachment.new(file_name: "FormulaireQF.json", mime_type: "application/json", type: "FormulaireQF", file_content: "content", recipients: []), ] end let(:expected_attachments) do @@ -35,7 +35,7 @@ describe "#rollback" do subject(:rollback) { interactor.rollback } - let(:attachment) { Hubee::Attachment.new(file: Tempfile.create, file_name: "FormulaireQF.json", mime_type: "application/json", type: "FormulaireQF", file_content: "content", recipients: []) } + let(:attachment) { HubEE::Attachment.new(file: Tempfile.create, file_name: "FormulaireQF.json", mime_type: "application/json", type: "FormulaireQF", file_content: "content", recipients: []) } let(:attachments) do [ attachment, diff --git a/spec/interactors/hubee/upload_attachments_spec.rb b/spec/interactors/hubee/upload_attachments_spec.rb index 40786b25..1f6f28ef 100644 --- a/spec/interactors/hubee/upload_attachments_spec.rb +++ b/spec/interactors/hubee/upload_attachments_spec.rb @@ -1,21 +1,21 @@ require "rails_helper" -RSpec.describe Hubee::UploadAttachments, type: :interactor do - subject(:interactor) { described_class.call(**params) } +RSpec.describe HubEE::UploadAttachments, type: :interactor do + describe ".call" do + subject(:interactor) { described_class.call(**params) } - let(:attachment) { double(Hubee::Attachment) } - let(:attachments) { [attachment] } - let(:folder) { double(Hubee::Folder, id: folder_id, attachments:) } - let(:folder_id) { "folder_id" } - let(:params) do - { - folder: folder, - session: session, - } - end - let(:session) { double(Hubee::Api) } + let(:attachment) { double(HubEE::Attachment) } + let(:attachments) { [attachment] } + let(:folder) { double(HubEE::Folder, id: folder_id, attachments:) } + let(:folder_id) { "folder_id" } + let(:params) do + { + folder: folder, + session: session, + } + end + let(:session) { double(HubEE::Api) } - describe ".call" do it "uploads the attachments" do expect(session).to receive(:upload_attachment).with(folder_id: folder_id, attachment: attachment) interactor diff --git a/spec/interactors/hubee/prepare_folder_spec.rb b/spec/interactors/prepare_quotient_familial_hubee_folder_spec.rb similarity index 72% rename from spec/interactors/hubee/prepare_folder_spec.rb rename to spec/interactors/prepare_quotient_familial_hubee_folder_spec.rb index ffef7f14..6b54c826 100644 --- a/spec/interactors/hubee/prepare_folder_spec.rb +++ b/spec/interactors/prepare_quotient_familial_hubee_folder_spec.rb @@ -1,13 +1,15 @@ require "rails_helper" -RSpec.describe Hubee::PrepareFolder, type: :interactor do +RSpec.describe PrepareQuotientFamilialHubEEFolder, type: :interactor do describe ".call" do subject(:interactor) { described_class.call(**params) } let(:expected_attributes) do { applicant: identity, - attachments: [an_object_having_attributes(file_name: "FormulaireQF.json"), an_object_having_attributes(file_name: "FormulaireQF.pdf")], + attachments: [an_object_having_attributes(file_name: "FormulaireQF.json")], + # TODO: Add back when we upload a proper PDF + # attachments: [an_object_having_attributes(file_name: "FormulaireQF.json"), an_object_having_attributes(file_name: "FormulaireQF.pdf")], cases: [ external_id: "Formulaire-QF-ABCDEF1234567-01", recipient:, @@ -23,7 +25,7 @@ recipient:, } end - let(:recipient) { double(Hubee::Recipient) } + let(:recipient) { double(HubEE::Recipient) } before do allow(SecureRandom).to receive(:hex).and_return("abcdef1234567thiswontbeused") diff --git a/spec/lib/hubee/api_spec.rb b/spec/lib/hubee/api_spec.rb index 09cb2891..caa4769b 100644 --- a/spec/lib/hubee/api_spec.rb +++ b/spec/lib/hubee/api_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe Hubee::Api, type: :api do +RSpec.describe HubEE::Api, type: :api do describe ".session" do subject(:session) { described_class.session } @@ -12,16 +12,8 @@ let(:session) { described_class.session } before do - stub_request(:post, "https://auth.bas.hubee.numerique.gouv.fr/oauth2/token") - .with( - body: "{\"scope\":\"OSL\",\"grant_type\":\"client_credentials\"}", - headers: { - "Authorization" => "Basic bm90X2FfcmVhbF9pZDpub3RfYV9yZWFsX3NlY3JldA==", - "Content-Type" => "application/json", - "Host" => "auth.bas.hubee.numerique.gouv.fr", - } - ) - .to_return(status: 200, body: "{\"access_token\":\"#{access_token}\"}", headers: {}) + stub_hubee_token + allow(SecureRandom).to receive(:hex).and_return("abcdef1234567thiswontbeused") end describe "#create_folder" do @@ -33,60 +25,8 @@ let(:folder) { build(:hubee_folder, attachments: attachments) } context "when the folder is succesfully created" do - let(:response) { - { - id: "3fa85f64-5717-4562-b3fc-2c963f66afa6", - globalStatus: "HUBEE_RECEIVED", - processCode: "FormulaireQF", - createDateTime: "2024-05-22T13:36:01.781Z", - closeDateTime: "2024-05-22T13:36:01.781Z", - applicant: { - firstName: "David", - lastName: "Heinemeier Hansson", - }, - externalId: "external_id", - updateDateTime: "2024-05-22T13:36:01.781Z", - cases: [ - { - id: "d7923df5-c071-400e-87b5-f0d01d539aa5", - status: "HUBEE_NOTIFIED", - recipient: { - type: "SI", - companyRegister: "123456789", - branchCode: "1234", - }, - externalId: "case_id", - updateDateTime: "2024-05-22T13:36:01.781Z", - transmissionDateTime: "2024-05-22T13:36:01.781Z", - }, - ], - attachments: [ - { - id: "a66abb0c-52d1-4e50-9195-22526fb7ce92", - status: "PENDING", - fileName: "FormulaireQF.json", - type: "FormulaireQF", - size: 22, - mimeType: "application/json", - recipients: [ - "external_id-01", - ], - }, - ], - } - } - before do - stub_request(:post, "https://api.bas.hubee.numerique.gouv.fr/teledossiers/v1/folders") - .with( - body: "{\"processCode\":\"FormulaireQF\",\"externalId\":\"external_id\",\"applicant\":{\"firstName\":\"David\",\"lastName\":\"Heinemeier Hansson\"},\"cases\":[{\"recipient\":{\"companyRegister\":\"123456789\",\"branchCode\":\"1234\",\"type\":\"SI\"},\"externalId\":\"case_id\"}],\"attachments\":[{\"fileName\":\"FormulaireQF.json\",\"type\":\"FormulaireQF\",\"size\":22,\"mimeType\":\"application/json\",\"recipients\":[\"external_id-01\"]}]}", - headers: { - "Authorization" => "Bearer access_token_123", - "Content-Type" => "application/json", - "Host" => "api.bas.hubee.numerique.gouv.fr", - } - ) - .to_return(status: 200, body: response.to_json, headers: {}) + stub_hubee_create_folder end it "fills in the ids from the response" do @@ -98,19 +38,11 @@ describe "#delete_folder" do subject(:delete_folder) { session.delete_folder(folder_id: folder_id) } - let(:folder_id) { "12345folder_id" } + let(:folder_id) { "3fa85f64-5717-4562-b3fc-2c963f66afa6" } context "when the folder is succesfully deleted" do before do - stub_request(:delete, "https://api.bas.hubee.numerique.gouv.fr/teledossiers/v1/folders/12345folder_id") - .with( - headers: { - "Authorization" => "Bearer access_token_123", - "Content-Type" => "application/json", - "Host" => "api.bas.hubee.numerique.gouv.fr", - } - ) - .to_return(status: 204, body: "", headers: {}) + stub_hubee_delete_folder end it "deletes the folder" do @@ -122,19 +54,12 @@ describe "#upload_attachment" do subject(:upload_attachment) { session.upload_attachment(attachment: attachment, folder_id: folder_id) } - let(:attachment) { build(:hubee_attachment, :with_file, id: "12345attachment_id") } - let(:folder_id) { "12345folder_id" } + let(:attachment) { build(:hubee_attachment, :with_file, id: "a66abb0c-52d1-4e50-9195-22526fb7ce92") } + let(:folder_id) { "3fa85f64-5717-4562-b3fc-2c963f66afa6" } context "when the attachment is succesfully uploaded" do before do - stub_request(:put, "https://api.bas.hubee.numerique.gouv.fr/teledossiers/v1/folders/12345folder_id/attachments/12345attachment_id") - .with( - headers: { - "Authorization" => "Bearer access_token_123", - "Host" => "api.bas.hubee.numerique.gouv.fr", - } - ) - .to_return(status: 204, body: "", headers: {}) + stub_hubee_upload_attachment end it "uploads the attachment" do @@ -146,19 +71,11 @@ describe "#mark_folder_complete" do subject(:mark_folder_complete) { session.mark_folder_complete(folder_id: folder_id) } - let(:folder_id) { "12345folder_id" } + let(:folder_id) { "3fa85f64-5717-4562-b3fc-2c963f66afa6" } context "when the folder is succesfully marked as complete" do before do - stub_request(:patch, "https://api.bas.hubee.numerique.gouv.fr/teledossiers/v1/folders/12345folder_id") - .with( - body: "{\"globalStatus\":\"HUBEE_COMPLETED\"}", - headers: { - "Authorization" => "Bearer access_token_123", - "Host" => "api.bas.hubee.numerique.gouv.fr", - } - ) - .to_return(status: 204, body: "", headers: {}) + stub_hubee_mark_folder_complete end it "marks the folder as complete" do diff --git a/spec/models/hubee/attachment_spec.rb b/spec/models/hubee/attachment_spec.rb index d00982b4..9150e5ff 100644 --- a/spec/models/hubee/attachment_spec.rb +++ b/spec/models/hubee/attachment_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" -RSpec.describe Hubee::Attachment do - subject(:attachment) { Hubee::Attachment.new(**params) } +RSpec.describe HubEE::Attachment do + subject(:attachment) { HubEE::Attachment.new(**params) } let(:file) { double(File) } let(:params) do @@ -29,7 +29,7 @@ describe "#initialize" do context "when all keywords are passed" do it "initializes a new attachment" do - expect(attachment).to be_an_instance_of(Hubee::Attachment) + expect(attachment).to be_an_instance_of(HubEE::Attachment) end end @@ -45,7 +45,7 @@ end it "initializes a new attachment" do - expect(attachment).to be_an_instance_of(Hubee::Attachment) + expect(attachment).to be_an_instance_of(HubEE::Attachment) end end end diff --git a/spec/models/hubee/folder_spec.rb b/spec/models/hubee/folder_spec.rb index bb7bf414..51cf6a52 100644 --- a/spec/models/hubee/folder_spec.rb +++ b/spec/models/hubee/folder_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" -RSpec.describe Hubee::Folder do - subject(:folder) { Hubee::Folder.new(**params) } +RSpec.describe HubEE::Folder do + subject(:folder) { HubEE::Folder.new(**params) } let(:params) do { @@ -24,7 +24,7 @@ describe "#initialize" do context "when all keywords are passed" do it "initializes a new folder" do - expect(folder).to be_an_instance_of(Hubee::Folder) + expect(folder).to be_an_instance_of(HubEE::Folder) end end @@ -40,7 +40,7 @@ end it "initializes a new folder" do - expect(folder).to be_an_instance_of(Hubee::Folder) + expect(folder).to be_an_instance_of(HubEE::Folder) end end end diff --git a/spec/models/hubee/recipient_spec.rb b/spec/models/hubee/recipient_spec.rb index 7e371299..d69befa4 100644 --- a/spec/models/hubee/recipient_spec.rb +++ b/spec/models/hubee/recipient_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" -RSpec.describe Hubee::Recipient do - subject(:recipient) { Hubee::Recipient.new(**params) } +RSpec.describe HubEE::Recipient do + subject(:recipient) { HubEE::Recipient.new(**params) } let(:params) do { @@ -18,7 +18,7 @@ describe "#initialize" do context "when all keywords are passed" do it "initializes a new recipient" do - expect(recipient).to be_an_instance_of(Hubee::Recipient) + expect(recipient).to be_an_instance_of(HubEE::Recipient) end end @@ -31,7 +31,7 @@ end it "initializes a new recipient" do - expect(recipient).to be_an_instance_of(Hubee::Recipient) + expect(recipient).to be_an_instance_of(HubEE::Recipient) end end end diff --git a/spec/interactors/store_quotient_familial_spec.rb b/spec/organizers/store_quotient_familial_spec.rb similarity index 87% rename from spec/interactors/store_quotient_familial_spec.rb rename to spec/organizers/store_quotient_familial_spec.rb index 0cbbd4da..f0fc5482 100644 --- a/spec/interactors/store_quotient_familial_spec.rb +++ b/spec/organizers/store_quotient_familial_spec.rb @@ -3,7 +3,7 @@ RSpec.describe StoreQuotientFamilial, type: :organizer do let(:interactors) do [ - Hubee::CreateFolder, + UploadQuotientFamilialToHubEE, ] end diff --git a/spec/organizers/upload_quotient_familial_to_hubee_spec.rb b/spec/organizers/upload_quotient_familial_to_hubee_spec.rb new file mode 100644 index 00000000..45d51807 --- /dev/null +++ b/spec/organizers/upload_quotient_familial_to_hubee_spec.rb @@ -0,0 +1,44 @@ +require "rails_helper" + +RSpec.describe UploadQuotientFamilialToHubEE, type: :organizer do + let(:interactors) do + [ + PrepareQuotientFamilialHubEEFolder, + HubEE::PrepareAttachments, + HubEE::CreateFolder, + HubEE::UploadAttachments, + HubEE::MarkFolderComplete, + HubEE::CleanAttachments, + ] + end + + it "creates the folder, uploads the attachments and marks the folder complete" do + expect(described_class).to organize interactors + end + + describe ".call" do + subject { described_class.call(**params) } + + let(:folder) { build(:hubee_folder) } + let(:identity) { PivotIdentity.new(recipient:, auth: {info: {first_name: "David", last_name: "Heinemeier Hansson"}}) } + let(:recipient) { build(:hubee_recipient) } + let(:params) do + { + folder:, + identity:, + recipient:, + } + end + + before do + allow(SecureRandom).to receive(:hex).and_return("abcdef1234567thiswontbeused") + + stub_hubee_token + stub_hubee_create_folder + stub_hubee_upload_attachment + stub_hubee_mark_folder_complete + end + + it { is_expected.to be_a_success } + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 14cf3121..bfd5571a 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -7,7 +7,6 @@ require "rspec/rails" # Add additional requires below this line. Rails is not loaded until this point! require "matchers/organizer" -require "support/factory_bot" # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are @@ -22,7 +21,7 @@ # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # -# Rails.root.glob('spec/support/**/*.rb').sort.each { |f| require f } +Rails.root.glob("spec/support/**/*.rb").sort.each { |f| require f } # Checks for pending migrations and applies them before tests are run. # If you are not using ActiveRecord, you can remove these lines. @@ -65,4 +64,5 @@ # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") config.include QuotientFamilial::Matchers + config.include ProviderStubs::HubEE end diff --git a/spec/support/provider_stubs.rb b/spec/support/provider_stubs.rb new file mode 100644 index 00000000..1b1ae530 --- /dev/null +++ b/spec/support/provider_stubs.rb @@ -0,0 +1 @@ +module ProviderStubs; end diff --git a/spec/support/provider_stubs/hubee.rb b/spec/support/provider_stubs/hubee.rb new file mode 100644 index 00000000..a460959f --- /dev/null +++ b/spec/support/provider_stubs/hubee.rb @@ -0,0 +1,104 @@ +require_relative "../provider_stubs" + +module ProviderStubs::HubEE + def stub_hubee_token(access_token: "access_token_123") + stub_request(:post, "https://auth.bas.hubee.numerique.gouv.fr/oauth2/token") + .with( + body: '{"scope":"OSL","grant_type":"client_credentials"}', + headers: { + "Authorization" => "Basic bm90X2FfcmVhbF9pZDpub3RfYV9yZWFsX3NlY3JldA==", + "Content-Type" => "application/json", + "Host" => "auth.bas.hubee.numerique.gouv.fr", + } + ) + .to_return( + status: 200, + body: {"access_token" => access_token}.to_json, + headers: {} + ) + end + + def stub_hubee_create_folder(access_token: "access_token_123") + stub_request(:post, "https://api.bas.hubee.numerique.gouv.fr/teledossiers/v1/folders") + .with( + body: '{"processCode":"FormulaireQF","externalId":"Formulaire-QF-ABCDEF1234567","applicant":{"firstName":"David","lastName":"Heinemeier Hansson"},"cases":[{"recipient":{"companyRegister":"123456789","branchCode":"1234","type":"SI"},"externalId":"Formulaire-QF-ABCDEF1234567-01"}],"attachments":[{"fileName":"FormulaireQF.json","type":"FormulaireQF","size":22,"mimeType":"application/json","recipients":["Formulaire-QF-ABCDEF1234567-01"]}]}', + headers: { + "Authorization" => "Bearer #{access_token}", + "Content-Type" => "application/json", + } + ) + .to_return(status: 200, body: { + id: "3fa85f64-5717-4562-b3fc-2c963f66afa6", + globalStatus: "HUBEE_RECEIVED", + processCode: "FormulaireQF", + createDateTime: "2024-05-22T13:36:01.781Z", + closeDateTime: "2024-05-22T13:36:01.781Z", + applicant: { + firstName: "David", + lastName: "Heinemeier Hansson", + }, + externalId: "Formulaire-QF-ABCDEF1234567", + updateDateTime: "2024-05-22T13:36:01.781Z", + cases: [ + { + id: "d7923df5-c071-400e-87b5-f0d01d539aa5", + status: "HUBEE_NOTIFIED", + recipient: { + type: "SI", + companyRegister: "123456789", + branchCode: "1234", + }, + externalId: "Formulaire-QF-ABCDEF1234567-01", + updateDateTime: "2024-05-22T13:36:01.781Z", + transmissionDateTime: "2024-05-22T13:36:01.781Z", + }, + ], + attachments: [ + { + id: "a66abb0c-52d1-4e50-9195-22526fb7ce92", + status: "PENDING", + fileName: "FormulaireQF.json", + type: "FormulaireQF", + size: 22, + mimeType: "application/json", + recipients: [ + "Formulaire-QF-ABCDEF1234567-01", + ], + }, + ], + }.to_json, headers: {}) + end + + def stub_hubee_upload_attachment(access_token: "access_token_123") + stub_request(:put, "https://api.bas.hubee.numerique.gouv.fr/teledossiers/v1/folders/3fa85f64-5717-4562-b3fc-2c963f66afa6/attachments/a66abb0c-52d1-4e50-9195-22526fb7ce92") + .with( + body: '{"first_name":"David"}', + headers: { + "Authorization" => "Bearer #{access_token}", + "Content-Length" => "22", + "Content-Type" => "application/octet-stream", + } + ) + .to_return(status: 204, body: "", headers: {}) + end + + def stub_hubee_mark_folder_complete(access_token: "access_token_123") + stub_request(:patch, "https://api.bas.hubee.numerique.gouv.fr/teledossiers/v1/folders/3fa85f64-5717-4562-b3fc-2c963f66afa6") + .with( + body: '{"globalStatus":"HUBEE_COMPLETED"}' + ) + .to_return(status: 204, body: "", headers: {}) + end + + def stub_hubee_delete_folder(access_token: "access_token_123") + stub_request(:delete, "https://api.bas.hubee.numerique.gouv.fr/teledossiers/v1/folders/3fa85f64-5717-4562-b3fc-2c963f66afa6") + .with( + headers: { + "Authorization" => "Bearer access_token_123", + "Content-Type" => "application/json", + "Host" => "api.bas.hubee.numerique.gouv.fr", + } + ) + .to_return(status: 204, body: "", headers: {}) + end +end