Skip to content

Commit 4e6fcc2

Browse files
committed
Improving testing and putting back yesterdays stupid changes
1 parent 1196db4 commit 4e6fcc2

File tree

5 files changed

+56
-21
lines changed

5 files changed

+56
-21
lines changed

app/jobs/send_cms_emails_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def process_template(template)
1717
users = Programmes::ProgressQuery.new(data.programme, data.activity_state, data.enrolled, data.completed_programme_activity_groups).call
1818

1919
users.each do |user|
20-
CmsMailer.with(template: data, user_id: user.id).send_template.deliver_later
20+
CmsMailer.with(template_slug: data.slug, user_id: user.id).send_template.deliver_later
2121
end
2222
end
2323
end

app/mailers/cms_mailer.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
class CmsMailer < ApplicationMailer
22
def send_template
3-
@template = params[:template]
3+
@template_slug = params[:template_slug]
44
@user = User.find(params[:user_id])
55

6-
@subject = @template.subject(@user)
6+
begin
7+
@template = Cms::Collections::EmailTemplate.get(@template_slug).template
78

8-
mail(to: @user.email, subject: @subject)
9+
@subject = @template.subject(@user)
10+
11+
mail(to: @user.email, subject: @subject)
12+
rescue ActiveRecord::RecordNotFound
13+
Sentry.capture_message("Failed to load the email template #{@template_slug}")
14+
end
915
end
1016
end

spec/jobs/send_cms_emails_job_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
RSpec.describe SendCmsEmailsJob, type: :job do
44
let(:email_template_1_slug) { "send-cms-email-template-1-slug" }
55
let(:email_template_2_slug) { "send-cms-email-template-2-slug" }
6+
let(:users) { create_list(:user, 2) }
67

78
let(:email_template_1) {
89
Cms::Mocks::EmailTemplate.generate_raw_data(slug: email_template_1_slug)
@@ -15,9 +16,16 @@
1516
stub_strapi_email_template(email_template_1_slug, email_template: email_template_1)
1617
stub_strapi_email_template(email_template_2_slug, email_template: email_template_2)
1718
stub_strapi_email_templates(email_templates: [email_template_1, email_template_2])
19+
20+
allow_any_instance_of(Programmes::ProgressQuery).to receive(:call).and_return(users)
1821
end
1922

20-
it "should iterate through templates" do
21-
described_class.perform_now
23+
it "should enqueue both templates" do
24+
expect do
25+
described_class.perform_now
26+
end.to have_enqueued_mail(CmsMailer, :send_template).with(a_hash_including(params: {user_id: users.first.id, template_slug: email_template_1_slug}))
27+
.and have_enqueued_mail(CmsMailer, :send_template).with(a_hash_including(params: {user_id: users.first.id, template_slug: email_template_2_slug}))
28+
.and have_enqueued_mail(CmsMailer, :send_template).with(a_hash_including(params: {user_id: users.second.id, template_slug: email_template_1_slug}))
29+
.and have_enqueued_mail(CmsMailer, :send_template).with(a_hash_including(params: {user_id: users.second.id, template_slug: email_template_2_slug}))
2230
end
2331
end

spec/mailers/cms_mailer_spec.rb

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,28 @@
5151
]
5252
}
5353
let(:email_template) {
54-
Cms::Providers::Strapi::Factories::ModelFactory.to_email_template(
55-
Cms::Mocks::EmailTemplate.generate_data(
56-
subject:,
57-
slug:,
58-
email_content:
59-
)
54+
Cms::Mocks::EmailTemplate.generate_raw_data(
55+
subject:,
56+
slug:,
57+
email_content:
6058
)
6159
}
6260
let(:email_template_merge_subject) {
63-
Cms::Providers::Strapi::Factories::ModelFactory.to_email_template(
64-
Cms::Mocks::EmailTemplate.generate_data(
65-
subject: "Congrats {first_name} you did {last_cpd_title}",
66-
slug: slug_with_merge_subject,
67-
email_content:
68-
)
61+
Cms::Mocks::EmailTemplate.generate_raw_data(
62+
subject: "Congrats {first_name} you did {last_cpd_title}",
63+
slug: slug_with_merge_subject,
64+
email_content:
6965
)
7066
}
7167

68+
before do
69+
stub_strapi_email_template(slug, email_template:)
70+
stub_strapi_email_template(slug_with_merge_subject, email_template: email_template_merge_subject)
71+
end
72+
7273
describe "send_template" do
7374
before do
74-
@mail = CmsMailer.with(user_id: user.id, template: email_template).send_template
75+
@mail = described_class.with(user_id: user.id, template_slug: slug).send_template
7576
end
7677

7778
it "renders the headers" do
@@ -130,7 +131,7 @@
130131
before do
131132
travel_to 1.day.from_now do
132133
create(:completed_achievement, activity: second_activity, user:)
133-
@future_mail = CmsMailer.with(user_id: user.id, template: email_template).send_template
134+
@future_mail = described_class.with(user_id: user.id, template_slug: slug).send_template
134135
end
135136
end
136137

@@ -146,7 +147,7 @@
146147

147148
describe "send_template with merged subject" do
148149
before do
149-
@mail = CmsMailer.with(user_id: user.id, template: email_template_merge_subject).send_template
150+
@mail = described_class.with(user_id: user.id, template_slug: slug_with_merge_subject).send_template
150151
end
151152

152153
it "renders the headers" do
@@ -155,4 +156,20 @@
155156
expect(@mail.from).to eq(["[email protected]"])
156157
end
157158
end
159+
160+
describe "with missing template" do
161+
let(:missing_slug) { "missing_template_slug" }
162+
163+
before do
164+
stub_strapi_email_template_missing(missing_slug)
165+
allow(Sentry).to receive(:capture_message)
166+
end
167+
168+
it "logs error to sentry" do
169+
described_class.with(user_id: user.id, template_slug: missing_slug).send_template.deliver_now
170+
expect(Sentry)
171+
.to have_received(:capture_message)
172+
.with("Failed to load the email template #{missing_slug}")
173+
end
174+
end
158175
end

spec/support/cms/providers/strapi/strapi_stubs.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ def stub_strapi_email_template(key, email_template: Cms::Mocks::EmailTemplate.ge
174174
stub_request(:get, /^https:\/\/strapi.teachcomputing.org\/api\/email-templates\/#{key}/).to_return_json(body: {data: email_template})
175175
end
176176

177+
def stub_strapi_email_template_missing(key)
178+
stub_request(:get, /^https:\/\/strapi.teachcomputing.org\/api\/email-templates\/#{key}/).to_return_json(body: not_found_response, status: 404)
179+
end
180+
177181
def stub_strapi_programme(key, programme: Cms::Mocks::Programme.generate_raw_data)
178182
if as_graphql
179183
stub_strapi_graphql_query("programmes", programme)

0 commit comments

Comments
 (0)