-
Notifications
You must be signed in to change notification settings - Fork 281
fix(breachAlerts): update cron job to autoscaling service #6287
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
base: main
Are you sure you want to change the base?
Changes from all commits
a5ef98c
980ed68
6782510
5c51af5
9fa68a5
2668aac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { parseMarkup } from "./parseMarkup"; | ||
|
|
||
| describe("parseMarkup", () => { | ||
| it("exits early if no brackets and returns text node", () => { | ||
| expect(parseMarkup("some text")).toStrictEqual([ | ||
| { nodeName: "#text", textContent: "some text" }, | ||
| ]); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { isUsingMockHIBPEndpoint, isUsingMockONEREPEndpoint } from "./mock"; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added due to coverage complaint (no changes to the files) |
||
|
|
||
| describe("mock detectors", () => { | ||
| const originalEnv = process.env; | ||
| afterEach(() => { | ||
| process.env = { ...originalEnv }; | ||
| }); | ||
| afterAll(() => (process.env = originalEnv)); | ||
| describe("isUsingMockHibpEndpoint", () => { | ||
| it.each([ | ||
| ["http://localhost/v1/api/mock/path", true], | ||
| ["api/mock", true], | ||
| ["http://localhost/v1/api/path", false], | ||
| [undefined, false], | ||
| ["", false], | ||
| ])("detects mock path in HIBP_KANON_API_ROOT env var", (path, expected) => { | ||
| process.env.HIBP_KANON_API_ROOT = path; | ||
| expect(isUsingMockHIBPEndpoint()).toEqual(expected); | ||
| }); | ||
| }); | ||
| describe("isUsingMockONEREPEndpoint", () => { | ||
| it.each([ | ||
| ["http://localhost/v1/api/mock/path", true], | ||
| ["api/mock", true], | ||
| ["http://localhost/v1/api/path", false], | ||
| [undefined, false], | ||
| ["", false], | ||
| ])("detects mock path in ONEREP_API_BASE env var", (path, expected) => { | ||
| process.env.ONEREP_API_BASE = path; | ||
| expect(isUsingMockONEREPEndpoint()).toEqual(expected); | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,11 @@ | |
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| export function isUsingMockHIBPEndpoint() { | ||
| return process.env.HIBP_KANON_API_ROOT?.includes("api/mock") as boolean; | ||
| return !!process.env.HIBP_KANON_API_ROOT?.includes("api/mock"); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was returning undefined if HIBP_KANON_API_ROOT was unset; |
||
| } | ||
|
|
||
| export function isUsingMockONEREPEndpoint() { | ||
| return process.env.ONEREP_API_BASE?.includes("api/mock") as boolean; | ||
| return !!process.env.ONEREP_API_BASE?.includes("api/mock"); | ||
| } | ||
|
|
||
| export const ONEREP_API_BASE = process.env.ONEREP_API_BASE; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { seeds } from "../../test/db"; | ||
| import createDbConnection from "../connect"; | ||
| import { faker } from "@faker-js/faker"; | ||
| import { breachNotificationSubscribersByHashes } from "./BreachNotificationSubscriber"; | ||
| import type { SubscriberRow } from "knex/types/tables"; | ||
|
|
||
| describe("BreachNotificationSubscriber", () => { | ||
| const subscriber = seeds.subscribers({ primary_verified: true }); | ||
| const chaffSubs = Array.from(Array(10).keys()).map((_) => | ||
| seeds.subscribers(), | ||
| ); | ||
| let insertedSubscriber: SubscriberRow; | ||
|
|
||
| const conn = createDbConnection(); | ||
| beforeEach(async () => { | ||
| insertedSubscriber = ( | ||
| await conn("subscribers").insert(subscriber).returning("*") | ||
| )[0]; | ||
| // Seed subscribers and emails | ||
| const insertedChaff = await conn("subscribers") | ||
| .insert(chaffSubs) | ||
| .returning("*"); | ||
| const chaffEmails = insertedChaff.flatMap((subscriber) => | ||
| Array.from(Array(faker.number.int({ max: 20 })).keys()).map((_) => | ||
| seeds.emails(subscriber.id), | ||
| ), | ||
| ); | ||
| await conn("email_addresses").insert(chaffEmails); | ||
| }); | ||
| afterEach(async () => { | ||
| await conn.raw(`TRUNCATE TABLE subscribers CASCADE`); | ||
| await conn.raw(`TRUNCATE TABLE email_addresses CASCADE`); | ||
| }); | ||
| afterAll(async () => { | ||
| await conn.destroy(); | ||
| }); | ||
| it("returns rows with matching primary and secondary emails", async () => { | ||
| const insertedEmails = await conn("email_addresses") | ||
| .insert([ | ||
| seeds.emails(insertedSubscriber.id, { verified: true }), | ||
| seeds.emails(insertedSubscriber.id, { verified: true }), | ||
| ]) | ||
| .returning("*"); | ||
| const hashes = [insertedEmails[0].sha1, subscriber.primary_sha1]; | ||
| const actual = await breachNotificationSubscribersByHashes(hashes); | ||
| expect(actual.length).toEqual(2); | ||
| expect(actual).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| subscriber_id: insertedSubscriber.id, | ||
| breached_email: insertedSubscriber.primary_email, | ||
| primary_email: insertedSubscriber.primary_email, | ||
| }), | ||
| expect.objectContaining({ | ||
| subscriber_id: insertedSubscriber.id, | ||
| breached_email: insertedEmails[0].email, | ||
| primary_email: insertedSubscriber.primary_email, | ||
| }), | ||
| ]), | ||
| ); | ||
| }); | ||
| it("returns primary email only if no secondary emails match", async () => { | ||
| await conn("email_addresses") | ||
| .insert([ | ||
| seeds.emails(insertedSubscriber.id, { verified: true }), | ||
| seeds.emails(insertedSubscriber.id, { verified: true }), | ||
| ]) | ||
| .returning("*"); | ||
| const hashes = ["0000000000000", subscriber.primary_sha1]; | ||
| const actual = await breachNotificationSubscribersByHashes(hashes); | ||
| expect(actual.length).toEqual(1); | ||
| expect(actual).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| subscriber_id: insertedSubscriber.id, | ||
| breached_email: insertedSubscriber.primary_email, | ||
| primary_email: insertedSubscriber.primary_email, | ||
| }), | ||
| ]), | ||
| ); | ||
| }); | ||
| it("returns secondary email only if no primary emails match", async () => { | ||
| const insertedEmails = await conn("email_addresses") | ||
| .insert([ | ||
| seeds.emails(insertedSubscriber.id, { verified: true }), | ||
| seeds.emails(insertedSubscriber.id, { verified: true }), | ||
| ]) | ||
| .returning("*"); | ||
| const hashes = ["0000000000000", insertedEmails[0].sha1]; | ||
| const actual = await breachNotificationSubscribersByHashes(hashes); | ||
| expect(actual.length).toEqual(1); | ||
| expect(actual).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| subscriber_id: insertedSubscriber.id, | ||
| breached_email: insertedEmails[0].email, | ||
| primary_email: insertedSubscriber.primary_email, | ||
| }), | ||
| ]), | ||
| ); | ||
| }); | ||
| it("sets notification email to primary if preferred", async () => { | ||
| const primaryDefaultSub = ( | ||
| await conn("subscribers") | ||
| .insert( | ||
| seeds.subscribers({ | ||
| primary_verified: true, | ||
| all_emails_to_primary: true, | ||
| }), | ||
| ) | ||
| .returning("*") | ||
| )[0]; | ||
| const insertedEmails = await conn("email_addresses") | ||
| .insert([ | ||
| seeds.emails(primaryDefaultSub.id, { verified: true }), | ||
| seeds.emails(primaryDefaultSub.id, { verified: true }), | ||
| ]) | ||
| .returning("*"); | ||
| const hashes = ["0000000000000", insertedEmails[0].sha1]; | ||
| const actual = await breachNotificationSubscribersByHashes(hashes); | ||
| expect(actual.length).toEqual(1); | ||
| expect(actual).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| subscriber_id: primaryDefaultSub.id, | ||
| breached_email: insertedEmails[0].email, | ||
| primary_email: primaryDefaultSub.primary_email, | ||
| notification_email: primaryDefaultSub.primary_email, | ||
| }), | ||
| ]), | ||
| ); | ||
| }); | ||
| it("sets notification email to secondary address if primary not preferred", async () => { | ||
| const primaryNotDefaultSub = ( | ||
| await conn("subscribers") | ||
| .insert( | ||
| seeds.subscribers({ | ||
| primary_verified: true, | ||
| all_emails_to_primary: false, | ||
| }), | ||
| ) | ||
| .returning("*") | ||
| )[0]; | ||
| const insertedEmails = await conn("email_addresses") | ||
| .insert([ | ||
| seeds.emails(primaryNotDefaultSub.id, { verified: true }), | ||
| seeds.emails(primaryNotDefaultSub.id, { verified: true }), | ||
| ]) | ||
| .returning("*"); | ||
| const hashes = ["0000000000000", insertedEmails[0].sha1]; | ||
| const actual = await breachNotificationSubscribersByHashes(hashes); | ||
| expect(actual.length).toEqual(1); | ||
| expect(actual).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| subscriber_id: primaryNotDefaultSub.id, | ||
| breached_email: insertedEmails[0].email, | ||
| primary_email: primaryNotDefaultSub.primary_email, | ||
| notification_email: insertedEmails[0].email, | ||
| }), | ||
| ]), | ||
| ); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added due to coverage complaint (no changes to method)