Skip to content

Commit 5b8e72c

Browse files
JadedBlueEyesmwiencek
authored andcommitted
MBS-13876: Send verification emails through new mail service
1 parent fa2413d commit 5b8e72c

File tree

2 files changed

+39
-71
lines changed

2 files changed

+39
-71
lines changed

lib/MusicBrainz/Server/Email.pm

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -88,43 +88,6 @@ sub _create_email
8888
});
8989
}
9090

91-
sub _create_email_verification_email
92-
{
93-
my ($self, %opts) = @_;
94-
95-
my @headers = (
96-
'To' => $opts{email},
97-
'From' => $EMAIL_NOREPLY_ADDRESS,
98-
'Reply-To' => $EMAIL_SUPPORT_ADDRESS,
99-
'Message-Id' => _message_id('verify-email-%s', generate_gid()),
100-
'Subject' => 'Please verify your email address',
101-
);
102-
103-
my $verification_link = $opts{verification_link};
104-
my $ip = $opts{ip};
105-
my $user_name = $opts{editor}->name;
106-
107-
my $body = <<"EOS";
108-
Hello $user_name,
109-
110-
This is a verification email for your MusicBrainz account. Please click
111-
on the link below to verify your email address:
112-
113-
$verification_link
114-
115-
If clicking the link above doesn't work, please copy and paste the URL in a
116-
new browser window instead.
117-
118-
This email was triggered by a request from the IP address [$ip].
119-
120-
Thanks for using MusicBrainz!
121-
122-
-- The MusicBrainz Team
123-
EOS
124-
125-
return $self->_create_email(\@headers, $body);
126-
}
127-
12891
sub _create_email_in_use_email
12992
{
13093
my ($self, %opts) = @_;
@@ -446,8 +409,23 @@ sub send_email_verification
446409
{
447410
my ($self, %opts) = @_;
448411

449-
my $email = $self->_create_email_verification_email(%opts);
450-
return $self->_send_email($email);
412+
my $verification_link = $opts{verification_link};
413+
my $user_name = $opts{editor}->name;
414+
415+
my $body = {
416+
template_id => 'verify-email',
417+
to => $opts{email},
418+
from => $EMAIL_NOREPLY_ADDRESS,
419+
reply_to => $EMAIL_NOREPLY_ADDRESS,
420+
# TODO: send the user's language preference here.
421+
message_id => _message_id('verify-email-%s', generate_gid()),
422+
params => {
423+
to_name => $user_name,
424+
verification_url => "$verification_link",
425+
},
426+
};
427+
428+
$self->_mb_mail_service_send_single($body);
451429
}
452430

453431
sub send_email_in_use

t/lib/t/MusicBrainz/Server/Email.pm

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -153,39 +153,29 @@ test all => sub {
153153
};
154154

155155
subtest 'send_email_verification' => sub {
156+
$email->send_email_verification(
157+
email => 'user@example.com',
158+
verification_link => "$server/verify-email",
159+
editor => $user1,
160+
);
156161

157-
$email->send_email_verification(
158-
email => 'user@example.com',
159-
verification_link => "$server/verify-email",
160-
ip => '127.0.0.1',
161-
editor => $user1,
162-
);
163-
164-
is($email->transport->delivery_count, 1);
165-
my $delivery = $email->transport->shift_deliveries;
166-
is($delivery->{envelope}->{from}, 'noreply@musicbrainz.org', "Envelope from is noreply@...");
167-
my $e = $delivery->{email};
168-
$email->transport->clear_deliveries;
169-
is($e->get_header('From'), 'MusicBrainz Server <noreply@musicbrainz.org>', 'From is noreply@...');
170-
is($e->get_header('To'), 'user@example.com', 'To is user@example.com');
171-
is($e->get_header('Subject'), 'Please verify your email address', 'Subject is Please verify your email address');
172-
like($e->get_header('Message-Id'), qr{<verify-email-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}@.*>}, 'Message-Id has right format');
173-
compare_body($e->object->body_str,
174-
"Hello Editor 1,\n".
175-
"\n".
176-
"This is a verification email for your MusicBrainz account. Please click\n".
177-
"on the link below to verify your email address:\n".
178-
"\n".
179-
"$server/verify-email\n".
180-
"\n".
181-
"If clicking the link above doesn't work, please copy and paste the URL in a\n".
182-
"new browser window instead.\n".
183-
"This email was triggered by a request from the IP address [127.0.0.1].\n".
184-
"\n".
185-
"Thanks for using MusicBrainz!\n".
186-
"\n".
187-
"-- The MusicBrainz Team\n");
188-
162+
my $mail_service_req = shift(@mail_service_reqs);
163+
my $mail_service_req_content = decode_json($mail_service_req->content);
164+
is($mail_service_req->method, 'POST', 'mail service request method is POST');
165+
is($mail_service_req->uri, 'http://localhost:3000/send_single', 'mail service request uri is send_single');
166+
is($mail_service_req->header('Accept'), 'application/json', 'client accepts application/json');
167+
is($mail_service_req->header('Content-Type'), 'application/json', 'mail service content-type is application/json');
168+
cmp_deeply($mail_service_req_content, {
169+
template_id => 'verify-email',
170+
to => 'user@example.com',
171+
from => 'MusicBrainz Server <noreply@musicbrainz.org>',
172+
reply_to => 'MusicBrainz Server <noreply@musicbrainz.org>',
173+
message_id => re(qr/^<verify-email-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\@localhost>$/),
174+
params => {
175+
verification_url => "$server/verify-email",
176+
to_name => 'Editor 1',
177+
},
178+
}, 'mail service request content is correct');
189179
};
190180

191181
subtest 'send_lost_username' => sub {

0 commit comments

Comments
 (0)