Skip to content

Commit

Permalink
MBS-13876: Send verification emails through new mail service
Browse files Browse the repository at this point in the history
  • Loading branch information
JadedBlueEyes authored and mwiencek committed Jan 13, 2025
1 parent cd8eb8e commit f018f79
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 71 deletions.
56 changes: 17 additions & 39 deletions lib/MusicBrainz/Server/Email.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,43 +88,6 @@ sub _create_email
});
}

sub _create_email_verification_email
{
my ($self, %opts) = @_;

my @headers = (
'To' => $opts{email},
'From' => $EMAIL_NOREPLY_ADDRESS,
'Reply-To' => $EMAIL_SUPPORT_ADDRESS,
'Message-Id' => _message_id('verify-email-%s', generate_gid()),
'Subject' => 'Please verify your email address',
);

my $verification_link = $opts{verification_link};
my $ip = $opts{ip};
my $user_name = $opts{editor}->name;

my $body = <<"EOS";
Hello $user_name,
This is a verification email for your MusicBrainz account. Please click
on the link below to verify your email address:
$verification_link
If clicking the link above doesn't work, please copy and paste the URL in a
new browser window instead.
This email was triggered by a request from the IP address [$ip].
Thanks for using MusicBrainz!
-- The MusicBrainz Team
EOS

return $self->_create_email(\@headers, $body);
}

sub _create_email_in_use_email
{
my ($self, %opts) = @_;
Expand Down Expand Up @@ -446,8 +409,23 @@ sub send_email_verification
{
my ($self, %opts) = @_;

my $email = $self->_create_email_verification_email(%opts);
return $self->_send_email($email);
my $verification_link = $opts{verification_link};
my $user_name = $opts{editor}->name;

my $body = {
template_id => 'verify-email',
to => $opts{email},
from => $EMAIL_NOREPLY_ADDRESS,
reply_to => $EMAIL_NOREPLY_ADDRESS,
# TODO: send the user's language preference here.
message_id => _message_id('verify-email-%s', generate_gid()),
params => {
to_name => $user_name,
verification_url => "$verification_link",
},
};

$self->_mb_mail_service_send_single($body);
}

sub send_email_in_use
Expand Down
54 changes: 22 additions & 32 deletions t/lib/t/MusicBrainz/Server/Email.pm
Original file line number Diff line number Diff line change
Expand Up @@ -153,39 +153,29 @@ test all => sub {
};

subtest 'send_email_verification' => sub {
$email->send_email_verification(
email => 'user@example.com',
verification_link => "$server/verify-email",
editor => $user1,
);

$email->send_email_verification(
email => 'user@example.com',
verification_link => "$server/verify-email",
ip => '127.0.0.1',
editor => $user1,
);

is($email->transport->delivery_count, 1);
my $delivery = $email->transport->shift_deliveries;
is($delivery->{envelope}->{from}, 'noreply@musicbrainz.org', "Envelope from is noreply@...");
my $e = $delivery->{email};
$email->transport->clear_deliveries;
is($e->get_header('From'), 'MusicBrainz Server <noreply@musicbrainz.org>', 'From is noreply@...');
is($e->get_header('To'), 'user@example.com', 'To is user@example.com');
is($e->get_header('Subject'), 'Please verify your email address', 'Subject is Please verify your email address');
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');
compare_body($e->object->body_str,
"Hello Editor 1,\n".
"\n".
"This is a verification email for your MusicBrainz account. Please click\n".
"on the link below to verify your email address:\n".
"\n".
"$server/verify-email\n".
"\n".
"If clicking the link above doesn't work, please copy and paste the URL in a\n".
"new browser window instead.\n".
"This email was triggered by a request from the IP address [127.0.0.1].\n".
"\n".
"Thanks for using MusicBrainz!\n".
"\n".
"-- The MusicBrainz Team\n");

my $mail_service_req = shift(@mail_service_reqs);
my $mail_service_req_content = decode_json($mail_service_req->content);
is($mail_service_req->method, 'POST', 'mail service request method is POST');
is($mail_service_req->uri, 'http://localhost:3000/send_single', 'mail service request uri is send_single');
is($mail_service_req->header('Accept'), 'application/json', 'client accepts application/json');
is($mail_service_req->header('Content-Type'), 'application/json', 'mail service content-type is application/json');
cmp_deeply($mail_service_req_content, {
template_id => 'verify-email',
to => 'user@example.com',
from => 'MusicBrainz Server <noreply@musicbrainz.org>',
reply_to => 'MusicBrainz Server <noreply@musicbrainz.org>',
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>$/),
params => {
verification_url => "$server/verify-email",
to_name => 'Editor 1',
},
}, 'mail service request content is correct');
};

subtest 'send_lost_username' => sub {
Expand Down

0 comments on commit f018f79

Please sign in to comment.