Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNTOR-3848: Fix invalid assertions #5521

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { useTelemetry } from "../../../../../../hooks/useTelemetry";
export type Props = {
user: Session["user"];
subscriber: SubscriberRow;
data: SubscriberEmailPreferencesOutput;
data?: SubscriberEmailPreferencesOutput;
enabledFeatureFlags: FeatureFlagName[];
};

Expand All @@ -52,9 +52,10 @@ export const AlertAddressForm = (props: Props) => {
const breachAlertsEmailsAllowed = props.subscriber.all_emails_to_primary;

// Extract monthly report preference from the right column
const monitorReportAllowed = hasPremium(props.user)
? props.data.monthly_monitor_report
: props.data.monthly_monitor_report_free;
const monitorReportAllowed =
(hasPremium(props.user)
? props.data?.monthly_monitor_report
: props.data?.monthly_monitor_report_free) ?? true;

// TODO: Deprecate this when monthly report for free users has been created
const canToggleFreeOrPlusMonthlyReport =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,25 @@ const mockedSubscriptionBillingAmount = {
yearly: 13.37,
monthly: 42.42,
};
const mockedPlusSubscriberEmailPreferences: SubscriberEmailPreferencesOutput = {
const mockedPlusSubscriberEmailPreferences = {
id: 1337,
primary_email: "[email protected]",
unsubscribe_token: "495398jfjvjfdj",
monthly_monitor_report_free: false,
monthly_monitor_report_free_at: new Date("1337-04-02T04:02:42.000Z"),
monthly_monitor_report: true,
monthly_monitor_report_at: new Date("1337-04-02T04:02:42.000Z"),
};
} as SubscriberEmailPreferencesOutput;

const mockedFreeSubscriberEmailPreferences: SubscriberEmailPreferencesOutput = {
const mockedFreeSubscriberEmailPreferences = {
id: 1337,
primary_email: "[email protected]",
unsubscribe_token: "495398jfjvjfdj",
monthly_monitor_report_free: true,
monthly_monitor_report_free_at: new Date("1337-04-02T04:02:42.000Z"),
monthly_monitor_report: false,
monthly_monitor_report_at: new Date("1337-04-02T04:02:42.000Z"),
};
} as SubscriberEmailPreferencesOutput;

it("passes the axe accessibility audit", async () => {
const { container } = render(
Expand Down Expand Up @@ -904,6 +904,44 @@ it("calls the right telemetry event if a user opts out of monthly report", async
);
});

it("does not crash if no email preferences were found for the current user", () => {
const component = (
<TestComponentWrapper>
<SettingsView
l10n={getL10n()}
user={{
...mockedUser,
subscriber: {
...mockedUser.subscriber!,
all_emails_to_primary: true,
monthly_monitor_report: true,
},
}}
subscriber={{
...mockedSubscriber,
all_emails_to_primary: true,
monthly_monitor_report: true,
}}
breachCountByEmailAddress={{
[mockedUser.email]: 42,
[mockedSecondaryVerifiedEmail.email]: 42,
}}
emailAddresses={[mockedSecondaryVerifiedEmail]}
fxaSettingsUrl=""
fxaSubscriptionsUrl=""
yearlySubscriptionUrl=""
monthlySubscriptionUrl=""
subscriptionBillingAmount={mockedSubscriptionBillingAmount}
enabledFeatureFlags={["UpdatedEmailPreferencesOption"]}
experimentData={defaultExperimentData["Features"]}
isMonthlySubscriber={true}
data={undefined}
/>
</TestComponentWrapper>
);
expect(() => render(component)).not.toThrow();
});

it("refreshes the session token after changing email alert preferences, to ensure the latest pref is available in it", async () => {
global.fetch = jest.fn().mockResolvedValueOnce({ ok: true });
const user = userEvent.setup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,25 +226,25 @@ const mockedSubscriptionBillingAmount = {
yearly: 13.37,
monthly: 42.42,
};
const mockedPlusSubscriberEmailPreferences: SubscriberEmailPreferencesOutput = {
const mockedPlusSubscriberEmailPreferences = {
id: 1337,
primary_email: "[email protected]",
unsubscribe_token: "495398jfjvjfdj",
monthly_monitor_report_free: false,
monthly_monitor_report_free_at: new Date("1337-04-02T04:02:42.000Z"),
monthly_monitor_report: true,
monthly_monitor_report_at: new Date("1337-04-02T04:02:42.000Z"),
};
} as SubscriberEmailPreferencesOutput;

const mockedFreeSubscriberEmailPreferences: SubscriberEmailPreferencesOutput = {
const mockedFreeSubscriberEmailPreferences = {
id: 1337,
primary_email: "[email protected]",
unsubscribe_token: "495398jfjvjfdj",
monthly_monitor_report_free: true,
monthly_monitor_report_free_at: new Date("1337-04-02T04:02:42.000Z"),
monthly_monitor_report: false,
monthly_monitor_report_at: new Date("1337-04-02T04:02:42.000Z"),
};
} as SubscriberEmailPreferencesOutput;

const mockedSession = {
expires: new Date().toISOString(),
Expand Down Expand Up @@ -2395,6 +2395,43 @@ describe("Settings page redesign", () => {
expect(monthlyMonitorReportBtn).toHaveAttribute("aria-checked", "true");
});

it("does not crash when the preference for the monthly monitor report was unset", () => {
const component = (
<SettingsWrapper>
<SettingsView
activeTab="notifications"
l10n={getL10n()}
user={{
...mockedFreeUser,
}}
subscriber={{
...mockedSubscriber,
}}
breachCountByEmailAddress={{
[mockedUser.email]: 42,
[mockedSecondaryVerifiedEmail.email]: 42,
}}
emailAddresses={[mockedSecondaryVerifiedEmail]}
fxaSettingsUrl=""
fxaSubscriptionsUrl=""
yearlySubscriptionUrl=""
monthlySubscriptionUrl=""
subscriptionBillingAmount={mockedSubscriptionBillingAmount}
enabledFeatureFlags={[
"UpdatedEmailPreferencesOption",
"MonthlyReportFreeUser",
"SettingsPageRedesign",
]}
experimentData={defaultExperimentData["Features"]}
isMonthlySubscriber={false}
data={undefined}
/>
</SettingsWrapper>
);

expect(() => render(component)).not.toThrow();
});

it("checks that monthly monitor report is enabled", () => {
render(
<SettingsWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type Props = {
l10n: ExtendedReactLocalization;
user: Session["user"];
subscriber: SubscriberRow;
data: SubscriberEmailPreferencesOutput;
data?: SubscriberEmailPreferencesOutput;
monthlySubscriptionUrl: string;
yearlySubscriptionUrl: string;
subscriptionBillingAmount: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { onRemoveEmail } from "../actions";

export type SettingsPanelEditInfoProps = {
breachCountByEmailAddress: Record<string, number>;
data: SubscriberEmailPreferencesOutput;
data?: SubscriberEmailPreferencesOutput;
emailAddresses: SanitizedEmailAddressRow[];
subscriber: SubscriberRow;
user: Session["user"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import { RadioInput } from "../../../../../../../components/client/RadioInput";
import { CONST_URL_MOZILLA_BASKET } from "../../../../../../../../constants";

export type SettingsPanelNotificationsProps = {
data: SubscriberEmailPreferencesOutput;
data?: SubscriberEmailPreferencesOutput;
subscriber: SubscriberRow;
user: Session["user"];
};

export type NotificationSettingsProps = {
user: Session["user"];
subscriber: SubscriberRow;
data: SubscriberEmailPreferencesOutput;
data?: SubscriberEmailPreferencesOutput;
};

const EmailCommOption = {
Expand All @@ -52,9 +52,10 @@ export const NotificationsSettings = (props: NotificationSettingsProps) => {
const breachAlertsEmailsAllowed = props.subscriber.all_emails_to_primary;

// Extract monthly report preference from the right column
const monitorReportAllowed = hasPremium(props.user)
? props.data.monthly_monitor_report
: props.data.monthly_monitor_report_free;
const monitorReportAllowed =
(hasPremium(props.user)
? props.data?.monthly_monitor_report
: props.data?.monthly_monitor_report_free) ?? true;

const defaultActivateAlertEmail =
typeof breachAlertsEmailsAllowed === "boolean";
Expand Down
10 changes: 3 additions & 7 deletions src/app/functions/cronjobs/unsubscribeLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export async function getMonthlyActivityFreeUnsubscribeLink(
) {
try {
const newUnsubToken = randomBytes(64).toString("hex");
let sub;
const getRes = await getEmailPreferenceForSubscriber(subscriber.id);
if (getRes.unsubscribe_token) {
// if record has been created and the token exists, return the token
Expand All @@ -27,18 +26,15 @@ export async function getMonthlyActivityFreeUnsubscribeLink(
!getRes.unsubscribe_token
) {
// if record in the new table has not been created
sub = await addUnsubscribeTokenForSubscriber(
subscriber.id,
newUnsubToken,
);
await addUnsubscribeTokenForSubscriber(subscriber.id, newUnsubToken);
} else {
// if record already exists, but token doesn't exist, add the token
sub = await updateEmailPreferenceForSubscriber(subscriber.id, true, {
await updateEmailPreferenceForSubscriber(subscriber.id, true, {
unsubscribe_token: newUnsubToken,
});
}

return `${process.env.SERVER_URL}/unsubscribe-email/monthly-report-free?token=${sub.unsubscribe_token}`;
return `${process.env.SERVER_URL}/unsubscribe-email/monthly-report-free?token=${newUnsubToken}`;
} catch (e) {
logger.error("generate_unsubscribe_link", {
exception: e,
Expand Down
Loading
Loading