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

Include not listed Safe Apps on transaction mappings #1450

Merged
merged 2 commits into from
Apr 25, 2024
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
27 changes: 27 additions & 0 deletions src/datasources/config-api/config-api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,33 @@ describe('ConfigApi', () => {
expect(mockHttpErrorFactory.from).toHaveBeenCalledTimes(0);
});

it('should return the safe apps retrieved by chainId, clientUrl and onlyListed', async () => {
const chainId = faker.string.numeric();
const clientUrl = faker.internet.url({ appendSlash: false });
const onlyListed = faker.datatype.boolean();
const data = [safeAppBuilder().build(), safeAppBuilder().build()];
mockDataSource.get.mockResolvedValue(data);

const actual = await service.getSafeApps({
chainId,
clientUrl,
onlyListed,
});

expect(actual).toBe(data);
expect(mockDataSource.get).toHaveBeenCalledTimes(1);
expect(mockDataSource.get).toHaveBeenCalledWith({
cacheDir: new CacheDir(`${chainId}_safe_apps`, `${clientUrl}_undefined`),
url: `${baseUri}/api/v1/safe-apps/`,
notFoundExpireTimeSeconds: notFoundExpirationTimeInSeconds,
networkRequest: {
params: { chainId, clientUrl, onlyListed, url: undefined },
},
expireTimeSeconds: expirationTimeInSeconds,
});
expect(mockHttpErrorFactory.from).toHaveBeenCalledTimes(0);
});

it('should forward error', async () => {
const expected = new DataSourceError('some unexpected error');
mockHttpErrorFactory.from.mockReturnValue(expected);
Expand Down
2 changes: 2 additions & 0 deletions src/datasources/config-api/config-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ export class ConfigApi implements IConfigApi {
async getSafeApps(args: {
chainId?: string;
clientUrl?: string;
onlyListed?: boolean;
url?: string;
}): Promise<SafeApp[]> {
try {
const providerUrl = `${this.baseUri}/api/v1/safe-apps/`;
const params = {
chainId: args.chainId,
clientUrl: args.clientUrl,
onlyListed: args.onlyListed,
url: args.url,
};
const cacheDir = CacheRouter.getSafeAppsCacheDir(args);
Expand Down
1 change: 1 addition & 0 deletions src/domain/interfaces/config-api.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface IConfigApi {
getSafeApps(args: {
chainId?: string;
clientUrl?: string;
onlyListed?: boolean;
url?: string;
}): Promise<SafeApp[]>;

Expand Down
1 change: 1 addition & 0 deletions src/domain/safe-apps/safe-apps.repository.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ISafeAppsRepository {
getSafeApps(args: {
chainId?: string;
clientUrl?: string;
onlyListed?: boolean;
url?: string;
}): Promise<SafeApp[]>;

Expand Down
1 change: 1 addition & 0 deletions src/domain/safe-apps/safe-apps.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class SafeAppsRepository implements ISafeAppsRepository {
async getSafeApps(args: {
chainId?: string;
clientUrl?: string;
onlyListed?: boolean;
url?: string;
}): Promise<SafeApp[]> {
const safeApps = await this.configApi.getSafeApps(args);
Expand Down
5 changes: 4 additions & 1 deletion src/routes/safe-apps/safe-apps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export class SafeAppsService {
clientUrl?: string;
url?: string;
}): Promise<SafeApp[]> {
const result = await this.safeAppsRepository.getSafeApps(args);
const result = await this.safeAppsRepository.getSafeApps({
...args,
onlyListed: true,
});

return result.map(
(safeApp) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('SafeAppInfo mapper (Unit)', () => {
const actual = await mapper.mapSafeAppInfo(chainId, transaction);

expect(actual).toBeNull();
expect(safeAppsRepositoryMock.getSafeApps).toHaveBeenCalledTimes(0);
});

it('should get a null SafeAppInfo for a transaction with no url into origin', async () => {
Expand All @@ -53,25 +54,44 @@ describe('SafeAppInfo mapper (Unit)', () => {
const actual = await mapper.mapSafeAppInfo(chainId, transaction);

expect(actual).toBeNull();
expect(safeAppsRepositoryMock.getSafeApps).toHaveBeenCalledTimes(0);
});

it('should return null if no SafeApp is found and origin is not null', async () => {
const chainId = faker.string.numeric();
const safeApps: Array<SafeApp> = [];
const transaction = multisigTransactionBuilder().build();
const transactionOrigin = {
url: faker.internet.url({ appendSlash: false }),
name: faker.word.words(),
};
const transaction = multisigTransactionBuilder()
.with('origin', JSON.stringify(transactionOrigin))
.build();
safeAppsRepositoryMock.getSafeApps.mockResolvedValue(safeApps);

const actual = await mapper.mapSafeAppInfo(chainId, transaction);

expect(actual).toBeNull();
expect(safeAppsRepositoryMock.getSafeApps).toHaveBeenCalledTimes(1);
expect(safeAppsRepositoryMock.getSafeApps).toHaveBeenCalledWith({
chainId,
onlyListed: false,
url: transactionOrigin.url,
});
});

it('should get SafeAppInfo for a transaction with origin', async () => {
const chainId = faker.string.numeric();
const safeApp = safeAppBuilder().build();
const anotherSafeApp = safeAppBuilder().build();
const safeApps = [safeApp, anotherSafeApp];
const transaction = multisigTransactionBuilder().build();
const transactionOrigin = {
url: faker.internet.url({ appendSlash: false }),
name: faker.word.words(),
};
const transaction = multisigTransactionBuilder()
.with('origin', JSON.stringify(transactionOrigin))
.build();
safeAppsRepositoryMock.getSafeApps.mockResolvedValue(safeApps);
const expected = new SafeAppInfo(
safeApp.name,
Expand All @@ -82,6 +102,12 @@ describe('SafeAppInfo mapper (Unit)', () => {
const actual = await mapper.mapSafeAppInfo(chainId, transaction);

expect(actual).toEqual(expected);
expect(safeAppsRepositoryMock.getSafeApps).toHaveBeenCalledTimes(1);
expect(safeAppsRepositoryMock.getSafeApps).toHaveBeenCalledWith({
chainId,
onlyListed: false,
url: transactionOrigin.url,
});
});

it('should return null origin on invalid JSON', async () => {
Expand All @@ -93,6 +119,7 @@ describe('SafeAppInfo mapper (Unit)', () => {
const actual = await mapper.mapSafeAppInfo(chainId, transaction);

expect(actual).toBeNull();
expect(safeAppsRepositoryMock.getSafeApps).toHaveBeenCalledTimes(0);
});

it('should replace IPFS origin urls', async () => {
Expand All @@ -101,7 +128,13 @@ describe('SafeAppInfo mapper (Unit)', () => {
const safeApp = safeAppBuilder().with('url', originUrl).build();
const safeApps = [safeApp];
const expectedUrl = 'https://cloudflare-ipfs.com/test';
const transaction = multisigTransactionBuilder().build();
const transactionOrigin = {
url: faker.internet.url({ appendSlash: false }),
name: faker.word.words(),
};
const transaction = multisigTransactionBuilder()
.with('origin', JSON.stringify(transactionOrigin))
.build();
safeAppsRepositoryMock.getSafeApps.mockResolvedValue(safeApps);
const expected = new SafeAppInfo(
safeApp.name,
Expand All @@ -112,5 +145,11 @@ describe('SafeAppInfo mapper (Unit)', () => {
const actual = await mapper.mapSafeAppInfo(chainId, transaction);

expect(actual).toEqual(expected);
expect(safeAppsRepositoryMock.getSafeApps).toHaveBeenCalledTimes(1);
expect(safeAppsRepositoryMock.getSafeApps).toHaveBeenCalledWith({
chainId,
onlyListed: false,
url: transactionOrigin.url,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class SafeAppInfoMapper {

const [safeApp] = await this.safeAppsRepository.getSafeApps({
chainId,
onlyListed: false,
url: originUrl,
});
if (!safeApp) {
Expand Down