Skip to content

Commit

Permalink
Merge pull request #5149 from mozilla/MNTOR-3672
Browse files Browse the repository at this point in the history
MNTOR-3672: fix unique contraints violation
  • Loading branch information
mansaj authored Oct 9, 2024
2 parents 566e5d9 + 5b90e0b commit c07b24d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
9 changes: 2 additions & 7 deletions src/app/api/utils/email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

import { SubscriberRow } from "knex/types/tables";
import { resetUnverifiedEmailAddress } from "../../../db/tables/emailAddresses";
import { sendEmail } from "../../../utils/email";
import { sendEmail, randomToken } from "../../../utils/email";
import { renderEmail } from "../../../emails/renderEmail";
import { VerifyEmailAddressEmail } from "../../../emails/templates/verifyEmailAddress/VerifyEmailAddressEmail";
import { sanitizeSubscriberRow } from "../../functions/server/sanitize";
import { getL10n } from "../../functions/l10n/serverComponents";
import { BadRequestError } from "../../../utils/error";
import { captureException } from "@sentry/node";
import crypto from "crypto";
import {
addUnsubscribeTokenForSubscriber,
getEmailPreferenceForSubscriber,
Expand Down Expand Up @@ -67,7 +66,7 @@ export async function unsubscribeLinkForSubscriber(
subscriber: SerializedSubscriber,
) {
try {
const newUnsubToken = randomString();
const newUnsubToken = randomToken();
let sub;
const getRes = await getEmailPreferenceForSubscriber(subscriber.id);
if (getRes.unsubscribe_token) {
Expand Down Expand Up @@ -98,7 +97,3 @@ export async function unsubscribeLinkForSubscriber(
return null;
}
}

function randomString(length: number = 64) {
return crypto.randomBytes(length).toString("hex");
}
3 changes: 2 additions & 1 deletion src/db/tables/subscriber_email_preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import createDbConnection from "../connect";
import { logger } from "../../app/functions/server/logging";
import { captureException } from "@sentry/node";
import { randomToken } from "../../utils/email";

const knex = createDbConnection();

Expand Down Expand Up @@ -47,7 +48,7 @@ async function addEmailPreferenceForSubscriber(
res = await knex("subscriber_email_preferences")
.insert({
subscriber_id: subscriberId,
unsubscribe_token: preference.unsubscribe_token || "",
unsubscribe_token: preference.unsubscribe_token || randomToken(),
monthly_monitor_report_free:
preference.monthly_monitor_report_free ?? true,
// @ts-ignore knex.fn.now() results in it being set to a date,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { test, expect, jest } from "@jest/globals";
import type { createTransport, Transporter } from "nodemailer";
import { randomToken } from "./email";

jest.mock("nodemailer", () => {
return {
Expand Down Expand Up @@ -135,3 +136,8 @@ test("EmailUtils.init with empty host uses jsonTransport. logs messages", async
).toBe(sendMailInfo);
expect(mockedConsoleInfo).toHaveBeenCalledWith("sent_email", sendMailInfo);
});

test("randomToken returns a random token of 2xlength (because of hex)", () => {
const token = randomToken(32);
expect(token).toHaveLength(64);
});
7 changes: 6 additions & 1 deletion src/utils/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { createTransport, Transporter } from "nodemailer";
import crypto from "crypto";

import { SentMessageInfo } from "nodemailer/lib/smtp-transport/index.js";
import { getEnvVarsOrThrow } from "../envVars";
Expand Down Expand Up @@ -84,4 +85,8 @@ async function sendEmail(
}
}

export { initEmail, sendEmail };
function randomToken(length: number = 64) {
return crypto.randomBytes(length).toString("hex");
}

export { initEmail, sendEmail, randomToken };

0 comments on commit c07b24d

Please sign in to comment.