-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Générisation des adresses magiques de réponse aux autres instances et…
… environnements (#4807) * refactor TransferEmailReplyJob to encapsulate perform specs * Générisation des adresses magiques de réponse aux autres instances et environnements * rename SENDINBLUE_INBOUND_PASSWORD env var to BREVO * ajout d’une route /inbound_emails/brevo pour déprécier celle en sendinblue
- Loading branch information
1 parent
3ced104
commit fb68d72
Showing
9 changed files
with
234 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,150 @@ | ||
RSpec.describe TransferEmailReplyJob do | ||
subject(:perform_job) { described_class.perform_now(sendinblue_payload) } | ||
|
||
before do | ||
# Set a fixed date so we can assert on dates within email body | ||
travel_to(Time.zone.parse("2022-05-17 16:00:00")) | ||
describe "#uuid_from_email_address" do | ||
%w[ | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
].each do |email_address| | ||
it "should extract UUID from #{email_address}" do | ||
expect(described_class.uuid_from_email_address(email_address)).to eq("b1a2-3c4f") | ||
end | ||
end | ||
end | ||
|
||
let!(:user) { create(:user, email: "[email protected]", first_name: "Bénédicte", last_name: "Ficiaire") } | ||
let!(:agent) { create(:agent, email: "[email protected]") } | ||
let(:rdv_uuid) { "8fae4d5f-4d63-4f60-b343-854d939881a3" } | ||
let!(:rdv) { create(:rdv, users: [user], agents: [agent], uuid: rdv_uuid) } | ||
|
||
let(:sendinblue_valid_payload) do | ||
# The usual payload has more info, but I removed non-essential fields for readability. | ||
# See: https://developers.sendinblue.com/docs/inbound-parsing-api-1#sample-payload | ||
{ | ||
Cc: [], | ||
ReplyTo: nil, | ||
Subject: "coucou", | ||
Attachments: [], | ||
Headers: { | ||
"Message-ID": "<[email protected]>", | ||
Subject: "coucou", | ||
From: "Bénédicte Ficiaire <[email protected]>", | ||
To: "rdv+8fae4d5f-4d63-4f60-b343-854d939881a3@reply.rdv-solidarites.fr", | ||
Date: "Thu, 12 May 2022 12:22:15 +0200", | ||
}, | ||
ExtractedMarkdownMessage: "Je souhaite annuler mon RDV", | ||
ExtractedMarkdownSignature: nil, | ||
RawHtmlBody: %(<html dir="ltr"><head></head><body style="text-align:left; direction:ltr;"><div>Je souhaite annuler mon RDV</div>\n</body></html>\n), | ||
RawTextBody: "Je souhaite annuler mon RDV\n", | ||
} | ||
end | ||
let(:sendinblue_payload) { sendinblue_valid_payload } # use valid payload by default | ||
|
||
context "when all goes well" do | ||
it "sends a notification email to the agent, containing the user reply" do | ||
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1) | ||
transferred_email = ActionMailer::Base.deliveries.last | ||
expect(transferred_email.to).to eq(["[email protected]"]) | ||
expect(transferred_email[:from].to_s).to eq(%("RDV Solidarités" <[email protected]>)) | ||
expect(transferred_email.html_part.body.to_s).to include("Dans le cadre du RDV du 20 mai, l'usager⋅e Bénédicte FICIAIRE a envoyé") | ||
expect(transferred_email.html_part.body.to_s).to include("Je souhaite annuler mon RDV") # reply content | ||
expect(transferred_email.html_part.body.to_s).to include(%(href="http://www.rdv-solidarites-test.localhost/admin/organisations/#{rdv.organisation_id}/rdvs/#{rdv.id})) | ||
describe "#reply_address_for_rdv" do | ||
it "uses rdv domain reply_host_name" do | ||
rdv = build(:rdv) | ||
domain = instance_double(Domain) | ||
|
||
allow(rdv).to receive(:uuid).and_return("aabb-1122") | ||
allow(rdv).to receive(:domain).and_return(domain) | ||
allow(domain).to receive(:reply_host_name).and_return "reply.rdv-solidarites.fr" | ||
|
||
expect(described_class.reply_address_for_rdv(rdv)).to eq("[email protected]") | ||
end | ||
end | ||
|
||
context "when reply token does not match any in DB" do | ||
let(:rdv_uuid) { "6df62597-632e-4be1-a273-708ab58e4765" } | ||
it "returns nil for a rdv whose domain reply_host_name is nil" do | ||
rdv = build(:rdv) | ||
domain = instance_double(Domain) | ||
|
||
it "sends a notification email to the default mailbox, containing the user reply" do | ||
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1) | ||
transferred_email = ActionMailer::Base.deliveries.last | ||
expect(transferred_email.to).to eq(["[email protected]"]) | ||
expect(transferred_email.from).to eq(["[email protected]"]) | ||
expect(transferred_email.html_part.body.to_s).to include(%(L'usager⋅e "Bénédicte Ficiaire" <[email protected]> a répondu)) | ||
expect(transferred_email.html_part.body.to_s).to include("Je souhaite annuler mon RDV") # reply content | ||
allow(rdv).to receive(:domain).and_return(domain) | ||
allow(domain).to receive(:reply_host_name).and_return nil | ||
|
||
expect(described_class.reply_address_for_rdv(rdv)).to be_nil | ||
end | ||
end | ||
|
||
context "when an e-mail address does not match our pattern" do | ||
let(:sendinblue_payload) do | ||
sendinblue_valid_payload.tap { |hash| hash[:Headers][:To] = "[email protected]" } | ||
describe "#perform" do | ||
subject(:perform_job) { described_class.perform_now(sendinblue_payload) } | ||
|
||
before do | ||
# Set a fixed date so we can assert on dates within email body | ||
travel_to(Time.zone.parse("2022-05-17 16:00:00")) | ||
end | ||
|
||
let!(:user) { create(:user, email: "[email protected]", first_name: "Bénédicte", last_name: "Ficiaire") } | ||
let!(:agent) { create(:agent, email: "[email protected]") } | ||
let(:rdv_uuid) { "8fae4d5f-4d63-4f60-b343-854d939881a3" } | ||
let!(:rdv) { create(:rdv, users: [user], agents: [agent], uuid: rdv_uuid) } | ||
|
||
let(:sendinblue_valid_payload) do | ||
# The usual payload has more info, but I removed non-essential fields for readability. | ||
# See: https://developers.sendinblue.com/docs/inbound-parsing-api-1#sample-payload | ||
{ | ||
Cc: [], | ||
ReplyTo: nil, | ||
Subject: "coucou", | ||
Attachments: [], | ||
Headers: { | ||
"Message-ID": "<[email protected]>", | ||
Subject: "coucou", | ||
From: "Bénédicte Ficiaire <[email protected]>", | ||
To: "rdv+8fae4d5f-4d63-4f60-b343-854d939881a3@reply.rdv-solidarites.fr", | ||
Date: "Thu, 12 May 2022 12:22:15 +0200", | ||
}, | ||
ExtractedMarkdownMessage: "Je souhaite annuler mon RDV", | ||
ExtractedMarkdownSignature: nil, | ||
RawHtmlBody: %(<html dir="ltr"><head></head><body style="text-align:left; direction:ltr;"><div>Je souhaite annuler mon RDV</div>\n</body></html>\n), | ||
RawTextBody: "Je souhaite annuler mon RDV\n", | ||
} | ||
end | ||
let(:sendinblue_payload) { sendinblue_valid_payload } # use valid payload by default | ||
|
||
it "is forwarded to default mailbox" do | ||
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1) | ||
transferred_email = ActionMailer::Base.deliveries.last | ||
expect(transferred_email.to).to eq(["[email protected]"]) | ||
expect(transferred_email.html_part.body.to_s).to include(%(L'usager⋅e "Bénédicte Ficiaire" <[email protected]> a répondu)) | ||
context "when all goes well" do | ||
it "sends a notification email to the agent, containing the user reply" do | ||
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1) | ||
transferred_email = ActionMailer::Base.deliveries.last | ||
expect(transferred_email.to).to eq(["[email protected]"]) | ||
expect(transferred_email[:from].to_s).to eq(%("RDV Solidarités" <[email protected]>)) | ||
expect(transferred_email.html_part.body.to_s).to include("Dans le cadre du RDV du 20 mai, l'usager⋅e Bénédicte FICIAIRE a envoyé") | ||
expect(transferred_email.html_part.body.to_s).to include("Je souhaite annuler mon RDV") # reply content | ||
expect(transferred_email.html_part.body.to_s).to include(%(href="http://www.rdv-solidarites-test.localhost/admin/organisations/#{rdv.organisation_id}/rdvs/#{rdv.id})) | ||
end | ||
end | ||
end | ||
|
||
context "when several agents are linked to the RDV" do | ||
let!(:other_agent) { create(:agent, email: "[email protected]").tap { |a| rdv.agents << a } } | ||
context "when reply token does not match any in DB" do | ||
let(:rdv_uuid) { "6df62597-632e-4be1-a273-708ab58e4765" } | ||
|
||
it "sends one email with all agents in the TO: field" do | ||
perform_job | ||
expect(ActionMailer::Base.deliveries.last.to).to contain_exactly("[email protected]", "[email protected]") | ||
it "sends a notification email to the default mailbox, containing the user reply" do | ||
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1) | ||
transferred_email = ActionMailer::Base.deliveries.last | ||
expect(transferred_email.to).to eq(["[email protected]"]) | ||
expect(transferred_email.from).to eq(["[email protected]"]) | ||
expect(transferred_email.html_part.body.to_s).to include(%(L'usager⋅e "Bénédicte Ficiaire" <[email protected]> a répondu)) | ||
expect(transferred_email.html_part.body.to_s).to include("Je souhaite annuler mon RDV") # reply content | ||
end | ||
end | ||
|
||
context "when an e-mail address does not match our pattern" do | ||
let(:sendinblue_payload) do | ||
sendinblue_valid_payload.tap { |hash| hash[:Headers][:To] = "[email protected]" } | ||
end | ||
|
||
it "is forwarded to default mailbox" do | ||
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1) | ||
transferred_email = ActionMailer::Base.deliveries.last | ||
expect(transferred_email.to).to eq(["[email protected]"]) | ||
expect(transferred_email.html_part.body.to_s).to include(%(L'usager⋅e "Bénédicte Ficiaire" <[email protected]> a répondu)) | ||
end | ||
end | ||
|
||
context "when an e-mail address matches our pattern for a demo host" do | ||
let(:sendinblue_payload) do | ||
sendinblue_valid_payload.tap { |hash| hash[:Headers][:To] = "rdv+8fae4d5f-4d63-4f60-b343-854d939881a3@reply.demo.rdv-solidarites.fr" } | ||
end | ||
|
||
it "sends a notification email to the agent" do | ||
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1) | ||
transferred_email = ActionMailer::Base.deliveries.last | ||
expect(transferred_email.to).to eq(["[email protected]"]) | ||
end | ||
end | ||
end | ||
|
||
context "when attachments are present" do | ||
let(:sendinblue_payload) do | ||
sendinblue_valid_payload.tap do |hash| | ||
hash[:Attachments] = [{ Name: "mon_scan.pdf", ContentType: "application/pdf" }] | ||
context "when several agents are linked to the RDV" do | ||
let!(:other_agent) { create(:agent, email: "[email protected]").tap { |a| rdv.agents << a } } | ||
|
||
it "sends one email with all agents in the TO: field" do | ||
perform_job | ||
expect(ActionMailer::Base.deliveries.last.to).to contain_exactly("[email protected]", "[email protected]") | ||
end | ||
end | ||
|
||
it "mentions the attachments in the notification e-mail" do | ||
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1) | ||
transferred_email = ActionMailer::Base.deliveries.last | ||
expect(transferred_email.html_part.body.to_s).to include(%(Le mail de l'usager⋅e avait en pièce jointe "mon_scan.pdf".)) | ||
context "when attachments are present" do | ||
let(:sendinblue_payload) do | ||
sendinblue_valid_payload.tap do |hash| | ||
hash[:Attachments] = [{ Name: "mon_scan.pdf", ContentType: "application/pdf" }] | ||
end | ||
end | ||
|
||
it "mentions the attachments in the notification e-mail" do | ||
expect { perform_job }.to change { ActionMailer::Base.deliveries.size }.by(1) | ||
transferred_email = ActionMailer::Base.deliveries.last | ||
expect(transferred_email.html_part.body.to_s).to include(%(Le mail de l'usager⋅e avait en pièce jointe "mon_scan.pdf".)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
it "renders the headers" do | ||
expect(mail[:from].to_s).to eq(%("RDV Solidarités" <[email protected]>)) | ||
expect(mail.to).to eq([user.email]) | ||
expect(mail.reply_to).to eq(["rdv+#{rdv.uuid}@reply.rdv-solidarites.fr"]) | ||
expect(mail.reply_to).to eq(["rdv+#{rdv.uuid}@reply.rdv-solidarites-test.localhost"]) | ||
end | ||
|
||
it "renders the subject" do | ||
|
@@ -82,7 +82,7 @@ | |
mail = described_class.with(rdv: rdv, user: user, token: token).rdv_updated(old_starts_at: previous_starting_time, lieu_id: nil) | ||
expect(mail[:from].to_s).to eq(%("RDV Solidarités" <[email protected]>)) | ||
expect(mail.to).to eq([user.email]) | ||
expect(mail.reply_to).to eq(["rdv+#{rdv.uuid}@reply.rdv-solidarites.fr"]) | ||
expect(mail.reply_to).to eq(["rdv+#{rdv.uuid}@reply.rdv-solidarites-test.localhost"]) | ||
end | ||
|
||
it "indicates the previous and current values" do | ||
|
@@ -118,7 +118,7 @@ | |
|
||
expect(mail[:from].to_s).to eq(%("RDV Solidarités" <[email protected]>)) | ||
expect(mail.to).to eq([user.email]) | ||
expect(mail.reply_to).to eq(["rdv+#{rdv.uuid}@reply.rdv-solidarites.fr"]) | ||
expect(mail.reply_to).to eq(["rdv+#{rdv.uuid}@reply.rdv-solidarites-test.localhost"]) | ||
end | ||
|
||
it "subject contains date of cancelled rdv" do | ||
|
@@ -176,7 +176,7 @@ | |
mail = described_class.with(rdv: rdv, user: user, token: token).rdv_upcoming_reminder | ||
expect(mail[:from].to_s).to eq(%("RDV Solidarités" <[email protected]>)) | ||
expect(mail.to).to eq([user.email]) | ||
expect(mail.reply_to).to eq(["rdv+#{rdv.uuid}@reply.rdv-solidarites.fr"]) | ||
expect(mail.reply_to).to eq(["rdv+#{rdv.uuid}@reply.rdv-solidarites-test.localhost"]) | ||
expect(mail.html_part.body).to include("Nous vous rappellons que vous avez un RDV prévu") | ||
expect(mail.html_part.body.raw_source).to include("/users/rdvs/#{rdv.id}?invitation_token=12345") | ||
end | ||
|
Oops, something went wrong.