forked from safe-global/safe-client-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add legacy relay service route (safe-global#1155)
This adds the legacy relayer routes to the relay controller for redirection purposes: - `POST` `/v1/relay/:chainId` - `GET` `/v1/relay/:chainId/:safeAddress`
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { AppModule } from '@/app.module'; | ||
import { CacheModule } from '@/datasources/cache/cache.module'; | ||
import { TestCacheModule } from '@/datasources/cache/__tests__/test.cache.module'; | ||
import configuration from '@/config/entities/__tests__/configuration'; | ||
import { RequestScopedLoggingModule } from '@/logging/logging.module'; | ||
import { TestLoggingModule } from '@/logging/__tests__/test.logging.module'; | ||
import { NetworkModule } from '@/datasources/network/network.module'; | ||
import { TestNetworkModule } from '@/datasources/network/__tests__/test.network.module'; | ||
import { TestAppProvider } from '@/__tests__/test-app.provider'; | ||
import { AccountDataSourceModule } from '@/datasources/account/account.datasource.module'; | ||
import { TestAccountDataSourceModule } from '@/datasources/account/__tests__/test.account.datasource.module'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { faker } from '@faker-js/faker'; | ||
|
||
describe('Relay controller', () => { | ||
let app: INestApplication; | ||
const supportedChainIds = Object.keys(configuration().relay.apiKey); | ||
|
||
beforeEach(async () => { | ||
jest.resetAllMocks(); | ||
|
||
const defaultConfiguration = configuration(); | ||
const testConfiguration = (): typeof defaultConfiguration => ({ | ||
...defaultConfiguration, | ||
features: { | ||
...defaultConfiguration.features, | ||
relay: true, | ||
}, | ||
}); | ||
|
||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule.register(testConfiguration)], | ||
}) | ||
.overrideModule(AccountDataSourceModule) | ||
.useModule(TestAccountDataSourceModule) | ||
.overrideModule(CacheModule) | ||
.useModule(TestCacheModule) | ||
.overrideModule(RequestScopedLoggingModule) | ||
.useModule(TestLoggingModule) | ||
.overrideModule(NetworkModule) | ||
.useModule(TestNetworkModule) | ||
.compile(); | ||
|
||
app = await new TestAppProvider().provide(moduleFixture); | ||
await app.init(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
describe('POST /v1/chains/:chainId/relay', () => { | ||
it('should return 302 and redirect to the new endpoint', async () => { | ||
const chainId = faker.helpers.arrayElement(supportedChainIds); | ||
const safeAddress = faker.finance.ethereumAddress(); | ||
const data = faker.string.hexadecimal(); | ||
|
||
await request(app.getHttpServer()) | ||
.post(`/v1/relay/${chainId}`) | ||
.send({ | ||
to: safeAddress, | ||
data, | ||
}) | ||
.expect(308) | ||
.expect((res) => { | ||
expect(res.get('location')).toBe('/v1/chains/:chainId/relay'); | ||
}); | ||
}); | ||
}); | ||
describe('GET /v1/relay/:chainId/:safeAddress', () => { | ||
it('should return 302 and redirect to the new endpoint', async () => { | ||
const chainId = faker.string.numeric(); | ||
const safeAddress = faker.finance.ethereumAddress(); | ||
|
||
await request(app.getHttpServer()) | ||
.get(`/v1/relay/${chainId}/${safeAddress}`) | ||
.expect(301) | ||
.expect((res) => { | ||
expect(res.get('location')).toBe( | ||
'/v1/chains/:chainId/relay/:safeAddress', | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Controller, Post, Redirect, Get, HttpStatus } from '@nestjs/common'; | ||
|
||
@Controller({ | ||
version: '1', | ||
path: 'relay/:chainId', | ||
}) | ||
export class RelayLegacyController { | ||
@Post() | ||
@Redirect('/v1/chains/:chainId/relay', HttpStatus.PERMANENT_REDIRECT) | ||
relay(): void {} | ||
|
||
@Get(':safeAddress') | ||
@Redirect( | ||
'/v1/chains/:chainId/relay/:safeAddress', | ||
HttpStatus.MOVED_PERMANENTLY, | ||
) | ||
getRelaysRemaining(): void {} | ||
} |