Skip to content
Merged
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
10 changes: 8 additions & 2 deletions extension/src/popup/components/AuthEntry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const AuthEntries = ({ invocations }: AuthEntriesProps) => {
args={detail.args}
contractId={detail.contractId}
fnName={detail.fnName}
isAuthEntry
/>
</div>
</div>
Expand Down Expand Up @@ -148,13 +149,18 @@ export const AuthEntries = ({ invocations }: AuthEntriesProps) => {
{details.map((detail, ind) => (
<div
className="AuthEntryContainer"
data-testid="AuthEntryContainer"
key={`${invocation.toXDR("raw").toString()}-${ind}`}
>
<div
className="AuthEntryBtn"
data-testid="AuthEntryBtn"
onClick={() => handleExpandDetail(ind)}
>
<div className="AuthEntryBtn__Title">
<div
className="AuthEntryBtn__Title"
data-testid="AuthEntryBtn__Title"
>
<Icon.CodeCircle01 />
{renderDetailTitle(detail)}
</div>
Expand All @@ -163,7 +169,7 @@ export const AuthEntries = ({ invocations }: AuthEntriesProps) => {
/>
</div>
{expandedIndex === ind ? (
<div className="AuthEntryContent">
<div className="AuthEntryContent" data-testid="AuthEntryContent">
{renderDetailContent(detail)}
</div>
) : null}
Expand Down
222 changes: 222 additions & 0 deletions extension/src/popup/components/__tests__/AuthEntry.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import React from "react";
import { render, waitFor, screen, fireEvent } from "@testing-library/react";
import { Address, Keypair, ScInt, StrKey, xdr } from "stellar-sdk";

import { mockAccounts, TEST_PUBLIC_KEY, Wrapper } from "popup/__testHelpers__";
import { AuthEntries } from "../AuthEntry";
import * as internalApi from "@shared/api/internal";
import { APPLICATION_STATE } from "@shared/constants/applicationState";
import {
TESTNET_NETWORK_DETAILS,
DEFAULT_NETWORKS,
} from "@shared/constants/stellar";
import { ROUTES } from "popup/constants/routes";

describe("AuthEntry", () => {
afterAll(() => {
jest.clearAllMocks();
});

const getContractSpecSpy = jest
.spyOn(internalApi, "getContractSpec")
.mockImplementation(() => {
return Promise.resolve({
definitions: {
create: {
properties: {
args: ["admin"],
},
},
},
});
});

it("renders auth entries for create contract v1", async () => {
const assetCode = "KHL1";
const assetType = new xdr.AlphaNum4({
assetCode: Buffer.from(assetCode),
issuer: Keypair.fromPublicKey(TEST_PUBLIC_KEY).xdrAccountId(),
});

const args = new xdr.CreateContractArgs({
contractIdPreimage: xdr.ContractIdPreimage.contractIdPreimageFromAsset(
xdr.Asset.assetTypeCreditAlphanum4(assetType),
),
executable: xdr.ContractExecutable.contractExecutableStellarAsset(),
});

const authorizedFn =
xdr.SorobanAuthorizedFunction.sorobanAuthorizedFunctionTypeCreateContractHostFn(
args,
);
const authorizedInvocation = new xdr.SorobanAuthorizedInvocation({
function: authorizedFn,
subInvocations: [],
});

render(
<Wrapper
routes={[ROUTES.reviewAuthorization]}
state={{
auth: {
error: null,
applicationState: APPLICATION_STATE.PASSWORD_CREATED,
TEST_PUBLIC_KEY,
allAccounts: mockAccounts,
hasPrivateKey: true,
},
settings: {
networkDetails: TESTNET_NETWORK_DETAILS,
networksList: DEFAULT_NETWORKS,
isSorobanPublicEnabled: true,
isRpcHealthy: true,
},
}}
>
<AuthEntries invocations={[authorizedInvocation]} />
</Wrapper>,
);
await waitFor(() => screen.getAllByTestId("AuthEntryContainer"));
await fireEvent.click(screen.getByTestId("AuthEntryBtn"));
await waitFor(() => screen.getAllByTestId("AuthEntryContent"));

expect(screen.getByTestId("AuthEntryBtn__Title")).toHaveTextContent(
"Contract creation",
);

expect(getContractSpecSpy).not.toHaveBeenCalled();
});

it("renders auth entries for create contract v2", async () => {
const assetCode = "KHL1";
const assetType = new xdr.AlphaNum4({
assetCode: Buffer.from(assetCode),
issuer: Keypair.fromPublicKey(TEST_PUBLIC_KEY).xdrAccountId(),
});

const args = new xdr.CreateContractArgsV2({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt about adding coverage for CreateContractArgsV1 as well as the other executable types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup - added in e3c414a

Added the other SorobanAuthorizedFunction types: sorobanAuthorizedFunctionTypeContractFn and sorobanAuthorizedFunctionTypeCreateContractHostFn

contractIdPreimage: xdr.ContractIdPreimage.contractIdPreimageFromAsset(
xdr.Asset.assetTypeCreditAlphanum4(assetType),
),
executable: xdr.ContractExecutable.contractExecutableStellarAsset(),
constructorArgs: [new Address(TEST_PUBLIC_KEY).toScVal()],
});

const authorizedFn =
xdr.SorobanAuthorizedFunction.sorobanAuthorizedFunctionTypeCreateContractV2HostFn(
args,
);
const authorizedInvocation = new xdr.SorobanAuthorizedInvocation({
function: authorizedFn,
subInvocations: [],
});

render(
<Wrapper
routes={[ROUTES.reviewAuthorization]}
state={{
auth: {
error: null,
applicationState: APPLICATION_STATE.PASSWORD_CREATED,
TEST_PUBLIC_KEY,
allAccounts: mockAccounts,
hasPrivateKey: true,
},
settings: {
networkDetails: TESTNET_NETWORK_DETAILS,
networksList: DEFAULT_NETWORKS,
isSorobanPublicEnabled: true,
isRpcHealthy: true,
},
}}
>
<AuthEntries invocations={[authorizedInvocation]} />
</Wrapper>,
);
await waitFor(() => screen.getAllByTestId("AuthEntryContainer"));
await fireEvent.click(screen.getByTestId("AuthEntryBtn"));
await waitFor(() => screen.getAllByTestId("AuthEntryContent"));

expect(screen.getByTestId("AuthEntryBtn__Title")).toHaveTextContent(
"Contract creation",
);

expect(getContractSpecSpy).not.toHaveBeenCalled();

const parameterKeys = screen.getAllByTestId("ParameterKey");
const parameterValues = screen.getAllByTestId("ParameterValue");

expect(parameterKeys).toHaveLength(1);
expect(parameterKeys[0]).toHaveTextContent("");

expect(parameterValues).toHaveLength(1);
expect(parameterValues[0]).toHaveTextContent(TEST_PUBLIC_KEY);
});

it("renders auth entries for invoke contract args", async () => {
const CONTRACT = "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE";
const args = new xdr.InvokeContractArgs({
functionName: Buffer.from("transfer"),
args: [
new Address(TEST_PUBLIC_KEY).toScVal(),
new Address(TEST_PUBLIC_KEY).toScVal(),
new ScInt(100).toI128(),
],
contractAddress: xdr.ScAddress.scAddressTypeContract(
StrKey.decodeContract(CONTRACT) as any,
),
});

const authorizedFn =
xdr.SorobanAuthorizedFunction.sorobanAuthorizedFunctionTypeContractFn(
args,
);
const authorizedInvocation = new xdr.SorobanAuthorizedInvocation({
function: authorizedFn,
subInvocations: [],
});

render(
<Wrapper
routes={[ROUTES.reviewAuthorization]}
state={{
auth: {
error: null,
applicationState: APPLICATION_STATE.PASSWORD_CREATED,
TEST_PUBLIC_KEY,
allAccounts: mockAccounts,
hasPrivateKey: true,
},
settings: {
networkDetails: TESTNET_NETWORK_DETAILS,
networksList: DEFAULT_NETWORKS,
isSorobanPublicEnabled: true,
isRpcHealthy: true,
},
}}
>
<AuthEntries invocations={[authorizedInvocation]} />
</Wrapper>,
);
await waitFor(() => screen.getAllByTestId("AuthEntryContainer"));
await fireEvent.click(screen.getByTestId("AuthEntryBtn"));
await waitFor(() => screen.getAllByTestId("AuthEntryContent"));

expect(screen.getByTestId("AuthEntryBtn__Title")).toHaveTextContent(
"transfer",
);

expect(getContractSpecSpy).not.toHaveBeenCalled();

const parameterKeys = screen.getAllByTestId("ParameterKey");
const parameterValues = screen.getAllByTestId("ParameterValue");

expect(parameterKeys).toHaveLength(3);
expect(parameterKeys[0]).toHaveTextContent("");
expect(parameterKeys[1]).toHaveTextContent("");
expect(parameterKeys[2]).toHaveTextContent("");

expect(parameterValues).toHaveLength(3);
expect(parameterValues[0]).toHaveTextContent(TEST_PUBLIC_KEY);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,13 @@ export const KeyValueInvokeHostFnArgs = ({
contractId,
fnName,
showHeader = true,
isAuthEntry = false,
}: {
args: xdr.ScVal[];
contractId?: string;
fnName?: string;
showHeader?: boolean;
isAuthEntry?: boolean;
}) => {
const [isLoading, setLoading] = React.useState(true);
const [argNames, setArgNames] = React.useState([] as string[]);
Expand All @@ -405,12 +407,12 @@ export const KeyValueInvokeHostFnArgs = ({
}
}

if (contractId && fnName) {
if (contractId && fnName && !isAuthEntry) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't fetch the spec if we're populating the authorization parameters

getSpec(contractId, fnName);
} else {
setLoading(false);
}
}, [contractId, fnName, networkDetails]);
}, [contractId, fnName, networkDetails, isAuthEntry]);

return isLoading ? (
<div className="Operations__pair--invoke" data-testid="OperationKeyVal">
Expand Down