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-3289-1: tests reverted in mntor-3289 #4742

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bccbb1e
feat: mock HIBP
mansaj May 29, 2024
b32ca9f
Merge branch 'MNTOR-3227-2' of https://github.com/mozilla/blurts-serv…
Jun 11, 2024
a0d9df7
able to default to mock endpoint on failure locally.
Jun 13, 2024
695e1fc
made it host-flexibile for HIBP fallback
Jun 13, 2024
e07af0e
npx prettified fakeBraches.json
Jun 13, 2024
c0b253a
made the code cleaner, and switch back to true endpoint after using f…
Jun 13, 2024
25b590b
got rid of second promise wait
Jun 13, 2024
ffb0550
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jun 17, 2024
ab680af
removed other features, kept only the mock endpoint
Jun 17, 2024
184cfcf
made breaches list match the fake breaches for a user
Jun 17, 2024
b7d9575
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jun 17, 2024
c922556
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jun 18, 2024
dda9c22
file endpoint additions
Jun 20, 2024
d809bea
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jun 24, 2024
0f1d778
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jun 25, 2024
988997c
funtional commit - mocked accessed endpoints
Jun 25, 2024
fb2d028
fixed the path overwriting issue, added responsive dynamic configurat…
Jun 25, 2024
2a4e504
moved mock user data into a separate file
Jun 26, 2024
0b1d9e5
fixed some code quality and substitued constants with function calls
Jun 26, 2024
f94238e
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jun 26, 2024
29b6f0e
Allowed for data to be configured dynamically using an endpoint
Jun 26, 2024
8ec080d
lint fail fixed
Jun 26, 2024
9140e01
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jun 26, 2024
f3e9b37
most tests pass when using mock onerep endpoint
Jun 26, 2024
bc7c4de
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jun 28, 2024
11cb314
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jun 28, 2024
71e5ddf
passes all tests except for those in purchase spec
Jun 28, 2024
d6eb4ed
added production checks
Jun 28, 2024
842b229
fixed lint errors
Jun 29, 2024
9d368c0
reverting smoke test
Jun 29, 2024
29af535
cleaned up query params and dynamic routing
Jun 29, 2024
fa16c41
fixed lint errors and corrected a POST endpoint
Jun 29, 2024
f3378b0
added form input
Jun 30, 2024
0a4e15b
substituted direct magic numbers access to wrapper methods
Jun 30, 2024
f076a56
merging with 3285
Jun 30, 2024
06b25ca
mock endpoint config - basic version
Jun 30, 2024
cf0ebf6
added more type checks and proper defaults
Jun 30, 2024
9fed164
functional OneRep config page, with styling
Jun 30, 2024
c98d0b2
lint error
Jun 30, 2024
f8e6b52
added a configuration endpoint and page for HIBP
Jul 1, 2024
a249654
reset package-lock
Jul 1, 2024
cb8e013
lint
Jul 1, 2024
21a6bc5
package-lock
Jul 1, 2024
839a9da
slightly modified hibp/breaches
Jul 1, 2024
feaf567
removed database retrieval from mock hibp endpoint
Jul 1, 2024
d3219fb
Merge branch 'main' of https://github.com/mozilla/blurts-server into …
Jul 1, 2024
f693e1a
Merged with main + minor error log change
Jul 1, 2024
9401fed
reverted e2e changes
Jul 2, 2024
d581682
e2e check
Jul 2, 2024
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
passes all tests except for those in purchase spec
  • Loading branch information
Mukhamediyar Kudaikulov committed Jun 28, 2024
commit 71e5ddf1f29be53761541c0796318daf532f9843
32 changes: 31 additions & 1 deletion src/app/api/mock/hibp/data/fakeAllBreaches.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@
"IsSpamList": false,
"IsMalware": false,
"FaviconUrl": null
}
},
{
"Id": 95,
"Name": "Bonobos",
"Title": "Bonobos",
"Domain": "bonobos.com",
"BreachDate": "2020-08-14T07:00:00.000Z",
"AddedDate": "2021-01-31T00:09:25.000Z",
"ModifiedDate": "2021-01-31T00:12:58.000Z",
"PwnCount": 2811929,
"Description": "In August 2020, the clothing store <a href=\"https://www.bleepingcomputer.com/news/security/bonobos-clothing-store-suffers-a-data-breach-hacker-leaks-70gb-database/\" target=\"_blank\" rel=\"noopener\">Bonobos suffered a data breach</a> that exposed almost 70GB of data containing 2.8 million unique email addresses. The breach also exposed names, physical and IP addresses, phone numbers, order histories and passwords stored as salted SHA-512 hashes, including historical passwords. The breach also exposed partial credit card data including card type, the name on the card, expiry date and the last 4 digits of the card. The data was provided to HIBP by <a href=\"https://dehashed.com/\" target=\"_blank\" rel=\"noopener\">dehashed.com</a>.",
"LogoPath": "https://haveibeenpwned.com/Content/Images/PwnedLogos/Bonobos.png",
"DataClasses": [
"email-addresses",
"historical-passwords",
"ip-addresses",
"names",
"partial-credit-card-data",
"passwords",
"phone-numbers",
"physical-addresses",
"purchases"
],
"IsVerified": true,
"IsFabricated": false,
"IsSensitive": false,
"IsRetired": false,
"IsSpamList": false,
"IsMalware": false,
"FaviconUrl": null
}
]
}
2 changes: 1 addition & 1 deletion src/app/api/mock/hibp/data/fakeBreaches.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"data": [
{
"hashSuffix": "",
"websites": ["000webhost", "123RF"]
"websites": ["000webhost", "123RF", "Bonobos"]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ export function GET() {
const userEmail = process.env.E2E_TEST_ACCOUNT_EMAIL;
//TODO: getServerSession doesn't work here for some reason
const currentUserSha = getSha1(userEmail as BinaryLike);
logger.info("Mock endpoint: /breachedaccount/range/");
logger.info("Mock endpoint: /range/search/");

let data = fakeBreaches.data as BreachedAccountResponse;

data = data.map((elem) => ({
...elem,
hashSuffix: currentUserSha.slice(6).toUpperCase(),
}));
return NextResponse.json(data);
const res = NextResponse.json(data);
console.log("opa1", data);
return res;
}
38 changes: 28 additions & 10 deletions src/app/api/mock/onerep/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
* 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 { randomInt } from "crypto";
import { StateAbbr } from "../../../../../utils/states";
import MockUser from "./mockUser.json";
// import { getLatestOnerepScanResults } from "../../../../../db/tables/onerep_scans";

interface Broker {
id: number;
Expand Down Expand Up @@ -31,8 +33,8 @@ export function MOCK_ONEREP_PROFILE_ID() {
return MockUser.PROFILE_ID;
}

export function MOCK_ONEREP_SCAN_ID() {
return MockUser.SCAN_ID;
export function MOCK_ONEREP_MAGIC_NUM_0() {
return MockUser.MAGIC_NUM_0;
}

export function MOCK_ONEREP_TIME() {
Expand Down Expand Up @@ -68,16 +70,30 @@ export function MOCK_ONEREP_ADDRESSES() {
})) as typeOfAddr;
}

const DEFAULT_NUMBER_BREACHES = 10;
const magicNum0 = 37680;
const magicNum1 = 23;
const DEFAULT_NUMBER_BREACHES = 5;
const MAGIC_NUM_brokerId = 78127;
const MAGIC_NUM_1 = 24623;
const MAGIC_NUM_2 = 2161;

export function MOCK_ONEREP_MAGIC_NUM_1() {
return MAGIC_NUM_1;
}

export function MOCK_ONEREP_MAGIC_NUM_2() {
return MAGIC_NUM_2;
}

function getScanId(profileId: string) {
return (Number(profileId) * MAGIC_NUM_1) % MAGIC_NUM_2;
}

export function MOCK_ONEREP_BROKERS(
profileId: string,
page: string,
perPage: string,
) {
const mockResponseData = MockUser.BROKERS_LIST;
const latestScanId = getScanId(profileId);

const mockLinks = {
first: `${process.env.ONEREP_API_BASE}/scan-results?profile_id%5B0%5D=${profileId}&per_page=${perPage}&page=${page}`,
Expand Down Expand Up @@ -112,19 +128,21 @@ export function MOCK_ONEREP_BROKERS(
return {
...(broker as Broker),
profile_id: profileId,
scan_id: MOCK_ONEREP_SCAN_ID(),
scan_id: latestScanId,
};
});
}

return mockResponseData;
}

const idStart = latestScanId * MOCK_ONEREP_MAGIC_NUM_0();

const responseData = {
data: new Array(DEFAULT_NUMBER_BREACHES).fill(null).map((_, index) => ({
id: magicNum0 - index,
id: idStart - index,
profile_id: profileId,
scan_id: MOCK_ONEREP_SCAN_ID(),
scan_id: latestScanId,
status: "new",
first_name: MOCK_ONEREP_FIRSTNAME(),
middle_name: null,
Expand All @@ -136,11 +154,11 @@ export function MOCK_ONEREP_BROKERS(
relatives: [],
link: `https://mockexample.com/link-to-databroker${index}`,
data_broker: `mockexample${index}.com`,
data_broker_id: magicNum1 - index,
data_broker_id: MAGIC_NUM_brokerId - index,
optout_attempts: 0,
created_at: MOCK_ONEREP_TIME(),
updated_at: MOCK_ONEREP_TIME(),
url: `${process.env.ONEREP_API_BASE}scan-results/${magicNum0 - index}`,
url: `${process.env.ONEREP_API_BASE}scan-results/${idStart - index}`,
})),
links: mockLinks,
meta: mockMeta,
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/mock/onerep/config/mockUser.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"PROFILE_ID": 777,
"SCAN_ID": 129837123,
"MAGIC_NUM_0": 2914,
"TIME": "2024-06-19T01:37:02+0000",
"FIRSTNAME": "John",
"LASTNAME": "Doe",
Expand All @@ -19,4 +19,4 @@
"meta": {},
"valid": false
}
}
}
2 changes: 2 additions & 0 deletions src/app/api/mock/onerep/config/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { NextRequest, NextResponse } from "next/server";
import fs from "fs";
import path from "path";
import { randomInt } from "crypto";

/*

Expand Down Expand Up @@ -43,6 +44,7 @@ export async function POST(req: NextRequest) {
if (erase) {
jsonData.BROKERS_LIST.data = [];
jsonData.BROKERS_LIST.valid = false;
jsonData.MAGIC_NUM_0 = randomInt(1000, 100000);
} else {
if (!newData.brokers || !newData.brokers.data) {
return NextResponse.json(
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/mock/onerep/config/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"data": [
{
"id": 45000,
"profile_id": 0,
"scan_id": 0,
"profile_id": -1,
"scan_id": -1,
"status": "new",
"first_name": "John",
"middle_name": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { NextRequest, NextResponse } from "next/server";

export function PUT(req: NextRequest) {
export function POST(req: NextRequest) {
const profileId: number = Number(req.url.match(/profiles\/([0-9]+)/)![1]);

if (!profileId || isNaN(profileId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function GET(req: NextRequest) {
const profileId = Number(req.url.match(/profiles\/([0-9]+)/)![1]);
const scanId = Number(req.url.match(/scans\/([0-9]+)/)![1]);

// Check for the availability of the scan
const responseData = {
id: scanId,
profile_id: profileId,
Expand Down
25 changes: 17 additions & 8 deletions src/app/api/mock/onerep/profiles/[profileId]/scans/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,38 @@
* 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 { MOCK_ONEREP_SCAN_ID, MOCK_ONEREP_TIME } from "../../../config/config";
import { NextRequest, NextResponse } from "next/server";

//TODO: mock out the id field and url
import {
MOCK_ONEREP_TIME,
MOCK_ONEREP_MAGIC_NUM_1,
MOCK_ONEREP_MAGIC_NUM_2,
} from "../../../config/config";

function extractProfileId(req: NextRequest) {
const idFromUrl = Number(req.url.match(/profiles\/([0-9]+)\/scans/)![1]);
return idFromUrl;
}

function getScanId(profileId: number) {
return (profileId * MOCK_ONEREP_MAGIC_NUM_1()) % MOCK_ONEREP_MAGIC_NUM_2();
}

export function POST(req: NextRequest) {
const profileId: number = extractProfileId(req);
if (!profileId || isNaN(profileId)) {
return NextResponse.json({ error: "Invalid profile ID" });
}

const scanId = getScanId(profileId);

const mockResponse = {
id: MOCK_ONEREP_SCAN_ID(),
id: scanId,
profile_id: profileId,
status: "finished",
reason: "manual",
created_at: MOCK_ONEREP_TIME(),
updated_at: MOCK_ONEREP_TIME(),
url: `${process.env.ONEREP_API_BASE}/profiles/${profileId}/scans/${MOCK_ONEREP_SCAN_ID()}`,
url: `${process.env.ONEREP_API_BASE}/profiles/${profileId}/scans/${scanId}`,
};

return NextResponse.json(mockResponse);
Expand All @@ -38,17 +46,18 @@ export function GET(req: NextRequest) {
return NextResponse.json({ error: "Invalid profile ID" });
}

//TODO: mock out ID here and urls
const scandId = getScanId(profileId);

const responseData = {
data: [
{
id: MOCK_ONEREP_SCAN_ID(),
id: scandId,
profile_id: profileId,
status: "finished",
reason: "manual",
created_at: MOCK_ONEREP_TIME(),
updated_at: MOCK_ONEREP_TIME(),
url: `${process.env.ONEREP_API_BASE}/profiles/${profileId}/scans/${MOCK_ONEREP_SCAN_ID()}`,
url: `${process.env.ONEREP_API_BASE}/profiles/${profileId}/scans/${scandId}`,
},
],
links: {
Expand Down
37 changes: 30 additions & 7 deletions src/db/tables/onerep_scans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import createDbConnection from "../connect.js";
import { logger } from "../../app/functions/server/logging";
import { MOCK_ONEREP_SCAN_ID } from "../../app/api/mock/onerep/config/config.ts";

import {
ScanResult,
Expand Down Expand Up @@ -206,7 +205,7 @@ async function addOnerepScanResults(
onerepProfileId: number,
onerepScanResults: Array<ScanResult>,
) {
const scanResultsMap = onerepScanResults.map((scanResult) => ({
let scanResultsMap = onerepScanResults.map((scanResult) => ({
onerep_scan_result_id: scanResult.id,
onerep_scan_id: scanResult.scan_id,
link: scanResult.link,
Expand Down Expand Up @@ -241,11 +240,35 @@ async function addOnerepScanResults(
});

if (scanResultsMap.length > 0) {
//Delete previous records to allow dynamic mock data configuration.
if (process.env.ONEREP_API_BASE!.includes("localhost")) {
await knex("onerep_scan_results")
.where("onerep_scan_id", MOCK_ONEREP_SCAN_ID())
.del();
const scan_id = scanResultsMap[0].onerep_scan_id;

// Delete previous records to allow dynamic mock data configuration, maintaining the 'manually_resolved' field.
if (!process.env.ONEREP_API_BASE!.includes("mozilla.api.onerep.com")) {
const existingRecords = await knex("onerep_scan_results")
.where("onerep_scan_id", scan_id)
.select("onerep_scan_result_id", "manually_resolved");

const resolvedStatusMap = new Map();
existingRecords.forEach((record) => {
resolvedStatusMap.set(
record.onerep_scan_result_id,
record.manually_resolved,
);
});

await knex("onerep_scan_results").where("onerep_scan_id", scan_id).del();

scanResultsMap = scanResultsMap.map((item) => {
if (resolvedStatusMap.has(item.onerep_scan_result_id)) {
return {
...item,
manually_resolved: resolvedStatusMap.get(
item.onerep_scan_result_id,
),
};
}
return item;
});
}

await knex("onerep_scan_results")
Expand Down
5 changes: 1 addition & 4 deletions src/e2e/specs/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ test.describe(`${process.env.E2E_TEST_ENV} - Authentication flow verification @s
await authPage.signUp(randomEmail, page);

// assert successful login
const successUrl =
process.env.E2E_TEST_ENV === "local"
? "/user/dashboard"
: "/user/welcome";
const successUrl = "/user/welcome";
expect(page.url()).toBe(`${process.env.E2E_TEST_BASE_URL}${successUrl}`);

await testInfo.attach(
Expand Down
Loading