Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review improvements on hubee storing
Browse files Browse the repository at this point in the history
jbfeldis committed May 31, 2024
1 parent fc6a96b commit ed74deb
Showing 38 changed files with 327 additions and 258 deletions.
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions app/controllers/claims_controller.rb
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion app/interactors/hubee/clean_attachments.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Hubee::CleanAttachments < BaseInteractor
class HubEE::CleanAttachments < BaseInteractor
def call
context.folder.attachments.each do |attachment|
attachment.close_file
13 changes: 0 additions & 13 deletions app/interactors/hubee/create.rb

This file was deleted.

14 changes: 11 additions & 3 deletions app/interactors/hubee/create_folder.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion app/interactors/hubee/mark_folder_complete.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/interactors/hubee/prepare_attachments.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Hubee::PrepareAttachments < BaseInteractor
class HubEE::PrepareAttachments < BaseInteractor
def call
context.folder = context.folder.with(attachments:)
end
6 changes: 5 additions & 1 deletion app/interactors/hubee/upload_attachments.rb
Original file line number Diff line number Diff line change
@@ -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:)
Original file line number Diff line number Diff line change
@@ -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],
2 changes: 0 additions & 2 deletions app/models/hubee.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/hubee/attachment.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/models/hubee/folder.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/models/hubee/recipient.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Hubee
module HubEE
class Recipient < Data.define(:siren, :branch_code, :type)
def initialize(siren:, branch_code:, type: "SI")
super
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class GetFamilyQuotient < BaseInteractor
def call
context.qf = {
context.quotient_familial = {
regime: "CNAF",
enfants: [
{
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class StoreQuotientFamilial < BaseOrganizer
organize Hubee::CreateFolder
organize UploadQuotientFamilialToHubEE
end
8 changes: 8 additions & 0 deletions app/organizers/upload_quotient_familial_to_hubee.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class UploadQuotientFamilialToHubEE < BaseOrganizer
organize PrepareQuotientFamilialHubEEFolder,
HubEE::PrepareAttachments,
HubEE::CreateFolder,
HubEE::UploadAttachments,
HubEE::MarkFolderComplete,
HubEE::CleanAttachments
end
3 changes: 3 additions & 0 deletions config/initializers/inflections.rb
Original file line number Diff line number Diff line change
@@ -14,3 +14,6 @@
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym "RESTful"
# end
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym "HubEE"
end
2 changes: 1 addition & 1 deletion lib/hubee/api.rb
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
require "net/http"
require "json"

class Hubee::Api
class HubEE::Api
def self.session
new
end
6 changes: 3 additions & 3 deletions spec/factories/hubee_attachment.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions spec/factories/hubee_folder.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/factories/hubee_recipient.rb
Original file line number Diff line number Diff line change
@@ -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" }
28 changes: 14 additions & 14 deletions spec/interactors/hubee/clean_attachments_spec.rb
Original file line number Diff line number Diff line change
@@ -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
50 changes: 38 additions & 12 deletions spec/interactors/hubee/create_folder_spec.rb
Original file line number Diff line number Diff line change
@@ -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
44 changes: 0 additions & 44 deletions spec/interactors/hubee/create_spec.rb

This file was deleted.

24 changes: 12 additions & 12 deletions spec/interactors/hubee/mark_folder_complete_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions spec/interactors/hubee/prepare_attachments_spec.rb
Original file line number Diff line number Diff line change
@@ -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,
28 changes: 14 additions & 14 deletions spec/interactors/hubee/upload_attachments_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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")
105 changes: 11 additions & 94 deletions spec/lib/hubee/api_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions spec/models/hubee/attachment_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions spec/models/hubee/folder_spec.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions spec/models/hubee/recipient_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
RSpec.describe StoreQuotientFamilial, type: :organizer do
let(:interactors) do
[
Hubee::CreateFolder,
UploadQuotientFamilialToHubEE,
]
end

44 changes: 44 additions & 0 deletions spec/organizers/upload_quotient_familial_to_hubee_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/support/provider_stubs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module ProviderStubs; end
104 changes: 104 additions & 0 deletions spec/support/provider_stubs/hubee.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ed74deb

Please sign in to comment.