Skip to content

Commit

Permalink
re use security alert mock across the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistevam committed Jan 13, 2025
1 parent 368489a commit 1fae67a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MockttpServer } from 'mockttp';
import { SECURITY_ALERTS_PROD_API_BASE_URL } from '../constants';

import FixtureBuilder from '../../../fixture-builder';

Expand All @@ -12,6 +11,7 @@ import {
SignatureType,
} from '../../confirmations/signatures/signature-helpers';
import Confirmation from '../../../page-objects/pages/confirmations/redesign/confirmation';
import { mockSecurityAlertValidateRequest } from './utils';

const SELECTED_ADDRESS = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';

Expand All @@ -38,7 +38,7 @@ const expectedTitle = 'This is a deceptive request';

describe('Security Alerts API - Contract Interaction @no-mmi', function () {
describe('Malicious Contract interaction', function () {
const maliciousTransferAlert: SecurityAlertResponse = {
const maliciousTransferAlertResponse: SecurityAlertResponse = {
block: 1,
result_type: 'Malicious',
reason: 'raw_native_token_transfer',
Expand All @@ -47,28 +47,24 @@ describe('Security Alerts API - Contract Interaction @no-mmi', function () {
features: ['Interaction with a known malicious address'],
};

async function mockRequest(
server: MockttpServer,
response: SecurityAlertResponse,
): Promise<void> {
await server
.forPost(`${SECURITY_ALERTS_PROD_API_BASE_URL}/validate/0x1`)
.withJsonBodyIncluding({
method: 'eth_sendTransaction',
params: [
{
from: SELECTED_ADDRESS,
data: '0xef5cfb8c0000000000000000000000000b3e87a076ac4b0d1975f0f232444af6deb96c59',
to: '0x00008f1149168c1d2fa1eba1ad3e9cd644510000',
value: '0x0',
},
],
})
.thenJson(201, response);
}
const contractInteractionRequestMock = {
method: 'eth_sendTransaction',
params: [
{
from: SELECTED_ADDRESS,
data: '0xef5cfb8c0000000000000000000000000b3e87a076ac4b0d1975f0f232444af6deb96c59',
to: '0x00008f1149168c1d2fa1eba1ad3e9cd644510000',
value: '0x0',
},
],
};

async function mockMaliciousResponses(mockServer: MockttpServer) {
await mockRequest(mockServer, maliciousTransferAlert);
await mockSecurityAlertValidateRequest(
mockServer,
contractInteractionRequestMock,
maliciousTransferAlertResponse,
);
}
it('should show banner alert', async function () {
await withFixtures(
Expand Down Expand Up @@ -115,29 +111,25 @@ describe('Security Alerts API - Contract Interaction @no-mmi', function () {
features: [],
};

async function mockRequest(
server: MockttpServer,
response: SecurityAlertResponse,
): Promise<void> {
await server
.forPost(`${SECURITY_ALERTS_PROD_API_BASE_URL}/validate/0x1`)
.withJsonBodyIncluding({
method: 'eth_sendTransaction',
params: [
{
from: SELECTED_ADDRESS,
data: '0x095ea7b3000000000000000000000000e50a2dbc466d01a34c3e8b7e8e45fce4f7da39e6000000000000000000000000000000000000000000000000ffffffffffffffff',
to: CONTRACT_ADDRESS.BUSD,
},
],
})
.thenJson(201, response);
}
const contractInteractionRequestMock = {
method: 'eth_sendTransaction',
params: [
{
from: SELECTED_ADDRESS,
data: '0x095ea7b3000000000000000000000000e50a2dbc466d01a34c3e8b7e8e45fce4f7da39e6000000000000000000000000000000000000000000000000ffffffffffffffff',
to: CONTRACT_ADDRESS.BUSD,
},
],
};

async function mockMaliciousResponses(
mockServer: MockttpServer,
): Promise<void> {
await mockRequest(mockServer, maliciousApprovalAlert);
await mockSecurityAlertValidateRequest(
mockServer,
contractInteractionRequestMock,
maliciousApprovalAlert,
);
}

it('should show banner alert', async function () {
Expand Down Expand Up @@ -187,28 +179,24 @@ describe('Security Alerts API - Contract Interaction @no-mmi', function () {
features: ['A known malicious address is involved in the transaction'],
};

async function mockRequest(
server: MockttpServer,
response: SecurityAlertResponse,
): Promise<void> {
await server
.forPost(`${SECURITY_ALERTS_PROD_API_BASE_URL}/validate/0x1`)
.withJsonBodyIncluding({
method: 'eth_sendTransaction',
params: [
{
from: SELECTED_ADDRESS,
data: '0xa9059cbb0000000000000000000000005fbdb2315678afecb367f032d93f642f64180aa30000000000000000000000000000000000000000000000000000000000000064',
to: CONTRACT_ADDRESS.USDC,
value: '0x0',
},
],
})
.thenJson(201, response);
}
const contractInteractionRequestMock = {
method: 'eth_sendTransaction',
params: [
{
from: SELECTED_ADDRESS,
data: '0xa9059cbb0000000000000000000000005fbdb2315678afecb367f032d93f642f64180aa30000000000000000000000000000000000000000000000000000000000000064',
to: CONTRACT_ADDRESS.USDC,
value: '0x0',
},
],
};

async function mockMaliciousResponses(mockServer: MockttpServer) {
await mockRequest(mockServer, maliciousTransferAlert);
await mockSecurityAlertValidateRequest(
mockServer,
contractInteractionRequestMock,
maliciousTransferAlert,
);
}

it('should show banner alert', async function () {
Expand Down Expand Up @@ -253,29 +241,25 @@ describe('Security Alerts API - Contract Interaction @no-mmi', function () {
features: [],
};

async function mockRequest(
server: MockttpServer,
response: SecurityAlertResponse,
): Promise<void> {
await server
.forPost(`${SECURITY_ALERTS_PROD_API_BASE_URL}/validate/0x1`)
.withJsonBodyIncluding({
method: 'eth_sendTransaction',
params: [
{
from: SELECTED_ADDRESS,
data: '0xa22cb465000000000000000000000000b85492afc686d5ca405e3cd4f50b05d358c75ede0000000000000000000000000000000000000000000000000000000000000001',
to: CONTRACT_ADDRESS.BAYC,
},
],
})
.thenJson(201, response);
}
const contractInteractionRequestMock = {
method: 'eth_sendTransaction',
params: [
{
from: SELECTED_ADDRESS,
data: '0xa22cb465000000000000000000000000b85492afc686d5ca405e3cd4f50b05d358c75ede0000000000000000000000000000000000000000000000000000000000000001',
to: CONTRACT_ADDRESS.BAYC,
},
],
};

async function mockMaliciousResponses(
mockServer: MockttpServer,
): Promise<void> {
await mockRequest(mockServer, maliciousApprovalAlert);
await mockSecurityAlertValidateRequest(
mockServer,
contractInteractionRequestMock,
maliciousApprovalAlert,
);
}
it('should show banner alert', async function () {
await withFixtures(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MockttpServer } from 'mockttp';
import { SECURITY_ALERTS_PROD_API_BASE_URL } from '../constants';
import FixtureBuilder from '../../../fixture-builder';
import { defaultGanacheOptions, withFixtures } from '../../../helpers';
import { SecurityAlertResponse } from '../../../../../app/scripts/lib/ppom/types';
Expand All @@ -9,33 +8,31 @@ import {
openDappAndTriggerSignature,
SignatureType,
} from '../../confirmations/signatures/signature-helpers';
import { mockSecurityAlertValidateRequest } from './utils';

const maliciousTradeAlert: SecurityAlertResponse = {
const tradeRequestMock = {
method: 'eth_signTypedData_v4',
params: ['0x5cfe73b6021e818b776b421b1c4db2474086a7e1'],
};

const maliciousTradeAlertResponse: SecurityAlertResponse = {
block: 1,
result_type: 'Malicious',
reason: 'trade_order_farming',
description: 'If you approve this request, you might lose your assets.',
features: [],
};

async function mockRequest(
server: MockttpServer,
response: SecurityAlertResponse,
): Promise<void> {
await server
.forPost(`${SECURITY_ALERTS_PROD_API_BASE_URL}/validate/0x1`)
.withJsonBodyIncluding({
method: 'eth_signTypedData_v4',
params: ['0x5cfe73b6021e818b776b421b1c4db2474086a7e1'],
})
.thenJson(201, response);
}

async function mockMaliciousResponses(
mockServer: MockttpServer,
): Promise<void> {
await mockRequest(mockServer, maliciousTradeAlert);
await mockSecurityAlertValidateRequest(
mockServer,
tradeRequestMock,
maliciousTradeAlertResponse,
);
}

describe('Security Alerts API - Signatures @no-mmi', function () {
describe('Set Trade farming order', function () {
it('should show banner alert', async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
withFixtures,
logInWithBalanceValidation,
} from '../../../helpers';
import { SECURITY_ALERTS_PROD_API_BASE_URL } from '../constants';
import { mockServerJsonRpc } from '../mocks/mock-server-json-rpc';
import { mockMultiNetworkBalancePolling } from '../../../mock-balance-polling/mock-balance-polling';
import { loginWithBalanceValidation } from '../../../page-objects/flows/login.flow';
import { createTransactionToAddress } from '../../../page-objects/flows/transaction';
import Confirmation from '../../../page-objects/pages/confirmations/redesign/confirmation';
import { mockSecurityAlertValidateRequest } from './utils';

const mockMaliciousAddress = '0x5fbdb2315678afecb367f032d93f642f64180aa3';
const mockBenignAddress = '0x50587E46C5B96a3F6f9792922EC647F13E6EFAE4';
Expand Down Expand Up @@ -47,19 +47,8 @@ async function mockInfura(mockServer: MockttpServer): Promise<void> {
await mockServerJsonRpc(mockServer, [['eth_getBalance']]);
}

async function mockRequest(
server: MockttpServer,
request: Record<string, unknown>,
response: Record<string, unknown>,
): Promise<void> {
await server
.forPost(`${SECURITY_ALERTS_PROD_API_BASE_URL}/validate/0x1`)
.withJsonBodyIncluding(request)
.thenJson((response.statusCode as number) ?? 201, response);
}

async function mockBenignResponses(mockServer: MockttpServer): Promise<void> {
await mockRequest(mockServer, SEND_REQUEST_BASE_MOCK, {
await mockSecurityAlertValidateRequest(mockServer, SEND_REQUEST_BASE_MOCK, {
block: 20733513,
result_type: 'Benign',
reason: '',
Expand All @@ -72,21 +61,25 @@ async function mockMaliciousResponses(
mockServer: MockttpServer,
): Promise<void> {
await mockInfura(mockServer);
await mockRequest(mockServer, SEND_REQUEST_MALICIOUS_MOCK, {
block: 20733277,
result_type: 'Malicious',
reason: 'transfer_farming',
description: '',
features: ['Interaction with a known malicious address'],
});
await mockSecurityAlertValidateRequest(
mockServer,
SEND_REQUEST_MALICIOUS_MOCK,
{
block: 20733277,
result_type: 'Malicious',
reason: 'transfer_farming',
description: '',
features: ['Interaction with a known malicious address'],
},
);
}

async function mockInfuraWithFailedResponses(
mockServer: MockttpServer,
): Promise<void> {
await mockInfura(mockServer);

await mockRequest(
await mockSecurityAlertValidateRequest(
mockServer,
{
...SEND_REQUEST_BASE_MOCK,
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/tests/ppom/security-alerts-api/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MockttpServer } from 'mockttp';
import { SECURITY_ALERTS_PROD_API_BASE_URL } from '../constants';

/**
* Mocks a security alert validation request to the server.
*
* @param server - The MockttpServer instance to mock the request on.
* @param request - The request payload to be validated.
* @param response - The response payload to be returned.
*/
export async function mockSecurityAlertValidateRequest(
server: MockttpServer,
request: Record<string, unknown>,
response: Record<string, unknown>,
): Promise<void> {
await server
.forPost(`${SECURITY_ALERTS_PROD_API_BASE_URL}/validate/0x1`)
.withJsonBodyIncluding(request)
.thenJson((response.statusCode as number) ?? 201, response);
}

0 comments on commit 1fae67a

Please sign in to comment.